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