|
diff --git a/cmk.c b/cmk.c new file mode 100644 index 0000000..111ed49 --- /dev/null +++ b/ cmk.c |
| @@ -0,0 +1,295 @@ |
| |
1 | // cmk.c --- build system of this repo -*- C -*- |
| |
2 | // |
| |
3 | // Copyright (c) 2026 Martin Michalec <martin@michalec.dev> |
| |
4 | // |
| |
5 | // SPDX-License-Identifier: MIT |
| |
6 | // |
| |
7 | // This file is part of COSMOS Codebase. |
| |
8 | // |
| |
9 | // Commentary: |
| |
10 | // |
| |
11 | // NEXT(cmmm): implement notion of profiles (debug/release) |
| |
12 | // NEXT(cmmm): release as much as possible |
| |
13 | // NEXT(cmmm): write ADRs and articles about core concepts |
| |
14 | // TODO(cmmm): build static lib from ./build/obj/*.o |
| |
15 | // TODO(cmmm): generate compile_commands.json |
| |
16 | // |
| |
17 | // Code: |
| |
18 | |
| |
19 | const char *cmk_c = __FILE__; |
| |
20 | |
| |
21 | #ifndef CMK_BOOTSTRAPPED |
| |
22 | #include "include/cmk-bootstrap.h" |
| |
23 | #else // CMK_BOOTSTRAPPED |
| |
24 | |
| |
25 | #include "include/cmk.h" |
| |
26 | #include "cmk.h" |
| |
27 | #include "arg.h" |
| |
28 | |
| |
29 | #define CONF cosmos_codebase_config |
| |
30 | #define SRC(p) CMK_CAT (CONF.src, MV0 ("/" p ".c")) |
| |
31 | #define OBJ(p) CMK_CAT (CONF.obj, MV0 ("/" p ".o")) |
| |
32 | #define DEP(p) CMK_CAT (CONF.dep, MV0 ("/" p ".d")) |
| |
33 | #define BIN(p) CMK_CAT (CONF.bin, MV0 ("/" p CMK_EXEC_EXT)) |
| |
34 | #define NSP(s) CMK_CAT (CONF.ns, MV0 ( s)) |
| |
35 | #define ROT(s) CMK_CAT (CONF.root, MV0 ("/" s)) |
| |
36 | #define BLD(s) CMK_CAT (CONF.build, MV0 ("/" s)) |
| |
37 | #define INC(s) CMK_CAT (CONF.include, MV0 ("/" s)) |
| |
38 | #define ARC(p) CMK_CAT (CONF.build, MV0 ("/lib" p ".a")) |
| |
39 | |
| |
40 | #define X(sym, str, ...) [COSMOS_CODEBASE_PROFILE_ ## sym] = MB0 (str), |
| |
41 | const struct mb cosmos_codebase_profile_names[COSMOS_CODEBASE_PROFILE_CNT] = { |
| |
42 | COSMOS_CODEBASE_PROFILE_LIST () |
| |
43 | }; |
| |
44 | #undef X |
| |
45 | |
| |
46 | struct cosmos_codebase_config cosmos_codebase_config; |
| |
47 | |
| |
48 | void |
| |
49 | cosmos_codebase_configure (enum cosmos_codebase_profile profile, |
| |
50 | struct mv ns, |
| |
51 | struct mv root, |
| |
52 | struct mv build, |
| |
53 | struct mv cc) |
| |
54 | { |
| |
55 | DBG_ASSERT_IN (0, profile, COSMOS_CODEBASE_PROFILE_CNT - 1, "%d", "%d", "%d"); |
| |
56 | |
| |
57 | struct cosmos_codebase_config *c = &cosmos_codebase_config; |
| |
58 | |
| |
59 | c->ns = mv_or (ns, MV0 ("")); |
| |
60 | c->root = mv_or (root, MV0 (".")); |
| |
61 | c->build = CMK_CAT (mv_or (build, MV0 ("./build")), MV0 ("/"), |
| |
62 | cosmos_codebase_profile_name (profile)); |
| |
63 | c->include = CMK_CAT (c->root, MV0 ("/include")); |
| |
64 | c->src = CMK_CAT (c->root, MV0 ("/src")); |
| |
65 | c->obj = CMK_CAT (c->build, MV0 ("/obj")); |
| |
66 | c->dep = CMK_CAT (c->build, MV0 ("/dep")); |
| |
67 | c->bin = CMK_CAT (c->build, MV0 ("/bin")); |
| |
68 | |
| |
69 | // DA_PUSH (&c->ldflags, MV0 ("-lbacktrace")); |
| |
70 | |
| |
71 | // choose cc |
| |
72 | switch (profile) { |
| |
73 | case COSMOS_CODEBASE_PROFILE_REL_WITH_DEBINFO: |
| |
74 | case COSMOS_CODEBASE_PROFILE_RELEASE: |
| |
75 | case COSMOS_CODEBASE_PROFILE_REL_MIN_SIZE: |
| |
76 | c->cc = mv_or (cc, cmk_cc ()); |
| |
77 | break; |
| |
78 | case COSMOS_CODEBASE_PROFILE_DEVEL: |
| |
79 | c->cc = mv_or (cc, cmk_cc ()); |
| |
80 | #ifdef __TINYC__ |
| |
81 | c->cc = mv_or (cc, MV0 ("tcc")); |
| |
82 | #endif // __TINYC__ |
| |
83 | } |
| |
84 | |
| |
85 | // choose cflags |
| |
86 | DA_PUSH (&c->cflags, MV0 ("-DENABLE_TRACE_ERROR")); |
| |
87 | DA_PUSH (&c->cflags, CMK_CAT (MV0 ("-I"), c->include)); |
| |
88 | DA_PUSH (&c->cflags, CMK_CAT (MV0 ("-I"), c->src )); |
| |
89 | DA_PUSH (&c->cflags, CMK_CAT (MV0 ("-I"), c->build )); |
| |
90 | // DA_PUSH (&c->cflags, MV0 ("-std=c99")); |
| |
91 | DA_PUSH (&c->cflags, MV0 ("-Wall")); |
| |
92 | DA_PUSH (&c->cflags, MV0 ("-Wextra")); |
| |
93 | DA_PUSH (&c->cflags, MV0 ("-Wno-unused-function")); |
| |
94 | DA_PUSH (&c->cflags, MV0 ("-Wno-unused-variable")); |
| |
95 | DA_PUSH (&c->cflags, MV0 ("-Wno-unused-parameter")); |
| |
96 | DA_PUSH (&c->cflags, MV0 ("-Wno-unused-result")); |
| |
97 | DA_PUSH (&c->cflags, MV0 ("-Wno-parentheses")); |
| |
98 | DA_PUSH (&c->cflags, MV0 ("-Wno-comment")); |
| |
99 | switch (profile) { |
| |
100 | case COSMOS_CODEBASE_PROFILE_REL_WITH_DEBINFO: |
| |
101 | DA_PUSH (&c->cflags, MV0 ("-ggdb3")); |
| |
102 | FALLTHROUGH; |
| |
103 | case COSMOS_CODEBASE_PROFILE_RELEASE: |
| |
104 | DA_PUSH (&c->cflags, MV0 ("-O3")); |
| |
105 | DA_PUSH (&c->cflags, MV0 ("-march=native")); |
| |
106 | break; |
| |
107 | case COSMOS_CODEBASE_PROFILE_REL_MIN_SIZE: |
| |
108 | DA_PUSH (&c->cflags, MV0 ("-Oz")); |
| |
109 | DA_PUSH (&c->cflags, MV0 ("-march=native")); |
| |
110 | break; |
| |
111 | case COSMOS_CODEBASE_PROFILE_DEVEL: |
| |
112 | DA_PUSH (&c->cflags, MV0 ("-Werror")); |
| |
113 | if (mv_equal (c->cc, MV0 ("tcc"))) { |
| |
114 | // DA_PUSH (&c->cflags, MV0 ("-gdwarf-3")); |
| |
115 | DA_PUSH (&c->cflags, MV0 ("-b")); |
| |
116 | DA_PUSH (&c->cflags, MV0 ("-bt")); |
| |
117 | } else { |
| |
118 | DA_PUSH (&c->cflags, MV0 ("-ggdb3")); |
| |
119 | DA_PUSH (&c->cflags, MV0 ("-Og")); |
| |
120 | } |
| |
121 | break; |
| |
122 | } |
| |
123 | } |
| |
124 | |
| |
125 | #define COMPILE(...) cosmos_codebase_compile (CMK_LOC, __VA_ARGS__, MV_ARR_NULL) |
| |
126 | #define COMPILE_X(obj, dep, src, ...) cosmos_codebase_compile (CMK_LOC, obj, dep, src, MV_ARR (__VA_ARGS__)) |
| |
127 | struct mv |
| |
128 | cosmos_codebase_compile (struct cmk_loc loc, |
| |
129 | struct mv obj, |
| |
130 | struct mv dep, |
| |
131 | struct mv src, |
| |
132 | struct mv_arr ext) |
| |
133 | { |
| |
134 | struct cosmos_codebase_config *c = &cosmos_codebase_config; |
| |
135 | |
| |
136 | cmk_cmd_push_deps_only (cmk_load_depfile (dep)); |
| |
137 | cmk_cmd_push_dep_only (mv_from_mb0 (mv_dup0 (mv_peek_upto_u8 (dep, '/', MV_END | MV_INV)))); |
| |
138 | cmk_cmd_push_dep_only (mv_from_mb0 (mv_dup0 (mv_peek_upto_u8 (obj, '/', MV_END | MV_INV)))); |
| |
139 | |
| |
140 | cmk_cmd_push_ords (MV_ARR (c->cc, MV0 ("-c"))); |
| |
141 | cmk_cmd_push_ords (MV_ARR_FROM_DA (c->cflags)); |
| |
142 | cmk_cmd_push_ords (ext); |
| |
143 | cmk_cmd_push_ords (MV_ARR (MV0 ("-MMD"), MV0 ("-MF"), dep)); |
| |
144 | cmk_cmd_push_ord (MV0 ("-o")); |
| |
145 | cmk_cmd_push_art (obj); |
| |
146 | cmk_cmd_push_dep (src); |
| |
147 | |
| |
148 | cmk_cmd_run (loc, CMK_CAT (c->ns, MV0 ("compile:"), obj)); |
| |
149 | |
| |
150 | return obj; |
| |
151 | } |
| |
152 | |
| |
153 | struct mv_da module_objs = {0}; |
| |
154 | #define COMPILE_MODULE(m) DA_PUSH (&module_objs, COMPILE (OBJ (m), DEP (m), SRC (m))) |
| |
155 | |
| |
156 | #define COMPILE_META(m) COMPILE (OBJ (m "-meta"), DEP (m "-meta"), SRC (m)) |
| |
157 | |
| |
158 | #define COMPILE_EXAMPLE(m) \ |
| |
159 | COMPILE (OBJ ("examples/" m), DEP ("examples/" m), ROT ("examples/" m ".c")) |
| |
160 | |
| |
161 | #define LINK(elf, ...) cosmos_codebase_link (CMK_LOC, elf, MV_ARR (__VA_ARGS__)) |
| |
162 | struct mv |
| |
163 | cosmos_codebase_link (struct cmk_loc loc, |
| |
164 | struct mv elf, |
| |
165 | struct mv_arr objs) |
| |
166 | { |
| |
167 | struct cosmos_codebase_config *c = &cosmos_codebase_config; |
| |
168 | |
| |
169 | cmk_cmd_push_dep_only ( |
| |
170 | mv_from_mb0 (mv_dup0 (mv_peek_upto_u8 (elf, '/', MV_END | MV_INV)))); |
| |
171 | |
| |
172 | cmk_cmd_push_ord (c->cc); |
| |
173 | cmk_cmd_push_ords (MV_ARR_FROM_DA (c->cflags)); |
| |
174 | cmk_cmd_push_ord (MV0 ("-o")); |
| |
175 | cmk_cmd_push_art (elf); |
| |
176 | cmk_cmd_push_deps (objs); |
| |
177 | cmk_cmd_push_ords (MV_ARR_FROM_DA (c->ldflags)); |
| |
178 | |
| |
179 | cmk_cmd_run (loc, CMK_CAT (c->ns, MV0 ("link:"), elf)); |
| |
180 | |
| |
181 | return elf; |
| |
182 | } |
| |
183 | |
| |
184 | void |
| |
185 | cosmos_codebase (void) |
| |
186 | { |
| |
187 | module_objs.hdr = DA_HDR_FROM (GPA_FROM_ARENA (&cmk.arena)); |
| |
188 | |
| |
189 | #define CMK_META |
| |
190 | |
| |
191 | COMPILE_MODULE ("dbg"); |
| |
192 | COMPILE_MODULE ("lgr"); |
| |
193 | COMPILE_MODULE ("unw"); |
| |
194 | COMPILE_MODULE ("gpa"); |
| |
195 | |
| |
196 | COMPILE_MODULE ("mv"); |
| |
197 | COMPILE_MODULE ("ms"); |
| |
198 | COMPILE_MODULE ("buf"); |
| |
199 | COMPILE_MODULE ("iobuf"); |
| |
200 | |
| |
201 | COMPILE_MODULE ("bump"); |
| |
202 | COMPILE_MODULE ("arena"); |
| |
203 | |
| |
204 | COMPILE_MODULE ("hash"); |
| |
205 | |
| |
206 | COMPILE_MODULE ("da"); |
| |
207 | COMPILE_MODULE ("sb"); |
| |
208 | COMPILE_MODULE ("hm"); |
| |
209 | |
| |
210 | COMPILE_MODULE ("utils"); |
| |
211 | COMPILE_MODULE ("arg"); |
| |
212 | COMPILE_MODULE ("cmk"); |
| |
213 | |
| |
214 | cmk_cmd_push_ord (MV0 ("ar")); |
| |
215 | cmk_cmd_push_ord (MV0 ("rcs")); |
| |
216 | cmk_cmd_push_art (ARC ("cosmos")); |
| |
217 | cmk_cmd_push_deps (MV_ARR_FROM_DA (module_objs)); |
| |
218 | CMK_CMD_RUN (CMK_CAT (MV0 ("archive:"), ARC ("cosmos"))); |
| |
219 | } |
| |
220 | |
| |
221 | #ifndef VENDORED_COSMOS_CODEBASE |
| |
222 | int |
| |
223 | main (int argc, char **argv) |
| |
224 | { |
| |
225 | CMK_BEG (argc, argv); |
| |
226 | |
| |
227 | arg_help_verbatim (MV0 ("\nCosmos codebase specific options:\n")); |
| |
228 | |
| |
229 | struct mv_da profiles = {0}; |
| |
230 | for (int i = 0; i < COSMOS_CODEBASE_PROFILE_CNT; ++i) |
| |
231 | DA_PUSH (&profiles, cosmos_codebase_profile_name (i)); |
| |
232 | DA_SHRINK (&profiles); |
| |
233 | enum cosmos_codebase_profile profile = |
| |
234 | ARG_REQ_ENUM ('P', "profile", "Choose a profile.", |
| |
235 | MV_ARR_FROM_DA (profiles), 0); |
| |
236 | |
| |
237 | struct mv root = ARG_REQ ('R', "root", "DIR", "Set project root to DIR.", "."); |
| |
238 | struct mv build = ARG_REQ ('B', "build", "DIR", "Set build root to DIR.", "./build"); |
| |
239 | struct mv cc = arg_opt ('C', MV0 ("cc"), MV0 ("CC"), MV0 ("Force using the C compiler CC."), MV_NULL, cmk_cc ()); |
| |
240 | |
| |
241 | arg_help_verbatim (MV0 ("\nReport bugs to <bug-codebase@cosmos.party>\n")); |
| |
242 | |
| |
243 | cosmos_codebase_configure (profile, MV_NULL, root, build, cc); |
| |
244 | cosmos_codebase (); |
| |
245 | |
| |
246 | struct mv dep = CMK_CAT (build, MV0 ("/cmk.d")); |
| |
247 | cmk_cmd_push_deps_only (cmk_load_depfile (dep)); |
| |
248 | DA_PUSH (&cosmos_codebase_config.cflags, MV0 ("-DCMK_BOOTSTRAPPED")); |
| |
249 | DA_PUSH (&cosmos_codebase_config.cflags, MV0 ("-MMD")); |
| |
250 | DA_PUSH (&cosmos_codebase_config.cflags, MV0 ("-MF")); |
| |
251 | DA_PUSH (&cosmos_codebase_config.cflags, dep); |
| |
252 | LINK (cmk.progname, ROT ("cmk.c"), |
| |
253 | SRC ("dbg"), |
| |
254 | SRC ("unw"), |
| |
255 | SRC ("gpa"), |
| |
256 | SRC ("lgr"), |
| |
257 | SRC ("ms"), |
| |
258 | SRC ("mv"), |
| |
259 | SRC ("buf"), |
| |
260 | SRC ("iobuf"), |
| |
261 | SRC ("bump"), |
| |
262 | SRC ("arena"), |
| |
263 | SRC ("da"), |
| |
264 | SRC ("sb"), |
| |
265 | SRC ("hm"), |
| |
266 | SRC ("arg"), |
| |
267 | SRC ("utils"), |
| |
268 | SRC ("cmk")); |
| |
269 | |
| |
270 | cmk_end (MV0 ("all")); |
| |
271 | // MAYBE(cmmm): end should also run... |
| |
272 | // But this way we can do something in between... |
| |
273 | // Or choose not to run... |
| |
274 | return cmk_run (); |
| |
275 | } |
| |
276 | #endif // VENDORED_COSMOS_CODEBASE |
| |
277 | |
| |
278 | #undef CONF |
| |
279 | #undef SRC |
| |
280 | #undef OBJ |
| |
281 | #undef DEP |
| |
282 | #undef BIN |
| |
283 | #undef NSP |
| |
284 | #undef ROT |
| |
285 | #undef BLD |
| |
286 | #undef INC |
| |
287 | #undef ARC |
| |
288 | #undef COMPILE |
| |
289 | #undef COMPILE_X |
| |
290 | #undef COMPILE_MODULE |
| |
291 | #undef LINK |
| |
292 | |
| |
293 | #endif // CMK_BOOTSTRAPPED |
| |
294 | |
| |
295 | // cmk.c ends here |
|
|
diff --git a/cmk.h b/cmk.h new file mode 100644 index 0000000..fe1b7eb --- /dev/null +++ b/ cmk.h |
| @@ -0,0 +1,63 @@ |
| |
1 | // cmk.c --- build system of this repo -*- C -*- |
| |
2 | // |
| |
3 | // Copyright (c) 2026 Martin Michalec <martin@michalec.dev> |
| |
4 | // |
| |
5 | // SPDX-License-Identifier: MIT |
| |
6 | // |
| |
7 | // This file is part of COSMOS Codebase. |
| |
8 | // |
| |
9 | // Commentary: |
| |
10 | // |
| |
11 | // Code: |
| |
12 | |
| |
13 | #ifndef INCLUDED_COSMOS_CODEBASE_CMK_H |
| |
14 | #define INCLUDED_COSMOS_CODEBASE_CMK_H |
| |
15 | |
| |
16 | #include "dbg.h" |
| |
17 | #include "mv.h" |
| |
18 | #include "da.h" |
| |
19 | |
| |
20 | #define COSMOS_CODEBASE_PROFILE_LIST(...) \ |
| |
21 | X (REL_WITH_DEBINFO, "rel-with-debinfo", ##__VA_ARGS__) \ |
| |
22 | X (RELEASE, "release", ##__VA_ARGS__) \ |
| |
23 | X (REL_MIN_SIZE, "rel-min-size", ##__VA_ARGS__) \ |
| |
24 | X (DEVEL, "devel", ##__VA_ARGS__) |
| |
25 | |
| |
26 | #define X(sym, ...) COSMOS_CODEBASE_PROFILE_ ## sym, |
| |
27 | enum cosmos_codebase_profile { |
| |
28 | COSMOS_CODEBASE_PROFILE_LIST () |
| |
29 | COSMOS_CODEBASE_PROFILE_CNT |
| |
30 | }; |
| |
31 | #undef X |
| |
32 | |
| |
33 | extern const struct mb cosmos_codebase_profile_names[COSMOS_CODEBASE_PROFILE_CNT]; |
| |
34 | |
| |
35 | static inline struct mv |
| |
36 | cosmos_codebase_profile_name (enum cosmos_codebase_profile profile) |
| |
37 | { |
| |
38 | DBG_ASSERT_IN (0, profile, COSMOS_CODEBASE_PROFILE_CNT - 1, "%d", "%d", "%d"); |
| |
39 | return mv_from_mb (cosmos_codebase_profile_names[profile]); |
| |
40 | } |
| |
41 | |
| |
42 | struct cosmos_codebase_config { |
| |
43 | struct mv ns; |
| |
44 | struct mv root; |
| |
45 | struct mv include; |
| |
46 | struct mv src; |
| |
47 | struct mv build; |
| |
48 | struct mv obj; |
| |
49 | struct mv dep; |
| |
50 | struct mv bin; |
| |
51 | struct mv cc; |
| |
52 | struct mv_da cflags; |
| |
53 | struct mv_da ldflags; |
| |
54 | }; |
| |
55 | |
| |
56 | extern struct cosmos_codebase_config cosmos_codebase_config; |
| |
57 | |
| |
58 | void cosmos_codebase_configure (enum cosmos_codebase_profile, struct mv ns, struct mv root, struct mv build, struct mv cc); |
| |
59 | void cosmos_codebase (void); |
| |
60 | |
| |
61 | #endif // INCLUDED_COSMOS_CODEBASE_CMK_H |
| |
62 | |
| |
63 | // cmk.h ends here |
|