ninja.c (9344B)
1 #include "ninqu.h" 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 /* ninja treats '$', ':' and ' ' as special in a build statement path 8 * (outputs, inputs, deps), so escape them with a leading '$'. this is 9 * ninja file syntax, separate from the shell quoting below */ 10 static char * 11 ninja_path_escape(const char *s) 12 { 13 size_t i, j = 0; 14 size_t len = strlen(s); 15 char *out = emalloc(len * 2 + 1); 16 17 for (i = 0; i < len; i++) { 18 if (s[i] == '$' || s[i] == ':' || s[i] == ' ') 19 out[j++] = '$'; 20 out[j++] = s[i]; 21 } 22 out[j] = '\0'; 23 return out; 24 } 25 26 /* argv is fully expanded at parse time, so the ninja command is a 27 * plain literal, never $in/$out. write one word as a single shell 28 * quoted token, and double each '$' so ninja preprocessing hands the 29 * shell exactly one */ 30 static void 31 fprint_shell_word_plain(FILE *f, const char *s) 32 { 33 fputc('\'', f); 34 for (; *s; s++) { 35 if (*s == '\'') 36 fputs("'\\''", f); 37 else if (*s == '$') 38 fputs("$$", f); 39 else 40 fputc(*s, f); 41 } 42 fputc('\'', f); 43 } 44 45 /* ninja ends a "command = ..." value at the physical line end and $\n 46 * strips the newline, and \n inside posix single quotes stays two 47 * characters. so emit a command substitution running the word through 48 * `printf '%b'`, which expands escapes at run time. the argument is 49 * single-quoted with backslashes doubled, and the `$` of the opening 50 * `$(` is doubled so ninja leaves it for the shell */ 51 static void 52 fprint_shell_word(FILE *f, const char *s) 53 { 54 const char *p; 55 int has_nl = 0; 56 57 for (p = s; *p; p++) { 58 if (*p == '\n') { 59 has_nl = 1; 60 break; 61 } 62 } 63 64 if (!has_nl) { 65 fprint_shell_word_plain(f, s); 66 return; 67 } 68 69 /* wrapped in double quotes so the shell treats the substitutions 70 * result as one word instead of splitting it on whitespace, which 71 * is exactly what an unquoted $(...) would do to a multi-line 72 * result */ 73 fputs("\"$$(printf '%b' '", f); 74 for (; *s; s++) { 75 if (*s == '\'') 76 fputs("'\\''", f); 77 else if (*s == '$') 78 fputs("$$", f); 79 else if (*s == '\\') 80 fputs("\\\\", f); 81 else if (*s == '\n') 82 fputs("\\n", f); 83 else 84 fputc(*s, f); 85 } 86 fputs("')\"", f); 87 } 88 89 /* an instance with no (out) has no on-disk path (a phony or pure 90 * grouping rule), but ninja needs a target name for its build 91 * statement, so one is made from the rule name and instance index. 92 * it never exists as a file, so ninja always re-runs it, matching the 93 * "always stale" behaviour inst_stale() gives phony instances. the 94 * name has no '/', so ninja never creates a parent directory for it */ 95 static const char * 96 inst_target(struct Inst *inst, int idx, char *buf, size_t bufsz) 97 { 98 if (inst->out[0]) 99 return inst->out; 100 snprintf(buf, bufsz, "__ninqu_phony__%s_%d", rules[inst->rule_idx].name, idx); 101 return buf; 102 } 103 104 /* does this instance look like "cc ... -c ... -o foo.o"? if so it is a 105 * real compile step, and ninja can track header dependencies through 106 * a gcc-style depfile instead of only the coarse order-only edges 107 * dep_inst gives. the check is on -c and a .o output, not the 108 * compiler name, so any CC the manifest resolved works as long as it 109 * understands -MMD -MF */ 110 static int 111 is_cc_compile(struct Inst *inst) 112 { 113 static const char *const compilers[] = {"cc", "gcc", "clang", "g++", "c++", NULL}; 114 const char *prog, *slash; 115 size_t len; 116 int k, has_c; 117 118 if (inst->phony || !inst->out[0] || inst->cmd.is_pipe || inst->cmd.argv.n < 2) 119 return 0; 120 121 len = strlen(inst->out); 122 if (len < 2 || strcmp(inst->out + len - 2, ".o") != 0) 123 return 0; 124 125 prog = inst->cmd.argv.v[0]; 126 slash = strrchr(prog, '/'); 127 if (slash) 128 prog = slash + 1; 129 130 for (k = 0; compilers[k]; k++) 131 if (strcmp(prog, compilers[k]) == 0) 132 break; 133 if (!compilers[k]) 134 return 0; 135 136 for (k = 1, has_c = 0; k < inst->cmd.argv.n; k++) 137 if (strcmp(inst->cmd.argv.v[k], "-c") == 0) 138 has_c = 1; 139 140 return has_c; 141 } 142 143 /* the command a rule runs: an optional workdir cd, the argv or 144 * piped stages shell-quoted word by word, and an optional stdout 145 * redirect into (out). mirrors spawn_inst()/spawn_pipe() exactly, 146 * just written out as text instead of forked and execd. a compile 147 * step (is_cc_compile()) gets "-MMD -MF <out>.d" spliced in right 148 * after the compiler name, paired with the depfile/deps lines 149 * emit_ninja() writes into that instances rule block */ 150 static void 151 emit_command(FILE *f, struct Inst *inst) 152 { 153 int is_cc = is_cc_compile(inst); 154 int i; 155 156 if (inst->workdir[0]) { 157 fputs("cd ", f); 158 fprint_shell_word(f, inst->workdir); 159 fputs(" && ", f); 160 } 161 162 if (inst->cmd.is_pipe) { 163 for (i = 0; i < inst->cmd.nstages; i++) { 164 int k; 165 if (i > 0) 166 fputs(" | ", f); 167 for (k = 0; k < inst->cmd.stages[i]->n; k++) { 168 if (k > 0) 169 fputc(' ', f); 170 fprint_shell_word(f, inst->cmd.stages[i]->v[k]); 171 } 172 } 173 } else { 174 for (i = 0; i < inst->cmd.argv.n; i++) { 175 if (i > 0) 176 fputc(' ', f); 177 fprint_shell_word(f, inst->cmd.argv.v[i]); 178 if (i == 0 && is_cc) { 179 fputs(" -MMD -MF ", f); 180 fprint_shell_word(f, inst->out); 181 fputs(".d", f); 182 } 183 } 184 } 185 186 if (inst->redirect) { 187 fputs(" > ", f); 188 fprint_shell_word(f, inst->out); 189 } 190 } 191 192 /* write every instance in insts[] (wanted targets plus their 193 * transitive deps) out as a build.ninja: one rule and one build 194 * statement per instance, since ninqu already resolved each to a 195 * literal command line. dep_inst edges become order-only ("||"), 196 * because inst_stale() only consults (in)/(stale_extra), so this 197 * keeps ninja's mtime check in agreement with ninqu's 198 * 199 * ninja only knows build outputs, not ninqu's rule/group/meta names, 200 * so `ninja LIBUTIL` has nothing to resolve. wanted holds the words 201 * ninqu was invoked with, and one phony alias per word makes each 202 * buildable. with several targets in one invocation every alias 203 * points at the whole combined closure, which is slightly 204 * conservative but fine for the makefile's one-target use */ 205 void 206 emit_ninja(const char *path, struct StrList *wanted) 207 { 208 FILE *f = fopen(path, "w"); 209 int i, k; 210 211 if (!f) 212 eprintf("emit_ninja: open %s:", path); 213 214 fputs("# autogenerated by `ninqu -G ninja`, do not edit\n", f); 215 fputs("ninja_required_version = 1.3\n\n", f); 216 217 for (i = 0; i < ninsts; i++) { 218 struct Inst *inst = &insts[i]; 219 fprintf(f, "rule r%d\n", i); 220 fprintf( 221 f, 222 " description = %s %s\n", 223 rules[inst->rule_idx].name, 224 inst->out[0] ? inst->out : "(phony)" 225 ); 226 if (is_cc_compile(inst)) { 227 char *esc = ninja_path_escape(inst->out); 228 fprintf(f, " depfile = %s.d\n", esc); 229 fputs(" deps = gcc\n", f); 230 free(esc); 231 } 232 fputs(" command = ", f); 233 emit_command(f, inst); 234 fputs("\n\n", f); 235 } 236 237 for (i = 0; i < ninsts; i++) { 238 struct Inst *inst = &insts[i]; 239 char buf[1024]; 240 const char *out = inst_target(inst, i, buf, sizeof buf); 241 char *esc = ninja_path_escape(out); 242 243 fprintf(f, "build %s: r%d", esc, i); 244 free(esc); 245 246 for (k = 0; k < inst->in.n; k++) { 247 esc = ninja_path_escape(inst->in.v[k]); 248 fprintf(f, " %s", esc); 249 free(esc); 250 } 251 for (k = 0; k < inst->stale_extra.n; k++) { 252 esc = ninja_path_escape(inst->stale_extra.v[k]); 253 fprintf(f, " %s", esc); 254 free(esc); 255 } 256 257 if (inst->n_dep > 0) { 258 int printed_bar = 0; 259 for (k = 0; k < inst->n_dep; k++) { 260 struct Inst *dep = &insts[inst->dep_inst[k]]; 261 char depbuf[1024]; 262 const char *depout = inst_target(dep, inst->dep_inst[k], depbuf, sizeof depbuf); 263 /* a rule ref that is also a real (dep ...) input already 264 * forces both order and staleness, listing it again as 265 * order-only is pure noise */ 266 if (sl_has(&inst->in, depout) || sl_has(&inst->stale_extra, depout)) 267 continue; 268 if (!printed_bar) { 269 fputs(" ||", f); 270 printed_bar = 1; 271 } 272 esc = ninja_path_escape(depout); 273 fprintf(f, " %s", esc); 274 free(esc); 275 } 276 } 277 fputc('\n', f); 278 } 279 280 fputs("\ndefault", f); 281 for (i = 0; i < ninsts; i++) { 282 struct Inst *inst = &insts[i]; 283 char buf[1024]; 284 const char *out = inst_target(inst, i, buf, sizeof buf); 285 char *esc = ninja_path_escape(out); 286 fprintf(f, " %s", esc); 287 free(esc); 288 } 289 fputc('\n', f); 290 291 for (i = 0; i < wanted->n; i++) { 292 const char *name = wanted->v[i]; 293 int dup = 0; 294 295 for (k = 0; k < ninsts && !dup; k++) { 296 char buf[1024]; 297 const char *out = inst_target(&insts[k], k, buf, sizeof buf); 298 if (strcmp(out, name) == 0) 299 dup = 1; 300 } 301 if (dup) 302 continue; 303 304 { 305 char *esc = ninja_path_escape(name); 306 fprintf(f, "build %s: phony", esc); 307 free(esc); 308 } 309 for (k = 0; k < ninsts; k++) { 310 char buf[1024]; 311 const char *out = inst_target(&insts[k], k, buf, sizeof buf); 312 char *esc = ninja_path_escape(out); 313 fprintf(f, " %s", esc); 314 free(esc); 315 } 316 fputc('\n', f); 317 } 318 319 if (fshut(f, path)) 320 eprintf("emit_ninja: %s:", path); 321 printf(" GEN %s\n", path); 322 }