(274 lines)
1 SHELL=bash -o pipefail2 # Run ./configure.sh to choose installation locations:3 ifeq ($(wildcard config.mk),)4 all: config.mk5 $(MAKE) all6 install: config.mk7 $(MAKE) install8 install-files: config.mk9 $(MAKE) install-files10 install-lib: config.mk11 $(MAKE) install-lib12 test: config.mk13 $(MAKE) test14 config.mk: configure.sh15 bash ./configure.sh16 else18 include config.mk20 # Modified progress counter based on: https://stackoverflow.com/a/3532089521 ifndef NO_PROGRESS22 ifndef ECHO23 T := $(shell $(MAKE) ECHO="COUNTTHIS" $(MAKECMDGOALS) --no-print-directory \24 -n | grep -c "COUNTTHIS")25 N := x26 C = $(words $N)$(eval N := x $N)27 ECHO = echo -e "[`expr $C '*' 100 / $T`%]"28 endif29 endif30 ifndef ECHO31 ECHO = echo32 endif33 # End of progress counter35 CC=$(DEFAULT_C_COMPILER)36 CCONFIG=-std=c2x -fPIC \37 -fno-signed-zeros -fno-trapping-math \38 -fvisibility=hidden -fdollars-in-identifiers \39 -DGC_THREADS40 LDFLAGS=41 INCLUDE_DIRS=42 CWARN=-Wall -Wextra -Wno-format -Wno-format-security -Wshadow \43 -Wno-pedantic \44 -Wno-pointer-arith \45 -Wtype-limits -Wunused-result -Wnull-dereference \46 -Walloca -Wcast-align \47 -Wdangling-else -Wdate-time -Wdisabled-optimization -Wdouble-promotion \48 -Wexpansion-to-defined -Wno-float-equal \49 -Wframe-address -Winline -Winvalid-pch \50 -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn \51 -Wno-missing-field-initializers \52 -Wnull-dereference -Woverlength-strings -Wpacked \53 -Wredundant-decls -Wshadow \54 -Wno-stack-protector -Wswitch-default \55 -Wundef -Wunused -Wunused-but-set-variable \56 -Wunused-const-variable -Wunused-local-typedefs -Wunused-macros -Wvariadic-macros \57 -Wwrite-strings59 ifeq ($(SUDO),)60 ifeq ($(shell command -v doas 2>/dev/null),)61 SUDO=sudo62 else63 SUDO=doas64 endif65 endif67 OWNER=$(shell ls -ld '$(PREFIX)' | awk '{print $$3}')69 ifeq ($(shell $(CC) -v 2>&1 | grep -c "gcc version"), 1)70 CWARN += -Werror -Wsign-conversion -Walloc-zero -Wduplicated-branches -Wduplicated-cond -Wjump-misses-init \71 -Wlogical-op -Wpacked-not-aligned -Wshadow=compatible-local -Wshadow=global -Wshadow=local \72 -Wsuggest-attribute=const -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure \73 -Wsync-nand -Wtrampolines -Wvector-operation-performance -Wcast-align=strict74 CCONFIG += -fsanitize=signed-integer-overflow -fno-sanitize-recover -fno-signaling-nans -fno-finite-math-only75 endif77 OS := $(shell uname -s)79 OSFLAGS != case $(OS) in *BSD|Darwin) echo '-D_BSD_SOURCE';; Linux) echo '-D_GNU_SOURCE';; *) echo '-D_DEFAULT_SOURCE';; esac80 EXTRA=81 G=-ggdb82 O=-O383 # Note: older versions of Make have buggy behavior with hash marks inside strings, so this ugly code is necessary:84 TOMO_VERSION=$(shell awk 'BEGIN{hashes=sprintf("%c%c",35,35)} $$1==hashes {print $$2; exit}' CHANGES.md)85 GIT_VERSION=$(shell git log -1 --pretty=format:"%as_%h" 2>/dev/null || echo "unknown")86 CFLAGS+=$(CCONFIG) $(INCLUDE_DIRS) $(EXTRA) $(CWARN) $(G) $(O) $(OSFLAGS) $(LTO) \87 -DSUDO='"$(SUDO)"' -DDEFAULT_C_COMPILER='"$(DEFAULT_C_COMPILER)"' \88 -DGIT_VERSION='"$(GIT_VERSION)"' -ffunction-sections -fdata-sections89 CFLAGS_PLACEHOLDER="$$(printf '\033[2m<flags...>\033[m\n')"90 LDLIBS=-lgc -lm -lunistring -lgmp -lcrypto92 ifeq ($(OS),OpenBSD)93 LDLIBS += -lexecinfo94 else95 LDLIBS += -ldl96 endif98 AR_FILE=libtomo@$(TOMO_VERSION).a99 ifeq ($(OS),Darwin)100 INCLUDE_DIRS += -I/opt/homebrew/include101 LDFLAGS += -L/opt/homebrew/lib102 endif103 EXE_FILE=tomo@$(TOMO_VERSION)105 COMPILER_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c src/compile/*.c src/parse/*.c src/formatter/*.c))106 STDLIB_OBJS=$(patsubst %.c,%.o,$(wildcard src/stdlib/*.c))107 TESTS=$(patsubst test/%.tm,test/results/%.tm.testresult,$(wildcard test/[!_]*.tm))108 API_YAML=$(wildcard api/*.yaml)109 API_MD=$(patsubst %.yaml,%.md,$(API_YAML))111 all: config.mk check-c-compiler check-libs build test/api.tm112 @$(ECHO) "All done!"114 dist: all115 rm -f build/tomo@$(TOMO_VERSION)_$$(uname -sm | tr ' ' '-').tar.gz116 tar czf build/tomo@$(TOMO_VERSION)_$$(uname -sm | tr ' ' '-').tar.gz -C build tomo@$(TOMO_VERSION)118 BUILD_DIR=build/tomo@$(TOMO_VERSION)119 headers := $(wildcard src/stdlib/*.h)120 build_headers := $(patsubst src/stdlib/%.h, $(BUILD_DIR)/include/tomo@$(TOMO_VERSION)/%.h, $(headers))122 # generate corresponding build paths with .gz123 build_manpages := $(patsubst %,$(BUILD_DIR)/%.gz,$(wildcard man/man*/*))125 # Ensure directories exist126 dirs := $(BUILD_DIR)/include/tomo@$(TOMO_VERSION) \127 $(BUILD_DIR)/lib \128 $(BUILD_DIR)/lib/tomo@$(TOMO_VERSION) \129 $(BUILD_DIR)/bin \130 $(BUILD_DIR)/man/man1 \131 $(BUILD_DIR)/man/man3 \132 $(BUILD_DIR)/share/licenses/tomo@$(TOMO_VERSION)134 $(dirs):135 mkdir -p $@137 # Rule for copying headers138 $(BUILD_DIR)/include/tomo@$(TOMO_VERSION)%.h: src/stdlib/%.h | $(BUILD_DIR)/include/tomo@$(TOMO_VERSION)139 cp $< $@141 # Rule for gzipping man pages142 $(BUILD_DIR)/man/%.gz: man/% | $(BUILD_DIR)/man/man1 $(BUILD_DIR)/man/man3143 gzip -c $< > $@145 $(BUILD_DIR)/bin/tomo: $(BUILD_DIR)/bin/tomo@$(TOMO_VERSION) | $(BUILD_DIR)/bin146 ln -sf tomo@$(TOMO_VERSION) $@148 $(BUILD_DIR)/bin/$(EXE_FILE): $(STDLIB_OBJS) $(COMPILER_OBJS) | $(BUILD_DIR)/bin149 @$(ECHO) $(CC) $(CFLAGS_PLACEHOLDER) $(LDFLAGS) $^ $(LDLIBS) -o $@150 @$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@152 $(BUILD_DIR)/lib/$(AR_FILE): $(STDLIB_OBJS) | $(BUILD_DIR)/lib153 ar -rcs $@ $^155 $(BUILD_DIR)/lib/tomo@$(TOMO_VERSION)/packages.ini: packages/core.ini packages/examples.ini | $(BUILD_DIR)/lib/tomo@$(TOMO_VERSION)156 @cat $^ > $@158 $(BUILD_DIR)/share/licenses/tomo@$(TOMO_VERSION)/LICENSE.md: LICENSE.md | $(BUILD_DIR)/share/licenses/tomo@$(TOMO_VERSION)159 cp $< $@161 build: $(BUILD_DIR)/bin/tomo $(BUILD_DIR)/bin/tomo@$(TOMO_VERSION) \162 $(BUILD_DIR)/lib/$(AR_FILE) $(BUILD_DIR)/lib/tomo@$(TOMO_VERSION)/packages.ini \163 $(BUILD_DIR)/share/licenses/tomo@$(TOMO_VERSION)/LICENSE.md $(build_headers) $(build_manpages)165 version:166 @echo $(TOMO_VERSION)168 check-c-compiler:169 @$(DEFAULT_C_COMPILER) -v 2>/dev/null >/dev/null \170 || { printf '\033[31;1m%s\033[m\n' "You have set your DEFAULT_C_COMPILER to $(DEFAULT_C_COMPILER) in your config.mk, but I can't run it!"; exit 1; }172 check-libs: check-c-compiler173 @echo 'int main() { return 0; }' | $(DEFAULT_C_COMPILER) $(LDFLAGS) $(LDLIBS) -x c - -o /dev/null 2>/dev/null >/dev/null \174 || { printf '\033[31;1m%s\033[m\n' "I expected to find the following libraries on your system, but I can't find them: $(LDLIBS)"; exit 1; }176 tags:177 ctags src/*.{c,h} src/stdlib/*.{c,h} src/compile/*.{c,h} src/parse/*.{c,h} src/formatter/*.{c,h}179 config.mk: configure.sh180 bash ./configure.sh182 %.o: %.c src/ast.h src/environment.h src/types.h config.mk183 @$(ECHO) $(CC) $(CFLAGS_PLACEHOLDER) -c $< -o $@184 @$(CC) $(CFLAGS) -c $< -o $@186 # Integer implementations depend on the shared header:187 src/stdlib/int64.o src/stdlib/int32.o src/stdlib/int16.o src/stdlib/int8.o: src/stdlib/intX.c.h src/stdlib/intX.h189 # Num implementations depend on the shared header:190 src/stdlib/num32.o src/stdlib/num64.o: src/stdlib/numX.c.h192 %: %.tm193 ./local-tomo -e $<195 test/results/%.tm.testresult: test/%.tm build196 @mkdir -p test/results197 @printf '\033[33;1;4m%s\033[m\n' $<198 @if ! COLOR=1 LC_ALL=C ./local-tomo -O 1 $< 2>&1 | tee $@; then \199 rm -f $@; \200 false; \201 fi203 test: $(TESTS)204 @printf '\033[32;7m ALL TESTS PASSED! \033[m\n'206 clean:207 rm -rf build/* $(COMPILER_OBJS) $(STDLIB_OBJS) test/*.tm.testresult test/.build lib/*/.build examples/.build examples/*/.build209 %: %.md210 pandoc --lua-filter=docs/.pandoc/bold-code.lua -s $< -t man -o $@212 %.md: %.yaml scripts/api_gen.py213 ./scripts/api_gen.py $< >$@215 api/api.md: $(API_YAML)216 ./scripts/api_gen.py $^ >$@218 test/api.tm: $(API_YAML) | ./scripts/api_tests.py219 ./scripts/api_tests.py $^ >$@221 .PHONY: api-docs222 api-docs: $(API_MD) api/api.md224 .PHONY: manpages225 manpages: $(API_YAML) man/man1/tomo.1226 ./scripts/mandoc_gen.py $(API_YAML)228 man/man1/tomo.1: docs/tomo.1.md229 pandoc --lua-filter=docs/.pandoc/bold-code.lua -s $< -t man -o $@231 examples:232 ./local-tomo -L packages/examples.ini233 ./local-tomo examples/learnxiny.tm235 core-libs:236 ./local-tomo -L packages/core.ini238 deps:239 bash ./install_dependencies.sh241 check-utilities: check-c-compiler242 @which debugedit 2>/dev/null >/dev/null \243 || printf '\033[33;1m%s\033[m\n' "I couldn't find 'debugedit' on your system! Try installing the package 'debugedit' with your package manager. (It's not required though)"245 install-files: build check-utilities246 @if ! echo "$$PATH" | tr ':' '\n' | grep -qx "$(PREFIX)/bin"; then \247 echo $$PATH; \248 printf "\033[31;1mError: '$(PREFIX)/bin' is not in your \$$PATH variable!\033[m\n" >&2; \249 printf "\033[31;1mSpecify a different prefix with 'make PREFIX=... install'\033[m\n" >&2; \250 printf "\033[31;1mor add the following line to your .profile:\033[m\n" >&2; \251 printf "\n\033[1mexport PATH=\"$(PREFIX):\$$PATH\"\033[m\n\n" >&2; \252 exit 1; \253 fi254 if ! [ -w "$(PREFIX)" ]; then \255 $(SUDO) -u $(OWNER) $(MAKE) install-files; \256 exit 0; \257 fi; \258 cp -R $(BUILD_DIR)/* $(PREFIX)/260 install: install-files262 uninstall:263 if ! [ -w "$(PREFIX)" ]; then \264 $(SUDO) -u $(OWNER) $(MAKE) uninstall; \265 exit 0; \266 fi; \267 rm -rvf "$(PREFIX)/bin/tomo" "$(PREFIX)/bin/tomo"* "$(PREFIX)/include/tomo"* \268 "$(PREFIX)/lib/libtomo@"* "$(PREFIX)/lib/tomo@"* "$(PREFIX)/share/licenses/tomo@"* \269 ~/.local/tomo/state/tomo@$(TOMO_VERSION); \271 endif273 .SUFFIXES:274 .PHONY: all build clean install install-files uninstall test tags core-libs examples deps check-utilities check-c-compiler check-libs version