aboutsummaryrefslogtreecommitdiff
path: root/cmk.c
blob: 111ed4927fdbf16adca2549db9bba64feed8cb90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// cmk.c --- build system of this repo                                 -*- C -*-
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
// This file is part of COSMOS Codebase.
//
// Commentary:
//
//   NEXT(cmmm): implement notion of profiles (debug/release)
//   NEXT(cmmm): release as much as possible
//   NEXT(cmmm): write ADRs and articles about core concepts
//   TODO(cmmm): build static lib from ./build/obj/*.o
//   TODO(cmmm): generate compile_commands.json
//
// Code:

const char *cmk_c = __FILE__;

#ifndef CMK_BOOTSTRAPPED
#include "include/cmk-bootstrap.h"
#else // CMK_BOOTSTRAPPED

#include "include/cmk.h"
#include "cmk.h"
#include "arg.h"

#define CONF cosmos_codebase_config
#define SRC(p) CMK_CAT (CONF.src, MV0 ("/" p ".c"))
#define OBJ(p) CMK_CAT (CONF.obj, MV0 ("/" p ".o"))
#define DEP(p) CMK_CAT (CONF.dep, MV0 ("/" p ".d"))
#define BIN(p) CMK_CAT (CONF.bin, MV0 ("/" p CMK_EXEC_EXT))
#define NSP(s) CMK_CAT (CONF.ns,      MV0 (    s))
#define ROT(s) CMK_CAT (CONF.root,    MV0 ("/" s))
#define BLD(s) CMK_CAT (CONF.build,   MV0 ("/" s))
#define INC(s) CMK_CAT (CONF.include, MV0 ("/" s))
#define ARC(p) CMK_CAT (CONF.build, MV0 ("/lib" p ".a"))

#define X(sym, str, ...) [COSMOS_CODEBASE_PROFILE_ ## sym] = MB0 (str),
const struct mb cosmos_codebase_profile_names[COSMOS_CODEBASE_PROFILE_CNT] = {
  COSMOS_CODEBASE_PROFILE_LIST ()
};
#undef X

struct cosmos_codebase_config cosmos_codebase_config;

void
cosmos_codebase_configure (enum cosmos_codebase_profile profile,
                           struct mv ns,
                           struct mv root,
                           struct mv build,
                           struct mv cc)
{
  DBG_ASSERT_IN (0, profile, COSMOS_CODEBASE_PROFILE_CNT - 1, "%d", "%d", "%d");

  struct cosmos_codebase_config *c = &cosmos_codebase_config;

  c->ns      = mv_or (ns, MV0 (""));
  c->root    = mv_or (root,  MV0 ("."));
  c->build   = CMK_CAT (mv_or (build, MV0 ("./build")), MV0 ("/"),
                        cosmos_codebase_profile_name (profile));
  c->include = CMK_CAT (c->root,  MV0 ("/include"));
  c->src     = CMK_CAT (c->root,  MV0 ("/src"));
  c->obj     = CMK_CAT (c->build, MV0 ("/obj"));
  c->dep     = CMK_CAT (c->build, MV0 ("/dep"));
  c->bin     = CMK_CAT (c->build, MV0 ("/bin"));

  // DA_PUSH (&c->ldflags, MV0 ("-lbacktrace"));

  // choose cc
  switch (profile) {
    case COSMOS_CODEBASE_PROFILE_REL_WITH_DEBINFO:
    case COSMOS_CODEBASE_PROFILE_RELEASE:
    case COSMOS_CODEBASE_PROFILE_REL_MIN_SIZE:
      c->cc = mv_or (cc, cmk_cc ());
      break;
    case COSMOS_CODEBASE_PROFILE_DEVEL:
      c->cc = mv_or (cc, cmk_cc ());
      #ifdef __TINYC__
      c->cc = mv_or (cc, MV0 ("tcc"));
      #endif // __TINYC__
  }

  // choose cflags
  DA_PUSH (&c->cflags, MV0 ("-DENABLE_TRACE_ERROR"));
  DA_PUSH (&c->cflags, CMK_CAT (MV0 ("-I"), c->include));
  DA_PUSH (&c->cflags, CMK_CAT (MV0 ("-I"), c->src    ));
  DA_PUSH (&c->cflags, CMK_CAT (MV0 ("-I"), c->build  ));
  // DA_PUSH (&c->cflags, MV0 ("-std=c99"));
  DA_PUSH (&c->cflags, MV0 ("-Wall"));
  DA_PUSH (&c->cflags, MV0 ("-Wextra"));
  DA_PUSH (&c->cflags, MV0 ("-Wno-unused-function"));
  DA_PUSH (&c->cflags, MV0 ("-Wno-unused-variable"));
  DA_PUSH (&c->cflags, MV0 ("-Wno-unused-parameter"));
  DA_PUSH (&c->cflags, MV0 ("-Wno-unused-result"));
  DA_PUSH (&c->cflags, MV0 ("-Wno-parentheses"));
  DA_PUSH (&c->cflags, MV0 ("-Wno-comment"));
  switch (profile) {
    case COSMOS_CODEBASE_PROFILE_REL_WITH_DEBINFO:
      DA_PUSH (&c->cflags, MV0 ("-ggdb3"));
      FALLTHROUGH;
    case COSMOS_CODEBASE_PROFILE_RELEASE:
      DA_PUSH (&c->cflags, MV0 ("-O3"));
      DA_PUSH (&c->cflags, MV0 ("-march=native"));
      break;
    case COSMOS_CODEBASE_PROFILE_REL_MIN_SIZE:
      DA_PUSH (&c->cflags, MV0 ("-Oz"));
      DA_PUSH (&c->cflags, MV0 ("-march=native"));
      break;
    case COSMOS_CODEBASE_PROFILE_DEVEL:
      DA_PUSH (&c->cflags, MV0 ("-Werror"));
      if (mv_equal (c->cc, MV0 ("tcc"))) {
        // DA_PUSH (&c->cflags, MV0 ("-gdwarf-3"));
        DA_PUSH (&c->cflags, MV0 ("-b"));
        DA_PUSH (&c->cflags, MV0 ("-bt"));
      } else {
        DA_PUSH (&c->cflags, MV0 ("-ggdb3"));
        DA_PUSH (&c->cflags, MV0 ("-Og"));
      }
      break;
  }
}

#define COMPILE(...) cosmos_codebase_compile (CMK_LOC, __VA_ARGS__, MV_ARR_NULL)
#define COMPILE_X(obj, dep, src, ...) cosmos_codebase_compile (CMK_LOC, obj, dep, src, MV_ARR (__VA_ARGS__))
struct mv
cosmos_codebase_compile (struct cmk_loc loc,
                         struct mv      obj,
                         struct mv      dep,
                         struct mv      src,
                         struct mv_arr  ext)
{
  struct cosmos_codebase_config *c = &cosmos_codebase_config;

  cmk_cmd_push_deps_only (cmk_load_depfile (dep));
  cmk_cmd_push_dep_only (mv_from_mb0 (mv_dup0 (mv_peek_upto_u8 (dep, '/', MV_END | MV_INV))));
  cmk_cmd_push_dep_only (mv_from_mb0 (mv_dup0 (mv_peek_upto_u8 (obj, '/', MV_END | MV_INV))));

  cmk_cmd_push_ords (MV_ARR (c->cc, MV0 ("-c")));
  cmk_cmd_push_ords (MV_ARR_FROM_DA (c->cflags));
  cmk_cmd_push_ords (ext);
  cmk_cmd_push_ords (MV_ARR (MV0 ("-MMD"), MV0 ("-MF"), dep));
  cmk_cmd_push_ord  (MV0 ("-o"));
  cmk_cmd_push_art  (obj);
  cmk_cmd_push_dep  (src);

  cmk_cmd_run (loc, CMK_CAT (c->ns, MV0 ("compile:"), obj));

  return obj;
}

struct mv_da module_objs = {0};
#define COMPILE_MODULE(m) DA_PUSH (&module_objs, COMPILE (OBJ (m), DEP (m), SRC (m)))

#define COMPILE_META(m) COMPILE (OBJ (m "-meta"), DEP (m "-meta"), SRC (m))

#define COMPILE_EXAMPLE(m)                                                     \
  COMPILE (OBJ ("examples/" m), DEP ("examples/" m), ROT ("examples/" m ".c"))

#define LINK(elf, ...) cosmos_codebase_link (CMK_LOC, elf, MV_ARR (__VA_ARGS__))
struct mv
cosmos_codebase_link (struct cmk_loc loc,
                      struct mv      elf,
                      struct mv_arr  objs)
{
  struct cosmos_codebase_config *c = &cosmos_codebase_config;

  cmk_cmd_push_dep_only (
    mv_from_mb0 (mv_dup0 (mv_peek_upto_u8 (elf, '/', MV_END | MV_INV))));

  cmk_cmd_push_ord  (c->cc);
  cmk_cmd_push_ords (MV_ARR_FROM_DA (c->cflags));
  cmk_cmd_push_ord  (MV0 ("-o"));
  cmk_cmd_push_art  (elf);
  cmk_cmd_push_deps (objs);
  cmk_cmd_push_ords (MV_ARR_FROM_DA (c->ldflags));

  cmk_cmd_run (loc, CMK_CAT (c->ns, MV0 ("link:"), elf));

  return elf;
}

void
cosmos_codebase (void)
{
  module_objs.hdr = DA_HDR_FROM (GPA_FROM_ARENA (&cmk.arena));

#define CMK_META

  COMPILE_MODULE ("dbg");
  COMPILE_MODULE ("lgr");
  COMPILE_MODULE ("unw");
  COMPILE_MODULE ("gpa");

  COMPILE_MODULE ("mv");
  COMPILE_MODULE ("ms");
  COMPILE_MODULE ("buf");
  COMPILE_MODULE ("iobuf");

  COMPILE_MODULE ("bump");
  COMPILE_MODULE ("arena");

  COMPILE_MODULE ("hash");

  COMPILE_MODULE ("da");
  COMPILE_MODULE ("sb");
  COMPILE_MODULE ("hm");

  COMPILE_MODULE ("utils");
  COMPILE_MODULE ("arg");
  COMPILE_MODULE ("cmk");

  cmk_cmd_push_ord (MV0 ("ar"));
  cmk_cmd_push_ord (MV0 ("rcs"));
  cmk_cmd_push_art (ARC ("cosmos"));
  cmk_cmd_push_deps (MV_ARR_FROM_DA (module_objs));
  CMK_CMD_RUN (CMK_CAT (MV0 ("archive:"), ARC ("cosmos")));
}

#ifndef VENDORED_COSMOS_CODEBASE
int
main (int argc, char **argv)
{
  CMK_BEG (argc, argv);

  arg_help_verbatim (MV0 ("\nCosmos codebase specific options:\n"));

  struct mv_da profiles = {0};
  for (int i = 0; i < COSMOS_CODEBASE_PROFILE_CNT; ++i)
    DA_PUSH (&profiles, cosmos_codebase_profile_name (i));
  DA_SHRINK (&profiles);
  enum cosmos_codebase_profile profile =
    ARG_REQ_ENUM ('P', "profile", "Choose a profile.",
                  MV_ARR_FROM_DA (profiles), 0);

  struct mv root  = ARG_REQ ('R', "root",  "DIR", "Set project root to DIR.", ".");
  struct mv build = ARG_REQ ('B', "build", "DIR", "Set build root to DIR.", "./build");
  struct mv cc    = arg_opt ('C', MV0 ("cc"), MV0 ("CC"), MV0 ("Force using the C compiler CC."), MV_NULL, cmk_cc ());

  arg_help_verbatim (MV0 ("\nReport bugs to <bug-codebase@cosmos.party>\n"));

  cosmos_codebase_configure (profile, MV_NULL, root, build, cc);
  cosmos_codebase ();

  struct mv dep = CMK_CAT (build, MV0 ("/cmk.d"));
  cmk_cmd_push_deps_only (cmk_load_depfile (dep));
  DA_PUSH (&cosmos_codebase_config.cflags, MV0 ("-DCMK_BOOTSTRAPPED"));
  DA_PUSH (&cosmos_codebase_config.cflags, MV0 ("-MMD"));
  DA_PUSH (&cosmos_codebase_config.cflags, MV0 ("-MF"));
  DA_PUSH (&cosmos_codebase_config.cflags, dep);
  LINK (cmk.progname, ROT ("cmk.c"),
        SRC ("dbg"),
        SRC ("unw"),
        SRC ("gpa"),
        SRC ("lgr"),
        SRC ("ms"),
        SRC ("mv"),
        SRC ("buf"),
        SRC ("iobuf"),
        SRC ("bump"),
        SRC ("arena"),
        SRC ("da"),
        SRC ("sb"),
        SRC ("hm"),
        SRC ("arg"),
        SRC ("utils"),
        SRC ("cmk"));

  cmk_end (MV0 ("all"));
  // MAYBE(cmmm): end should also run...
  //              But this way we can do something in between...
  //              Or choose not to run...
  return cmk_run ();
}
#endif // VENDORED_COSMOS_CODEBASE

#undef CONF
#undef SRC
#undef OBJ
#undef DEP
#undef BIN
#undef NSP
#undef ROT
#undef BLD
#undef INC
#undef ARC
#undef COMPILE
#undef COMPILE_X
#undef COMPILE_MODULE
#undef LINK

#endif // CMK_BOOTSTRAPPED

// cmk.c ends here