_build.inc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. /* ----- strlist.c ----- */
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define check_alloc(x) \
  5. if((x)->len + 1 >= (x)->alloc) { \
  6. (x)->alloc *= 2; \
  7. (x)->entries = realloc((x)->entries, (x)->alloc * sizeof(*(x)->entries)); \
  8. }
  9. typedef struct strlist {
  10. int len;
  11. int alloc;
  12. char** entries;
  13. } strlist;
  14. void strlist_init(strlist* sl) {
  15. sl->len = 0;
  16. sl->alloc = 32;
  17. sl->entries = malloc(sl->alloc * sizeof(*sl->entries));
  18. }
  19. strlist* strlist_new() {
  20. strlist* sl = malloc(sizeof(*sl));
  21. strlist_init(sl);
  22. return sl;
  23. }
  24. void strlist_push(strlist* sl, char* e) {
  25. check_alloc(sl);
  26. sl->entries[sl->len++] = e;
  27. sl->entries[sl->len] = 0;
  28. }
  29. /* -END- strlist.c ----- */
  30. /* ----- header.c ----- */
  31. //#include <stdlib.h>
  32. #include <stdio.h>
  33. //#include <string.h>
  34. #include <ctype.h>
  35. #include <errno.h>
  36. #include <stdarg.h>
  37. #include <signal.h>
  38. #include <unistd.h>
  39. #include <sys/prctl.h>
  40. #include <pty.h>
  41. #include <fcntl.h>
  42. #include <fnmatch.h>
  43. #include <dirent.h>
  44. #include <libgen.h>
  45. #include <sys/stat.h>
  46. #include <sys/types.h>
  47. #include <sys/wait.h>
  48. #include <sys/sysinfo.h>
  49. // link with -lutil
  50. // list all <header> files in use
  51. //egrep -ro '^\s*#\s*include\s+<(.*)>' src/ | sed -e 's/^.*<\(.*\)>.*$/\1/'
  52. #define PP_ARG_N(_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, N, ...) N
  53. #define PP_RSEQ_N() 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  54. #define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
  55. #define PP_NARG(...) PP_NARG_(__VA_ARGS__, PP_RSEQ_N())
  56. char** g_gcc_opts_list;
  57. char* g_gcc_opts_flat;
  58. char* g_gcc_include;
  59. char* g_gcc_libs;
  60. int g_nprocs;
  61. struct strlist;
  62. typedef struct objfile {
  63. // user config
  64. char** sources;
  65. char** debug_cflags;
  66. char** release_cflags;
  67. char** profiling_cflags;
  68. char** common_cflags;
  69. char** libs_needed;
  70. char** lib_headers_needed;
  71. char** ld_add;
  72. char* source_dir;// "src"
  73. char* exe_path; // the executable name
  74. char* base_build_dir; // = "build";
  75. // returns a dup'd string with the full, raw command to execute
  76. char* (*compile_source_cmd)(char* src_path, char* obj_path, struct objfile* obj);
  77. char mode_debug;
  78. char mode_profiling;
  79. char mode_release;
  80. char clean_first;
  81. char verbose;
  82. // internally calculated
  83. char** gcc_opts_list;
  84. char* gcc_opts_flat;
  85. char* gcc_include;
  86. char* gcc_libs;
  87. char* build_dir; // full build path, including debug/release options
  88. char* build_subdir;
  89. strlist objs; // .o's to be created
  90. strlist compile_cache; // list of compile commands to run
  91. char* archive_path; // temporary .a during build process
  92. } objfile;
  93. /* -END- header.c ----- */
  94. /* ----- string.c ----- */
  95. #define strjoin(j, ...) strjoin_(j, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  96. char* strjoin_(char* joiner, size_t nargs, ...);
  97. #define strcatdup(...) strcatdup_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
  98. char* strcatdup_(size_t nargs, ...);
  99. size_t list_len(char** list);
  100. char* join_str_list(char* list[], char* joiner);
  101. char* sprintfdup(char* fmt, ...);
  102. #define path_join(...) path_join_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
  103. char* path_join_(size_t nargs, ...);
  104. #define concat_lists(...) concat_lists_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
  105. char** concat_lists_(int nargs, ...);
  106. size_t span_path(char* s) {
  107. size_t n = 0;
  108. for(; *s; s++, n++) {
  109. if(isspace(*s)) break;
  110. if(*s == '\\') {
  111. s++;
  112. n++;
  113. }
  114. }
  115. return n;
  116. }
  117. // splits on whitespace
  118. char** strsplit(char* splitters, char* in) {
  119. char* e;
  120. int alloc = 32;
  121. int len = 0;
  122. char** list = malloc(alloc * sizeof(*list));
  123. for(char* s = in; *s;) {
  124. e = strpbrk(s, splitters);
  125. if(!e) e = s + strlen(s);
  126. if(len >= alloc - 1) {
  127. alloc *= 2;
  128. list = realloc(list, alloc* sizeof(*list));
  129. }
  130. list[len++] = strndup(s, e - s);
  131. e += strspn(e, splitters);
  132. s = e;
  133. }
  134. list[len] = NULL;
  135. return list;
  136. }
  137. static inline char* strskip(char* s, char* skip) {
  138. return s + strspn(s, skip);
  139. }
  140. // concatenate all argument strings together in a new buffer
  141. char* strcatdup_(size_t nargs, ...) {
  142. size_t total = 0;
  143. char* out, *end;
  144. if(nargs == 0) return NULL;
  145. // calculate total buffer len
  146. va_list va;
  147. va_start(va, nargs);
  148. for(size_t i = 0; i < nargs; i++) {
  149. char* s = va_arg(va, char*);
  150. if(s) total += strlen(s);
  151. }
  152. va_end(va);
  153. out = malloc((total + 1) * sizeof(char*));
  154. end = out;
  155. va_start(va, nargs);
  156. for(size_t i = 0; i < nargs; i++) {
  157. char* s = va_arg(va, char*);
  158. if(s) {
  159. strcpy(end, s); // not exactly the ost efficient, but maybe faster than
  160. end += strlen(s); // a C version. TODO: test the speed
  161. };
  162. }
  163. va_end(va);
  164. *end = 0;
  165. return out;
  166. }
  167. // concatenate all argument strings together in a new buffer,
  168. // with the given joining string between them
  169. char* strjoin_(char* joiner, size_t nargs, ...) {
  170. size_t total = 0;
  171. char* out, *end;
  172. size_t j_len;
  173. if(nargs == 0) return NULL;
  174. // calculate total buffer len
  175. va_list va;
  176. va_start(va, nargs);
  177. for(size_t i = 0; i < nargs; i++) {
  178. char* s = va_arg(va, char*);
  179. if(s) total += strlen(s);
  180. }
  181. va_end(va);
  182. j_len = strlen(joiner);
  183. total += j_len * (nargs - 1);
  184. out = malloc((total + 1) * sizeof(char*));
  185. end = out;
  186. va_start(va, nargs);
  187. for(size_t i = 0; i < nargs; i++) {
  188. char* s = va_arg(va, char*);
  189. if(s) {
  190. if(i > 0) {
  191. strcpy(end, joiner);
  192. end += j_len;
  193. }
  194. strcpy(end, s); // not exactly the ost efficient, but maybe faster than
  195. end += strlen(s); // a C version. TODO: test the speed
  196. };
  197. }
  198. va_end(va);
  199. *end = 0;
  200. return out;
  201. }
  202. // allocates a new buffer and calls sprintf with it
  203. // why isn't this a standard function?
  204. char* sprintfdup(char* fmt, ...) {
  205. va_list va;
  206. va_start(va, fmt);
  207. size_t n = vsnprintf(NULL, 0, fmt, va);
  208. char* buf = malloc(n + 1);
  209. va_end(va);
  210. va_start(va, fmt);
  211. vsnprintf(buf, n + 1, fmt, va);
  212. va_end(va);
  213. return buf;
  214. }
  215. void free_strpp(char** l) {
  216. for(char** s = l; *s; s++) free(*s);
  217. free(l);
  218. }
  219. size_t list_len(char** list) {
  220. size_t total = 0;
  221. for(; *list; list++) total++;
  222. return total;
  223. }
  224. char** concat_lists_(int nargs, ...) {
  225. size_t total = 0;
  226. char** out, **end;
  227. if(nargs == 0) return NULL;
  228. // calculate total list length
  229. va_list va;
  230. va_start(va, nargs);
  231. for(size_t i = 0; i < nargs; i++) {
  232. char** s = va_arg(va, char**);
  233. if(s) total += list_len(s);
  234. }
  235. va_end(va);
  236. out = malloc((total + 1) * sizeof(char**));
  237. end = out;
  238. va_start(va, nargs);
  239. // concat lists
  240. for(size_t i = 0; i < nargs; i++) {
  241. char** s = va_arg(va, char**);
  242. size_t l = list_len(s);
  243. if(s) {
  244. memcpy(end, s, l * sizeof(*s));
  245. end += l;
  246. }
  247. }
  248. va_end(va);
  249. *end = 0;
  250. return out;
  251. }
  252. char* join_str_list(char* list[], char* joiner) {
  253. size_t list_len = 0;
  254. size_t total = 0;
  255. size_t jlen = strlen(joiner);
  256. // calculate total length
  257. for(int i = 0; list[i]; i++) {
  258. list_len++;
  259. total += strlen(list[i]);
  260. }
  261. if(total == 0) return strdup("");
  262. total += (list_len - 1) * jlen;
  263. char* out = malloc((total + 1) * sizeof(*out));
  264. char* end = out;
  265. for(int i = 0; list[i]; i++) {
  266. char* s = list[i];
  267. size_t l = strlen(s);
  268. if(i > 0) {
  269. memcpy(end, joiner, jlen);
  270. end += jlen;
  271. }
  272. if(s) {
  273. memcpy(end, s, l);
  274. end += l;
  275. }
  276. total += strlen(list[i]);
  277. }
  278. *end = 0;
  279. return out;
  280. }
  281. char* path_join_(size_t nargs, ...) {
  282. size_t total = 0;
  283. char* out, *end;
  284. size_t j_len;
  285. char* joiner = "/";
  286. int escape;
  287. if(nargs == 0) return NULL;
  288. // calculate total buffer length
  289. va_list va;
  290. va_start(va, nargs);
  291. for(size_t i = 0; i < nargs; i++) {
  292. char* s = va_arg(va, char*);
  293. if(s) total += strlen(s);
  294. }
  295. va_end(va);
  296. j_len = strlen(joiner);
  297. total += j_len * (nargs - 1);
  298. out = malloc((total + 1) * sizeof(char*));
  299. end = out;
  300. va_start(va, nargs);
  301. for(size_t i = 0; i < nargs; i++) {
  302. char* s = va_arg(va, char*);
  303. size_t l = strlen(s);
  304. if(s) {
  305. if(l > 1) {
  306. escape = s[l-2] == '\\' ? 1 : 0;
  307. }
  308. if(i > 0 && (s[0] == joiner[0])) {
  309. s++;
  310. l--;
  311. }
  312. if(i > 0 && i != nargs-1 && !escape && (s[l-1] == joiner[0])) {
  313. l--;
  314. }
  315. if(i > 0) {
  316. strcpy(end, joiner);
  317. end += j_len;
  318. }
  319. // should be strncpy, but GCC is so fucking stupid that it
  320. // has a warning about using strncpy to do exactly what
  321. // strncpy does if you read the fucking man page.
  322. // fortunately, we are already terminating our strings
  323. // manually so memcpy is a drop-in replacement here.
  324. memcpy(end, s, l);
  325. end += l;
  326. }
  327. }
  328. va_end(va);
  329. *end = 0;
  330. return out;
  331. }
  332. /* -END- string.c ----- */
  333. /* ----- strcache.c ----- */
  334. typedef unsigned long hash_t;
  335. hash_t strhash(char* str) {
  336. unsigned long h = 0;
  337. int c;
  338. while(c = *str++) {
  339. h = c + (h << 6) + (h << 16) - h;
  340. }
  341. return h;
  342. }
  343. hash_t strnhash(char* str, size_t n) {
  344. unsigned long h = 0;
  345. int c;
  346. while((c = *str++) && n--) {
  347. h = c + (h << 6) + (h << 16) - h;
  348. }
  349. return h;
  350. }
  351. typedef struct {
  352. hash_t hash;
  353. char* str;
  354. unsigned int len;
  355. int refs;
  356. } string_cache_bucket;
  357. typedef struct {
  358. int fill;
  359. int alloc;
  360. string_cache_bucket* buckets;
  361. } string_cache_t;
  362. string_cache_t string_cache;
  363. void string_cache_init(int alloc) {
  364. string_cache.fill = 0;
  365. string_cache.alloc = alloc ? alloc : 1024;
  366. string_cache.buckets = calloc(1, string_cache.alloc * sizeof(*string_cache.buckets));
  367. }
  368. int string_cache_find_bucket(string_cache_t* ht, hash_t hash, char* key) {
  369. int b = hash % ht->alloc;
  370. for(int i = 0; i < ht->alloc; i++) {
  371. // empty bucket
  372. if(ht->buckets[b].str == NULL) return b;
  373. // full bucket
  374. if(ht->buckets[b].hash == hash) {
  375. if(0 == strcmp(key, ht->buckets[b].str)) {
  376. return b;
  377. }
  378. }
  379. // probe forward on collisions
  380. b = (b + 1) % ht->alloc;
  381. }
  382. // should never reach here
  383. printf("oops. -1 bucket \n");
  384. return -1;
  385. }
  386. void string_cache_expand(string_cache_t* ht) {
  387. int old_alloc = ht->alloc;
  388. ht->alloc *= 2;
  389. string_cache_bucket* old = ht->buckets;
  390. ht->buckets = calloc(1, ht->alloc * sizeof(*ht->buckets));
  391. for(int i = 0, f = 0; i < old_alloc && f < ht->fill; i++) {
  392. if(!old[i].str) continue;
  393. int b = string_cache_find_bucket(ht, old[i].hash, old[i].str);
  394. ht->buckets[b] = old[i];
  395. f++;
  396. }
  397. free(old);
  398. }
  399. char* strcache(char* in) {
  400. hash_t hash = strhash(in);
  401. int b = string_cache_find_bucket(&string_cache, hash, in);
  402. if(!string_cache.buckets[b].str) {
  403. if(string_cache.fill > string_cache.alloc * .80) {
  404. string_cache_expand(&string_cache);
  405. // the bucket location has changed
  406. b = string_cache_find_bucket(&string_cache, hash, in);
  407. }
  408. string_cache.fill++;
  409. string_cache.buckets[b].str = strdup(in);
  410. string_cache.buckets[b].hash = hash;
  411. string_cache.buckets[b].refs = 1;
  412. string_cache.buckets[b].len = strlen(in);
  413. }
  414. else {
  415. string_cache.buckets[b].refs++;
  416. }
  417. return string_cache.buckets[b].str;
  418. }
  419. char* strncache(char* in, size_t n) {
  420. hash_t hash = strnhash(in, n);
  421. int b = string_cache_find_bucket(&string_cache, hash, in);
  422. if(!string_cache.buckets[b].str) {
  423. if(string_cache.fill > string_cache.alloc * .80) {
  424. string_cache_expand(&string_cache);
  425. // the bucket location has changed
  426. b = string_cache_find_bucket(&string_cache, hash, in);
  427. }
  428. string_cache.fill++;
  429. string_cache.buckets[b].str = strndup(in, n);
  430. string_cache.buckets[b].hash = hash;
  431. string_cache.buckets[b].refs = 1;
  432. string_cache.buckets[b].len = n;
  433. }
  434. else {
  435. string_cache.buckets[b].refs++;
  436. }
  437. return string_cache.buckets[b].str;
  438. }
  439. void struncache(char* in) {
  440. hash_t hash = strhash(in);
  441. int b = string_cache_find_bucket(&string_cache, hash, in);
  442. if(!string_cache.buckets[b].str) {
  443. // normal string, free it
  444. free(in);
  445. return;
  446. }
  447. string_cache.buckets[b].refs--;
  448. if(string_cache.buckets[b].refs == 0) {
  449. // just do nothing for now. deletion is a pain.
  450. }
  451. }
  452. /* -END- strcache.c ----- */
  453. /* ----- hash.c ----- */
  454. typedef struct {
  455. hash_t hash;
  456. char* key;
  457. void* value;
  458. } hashbucket;
  459. typedef struct hashtable {
  460. int fill;
  461. int alloc;
  462. hashbucket* buckets;
  463. } hashtable;
  464. void hash_init(hashtable* ht, int alloc) {
  465. ht->fill = 0;
  466. ht->alloc = alloc ? alloc : 128;
  467. ht->buckets = calloc(1, ht->alloc * sizeof(*ht->buckets));
  468. }
  469. hashtable* hash_new(int alloc) {
  470. hashtable* ht = malloc(sizeof(*ht));
  471. hash_init(ht, alloc);
  472. return ht;
  473. }
  474. int hash_find_bucket(hashtable* ht, hash_t hash, char* key) {
  475. int b = hash % ht->alloc;
  476. for(int i = 0; i < ht->alloc; i++) {
  477. // empty bucket
  478. if(ht->buckets[b].key == NULL) return b;
  479. // full bucket
  480. if(ht->buckets[b].hash == hash) {
  481. if(0 == strcmp(key, ht->buckets[b].key)) {
  482. return b;
  483. }
  484. }
  485. // probe forward on collisions
  486. b = (b + 1) % ht->alloc;
  487. }
  488. // should never reach here
  489. printf("oops. -1 bucket \n");
  490. return -1;
  491. }
  492. void hash_expand(hashtable* ht) {
  493. int old_alloc = ht->alloc;
  494. ht->alloc *= 2;
  495. hashbucket* old = ht->buckets;
  496. ht->buckets = calloc(1, ht->alloc * sizeof(*ht->buckets));
  497. for(int i = 0, f = 0; i < old_alloc && f < ht->fill; i++) {
  498. if(!old[i].key) continue;
  499. int b = hash_find_bucket(ht, old[i].hash, old[i].key);
  500. ht->buckets[b] = old[i];
  501. f++;
  502. }
  503. free(old);
  504. }
  505. void hash_insert(hashtable* ht, char* key, void* value) {
  506. hash_t hash = strhash(key);
  507. int b = hash_find_bucket(ht, hash, key);
  508. if(!ht->buckets[b].key) {
  509. if(ht->fill > ht->alloc * .80) {
  510. hash_expand(ht);
  511. // the bucket location has changed
  512. b = hash_find_bucket(ht, hash, key);
  513. }
  514. ht->fill++;
  515. }
  516. ht->buckets[b].key = strcache(key);
  517. ht->buckets[b].hash = hash;
  518. ht->buckets[b].value = value;
  519. }
  520. void* hash_find(hashtable* ht, char* key) {
  521. hash_t hash = strhash(key);
  522. int b = hash_find_bucket(ht, hash, key);
  523. return ht->buckets[b].value;
  524. }
  525. /* -END- hash.c ----- */
  526. /* ----- fs.c ----- */
  527. typedef struct realname_entry {
  528. char* realname;
  529. time_t mtime;
  530. } realname_entry;
  531. struct {
  532. hashtable names;
  533. int len;
  534. int alloc;
  535. struct realname_entry* entries;
  536. } realname_cache;
  537. hashtable mkdir_cache;
  538. #define FSU_EXCLUDE_HIDDEN (1<<0)
  539. #define FSU_NO_FOLLOW_SYMLINKS (1<<1)
  540. #define FSU_INCLUDE_DIRS (1<<2)
  541. #define FSU_EXCLUDE_FILES (1<<3)
  542. // return 0 to continue, nonzero to stop all directory scanning
  543. typedef int (*readDirCallbackFn)(char* /*fullPath*/, char* /*fileName*/, unsigned char /*type*/, void* /*data*/);
  544. // returns negative on error, nonzero if scanning was halted by the callback
  545. int recurse_dirs(
  546. char* path,
  547. readDirCallbackFn fn,
  548. void* data,
  549. int depth,
  550. unsigned int flags
  551. );
  552. void realname_cache_init();
  553. time_t realname_cache_add(char* fake_name, char* real_name);
  554. realname_entry* realname_cache_search_real(char* real_name);
  555. realname_entry* realname_cache_search(char* fake_name);
  556. char* realname_cache_find(char* fake_name);
  557. char* resolve_path(char* in, time_t* mtime_out);
  558. char* read_whole_file(char* path, size_t* srcLen);
  559. char* dir_name(char* path) {
  560. char* n = strdup(path);
  561. char* o = dirname(n);
  562. return strcache(o);
  563. }
  564. char* base_name(char* path) {
  565. char* n = strdup(path);
  566. char* o = basename(n);
  567. return strcache(o);
  568. }
  569. // does not handle escaped slashes
  570. int mkdirp(char* path, mode_t mode) {
  571. char* clean_path = strdup(path);
  572. // inch along the path creating each directory in line
  573. for(char* p = clean_path; *p; p++) {
  574. if(*p == '/') {
  575. *p = 0;
  576. if(mkdir(clean_path, mode)) {
  577. if(errno != EEXIST) goto FAIL;
  578. }
  579. *p = '/';
  580. }
  581. }
  582. // mop up the last dir
  583. if(mkdir(clean_path, mode)) {
  584. if(errno != EEXIST) goto FAIL;
  585. }
  586. free(clean_path);
  587. return 0;
  588. FAIL:
  589. free(clean_path);
  590. return -1;
  591. }
  592. void mkdirp_cached(char* path, mode_t mode) {
  593. void* there = hash_find(&mkdir_cache, path);
  594. if(!there) {
  595. hash_insert(&mkdir_cache, path, NULL);
  596. mkdirp(path, mode);
  597. }
  598. }
  599. // works like realpath(), except also handles ~/
  600. char* resolve_path(char* in, time_t* mtime_out) {
  601. int tmp_was_malloced = 0;
  602. char* out, *tmp;
  603. if(!in) return NULL;
  604. realname_entry* e = realname_cache_search(in);
  605. if(e) {
  606. if(mtime_out) *mtime_out = e->mtime;
  607. return strcache(e->realname);
  608. }
  609. // skip leading whitespace
  610. while(isspace(*in)) in++;
  611. // handle home dir shorthand
  612. if(in[0] == '~') {
  613. char* home = getenv("HOME");
  614. tmp_was_malloced = 1;
  615. tmp = malloc(sizeof(*tmp) * (strlen(home) + strlen(in) + 2));
  616. strcpy(tmp, home);
  617. strcat(tmp, "/"); // just in case
  618. strcat(tmp, in + 1);
  619. }
  620. else tmp = in;
  621. out = realpath(tmp, NULL);
  622. if(tmp_was_malloced) free(tmp);
  623. time_t t = 0;
  624. if(out) {
  625. // put it in the cache
  626. t = realname_cache_add(in, out);
  627. }
  628. else {
  629. // temporary
  630. struct stat st;
  631. if(!lstat(in, &st))
  632. t = st.st_mtim.tv_sec;
  633. }
  634. if(mtime_out) *mtime_out = t;
  635. return out ? out : in;
  636. }
  637. void realname_cache_init() {
  638. realname_cache.len = 0;
  639. realname_cache.alloc = 1024;
  640. realname_cache.entries = malloc(realname_cache.alloc * sizeof(*realname_cache.entries));
  641. hash_init(&realname_cache.names, 1024);
  642. }
  643. time_t realname_cache_add(char* fake_name, char* real_name) {
  644. realname_entry* e = hash_find(&realname_cache.names, fake_name);
  645. if(e) return e->mtime;
  646. e = hash_find(&realname_cache.names, real_name);
  647. if(!e) {
  648. struct stat st;
  649. lstat(real_name, &st);
  650. e = &realname_cache.entries[realname_cache.len];
  651. e->realname = strcache(real_name);
  652. e->mtime = st.st_mtim.tv_sec;
  653. realname_cache.len++;
  654. hash_insert(&realname_cache.names, real_name, e);
  655. }
  656. hash_insert(&realname_cache.names, fake_name, e);
  657. return e->mtime;
  658. }
  659. realname_entry* realname_cache_search_real(char* real_name) {
  660. for(int i = 0; i < realname_cache.len; i++) {
  661. if(0 == strcmp(real_name, realname_cache.entries[i].realname)) {
  662. return &realname_cache.entries[i];
  663. }
  664. }
  665. return NULL;
  666. }
  667. realname_entry* realname_cache_search(char* fake_name) {
  668. return hash_find(&realname_cache.names, fake_name);
  669. }
  670. char* realname_cache_find(char* fake_name) {
  671. realname_entry* r = realname_cache_search(fake_name);
  672. return r ? r->realname : NULL;
  673. }
  674. // returns negative on error, nonzero if scanning was halted by the callback
  675. int recurse_dirs(
  676. char* path,
  677. readDirCallbackFn fn,
  678. void* data,
  679. int depth,
  680. unsigned int flags
  681. ) {
  682. DIR* derp;
  683. struct dirent* result;
  684. int stop = 0;
  685. if(fn == NULL) {
  686. fprintf(stderr, "Error: readAllDir called with null function pointer.\n");
  687. return -1;
  688. }
  689. derp = opendir(path);
  690. if(derp == NULL) {
  691. fprintf(stderr, "Error opening directory '%s': %s\n", path, strerror(errno));
  692. return -1;
  693. }
  694. while((result = readdir(derp)) && !stop) {
  695. char* n = result->d_name;
  696. unsigned char type = DT_UNKNOWN;
  697. char* fullPath;
  698. // skip self and parent dir entries
  699. if(n[0] == '.') {
  700. if(n[1] == '.' && n[2] == 0) continue;
  701. if(n[1] == 0) continue;
  702. if(flags & FSU_EXCLUDE_HIDDEN) continue;
  703. }
  704. #ifdef _DIRENT_HAVE_D_TYPE
  705. type = result->d_type; // the way life should be
  706. #else
  707. // do some slow extra bullshit to get the type
  708. fullPath = path_join(path, n);
  709. struct stat upgrade_your_fs;
  710. lstat(fullPath, &upgrade_your_fs);
  711. if(S_ISREG(upgrade_your_fs.st_mode)) type = DT_REG;
  712. else if(S_ISDIR(upgrade_your_fs.st_mode)) type = DT_DIR;
  713. else if(S_ISLNK(upgrade_your_fs.st_mode)) type = DT_LNK;
  714. #endif
  715. if(flags & FSU_NO_FOLLOW_SYMLINKS && type == DT_LNK) {
  716. continue;
  717. }
  718. #ifdef _DIRENT_HAVE_D_TYPE
  719. fullPath = path_join(path, n);
  720. #endif
  721. if(type == DT_DIR) {
  722. if(flags & FSU_INCLUDE_DIRS) {
  723. stop = fn(fullPath, n, type, data);
  724. }
  725. if(depth != 0) {
  726. stop |= recurse_dirs(fullPath, fn, data, depth - 1, flags);
  727. }
  728. }
  729. else if(type == DT_REG) {
  730. if(!(flags & FSU_EXCLUDE_FILES)) {
  731. stop = fn(fullPath, n, type, data);
  732. }
  733. }
  734. free(fullPath);
  735. }
  736. closedir(derp);
  737. return stop;
  738. }
  739. char* read_whole_file(char* path, size_t* srcLen) {
  740. size_t fsize, total_read = 0, bytes_read;
  741. char* contents;
  742. FILE* f;
  743. f = fopen(path, "rb");
  744. if(!f) {
  745. fprintf(stderr, "Could not open file \"%s\"\n", path);
  746. return NULL;
  747. }
  748. fseek(f, 0, SEEK_END);
  749. fsize = ftell(f);
  750. rewind(f);
  751. contents = malloc(fsize + 1);
  752. while(total_read < fsize) {
  753. bytes_read = fread(contents + total_read, sizeof(char), fsize - total_read, f);
  754. total_read += bytes_read;
  755. }
  756. contents[fsize] = 0;
  757. fclose(f);
  758. if(srcLen) *srcLen = fsize;
  759. return contents;
  760. }
  761. /* -END- fs.c ----- */
  762. /* ----- pkgconfig.c ----- */
  763. char* pkg_config(char** packages, char* opts) {
  764. char* tmp;
  765. int num_pkgs = list_len(packages);
  766. if(num_pkgs == 0) return strdup("");
  767. char* pkgs = join_str_list(packages, " ");
  768. for(char* c = opts; *c; c++) {
  769. switch(*c) {
  770. case 'c':
  771. case 'C':
  772. case 'i':
  773. case 'I':
  774. tmp = strjoin(" ", "--cflags", pkgs);
  775. free(pkgs);
  776. pkgs = tmp;
  777. break;
  778. case 'l':
  779. case 'L':
  780. tmp = strjoin(" ", "--libs", pkgs);
  781. free(pkgs);
  782. pkgs = tmp;
  783. break;
  784. }
  785. }
  786. tmp = strjoin(" ", "pkg-config", pkgs);
  787. free(pkgs);
  788. FILE* f = popen(tmp, "r");
  789. free(tmp);
  790. if(!f) {
  791. fprintf(stderr, "Could not run command '%s'\n", tmp);
  792. exit(1);
  793. return NULL;
  794. }
  795. int len = 2048;
  796. int fill = 0;
  797. char* buffer = malloc(len * sizeof(*buffer));
  798. while(!feof(f)) {
  799. if(fill + 1 >= len) {
  800. len *= 2;
  801. buffer = realloc(buffer, len * sizeof(*buffer));
  802. }
  803. fill += fread(buffer + fill, 1, len - fill - 1, f);
  804. }
  805. buffer[fill] = 0;
  806. pclose(f);
  807. // strip out newlines and other garbage
  808. for(char* c = buffer; *c; c++) if(isspace(*c)) *c = ' ';
  809. return buffer;
  810. }
  811. /* -END- pkgconfig.c ----- */
  812. void parse_cli_opts(int argc, char** argv, objfile* obj) {
  813. for(int a = 1; a < argc; a++) {
  814. if(argv[a][0] == '-') {
  815. for(int i = 0; argv[a][i]; i++) {
  816. switch(argv[a][i]) {
  817. case 'd': // debug: -ggdb
  818. obj->mode_debug = 1;
  819. if(obj->mode_release == 1) {
  820. fprintf(stderr, "Debug and Release set at the same time.\n");
  821. }
  822. break;
  823. case 'p': // profiling: -pg
  824. obj->mode_profiling = 1;
  825. break;
  826. case 'r': // release: -O3
  827. obj->mode_release = 1;
  828. obj->mode_debug = 0;
  829. break;
  830. case 'c': // clean
  831. obj->clean_first = 1;
  832. break;
  833. case 'v': // verbose
  834. obj->verbose = 1;
  835. break;
  836. }
  837. }
  838. }
  839. }
  840. }
  841. char* default_compile_source(char* src_path, char* obj_path, objfile* obj);
  842. void start_obj(objfile* obj) {
  843. obj->build_subdir = calloc(1, 20);
  844. if(obj->mode_debug) strcat(obj->build_subdir, "d");
  845. if(obj->mode_profiling) strcat(obj->build_subdir, "p");
  846. if(obj->mode_release) strcat(obj->build_subdir, "r");
  847. obj->build_dir = path_join(obj->base_build_dir, obj->build_subdir);
  848. if(!obj->archive_path) {
  849. obj->archive_path = path_join(obj->build_dir, "tmp.a");
  850. }
  851. // clean up old executables
  852. unlink(obj->exe_path);
  853. unlink(obj->archive_path);
  854. // delete old build files if needed
  855. if(obj->clean_first) {
  856. printf("Cleaning directory %s/\n", obj->build_dir);
  857. system(sprintfdup("rm -rf %s/*", obj->build_dir));
  858. }
  859. // ensure the build dir exists
  860. mkdirp_cached(obj->build_dir, 0755);
  861. // flatten all the gcc options
  862. obj->gcc_opts_list = concat_lists(obj->ld_add, obj->common_cflags);
  863. if(obj->mode_debug) obj->gcc_opts_list = concat_lists(obj->gcc_opts_list, obj->debug_cflags);
  864. if(obj->mode_profiling) obj->gcc_opts_list = concat_lists(obj->gcc_opts_list, obj->profiling_cflags);
  865. if(obj->mode_release) obj->gcc_opts_list = concat_lists(obj->gcc_opts_list, obj->release_cflags);
  866. obj->gcc_opts_flat = join_str_list(obj->gcc_opts_list, " ");
  867. obj->gcc_include = pkg_config(obj->lib_headers_needed, "I");
  868. obj->gcc_libs = pkg_config(obj->libs_needed, "L");
  869. char* tmp = obj->gcc_opts_flat;
  870. obj->gcc_opts_flat = strjoin(" ", obj->gcc_opts_flat, obj->gcc_include);
  871. free(tmp);
  872. // initialze the object file list
  873. strlist_init(&obj->objs);
  874. strlist_init(&obj->compile_cache);
  875. if(!obj->compile_source_cmd) obj->compile_source_cmd = default_compile_source;
  876. }
  877. /* ----- cprocs.c ----- */
  878. struct child_process_info {
  879. int pid;
  880. char state; // 'q'ueued, 'r'unning, 'd'ead
  881. int exit_status;
  882. int pty;
  883. int child_stdin;
  884. int child_stdout;
  885. int child_stderr;
  886. FILE* f_stdin;
  887. FILE* f_stdout;
  888. FILE* f_stderr;
  889. char* output_buffer;
  890. size_t buf_alloc;
  891. size_t buf_len;
  892. };
  893. struct job {
  894. int type; // c = child process, f = function
  895. volatile int state;
  896. union {
  897. int (*fn)(void*);
  898. char* cmd;
  899. };
  900. union {
  901. struct child_process_info* cpi;
  902. void* user_data;
  903. };
  904. };
  905. struct child_process_info* exec_cmdline_pipe(char* cmdline);
  906. struct child_process_info* exec_process_pipe(char* exec_path, char* args[]);
  907. void free_cpi(struct child_process_info* cpi) {
  908. if(cpi->output_buffer) free(cpi->output_buffer);
  909. if(cpi->f_stdin) fclose(cpi->f_stdin);
  910. if(cpi->f_stdout) fclose(cpi->f_stdout);
  911. if(cpi->f_stderr) fclose(cpi->f_stderr);
  912. if(cpi->child_stdin) close(cpi->child_stdin);
  913. if(cpi->child_stdout) close(cpi->child_stdout);
  914. if(cpi->child_stderr) close(cpi->child_stderr);
  915. if(cpi->pty) close(cpi->pty);
  916. free(cpi);
  917. }
  918. void read_cpi(struct child_process_info* cpi) {
  919. while(1) {
  920. if(cpi->buf_len > cpi->buf_alloc - 128) {
  921. cpi->buf_alloc *= 2;
  922. cpi->output_buffer = realloc(cpi->output_buffer, cpi->buf_alloc * sizeof(*cpi->output_buffer));
  923. }
  924. int ret = read(cpi->pty, cpi->output_buffer + cpi->buf_len, cpi->buf_alloc - cpi->buf_len - 1);
  925. if(ret > 0) cpi->buf_len += ret;
  926. else return;
  927. }
  928. return ;
  929. }
  930. char* printpct(float f) {
  931. static char buf[32];
  932. if(f != 100) sprintf(buf, "\e[1;33m %2.0f%%\e[0m", f);
  933. else sprintf(buf, "\e[1;32m100%%\e[0m");
  934. return buf;
  935. }
  936. int execute_mt(strlist* cmds, int threads, char* fmt, struct child_process_info*** cpis) {
  937. int ret = 0;
  938. int running = 0;
  939. struct child_process_info** procs = calloc(1, cmds->len * sizeof(*procs));
  940. if(cpis) *cpis = procs;
  941. if(fmt) printf(fmt, printpct(0)), fflush(stdout);
  942. int waiting = cmds->len;
  943. while(waiting > 0) {
  944. for(int i = 0; i < cmds->len; i++) {
  945. // keep the cores full
  946. if(!procs[i] && running < threads) {
  947. procs[i] = exec_cmdline_pipe(cmds->entries[i]);
  948. procs[i]->state = 'r';
  949. running++;
  950. }
  951. if(!procs[i] || procs[i]->state == 'd') continue;
  952. read_cpi(procs[i]);
  953. int status;
  954. // returns 0 if nothing happened, -1 on error, childpid if it exited
  955. int pid = waitpid(procs[i]->pid, &status, WNOHANG);
  956. if(pid != 0) {
  957. procs[i]->state = 'd';
  958. waiting--;
  959. running--;
  960. if(fmt) printf("\r"), printf(fmt, printpct((cmds->len - waiting) * 100.0f / (float)cmds->len)), fflush(stdout);
  961. read_cpi(procs[i]);
  962. procs[i]->output_buffer[procs[i]->buf_len] = 0;
  963. procs[i]->exit_status = WEXITSTATUS(status);
  964. if(!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  965. // printf("error on pid %d/%d = %d\n", pid, procs[i]->pid, WEXITSTATUS(status));
  966. // printf("%s\n", procs[i]->output_buffer);
  967. ret = 1;
  968. }
  969. }
  970. }
  971. usleep(100);
  972. }
  973. if(fmt) {
  974. printf("\r");
  975. if(ret) printf(fmt, "\e[1;31mFAIL\e[0m");
  976. else printf(fmt, "\e[32mDONE\e[0m");
  977. fflush(stdout);
  978. }
  979. printf("\n");
  980. return ret;
  981. }
  982. struct child_process_info* exec_cmdline_pipe(char* cmdline) {
  983. // kinda janky. it doesn't handle quotes
  984. char** args = strsplit(" ", cmdline);
  985. struct child_process_info* cpi = exec_process_pipe(args[0], args);
  986. // printf("executing '%s'\n", cmdline);
  987. // for(char** s = args; *s; s++) printf("%d - '%s'\n", (s-args), *s);
  988. free_strpp(args);
  989. return cpi;
  990. }
  991. // effectively a better, asynchronous version of system()
  992. // redirects and captures the child process i/o
  993. struct child_process_info* exec_process_pipe(char* exec_path, char* args[]) {
  994. int master, slave; // pty
  995. errno = 0;
  996. if(openpty(&master, &slave, NULL, NULL, NULL) < 0) {
  997. fprintf(stderr, "Error opening new pty for '%s' [errno=%d]\n", exec_path, errno);
  998. return NULL;
  999. }
  1000. errno = 0;
  1001. int childPID = fork();
  1002. if(childPID == -1) {
  1003. fprintf(stderr, "failed to fork trying to execute '%s'\n", exec_path);
  1004. perror(strerror(errno));
  1005. return NULL;
  1006. }
  1007. else if(childPID == 0) { // child process
  1008. setsid();
  1009. // redirect standard fd's to the pipe fd's
  1010. if(dup2(slave, fileno(stdin)) == -1) {
  1011. printf("failed 1\n");
  1012. exit(errno);
  1013. }
  1014. if(dup2(slave, fileno(stdout)) == -1) {
  1015. printf("failed 2\n");
  1016. exit(errno);
  1017. }
  1018. if(dup2(slave, fileno(stderr)) == -1) {
  1019. printf("failed 3\n");
  1020. exit(errno);
  1021. }
  1022. if(ioctl(slave, TIOCSCTTY, NULL) < 0) {
  1023. fprintf(stderr, "ioctl TIOCSCTTY failed: %s, %d\n", exec_path, errno);
  1024. }
  1025. // close original fd's
  1026. close(master);
  1027. close(slave);
  1028. // die when the parent does (linux only)
  1029. prctl(PR_SET_PDEATHSIG, SIGHUP);
  1030. // swap for the desired program
  1031. execvp(exec_path, args); // never returns if successful
  1032. fprintf(stderr, "failed to execute '%s'\n", exec_path);
  1033. exit(1); // kill the forked process
  1034. }
  1035. else { // parent process
  1036. // close the child-end of the pipes
  1037. struct child_process_info* cpi;
  1038. cpi = calloc(1, sizeof(*cpi));
  1039. cpi->pid = childPID;
  1040. cpi->pty = master;
  1041. cpi->state = 'q';
  1042. cpi->buf_alloc = 4096;
  1043. cpi->output_buffer = malloc(cpi->buf_alloc * sizeof(*cpi->output_buffer));
  1044. // set to non-blocking
  1045. fcntl(master, F_SETFL, fcntl(master, F_GETFL) | FNDELAY | O_NONBLOCK);
  1046. close(slave);
  1047. // tcsetattr(STDIN_FILENO, TCSANOW, &master);
  1048. // fcntl(master, F_SETFL, FNDELAY);
  1049. // int status;
  1050. // returns 0 if nothing happened, -1 on error
  1051. // pid = waitpid(childPID, &status, WNOHANG);
  1052. return cpi;
  1053. }
  1054. return NULL; // shouldn't reach here
  1055. }
  1056. /* -END- cprocs.c ----- */
  1057. /* ----- rglob.c ----- */
  1058. typedef struct rglob_entry {
  1059. char type;
  1060. char* full_path;
  1061. char* file_name;
  1062. // char* dir_name;
  1063. } rglob_entry;
  1064. typedef struct rglob {
  1065. char* pattern;
  1066. int len;
  1067. int alloc;
  1068. rglob_entry* entries;
  1069. } rglob;
  1070. int rglob_fn(char* full_path, char* file_name, unsigned char type, void* _results) {
  1071. rglob* res = (rglob*)_results;
  1072. if(0 == fnmatch(res->pattern, file_name, 0)) {
  1073. check_alloc(res);
  1074. res->entries[res->len].type = type;
  1075. res->entries[res->len].full_path = strcache(full_path);
  1076. res->entries[res->len].file_name = strcache(file_name);
  1077. res->len++;
  1078. }
  1079. return 0;
  1080. }
  1081. void recursive_glob(char* base_path, char* pattern, int flags, rglob* results) {
  1082. // to pass into recurse_dirs()
  1083. results->pattern = pattern;
  1084. results->len = 0;
  1085. results->alloc = 32;
  1086. results->entries = malloc(sizeof(*results->entries) * results->alloc);
  1087. recurse_dirs(base_path, rglob_fn, results, -1, flags);
  1088. }
  1089. /* -END- rglob.c ----- */
  1090. /* ----- gcc.c ----- */
  1091. strlist* parse_gcc_dep_file(char* dep_file_path, time_t* newest_mtime) {
  1092. size_t dep_src_len = 0;
  1093. strlist* dep_list;
  1094. time_t newest = 0;
  1095. char* dep_src = read_whole_file(dep_file_path, &dep_src_len);
  1096. if(!dep_src) return NULL;
  1097. dep_list = strlist_new();
  1098. // skip the first filename junk
  1099. char* s = strchr(dep_src, ':');
  1100. s++;
  1101. int ret = 0;
  1102. // gather dep strings, ignoring line continuations
  1103. while(*s) {
  1104. do {
  1105. s = strskip(s, " \t\r\n");
  1106. if(*s == '\\') {
  1107. if(s[1] == '\r') s++;
  1108. if(s[1] == '\n') s++;
  1109. }
  1110. } while(isspace(*s));
  1111. int dlen = span_path(s);
  1112. if(dlen == 0) break;
  1113. time_t dep_mtime;
  1114. char* dep_fake = strncache(s, dlen);
  1115. char* dep_real = resolve_path(dep_fake, &dep_mtime);
  1116. if(dep_mtime > newest) newest = dep_mtime;
  1117. strlist_push(dep_list, dep_real);
  1118. struncache(dep_fake);
  1119. struncache(dep_real);
  1120. s += dlen;
  1121. }
  1122. free(dep_src);
  1123. if(newest_mtime) *newest_mtime = newest;
  1124. return dep_list;
  1125. }
  1126. int gen_deps(char* src_path, char* dep_path, time_t src_mtime, time_t obj_mtime, objfile* obj) {
  1127. time_t dep_mtime = 0;
  1128. time_t newest_mtime = 0;
  1129. char* real_dep_path = resolve_path(dep_path, &dep_mtime);
  1130. if(dep_mtime < src_mtime) {
  1131. //gcc -MM -MG -MT $1 -MF "build/$1.d" $1 $CFLAGS $LDADD
  1132. // printf(" generating deps\n");
  1133. char* cmd = sprintfdup("gcc -MM -MG -MT '' -MF %s %s %s", dep_path, src_path, obj->gcc_opts_flat);
  1134. system(cmd);
  1135. free(cmd);
  1136. }
  1137. strlist* deps = parse_gcc_dep_file(real_dep_path, &newest_mtime);
  1138. // free or process deps
  1139. return newest_mtime > obj_mtime;
  1140. FAIL:
  1141. return 0;
  1142. }
  1143. char* default_compile_source(char* src_path, char* obj_path, objfile* obj) {
  1144. char* cmd = sprintfdup("gcc -c -o %s %s %s", obj_path, src_path, obj->gcc_opts_flat);
  1145. if(obj->verbose) puts(cmd);
  1146. return cmd;
  1147. }
  1148. void check_source(char* raw_src_path, objfile* o) {
  1149. time_t src_mtime, obj_mtime = 0, dep_mtime = 0;
  1150. char* src_path = resolve_path(raw_src_path, &src_mtime);
  1151. char* src_dir = dir_name(raw_src_path);
  1152. char* base = base_name(src_path);
  1153. // char* build_base = "debug";
  1154. char* src_build_dir = path_join(o->build_dir, src_dir);
  1155. char* obj_path = path_join(src_build_dir, base);
  1156. // cheap and dirty
  1157. size_t olen = strlen(obj_path);
  1158. obj_path[olen-1] = 'o';
  1159. strlist_push(&o->objs, obj_path);
  1160. char* dep_path = strcatdup(src_build_dir, "/", base, ".d");
  1161. mkdirp_cached(src_build_dir, 0755);
  1162. char* real_obj_path = resolve_path(obj_path, &obj_mtime);
  1163. if(obj_mtime < src_mtime) {
  1164. strlist_push(&o->compile_cache, o->compile_source_cmd(src_path, real_obj_path, o));
  1165. return;
  1166. }
  1167. if(gen_deps(src_path, dep_path, src_mtime, obj_mtime, o)) {
  1168. strlist_push(&o->compile_cache, o->compile_source_cmd(src_path, real_obj_path, o));
  1169. }
  1170. //gcc -c -o $2 $1 $CFLAGS $LDADD
  1171. }
  1172. int compile_cache_execute(objfile* o) {
  1173. int ret = 0;
  1174. struct child_process_info** cpis;
  1175. // printf("compile cache length %d", compile_cache.len);
  1176. ret = execute_mt(&o->compile_cache, g_nprocs, "Compiling... %s", &cpis);
  1177. if(ret) {
  1178. for(int i = 0; i < o->compile_cache.len; i++ ) {
  1179. if(cpis[i]->exit_status) {
  1180. printf("%.*s\n", (int)cpis[i]->buf_len, cpis[i]->output_buffer);
  1181. }
  1182. }
  1183. }
  1184. // TODO free compile cache
  1185. // TODO free cpis
  1186. return ret;
  1187. }
  1188. /* -END- gcc.c ----- */