Skip to content

Instantly share code, notes, and snippets.

@dangarbri
Last active May 18, 2024 21:08
Show Gist options
  • Save dangarbri/6de8e8b0a98f4b3bb4cd75d3ae1e0129 to your computer and use it in GitHub Desktop.
Save dangarbri/6de8e8b0a98f4b3bb4cd75d3ae1e0129 to your computer and use it in GitHub Desktop.
Makefile for building programs with cosmopolitan libc
# License: MIT
# Copyright (c) 2022 Daniel Garcia-Briseno
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Define the name of your program here
PROGRAM = pico
# Define your source files here
SOURCES = main.cpp env.cpp
# Set the source directory here
SRC_DIR = src
# Include directories
INCLUDE_DIRS = src
# Change to c if using .c files
SOURCE_EXTENSION = cpp
# Any flags you would like to add to the compiler
# The above is all you should need to edit. Below is the monstrosity
# that manages the cosmopolitan build.
# Generate make targets from source file list
TARGETS = $(shell for source in $(SOURCES); do echo $(SRC_DIR)/$${source%.*}.o; done)
# Source files with src_dir prepended
TARGET_DEPENDENCIES = $(shell for source in $(SOURCES); do echo $(SRC_DIR)/$$source; done)
# Include directories as flags to the compiler
INCLUDE_DIR_FLAGS = $(shell for dir in $(INCLUDE_DIRS); do echo -I$$dir; done)
# Define the folder I'm going to put the library into
COSMOPOLITAN_DIR = cosmopolitan
# The URL I'm going to download
COSMOPOLITAN_URL = https://justine.lol/cosmopolitan/cosmopolitan.zip
# Desired cosmopolitan files (note the .h should be first due to how it's included in the FLAGS
COSMOPOLITAN_BINS = crt.o ape-no-modify-self.o cosmopolitan.a
# ape.lds gets its own variable since it needs to be added to the gcc command in its own section
COSMOPOLITAN_LDS = $(COSMOPOLITAN_DIR)/ape.lds
# header gets its own variable since it needs to be added to the gcc command in its own section too.
COSMOPOLITAN_HEADER = $(COSMOPOLITAN_DIR)/cosmopolitan.h
# Create targets from bin names (i.e. cosmopolitan/cosmopolitan.a, cosmopolitan/ape.lds, etc
COSMOPOLITAN_TARGETS = $(shell for bin in $(COSMOPOLITAN_BINS); do echo $(COSMOPOLITAN_DIR)/$$bin; done)
# gcc compile flags for cosmopolitan per https://justine.lol/cosmopolitan/index.html
COSMOPOLITAN_COMPILE_FLAGS = -g -Os -static -fno-pie -no-pie -nostdlib -nostdinc -fno-omit-frame-pointer \
-pg -mnop-mcount -mno-tls-direct-seg-refs -Wl,--gc-sections -fuse-ld=bfd -Wl,--gc-sections -Wl,-T,$(COSMOPOLITAN_LDS) \
-include $(COSMOPOLITAN_HEADER)
# This target builds the main program
$(PROGRAM).com: $(TARGETS)
g++ $(COSMOPOLITAN_COMPILE_FLAGS) -o $(PROGRAM).com.dbg $(TARGETS) $(COSMOPOLITAN_TARGETS)
objcopy -S -O binary $(PROGRAM).com.dbg $(PROGRAM).com
# Make target that depends on .c files and maps to .o files
%.o: %.$(SOURCE_EXTENSION)
g++ $(INCLUDE_DIR_FLAGS) $(COSMOPOLITAN_COMPILE_FLAGS) -c -o $@ $<
# This target downloads and unzips the cosmopolitan library
$(COSMOPOLITAN_TARGETS) $(COSMOPOLITAN_LDS) $(COSMOPOLITAN_HEADER):
@mkdir -p $(COSMOPOLITAN_DIR)
@curl -s $(COSMOPOLITAN_URL) -o $(COSMOPOLITAN_DIR)/cosmopolitan.zip
@unzip -qq $(COSMOPOLITAN_DIR)/cosmopolitan.zip -d $(COSMOPOLITAN_DIR)
@rm $(COSMOPOLITAN_DIR)/cosmopolitan.zip
@dangarbri
Copy link
Author

How to use:
Follow the comments on top. You just have to set the name of the program, and add your source files to the list of sources. Maybe update src_dir if you're not using the default "src" folder.

How it works:
A bunch of variables just pointing to files, all the compiler flags listed on https://justine.lol/cosmopolitan/index.html, and some shell commands to convert *.c into *.o for the make targets.

Line 66 creates the targets for cosmopolitan.a, crt.o, etc, the parts of the cosmopolitan library. The action is to download and unzip it into a directory named cosmopolitan.

Line 62 is your targets, created on line 29 from your list of source files. Basically any "source.*" becomes a "source.o" target

Line 57 is the final output, the program that's generated.

@dangarbri
Copy link
Author

All the targets are files like they should be. So make will automagically determine if anything needs to be rebuilt. So updating a C file will to make to rebuild the associated object file, which will make it automagically rebuild the main program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment