code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(274 lines)
1 SHELL=bash -o pipefail
2 # Run ./configure.sh to choose installation locations:
3 ifeq ($(wildcard config.mk),)
4 all: config.mk
5 $(MAKE) all
6 install: config.mk
7 $(MAKE) install
8 install-files: config.mk
9 $(MAKE) install-files
10 install-lib: config.mk
11 $(MAKE) install-lib
12 test: config.mk
13 $(MAKE) test
14 config.mk: configure.sh
15 bash ./configure.sh
16 else
18 include config.mk
20 # Modified progress counter based on: https://stackoverflow.com/a/35320895
21 ifndef NO_PROGRESS
22 ifndef ECHO
23 T := $(shell $(MAKE) ECHO="COUNTTHIS" $(MAKECMDGOALS) --no-print-directory \
24 -n | grep -c "COUNTTHIS")
25 N := x
26 C = $(words $N)$(eval N := x $N)
27 ECHO = echo -e "[`expr $C '*' 100 / $T`%]"
28 endif
29 endif
30 ifndef ECHO
31 ECHO = echo
32 endif
33 # End of progress counter
35 CC=$(DEFAULT_C_COMPILER)
36 CCONFIG=-std=c2x -fPIC \
37 -fno-signed-zeros -fno-trapping-math \
38 -fvisibility=hidden -fdollars-in-identifiers \
39 -DGC_THREADS
40 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-strings
59 ifeq ($(SUDO),)
60 ifeq ($(shell command -v doas 2>/dev/null),)
61 SUDO=sudo
62 else
63 SUDO=doas
64 endif
65 endif
67 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=strict
74 CCONFIG += -fsanitize=signed-integer-overflow -fno-sanitize-recover -fno-signaling-nans -fno-finite-math-only
75 endif
77 OS := $(shell uname -s)
79 OSFLAGS != case $(OS) in *BSD|Darwin) echo '-D_BSD_SOURCE';; Linux) echo '-D_GNU_SOURCE';; *) echo '-D_DEFAULT_SOURCE';; esac
80 EXTRA=
81 G=-ggdb
82 O=-O3
83 # 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-sections
89 CFLAGS_PLACEHOLDER="$$(printf '\033[2m<flags...>\033[m\n')"
90 LDLIBS=-lgc -lm -lunistring -lgmp -lcrypto
92 ifeq ($(OS),OpenBSD)
93 LDLIBS += -lexecinfo
94 else
95 LDLIBS += -ldl
96 endif
98 AR_FILE=libtomo@$(TOMO_VERSION).a
99 ifeq ($(OS),Darwin)
100 INCLUDE_DIRS += -I/opt/homebrew/include
101 LDFLAGS += -L/opt/homebrew/lib
102 endif
103 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.tm
112 @$(ECHO) "All done!"
114 dist: all
115 rm -f build/tomo@$(TOMO_VERSION)_$$(uname -sm | tr ' ' '-').tar.gz
116 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 .gz
123 build_manpages := $(patsubst %,$(BUILD_DIR)/%.gz,$(wildcard man/man*/*))
125 # Ensure directories exist
126 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 headers
138 $(BUILD_DIR)/include/tomo@$(TOMO_VERSION)%.h: src/stdlib/%.h | $(BUILD_DIR)/include/tomo@$(TOMO_VERSION)
139 cp $< $@
141 # Rule for gzipping man pages
142 $(BUILD_DIR)/man/%.gz: man/% | $(BUILD_DIR)/man/man1 $(BUILD_DIR)/man/man3
143 gzip -c $< > $@
145 $(BUILD_DIR)/bin/tomo: $(BUILD_DIR)/bin/tomo@$(TOMO_VERSION) | $(BUILD_DIR)/bin
146 ln -sf tomo@$(TOMO_VERSION) $@
148 $(BUILD_DIR)/bin/$(EXE_FILE): $(STDLIB_OBJS) $(COMPILER_OBJS) | $(BUILD_DIR)/bin
149 @$(ECHO) $(CC) $(CFLAGS_PLACEHOLDER) $(LDFLAGS) $^ $(LDLIBS) -o $@
150 @$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
152 $(BUILD_DIR)/lib/$(AR_FILE): $(STDLIB_OBJS) | $(BUILD_DIR)/lib
153 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-compiler
173 @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.sh
180 bash ./configure.sh
182 %.o: %.c src/ast.h src/environment.h src/types.h config.mk
183 @$(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.h
189 # Num implementations depend on the shared header:
190 src/stdlib/num32.o src/stdlib/num64.o: src/stdlib/numX.c.h
192 %: %.tm
193 ./local-tomo -e $<
195 test/results/%.tm.testresult: test/%.tm build
196 @mkdir -p test/results
197 @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; \
203 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/*/.build
209 %: %.md
210 pandoc --lua-filter=docs/.pandoc/bold-code.lua -s $< -t man -o $@
212 %.md: %.yaml scripts/api_gen.py
213 ./scripts/api_gen.py $< >$@
215 api/api.md: $(API_YAML)
216 ./scripts/api_gen.py $^ >$@
218 test/api.tm: $(API_YAML) | ./scripts/api_tests.py
219 ./scripts/api_tests.py $^ >$@
221 .PHONY: api-docs
222 api-docs: $(API_MD) api/api.md
224 .PHONY: manpages
225 manpages: $(API_YAML) man/man1/tomo.1
226 ./scripts/mandoc_gen.py $(API_YAML)
228 man/man1/tomo.1: docs/tomo.1.md
229 pandoc --lua-filter=docs/.pandoc/bold-code.lua -s $< -t man -o $@
231 examples:
232 ./local-tomo -L packages/examples.ini
233 ./local-tomo examples/learnxiny.tm
235 core-libs:
236 ./local-tomo -L packages/core.ini
238 deps:
239 bash ./install_dependencies.sh
241 check-utilities: check-c-compiler
242 @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-utilities
246 @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; \
254 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-files
262 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 endif
273 .SUFFIXES:
274 .PHONY: all build clean install install-files uninstall test tags core-libs examples deps check-utilities check-c-compiler check-libs version