swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 1 | # Rewrite of JMK in TCL |
| 2 | |
| 3 | variable jmk_name {} |
| 4 | variable jmk_target {} |
| 5 | variable cflags {} |
| 6 | variable asmflags {} |
| 7 | |
| 8 | variable asm as |
| 9 | variable cc cc |
| 10 | |
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame^] | 11 | # variable options |
| 12 | |
| 13 | variable src \$^ |
| 14 | variable first_src \$< |
| 15 | variable target \$@ |
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 16 | |
| 17 | proc init {name {target {DEFAULT_TARGET}}} { |
| 18 | if {$target eq {DEFAULT_TARGET}} { |
| 19 | set target $name |
| 20 | } |
| 21 | |
| 22 | global jmk_name |
| 23 | global jmk_target |
| 24 | |
| 25 | set jmk_name $name |
| 26 | set jmk_target $target |
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame^] | 27 | |
| 28 | puts {MAKEFILE_DEPTH ?= 1} |
| 29 | |
| 30 | rule all $target {} |
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | proc preset {p} { |
| 34 | ::preset::$p |
| 35 | } |
| 36 | |
| 37 | proc presets {args} { |
| 38 | foreach arg $args { |
| 39 | preset $arg |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | proc cflag {arg} { |
| 44 | global cflags |
| 45 | set cflags "$cflags $arg" |
| 46 | } |
| 47 | |
| 48 | proc cflags {args} { |
| 49 | foreach arg $args { |
| 50 | cflag $arg |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | proc asmflag {arg} { |
| 55 | global asmflags |
| 56 | set asmflags "$asmflags $arg" |
| 57 | } |
| 58 | |
| 59 | proc asmflags {args} { |
| 60 | foreach arg $args { |
| 61 | asmflag $arg |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | proc option {name val} { |
| 66 | global options |
| 67 | if {![info exists options($name)]} { |
| 68 | set options($name) $val |
| 69 | } |
| 70 | } |
| 71 | |
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame^] | 72 | proc log {category message} { |
| 73 | puts "\t@printf '\\e\[1;34m%8s\\e\[m %s\\n' '$category' '$message' > /dev/stderr" |
| 74 | } |
| 75 | |
| 76 | proc cc {command} { |
| 77 | puts "\t@$::cc $command $::cflags" |
| 78 | } |
| 79 | |
| 80 | proc asm {command} { |
| 81 | puts "\t@$::asm $command $::asmflags" |
| 82 | } |
| 83 | |
| 84 | proc shell {command} { |
| 85 | puts "\t@$command" |
| 86 | } |
| 87 | |
| 88 | proc rule {target deps does} { |
| 89 | puts "" |
| 90 | puts "$target: $deps" |
| 91 | eval $does |
| 92 | } |
| 93 | |
| 94 | proc type {type} { |
| 95 | ::type::$type |
| 96 | } |
| 97 | |
| 98 | proc srcs {args} { |
| 99 | puts "" |
| 100 | variable objs "" |
| 101 | |
| 102 | foreach src $args { |
| 103 | variable obj [regsub -- {(.+)\.\w+} $src {\1.o}] |
| 104 | variable objs "$objs $obj" |
| 105 | } |
| 106 | |
| 107 | puts "OBJECTS += $objs" |
| 108 | } |
| 109 | |
| 110 | namespace eval type { |
| 111 | proc executable {} { |
| 112 | global jmk_target |
| 113 | |
| 114 | rule $jmk_target "\$(OBJECTS)" { |
| 115 | log LD $::target |
| 116 | cc "-o $::target $::src" |
| 117 | } |
| 118 | |
| 119 | helpers |
| 120 | } |
| 121 | |
| 122 | proc helpers {} { |
| 123 | rule .c.o {} { |
| 124 | log CC $::first_src |
| 125 | cc "-c $::first_src -o $::target" |
| 126 | } |
| 127 | |
| 128 | rule .s.o {} { |
| 129 | log ASM $::first_src |
| 130 | asm "\$(ASMFLAGS) $::first_src -o $::target" |
| 131 | } |
| 132 | |
| 133 | rule clean {} { |
| 134 | shell "rm -f **/*.o **/*.a *.so $::target \$(OBJECTS)" |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 139 | namespace eval preset { |
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame^] | 140 | proc freestanding {} { |
| 141 | cflags -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding |
| 142 | } |
| 143 | |
| 144 | proc optimize {} { |
| 145 | cflags -O2 |
| 146 | } |
| 147 | |
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 148 | proc 32 {} { |
| 149 | cflag -m32 |
| 150 | asmflag -felf32 |
| 151 | } |
| 152 | |
| 153 | proc debug {} { |
| 154 | cflag -g |
| 155 | asmflag -Fdwarf |
| 156 | } |
| 157 | |
| 158 | proc warn {} { |
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame^] | 159 | cflags -Wall -Wextra -Wno-unused-function -Wno-unused-variable -Wno-incompatible-pointer-types -Werror |
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | proc nasm {} { |
| 163 | global asm |
| 164 | set asm nasm |
| 165 | } |
| 166 | } |