diff options
| author | 2026-11-06 03:42:39 +0000 | |
|---|---|---|
| committer | 2026-11-06 07:09:56 +0000 | |
| commit | f26d018676d0b4de4393448b4da669f281415055 (patch) | |
| tree | 838f7a1fc854f64747809b6c80a730988535b666 /include/cmk-bootstrap.h | |
| parent | feat(arg): add immediate mode argument parser (diff) | |
| download | codebase-f26d018676d0b4de4393448b4da669f281415055.tar.gz | |
feat(cmk): add C make
Diffstat (limited to 'include/cmk-bootstrap.h')
| -rw-r--r-- | include/cmk-bootstrap.h | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/include/cmk-bootstrap.h b/include/cmk-bootstrap.h new file mode 100644 index 0000000..b05b9a1 --- /dev/null +++ b/include/cmk-bootstrap.h | |||
| @@ -0,0 +1,211 @@ | |||
| 1 | // cmk-bootstrap.h --- bootstrap cmk -*- 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 | #include <stdio.h> | ||
| 14 | #include <stdlib.h> | ||
| 15 | #include <stdarg.h> | ||
| 16 | #include <string.h> | ||
| 17 | #include <errno.h> | ||
| 18 | #include <unistd.h> | ||
| 19 | #include <sys/wait.h> | ||
| 20 | |||
| 21 | #ifdef INCLUDED_CMK_BOOTSTRAP_H | ||
| 22 | #error "This file is supposed to take over your compilation unit!" | ||
| 23 | #endif // INCLUDED_CMK_BOOTSTRAP_H | ||
| 24 | #define INCLUDED_CMK_BOOTSTRAP_H | ||
| 25 | |||
| 26 | // TODO(cmmm): move to cmk.h | ||
| 27 | #define CMK_DEF_LIST(...) \ | ||
| 28 | X (CMK_BOOTSTRAPPED, ##__VA_ARGS__) \ | ||
| 29 | X (GPA_NO_FEAT_UNW, ##__VA_ARGS__) \ | ||
| 30 | X (GPA_NO_FEAT_DBG, ##__VA_ARGS__) \ | ||
| 31 | X (DBG_NO_FEAT_BACKTRACE, ##__VA_ARGS__) | ||
| 32 | #define X(def, ...) #def, | ||
| 33 | const char *cmk_defs[] = { CMK_DEF_LIST () }; | ||
| 34 | #undef X | ||
| 35 | |||
| 36 | // TODO(cmmm): move to cmk.h | ||
| 37 | #define CMK_DEP_LIST(...) \ | ||
| 38 | X (dbg ##__VA_ARGS__) \ | ||
| 39 | X (gpa, ##__VA_ARGS__) \ | ||
| 40 | X (lgr, ##__VA_ARGS__) \ | ||
| 41 | X (ms, ##__VA_ARGS__) \ | ||
| 42 | X (mv, ##__VA_ARGS__) \ | ||
| 43 | X (buf, ##__VA_ARGS__) \ | ||
| 44 | X (iobuf, ##__VA_ARGS__) \ | ||
| 45 | X (bump, ##__VA_ARGS__) \ | ||
| 46 | X (arena, ##__VA_ARGS__) \ | ||
| 47 | X (da, ##__VA_ARGS__) \ | ||
| 48 | X (sb, ##__VA_ARGS__) \ | ||
| 49 | X (hm, ##__VA_ARGS__) \ | ||
| 50 | X (arg, ##__VA_ARGS__) \ | ||
| 51 | X (utils, ##__VA_ARGS__) \ | ||
| 52 | X (cmk, ##__VA_ARGS__) | ||
| 53 | #define X(dep, ...) #dep, | ||
| 54 | const char *cmk_deps[] = { CMK_DEP_LIST () }; | ||
| 55 | #undef X | ||
| 56 | |||
| 57 | char *progname = NULL; | ||
| 58 | |||
| 59 | #define LOG( fmt, ...) fprintf (stderr, "%s (bootstrap): " fmt "\n", progname, ##__VA_ARGS__) | ||
| 60 | #define LOG_BEG(fmt, ...) fprintf (stderr, "%s (bootstrap): " fmt, progname, "\n") | ||
| 61 | #define LOG_CNT(fmt, ...) fprintf (stderr, fmt, ##__VA_ARGS__) | ||
| 62 | #define LOG_END(fmt, ...) fprintf (stderr, fmt "\n", ##__VA_ARGS__) | ||
| 63 | |||
| 64 | #define ARRAY_LENGTH(arr) (sizeof (arr)/sizeof (*(arr))) | ||
| 65 | |||
| 66 | void * | ||
| 67 | memdup (const void *mem, size_t sz) | ||
| 68 | { | ||
| 69 | void *ptr = malloc (sz); | ||
| 70 | memcpy (ptr, mem, sz); | ||
| 71 | return ptr; | ||
| 72 | } | ||
| 73 | |||
| 74 | void * | ||
| 75 | memdup0 (const void *mem, size_t sz) | ||
| 76 | { | ||
| 77 | char *ptr = malloc (sz + 1); | ||
| 78 | memcpy (ptr, mem, sz); | ||
| 79 | ptr[sz] = '\0'; | ||
| 80 | return ptr; | ||
| 81 | } | ||
| 82 | |||
| 83 | void * | ||
| 84 | mempush (void *to, const void *from, size_t sz) | ||
| 85 | { | ||
| 86 | memcpy (to, from, sz); | ||
| 87 | return (char *) to + sz;; | ||
| 88 | } | ||
| 89 | |||
| 90 | #define CAT(...) cat (__VA_ARGS__, NULL) | ||
| 91 | char * | ||
| 92 | cat (char *first, ...) | ||
| 93 | { | ||
| 94 | va_list ap; | ||
| 95 | size_t len = strlen (first); | ||
| 96 | char *str, *out, *ptr; | ||
| 97 | |||
| 98 | va_start (ap, first); | ||
| 99 | for (;;) { | ||
| 100 | str = va_arg (ap, char *); | ||
| 101 | if (!str) break; | ||
| 102 | len += strlen (str); | ||
| 103 | } va_end (ap); | ||
| 104 | |||
| 105 | out = ptr = malloc (len + 1); | ||
| 106 | ptr = mempush (ptr, first, strlen (first)); | ||
| 107 | |||
| 108 | va_start (ap, first); | ||
| 109 | for (;;) { | ||
| 110 | str = va_arg (ap, char *); | ||
| 111 | if (!str) break; | ||
| 112 | ptr = mempush (ptr, str, strlen (str)); | ||
| 113 | } va_end (ap); | ||
| 114 | *ptr = '\0'; | ||
| 115 | |||
| 116 | return out; | ||
| 117 | } | ||
| 118 | |||
| 119 | char ** | ||
| 120 | pushflags (char **argv, char *flags) | ||
| 121 | { | ||
| 122 | if (!flags) return argv; | ||
| 123 | for (;;) { | ||
| 124 | while (*flags == ' ') flags += 1; | ||
| 125 | if (*flags == '\0') return argv; | ||
| 126 | *argv++ = flags; | ||
| 127 | while (*flags != '\0' && *flags != ' ') flags += 1; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | char * | ||
| 132 | find_cc (void) | ||
| 133 | { | ||
| 134 | #if defined (__TINYC__) | ||
| 135 | return "tcc"; | ||
| 136 | #elif defined (__clang__) | ||
| 137 | return "clang"; | ||
| 138 | #elif defined (_MSC_VER) | ||
| 139 | return "cl"; | ||
| 140 | #else | ||
| 141 | return "cc"; | ||
| 142 | #endif | ||
| 143 | } | ||
| 144 | |||
| 145 | char * | ||
| 146 | find_root (void) | ||
| 147 | { | ||
| 148 | char *hit = strrchr (cmk_c, '/'); | ||
| 149 | if (!hit) return "."; | ||
| 150 | return memdup0 (cmk_c, hit - cmk_c); | ||
| 151 | } | ||
| 152 | |||
| 153 | int | ||
| 154 | main (int argc, char **argv) | ||
| 155 | { | ||
| 156 | char *cc_argv[512]; | ||
| 157 | char **cc_arg = cc_argv; | ||
| 158 | |||
| 159 | progname = argv[0]; | ||
| 160 | LOG ("info: bootstrapping..."); | ||
| 161 | LOG ("info: cmk.c path: %s", cmk_c); | ||
| 162 | char *root = find_root (); | ||
| 163 | LOG ("info: root: %s", root); | ||
| 164 | |||
| 165 | *cc_arg++ = find_cc (); | ||
| 166 | #ifdef __TCC_BACKTRACE__ | ||
| 167 | *cc_arg++ = "-b"; | ||
| 168 | *cc_arg++ = "-bt"; | ||
| 169 | #endif | ||
| 170 | *cc_arg++ = "-o"; | ||
| 171 | *cc_arg++ = progname; | ||
| 172 | for (int i = 0; i < ARRAY_LENGTH (cmk_defs); ++i) | ||
| 173 | *cc_arg++ = CAT ("-D", cmk_defs[i]); | ||
| 174 | *cc_arg++ = CAT ("-I", root, "/include"); | ||
| 175 | *cc_arg++ = CAT ("-I", root); | ||
| 176 | cc_arg = pushflags (cc_arg, getenv ("CFLAGS")); | ||
| 177 | for (int i = 0; i < ARRAY_LENGTH (cmk_deps); ++i) | ||
| 178 | *cc_arg++ = CAT (root, "/src/", cmk_deps[i], ".c"); | ||
| 179 | *cc_arg++ = CAT (root, "/cmk.c"); | ||
| 180 | cc_arg = pushflags (cc_arg, getenv ("LDFLAGS")); | ||
| 181 | *cc_arg = NULL; | ||
| 182 | |||
| 183 | LOG_BEG ("info: +"); | ||
| 184 | for (char **arg = cc_argv; *arg; ++arg) | ||
| 185 | LOG_CNT (" %s", *arg); | ||
| 186 | LOG_END (""); | ||
| 187 | |||
| 188 | pid_t child = fork (); | ||
| 189 | if (child == 0) { | ||
| 190 | execvp (cc_argv[0], cc_argv); | ||
| 191 | _Exit (127); | ||
| 192 | } | ||
| 193 | |||
| 194 | int status = -1; | ||
| 195 | if (waitpid (child, &status, 0) == -1) { | ||
| 196 | LOG ("waitpid: %s", strerror (errno)); | ||
| 197 | goto failed; | ||
| 198 | } | ||
| 199 | if (status != 0) { | ||
| 200 | goto failed; | ||
| 201 | } | ||
| 202 | |||
| 203 | LOG ("info: bootstrapped! (rerunning)"); | ||
| 204 | execvp (progname, argv); | ||
| 205 | |||
| 206 | failed: | ||
| 207 | LOG ("fatal: failed to bootstrap!"); | ||
| 208 | return status; | ||
| 209 | } | ||
| 210 | |||
| 211 | // cmk-bootstrap.h ends here | ||
