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