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 | |
| 12 | |
| 13 | |
| 14 | define(init, |
| 15 | `jmk_project := $1 |
| 16 | jmk_target = ifelse(`$#', 2, $2, $1) |
| 17 | ROOT := jmk_root |
| 18 | ASM ?= nasm |
| 19 | CC ?= gcc |
| 20 | LD ?= ld |
| 21 | CFLAGS += -I$(ROOT)/include |
| 22 | |
| 23 | all: $(jmk_target)') |
| 24 | |
| 25 | dnl preset applies a certain configuration preset to the project |
| 26 | define(preset, |
| 27 | `ifelse($1, `freestanding', |
| 28 | `CFLAGS += -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding', |
| 29 | $1, `optimize', `CFLAGS += -O2', |
| 30 | $1, `debug', `CFLAGS += -g', |
| 31 | $1, `32', `CFLAGS += -m32', |
| 32 | $1, `warn', `CFLAGS += -Wall -Wno-unused-function -Wno-unused-variable', |
| 33 | $1, `nasm', `ASM = nasm' |
| 34 | )') |
| 35 | |
| 36 | dnl this is really, really terrible, but to my knowledge there is no |
| 37 | dnl other way to escape a $. The manual says nothing about this. |
| 38 | define(ident, $1) |
| 39 | define(dollar_at, `ident($)ident(@)') |
| 40 | |
| 41 | dnl archetype enables a language archetype |
| 42 | define(archetype, |
| 43 | `ifelse($1, c, `.c.o: |
| 44 | $(CC) -c $< -o dollar_at $(CFLAGS)', |
| 45 | $1, asm, `.s.o: |
| 46 | $(ASM) $(ASMFLAGS) $< -o dollar_at' |
| 47 | )' |
| 48 | ) |
| 49 | |
| 50 | dnl depends declares an external dependency |
| 51 | define(`depends', ` |
| 52 | jmk_lib_path_$1 = $2 |
| 53 | jmk_lib_target_$1 = ifelse($3, `', $1.a, $3) |
| 54 | jmk_libs_phony += $(jmk_lib_path_$1)/$(jmk_lib_target_$1) |
| 55 | |
| 56 | $(jmk_lib_path_$1)/$(jmk_lib_target_$1): |
| 57 | $(MAKE) -C $(jmk_lib_path_$1) $(jmk_lib_target_$1)') |
| 58 | |
| 59 | dnl lib is used to list an external dependency declared with depends() |
| 60 | define(`lib', `$(jmk_lib_path_$1)/$(jmk_lib_target_$1)') |
| 61 | |
| 62 | define(`phony', `jmk_custom_phony += $1') |
| 63 | |
| 64 | dnl type is used to specify the target type |
| 65 | define(type, |
| 66 | `ifelse($1, executable, |
| 67 | `$(jmk_target): $(OBJECTS) |
| 68 | $(CC) -o dollar_at $^ $(CFLAGS)', |
| 69 | $1, static_lib, |
| 70 | `$(jmk_target): $(OBJECTS) |
| 71 | ar rcs dollar_at $^', |
| 72 | $1, custom_link, |
| 73 | `$(jmk_target): $(OBJECTS) |
| 74 | $(LD) $(LDFLAGS) -o dollar_at $^' |
| 75 | )') |
| 76 | |
| 77 | dnl finish is required at the end of the Jmk file to generate some |
| 78 | dnl final declarations |
| 79 | |
| 80 | define(finish, |
| 81 | `clean: |
| 82 | rm -f *.o *.a *.so $(jmk_target) |
| 83 | |
| 84 | Makefile: Jmk |
| 85 | cd "jmk_build_dir" && jmk_build_cmd |
| 86 | |
| 87 | .PHONY: $(jmk_libs_phony) $(jmk_custom_phony) clean all') |
| 88 | |
| 89 | divert(0)dnl |