swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 1 | divert(-1) |
| 2 | |
| 3 | dnl |
| 4 | dnl JMK (Jay MaKe) build system |
| 5 | dnl |
| 6 | |
| 7 | define(`foreach', `pushdef(`$1')_foreach($@)popdef(`$1')') |
| 8 | define(`_arg1', `$1') |
| 9 | define(`_foreach', `ifelse(`$2', `()', `', |
| 10 | `define(`$1', _arg1$2)$3`'$0(`$1', (shift$2), `$3')')') |
| 11 | |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 12 | define(status_log, ` @printf " \e[1;34m%8s\e[m %s\n" "$1" "$2"') |
| 13 | define(DO_MAKE, `$(MAKE) --no-print-directory MAKEFILE_DEPTH="$$(( $(MAKEFILE_DEPTH) + 1))"') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 14 | |
| 15 | define(init, |
| 16 | `jmk_project := $1 |
| 17 | jmk_target = ifelse(`$#', 2, $2, $1) |
| 18 | ROOT := jmk_root |
| 19 | ASM ?= nasm |
| 20 | CC ?= gcc |
| 21 | LD ?= ld |
| 22 | CFLAGS += -I$(ROOT)/include |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 23 | jmk_clean_libs = |
| 24 | |
| 25 | MAKEFILE_DEPTH ?= 1 |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 26 | |
| 27 | all: $(jmk_target)') |
| 28 | |
| 29 | dnl preset applies a certain configuration preset to the project |
| 30 | define(preset, |
| 31 | `ifelse($1, `freestanding', |
| 32 | `CFLAGS += -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding', |
| 33 | $1, `optimize', `CFLAGS += -O2', |
| 34 | $1, `debug', `CFLAGS += -g', |
| 35 | $1, `32', `CFLAGS += -m32', |
| 36 | $1, `warn', `CFLAGS += -Wall -Wno-unused-function -Wno-unused-variable', |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 37 | $1, `nasm', `ASM = nasm')') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 38 | |
| 39 | dnl this is really, really terrible, but to my knowledge there is no |
| 40 | dnl other way to escape a $. The manual says nothing about this. |
| 41 | define(ident, $1) |
| 42 | define(dollar_at, `ident($)ident(@)') |
| 43 | |
| 44 | dnl archetype enables a language archetype |
| 45 | define(archetype, |
| 46 | `ifelse($1, c, `.c.o: |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 47 | status_log(CC, $<) |
| 48 | @$(CC) -c $< -o dollar_at $(CFLAGS)', |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 49 | $1, asm, `.s.o: |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 50 | status_log(AS, $<) |
| 51 | @$(ASM) $(ASMFLAGS) $< -o dollar_at')') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 52 | |
| 53 | dnl depends declares an external dependency |
| 54 | define(`depends', ` |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 55 | jmk_clean_libs += jmk_lib_clean_$1 |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 56 | jmk_lib_path_$1 = $2 |
| 57 | jmk_lib_target_$1 = ifelse($3, `', $1.a, $3) |
| 58 | jmk_libs_phony += $(jmk_lib_path_$1)/$(jmk_lib_target_$1) |
| 59 | |
| 60 | $(jmk_lib_path_$1)/$(jmk_lib_target_$1): |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 61 | status_log(MAKE[$(MAKEFILE_DEPTH)], Entering $2) |
| 62 | @DO_MAKE -C $(jmk_lib_path_$1) $(jmk_lib_target_$1) |
| 63 | status_log(MAKE[$(MAKEFILE_DEPTH)], Leaving $2) |
| 64 | |
| 65 | jmk_lib_clean_$1: |
| 66 | @DO_MAKE -C $2 clean') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 67 | |
| 68 | dnl lib is used to list an external dependency declared with depends() |
| 69 | define(`lib', `$(jmk_lib_path_$1)/$(jmk_lib_target_$1)') |
| 70 | |
| 71 | define(`phony', `jmk_custom_phony += $1') |
| 72 | |
| 73 | dnl type is used to specify the target type |
| 74 | define(type, |
| 75 | `ifelse($1, executable, |
| 76 | `$(jmk_target): $(OBJECTS) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 77 | status_log(LD, dollar_at) |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 78 | @$(CC) -o dollar_at $^ $(CFLAGS)', |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 79 | $1, static_lib, |
| 80 | `$(jmk_target): $(OBJECTS) |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 81 | status_log(AR, dollar_at) |
| 82 | @ar rcs dollar_at $^', |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 83 | $1, custom_link, |
| 84 | `$(jmk_target): $(OBJECTS) |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 85 | status_log(LD, dollar_at) |
| 86 | @$(LD) $(LDFLAGS) -o dollar_at $^')') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 87 | |
| 88 | dnl finish is required at the end of the Jmk file to generate some |
| 89 | dnl final declarations |
| 90 | |
| 91 | define(finish, |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 92 | `clean: $(jmk_clean_libs) |
| 93 | @rm -f *.o *.a *.so $(jmk_target) |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 94 | |
| 95 | Makefile: Jmk |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 96 | status_log(JMK, jmk_build_dir) |
| 97 | @cd "jmk_build_dir" && jmk_build_cmd |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 98 | |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 99 | .PHONY: $(jmk_libs_phony) $(jmk_custom_phony) $(jmk_clean_libs) clean all') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 100 | |
| 101 | divert(0)dnl |