ninqu.h (8167B)
1 #if !defined(_DEFAULT_SOURCE) 2 #define _DEFAULT_SOURCE 3 #endif 4 #if !defined(_POSIX_C_SOURCE) 5 #define _POSIX_C_SOURCE 200809L 6 #endif 7 8 #include <sys/types.h> 9 10 #include "arg.h" 11 #include "util.h" 12 13 /* grow a struct-array when full, n/cap doubles each time */ 14 #define GROW(arr, n, cap, init) \ 15 do { \ 16 if ((n) >= (cap)) { \ 17 (cap) = (cap) ? (cap) * 2 : (init); \ 18 (arr) = erealloc((arr), (size_t)(cap) * sizeof *(arr)); \ 19 } \ 20 } while (0) 21 22 struct StrList { 23 char **v; 24 int n; 25 int cap; 26 }; 27 28 void sl_push(struct StrList *sl, const char *s); 29 int sl_has(struct StrList *sl, const char *s); 30 void sl_split(struct StrList *sl, const char *s); 31 char *sl_join(struct StrList *sl); 32 char **sl_argv(struct StrList *sl); 33 const char *base_of(const char *path); 34 void child_execvp(char **av) __attribute__((noreturn)); 35 36 struct Kv { 37 char *key; 38 char *val; 39 }; 40 41 struct KvStore { 42 struct Kv *v; 43 int n, cap; 44 }; 45 46 struct Map { 47 char name[128]; 48 struct KvStore entries; 49 }; 50 51 char *kv_get(const char *key); 52 const char *kv_get_or(const char *key, const char *def); 53 void kv_set(const char *key, const char *val); 54 void kv_set_manifest(const char *key, const char *val); 55 void kv_set_cli(const char *key, const char *val); 56 char *kv_expand(const char *s); 57 char *kv_get_raw(struct KvStore *s, const char *key); 58 void kv_set_raw(struct KvStore *s, const char *key, const char *val); 59 struct Map *map_find(const char *name); 60 struct Map *map_new(const char *name); 61 62 extern struct KvStore kv; 63 extern struct KvStore *local_overlay; 64 extern struct StrList kv_locked; 65 extern struct Map *maps; 66 extern int nmaps, mapscap; 67 68 enum SKind { S_ATOM, S_LIST }; 69 70 struct SNode { 71 enum SKind kind; 72 char *atom; 73 struct SNode **kids; 74 int nkids; 75 int line; 76 }; 77 78 struct SLex { 79 const char *src; 80 size_t pos; 81 int line; 82 }; 83 84 struct SNode *snode_atom(const char *s, int line); 85 struct SNode *snode_list(int line); 86 void snode_push(struct SNode *list, struct SNode *kid); 87 void snode_free(struct SNode *n); 88 struct SNode *snode_clone(struct SNode *n); 89 struct SNode *snode_parse(struct SLex *lx); 90 int s_is_list(struct SNode *n); 91 const char *s_head(struct SNode *n); 92 char *subst_tmpl(const char *s, struct StrList *params, char **vals); 93 void snode_subst(struct SNode *n, struct StrList *params, char **vals); 94 void lex_swallow_dollar(struct SLex *lx, char *buf, size_t *blen, size_t bsz); 95 void lex_string(struct SLex *lx, char *buf, size_t bsz); 96 const char *find_dollar_close(const char *inner); 97 98 /* gate: (not x) | (and x...) | (or x...) | bare var */ 99 enum GKind { G_TRUE, G_VAR, G_NOT, G_AND, G_OR }; 100 101 struct Gate { 102 enum GKind kind; 103 char *var; 104 struct Gate **kids; 105 int nkids; 106 }; 107 108 struct Gate *gate_new(enum GKind kind); 109 void gate_free(struct Gate *g); 110 struct Gate *gate_parse(struct SNode *expr); 111 int gate_has_dyn(struct Gate *g); 112 int gate_eval(struct Gate *g); 113 struct Gate *gate_materialize(struct Gate *g); 114 115 struct Cmd { 116 struct StrList argv; 117 int is_pipe; 118 struct StrList **stages; 119 int nstages; 120 }; 121 122 struct Rule { 123 char name[128]; 124 struct StrList globs; 125 struct StrList skip; 126 char require_marker[1024]; 127 char require_file[1024]; 128 struct StrList in; 129 struct StrList extra_deps; 130 char out[1024]; 131 struct Cmd cmd; 132 struct Gate *gate; 133 char workdir[1024]; 134 int phony; 135 int redirect; 136 char produces[128]; 137 char description[256]; 138 char group_name[128]; 139 char member_of[128]; 140 char member_alias[128]; 141 struct StrList features; 142 143 int *inst_idx; 144 int n_inst, cap_inst; 145 int expanded; 146 }; 147 148 struct Group { 149 char name[128]; 150 struct StrList refs; 151 }; 152 153 struct Meta { 154 char name[128]; 155 struct StrList keys; 156 struct StrList vals; 157 }; 158 159 struct Template { 160 char name[128]; 161 struct StrList params; 162 struct SNode *body; 163 }; 164 165 struct Rule *rule_find(const char *name); 166 struct Rule *rule_find_output(const char *path); 167 struct Rule *rule_find_produces(const char *name); 168 struct Rule *rule_new(const char *name); 169 struct Group *group_find(const char *name); 170 struct Group *group_new(const char *name); 171 struct Meta *meta_find(const char *name); 172 struct Meta *meta_new(const char *name); 173 struct Template *template_find(const char *name); 174 struct Template *template_new(const char *name); 175 176 extern struct Rule *rules; 177 extern int nrules, rulescap; 178 extern struct Group *groups; 179 extern int ngroups, groupscap; 180 extern struct Meta *metas; 181 extern int nmetas, metascap; 182 extern struct Template *templates; 183 extern int ntemplates, templatescap; 184 185 struct Inst { 186 int rule_idx; 187 struct StrList in; 188 struct StrList stale_extra; 189 char out[1024]; 190 struct Cmd cmd; 191 char workdir[1024]; 192 int phony; 193 int redirect; 194 195 struct StrList dep_rule_names; 196 int *dep_inst; 197 int n_dep, cap_dep; 198 int will_build; 199 }; 200 201 int inst_new(int rule_idx); 202 int inst_stale(struct Inst *inst); 203 void dep_push(struct Inst *inst, int idx); 204 void resolve_deps(void); 205 206 extern struct Inst *insts; 207 extern int ninsts, instscap; 208 209 long mtime_of(const char *path); 210 int file_exists(const char *path); 211 int file_contains(const char *path, const char *needle); 212 void glob_expand(const char *pattern, struct StrList *out); 213 void glob_all(struct Rule *r, struct StrList *out); 214 int is_wildcard_pattern(const char *pattern); 215 int rule_is_literal(struct Rule *r); 216 int path_claimed_elsewhere(const char *path, struct Rule *self); 217 void splitext(const char *base, char *stem, size_t stemsz, char *ext, size_t extsz); 218 void to_ident(const char *s, char *out, size_t outsz); 219 220 pid_t spawn_inst(struct Inst *inst, struct StrList *argv); 221 pid_t spawn_pipe(struct Inst *inst); 222 void mk_out_dir(const char *out); 223 void announce_inst(struct Inst *inst); 224 void run_inst_sync(int idx); 225 void run_batch(int *idxs, int n); 226 void run_inst_with_deps(int idx); 227 void materialize_if_missing(const char *path); 228 char *capture_argv(struct StrList *argv); 229 230 extern int jobs_n; 231 extern int summary_mode; 232 extern int failed_any; 233 234 void load_file(const char *path); 235 void load_manifest(struct SNode *root); 236 void dispatch_top_form(struct SNode *form); 237 void parse_exec_clause(struct SNode *clause, struct Cmd *cmd); 238 void parse_shell_clause(struct SNode *clause, struct Cmd *cmd); 239 void parse_pipe_clause(struct SNode *clause, struct Cmd *cmd); 240 void meta_apply(struct Meta *m); 241 const char *first_atom(struct SNode *node, const char *msg); 242 const char *sole_atom(struct SNode *node, const char *msg); 243 244 extern struct StrList files_read; 245 246 void expand_rule(const char *name); 247 248 void schedule_and_run(void); 249 void emit_ninja(const char *path, struct StrList *wanted); 250 251 struct Backend { 252 const char *name; 253 const char *try_key; 254 const char *file; 255 void (*emit)(const char *path, struct StrList *wanted); 256 }; 257 258 const struct Backend *backend_by_name(const char *name); 259 const struct Backend *backend_resolve(char **bin); 260 void backend_exec(const struct Backend *be, const char *bin, struct StrList *wanted) 261 __attribute__((noreturn)); 262 263 void do_features(void); 264 void do_query(const char *sub, const char *arg); 265 266 void include_ref(const char *name); 267 void build_default_group(void); 268 void resolve_wanted(const char *w); 269 void resolve_all_bareword_deps(void);