_build.inc.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  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, strlist* objs, 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(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. // printf(" objtime compile\n");
  1165. strlist_push(&o->compile_cache, o->compile_source_cmd(src_path, real_obj_path, o));
  1166. return;
  1167. }
  1168. if(gen_deps(src_path, dep_path, src_mtime, obj_mtime, o)) {
  1169. // printf(" deep dep compile\n");
  1170. strlist_push(&o->compile_cache, o->compile_source_cmd(src_path, real_obj_path, o));
  1171. }
  1172. //gcc -c -o $2 $1 $CFLAGS $LDADD
  1173. }
  1174. int compile_cache_execute(objfile* o) {
  1175. int ret = 0;
  1176. struct child_process_info** cpis;
  1177. // printf("compile cache length %d", compile_cache.len);
  1178. ret = execute_mt(&o->compile_cache, g_nprocs, "Compiling... %s", &cpis);
  1179. if(ret) {
  1180. for(int i = 0; i < o->compile_cache.len; i++ ) {
  1181. if(cpis[i]->exit_status) {
  1182. printf("%.*s\n", (int)cpis[i]->buf_len, cpis[i]->output_buffer);
  1183. }
  1184. }
  1185. }
  1186. // TODO free compile cache
  1187. // TODO free cpis
  1188. return ret;
  1189. }
  1190. /* -END- gcc.c ----- */