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