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 {} | ||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 5 | variable jmk_clean_libs {} |
6 | variable jmk_phony_libs {} | ||||
7 | variable jmk_lib_paths | ||||
8 | variable jmk_lib_targets | ||||
9 | |||||
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 10 | variable cflags {} |
11 | variable asmflags {} | ||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 12 | variable ldflags {} |
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 13 | |
14 | variable asm as | ||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 15 | variable cc gcc |
16 | variable ld ld | ||||
17 | |||||
18 | variable objs {} | ||||
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 19 | |
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 20 | # variable options |
21 | |||||
22 | variable src \$^ | ||||
23 | variable first_src \$< | ||||
24 | variable target \$@ | ||||
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 25 | |
26 | proc init {name {target {DEFAULT_TARGET}}} { | ||||
27 | if {$target eq {DEFAULT_TARGET}} { | ||||
28 | set target $name | ||||
29 | } | ||||
30 | |||||
31 | global jmk_name | ||||
32 | global jmk_target | ||||
33 | |||||
34 | set jmk_name $name | ||||
35 | set jmk_target $target | ||||
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 36 | |
37 | puts {MAKEFILE_DEPTH ?= 1} | ||||
38 | |||||
39 | rule all $target {} | ||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 40 | |
41 | rule Makefile Jmk2 { | ||||
42 | log JMK2 "" | ||||
43 | shell "cd $::jmk_build_dir && $::jmk_build_cmd" | ||||
44 | } | ||||
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 45 | } |
46 | |||||
47 | proc preset {p} { | ||||
48 | ::preset::$p | ||||
49 | } | ||||
50 | |||||
51 | proc presets {args} { | ||||
52 | foreach arg $args { | ||||
53 | preset $arg | ||||
54 | } | ||||
55 | } | ||||
56 | |||||
57 | proc cflag {arg} { | ||||
58 | global cflags | ||||
59 | set cflags "$cflags $arg" | ||||
60 | } | ||||
61 | |||||
62 | proc cflags {args} { | ||||
63 | foreach arg $args { | ||||
64 | cflag $arg | ||||
65 | } | ||||
66 | } | ||||
67 | |||||
68 | proc asmflag {arg} { | ||||
69 | global asmflags | ||||
70 | set asmflags "$asmflags $arg" | ||||
71 | } | ||||
72 | |||||
73 | proc asmflags {args} { | ||||
74 | foreach arg $args { | ||||
75 | asmflag $arg | ||||
76 | } | ||||
77 | } | ||||
78 | |||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 79 | proc ldflag {arg} { |
80 | global ldflags | ||||
81 | set ldflags "$ldflags $arg" | ||||
82 | } | ||||
83 | |||||
84 | proc ldflags {args} { | ||||
85 | foreach arg $args { | ||||
86 | ldflag $arg | ||||
87 | } | ||||
88 | } | ||||
89 | |||||
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 90 | proc option {name val} { |
91 | global options | ||||
92 | if {![info exists options($name)]} { | ||||
93 | set options($name) $val | ||||
94 | } | ||||
95 | } | ||||
96 | |||||
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 97 | proc log {category message} { |
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 98 | puts "\t@printf ' \\e\[1;34m%8s\\e\[m %s\\n' '$category' '$message' > /dev/stderr" |
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 99 | } |
100 | |||||
101 | proc cc {command} { | ||||
102 | puts "\t@$::cc $command $::cflags" | ||||
103 | } | ||||
104 | |||||
105 | proc asm {command} { | ||||
106 | puts "\t@$::asm $command $::asmflags" | ||||
107 | } | ||||
108 | |||||
109 | proc shell {command} { | ||||
110 | puts "\t@$command" | ||||
111 | } | ||||
112 | |||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 113 | proc make {command} { |
114 | shell "\$(MAKE) --no-print-directory MAKEFILE_DEPTH=\$\$((\$(MAKEFILE_DEPTH)+1)) $command" | ||||
115 | } | ||||
116 | |||||
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 117 | proc rule {target deps does} { |
118 | puts "" | ||||
119 | puts "$target: $deps" | ||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 120 | uplevel 1 $does |
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 121 | } |
122 | |||||
123 | proc type {type} { | ||||
124 | ::type::$type | ||||
125 | } | ||||
126 | |||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 127 | proc objs {args} { |
128 | foreach obj $args { | ||||
129 | set ::objs "$::objs $obj" | ||||
130 | } | ||||
131 | } | ||||
132 | |||||
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 133 | proc srcs {args} { |
134 | puts "" | ||||
135 | variable objs "" | ||||
136 | |||||
137 | foreach src $args { | ||||
138 | variable obj [regsub -- {(.+)\.\w+} $src {\1.o}] | ||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 139 | set ::objs "$::objs $obj" |
140 | } | ||||
141 | } | ||||
142 | |||||
143 | proc depends {name path {target DEFAULT_TARGET}} { | ||||
144 | if {$target eq {DEFAULT_TARGET}} { | ||||
145 | variable target "lib${name}.a" | ||||
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 146 | } |
147 | |||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 148 | set ::jmk_clean_libs "$::jmk_clean_libs $path" |
149 | set ::jmk_lib_paths($name) $path | ||||
150 | set ::jmk_lib_target($name) $target | ||||
151 | set ::jmk_phony_libs "$::jmk_phony_libs $path" | ||||
152 | |||||
153 | rule "$path/$target" {} { | ||||
154 | log "MAKE\[\$(MAKEFILE_DEPTH)\]" "Entering $name" | ||||
155 | make "-C $path $target" | ||||
156 | log "MAKE\[\$(MAKEFILE_DEPTH)\]" "Leaving $name" | ||||
157 | } | ||||
158 | } | ||||
159 | |||||
160 | proc lib {name} { | ||||
161 | return "$::jmk_lib_paths($name)/$::jmk_lib_target($name)" | ||||
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 162 | } |
163 | |||||
164 | namespace eval type { | ||||
165 | proc executable {} { | ||||
166 | global jmk_target | ||||
167 | |||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 168 | rule $jmk_target $::objs { |
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 169 | log LD $::target |
170 | cc "-o $::target $::src" | ||||
171 | } | ||||
172 | |||||
173 | helpers | ||||
174 | } | ||||
175 | |||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 176 | proc custom_link {} { |
177 | global jmk_target | ||||
178 | |||||
179 | rule $jmk_target $::objs { | ||||
180 | log LD $::target | ||||
181 | shell "$::ld $::ldflags -o $::target $::src" | ||||
182 | } | ||||
183 | |||||
184 | helpers | ||||
185 | } | ||||
186 | |||||
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 187 | proc helpers {} { |
188 | rule .c.o {} { | ||||
189 | log CC $::first_src | ||||
190 | cc "-c $::first_src -o $::target" | ||||
191 | } | ||||
192 | |||||
193 | rule .s.o {} { | ||||
194 | log ASM $::first_src | ||||
195 | asm "\$(ASMFLAGS) $::first_src -o $::target" | ||||
196 | } | ||||
197 | |||||
198 | rule clean {} { | ||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 199 | shell "rm -f **/*.o **/*.a *.so $::jmk_target $::objs" |
200 | |||||
201 | foreach lib $::jmk_clean_libs { | ||||
202 | make "-C $lib clean" | ||||
203 | } | ||||
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 204 | } |
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 205 | } |
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 206 | } |
207 | |||||
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 208 | namespace eval preset { |
swissChili | 3555885 | 2022-07-02 18:15:45 -0700 | [diff] [blame] | 209 | proc freestanding {} { |
210 | cflags -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding | ||||
211 | } | ||||
212 | |||||
213 | proc optimize {} { | ||||
214 | cflags -O2 | ||||
215 | } | ||||
216 | |||||
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 217 | proc 32 {} { |
218 | cflag -m32 | ||||
219 | asmflag -felf32 | ||||
220 | } | ||||
221 | |||||
222 | proc debug {} { | ||||
223 | cflag -g | ||||
224 | asmflag -Fdwarf | ||||
225 | } | ||||
226 | |||||
227 | proc warn {} { | ||||
swissChili | f3d6576 | 2022-07-03 22:34:54 -0700 | [diff] [blame^] | 228 | cflags -Wall -Wno-unused-function -Wno-unused-variable -Wno-incompatible-pointer-types -Wno-sign-compare |
swissChili | 0fc3f26 | 2021-08-09 22:05:17 -0700 | [diff] [blame] | 229 | } |
230 | |||||
231 | proc nasm {} { | ||||
232 | global asm | ||||
233 | set asm nasm | ||||
234 | } | ||||
235 | } |