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 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 27 | ifneq (,$(wildcard ./Jmk.options)) |
| 28 | include Jmk.options |
| 29 | endif |
| 30 | |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 31 | all: $(jmk_target)') |
| 32 | |
| 33 | dnl preset applies a certain configuration preset to the project |
| 34 | define(preset, |
| 35 | `ifelse($1, `freestanding', |
| 36 | `CFLAGS += -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding', |
| 37 | $1, `optimize', `CFLAGS += -O2', |
| 38 | $1, `debug', `CFLAGS += -g', |
| 39 | $1, `32', `CFLAGS += -m32', |
| 40 | $1, `warn', `CFLAGS += -Wall -Wno-unused-function -Wno-unused-variable', |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 41 | $1, `nasm', `ASM = nasm')') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 42 | |
| 43 | dnl this is really, really terrible, but to my knowledge there is no |
| 44 | dnl other way to escape a $. The manual says nothing about this. |
| 45 | define(ident, $1) |
| 46 | define(dollar_at, `ident($)ident(@)') |
| 47 | |
| 48 | dnl archetype enables a language archetype |
| 49 | define(archetype, |
| 50 | `ifelse($1, c, `.c.o: |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 51 | status_log(CC, $<) |
| 52 | @$(CC) -c $< -o dollar_at $(CFLAGS)', |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 53 | $1, asm, `.s.o: |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 54 | status_log(AS, $<) |
| 55 | @$(ASM) $(ASMFLAGS) $< -o dollar_at')') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 56 | |
| 57 | dnl depends declares an external dependency |
| 58 | define(`depends', ` |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 59 | jmk_clean_libs += jmk_lib_clean_$1 |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 60 | jmk_lib_path_$1 = $2 |
| 61 | jmk_lib_target_$1 = ifelse($3, `', $1.a, $3) |
| 62 | jmk_libs_phony += $(jmk_lib_path_$1)/$(jmk_lib_target_$1) |
| 63 | |
| 64 | $(jmk_lib_path_$1)/$(jmk_lib_target_$1): |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 65 | status_log(MAKE[$(MAKEFILE_DEPTH)], Entering $2) |
| 66 | @DO_MAKE -C $(jmk_lib_path_$1) $(jmk_lib_target_$1) |
| 67 | status_log(MAKE[$(MAKEFILE_DEPTH)], Leaving $2) |
| 68 | |
| 69 | jmk_lib_clean_$1: |
| 70 | @DO_MAKE -C $2 clean') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 71 | |
| 72 | dnl lib is used to list an external dependency declared with depends() |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame^] | 73 | define_blind(`lib', `$(jmk_lib_path_$1)/$(jmk_lib_target_$1)') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 74 | |
| 75 | define(`phony', `jmk_custom_phony += $1') |
| 76 | |
| 77 | dnl type is used to specify the target type |
| 78 | define(type, |
| 79 | `ifelse($1, executable, |
| 80 | `$(jmk_target): $(OBJECTS) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 81 | status_log(LD, dollar_at) |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 82 | @$(CC) -o dollar_at $^ $(CFLAGS)', |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 83 | $1, static_lib, |
| 84 | `$(jmk_target): $(OBJECTS) |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 85 | status_log(AR, dollar_at) |
| 86 | @ar rcs dollar_at $^', |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 87 | $1, custom_link, |
| 88 | `$(jmk_target): $(OBJECTS) |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 89 | status_log(LD, dollar_at) |
| 90 | @$(LD) $(LDFLAGS) -o dollar_at $^')') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 91 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 92 | define(option, |
| 93 | `$1 ?= $3 |
| 94 | jmk_options += $1 |
| 95 | jmk_option_help_$1 = $2 (default: $3)') |
| 96 | |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 97 | dnl finish is required at the end of the Jmk file to generate some |
| 98 | dnl final declarations |
| 99 | |
| 100 | define(finish, |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 101 | `clean: $(jmk_clean_libs) |
| 102 | @rm -f *.o *.a *.so $(jmk_target) |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 103 | |
| 104 | Makefile: Jmk |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 105 | status_log(JMK, jmk_build_dir) |
| 106 | @cd "jmk_build_dir" && jmk_build_cmd |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 107 | |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 108 | .PHONY: $(jmk_libs_phony) $(jmk_custom_phony) $(jmk_clean_libs) clean all') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 109 | |
| 110 | divert(0)dnl |