12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601 |
- /* ----- header.c ----- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <errno.h>
- #include <stdarg.h>
- #include <signal.h>
- #include <unistd.h>
- #include <sys/prctl.h>
- #include <pty.h>
- #include <fcntl.h>
- #include <fnmatch.h>
- #include <dirent.h>
- #include <libgen.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/sysinfo.h>
- // link with -lutil
- // list all <header> files in use
- //egrep -ro '^\s*#\s*include\s+<(.*)>' src/ | sed -e 's/^.*<\(.*\)>.*$/\1/'
- #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
- #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
- #define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
- #define PP_NARG(...) PP_NARG_(__VA_ARGS__, PP_RSEQ_N())
- #define check_alloc(x) \
- if((x)->len + 1 >= (x)->alloc) { \
- (x)->alloc *= 2; \
- (x)->entries = realloc((x)->entries, (x)->alloc * sizeof(*(x)->entries)); \
- }
-
- char** g_gcc_opts_list;
- char* g_gcc_opts_flat;
- char* g_gcc_include;
- char* g_gcc_libs;
- int g_nprocs;
- typedef struct {
- // user config
- char** sources;
- char** debug_cflags;
- char** release_cflags;
- char** profiling_cflags;
- char** common_cflags;
- char** libs_needed;
- char** lib_headers_needed;
- char** ld_add;
-
- char* source_dir;// "src"
- char* exe_path; // the executable name
- char* base_build_dir; // = "build";
-
- char mode_debug;
- char mode_profiling;
- char mode_release;
- char clean_first;
-
- char verbose;
-
- // internally calculated
- char** gcc_opts_list;
- char* gcc_opts_flat;
- char* gcc_include;
- char* gcc_libs;
-
- char* build_dir; // full build path, including debug/release options
- char* build_subdir;
- } objfile;
- /* -END- header.c ----- */
- /* ----- string.c ----- */
- #define strjoin(j, ...) strjoin_(j, PP_NARG(__VA_ARGS__), __VA_ARGS__)
- char* strjoin_(char* joiner, size_t nargs, ...);
- #define strcatdup(...) strcatdup_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
- char* strcatdup_(size_t nargs, ...);
- size_t list_len(char** list);
- char* join_str_list(char* list[], char* joiner);
- char* sprintfdup(char* fmt, ...);
- #define path_join(...) path_join_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
- char* path_join_(size_t nargs, ...);
- #define concat_lists(...) concat_lists_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
- char** concat_lists_(int nargs, ...);
- size_t span_path(char* s) {
- size_t n = 0;
- for(; *s; s++, n++) {
- if(isspace(*s)) break;
- if(*s == '\\') {
- s++;
- n++;
- }
- }
- return n;
- }
- // splits on whitespace
- char** strsplit(char* splitters, char* in) {
- char* e;
- int alloc = 32;
- int len = 0;
- char** list = malloc(alloc * sizeof(*list));
-
- for(char* s = in; *s;) {
- e = strpbrk(s, splitters);
- if(!e) e = s + strlen(s);
-
- if(len >= alloc - 1) {
- alloc *= 2;
- list = realloc(list, alloc* sizeof(*list));
- }
-
- list[len++] = strndup(s, e - s);
-
- e += strspn(e, splitters);
- s = e;
- }
-
- list[len] = NULL;
-
- return list;
- }
- static inline char* strskip(char* s, char* skip) {
- return s + strspn(s, skip);
- }
- // concatenate all argument strings together in a new buffer
- char* strcatdup_(size_t nargs, ...) {
- size_t total = 0;
- char* out, *end;
-
- if(nargs == 0) return NULL;
-
- // calculate total buffer len
- va_list va;
- va_start(va, nargs);
-
- for(size_t i = 0; i < nargs; i++) {
- char* s = va_arg(va, char*);
- if(s) total += strlen(s);
- }
-
- va_end(va);
-
- out = malloc((total + 1) * sizeof(char*));
- end = out;
-
- va_start(va, nargs);
-
- for(size_t i = 0; i < nargs; i++) {
- char* s = va_arg(va, char*);
- if(s) {
- strcpy(end, s); // not exactly the ost efficient, but maybe faster than
- end += strlen(s); // a C version. TODO: test the speed
- };
- }
-
- va_end(va);
-
- *end = 0;
-
- return out;
- }
- // concatenate all argument strings together in a new buffer,
- // with the given joining string between them
- char* strjoin_(char* joiner, size_t nargs, ...) {
- size_t total = 0;
- char* out, *end;
- size_t j_len;
-
- if(nargs == 0) return NULL;
-
- // calculate total buffer len
- va_list va;
- va_start(va, nargs);
-
- for(size_t i = 0; i < nargs; i++) {
- char* s = va_arg(va, char*);
- if(s) total += strlen(s);
- }
-
- va_end(va);
-
- j_len = strlen(joiner);
- total += j_len * (nargs - 1);
-
- out = malloc((total + 1) * sizeof(char*));
- end = out;
-
- va_start(va, nargs);
-
- for(size_t i = 0; i < nargs; i++) {
- char* s = va_arg(va, char*);
- if(s) {
- if(i > 0) {
- strcpy(end, joiner);
- end += j_len;
- }
-
- strcpy(end, s); // not exactly the ost efficient, but maybe faster than
- end += strlen(s); // a C version. TODO: test the speed
- };
- }
-
- va_end(va);
-
- *end = 0;
-
- return out;
- }
- // allocates a new buffer and calls sprintf with it
- // why isn't this a standard function?
- char* sprintfdup(char* fmt, ...) {
- va_list va;
-
- va_start(va, fmt);
- size_t n = vsnprintf(NULL, 0, fmt, va);
- char* buf = malloc(n + 1);
- va_end(va);
-
- va_start(va, fmt);
- vsnprintf(buf, n + 1, fmt, va);
- va_end(va);
-
- return buf;
- }
- void free_strpp(char** l) {
- for(char** s = l; *s; s++) free(*s);
- free(l);
- }
- size_t list_len(char** list) {
- size_t total = 0;
- for(; *list; list++) total++;
- return total;
- }
- char** concat_lists_(int nargs, ...) {
- size_t total = 0;
- char** out, **end;
- if(nargs == 0) return NULL;
- // calculate total list length
- va_list va;
- va_start(va, nargs);
- for(size_t i = 0; i < nargs; i++) {
- char** s = va_arg(va, char**);
- if(s) total += list_len(s);
- }
- va_end(va);
- out = malloc((total + 1) * sizeof(char**));
- end = out;
- va_start(va, nargs);
-
- // concat lists
- for(size_t i = 0; i < nargs; i++) {
- char** s = va_arg(va, char**);
- size_t l = list_len(s);
-
- if(s) {
- memcpy(end, s, l * sizeof(*s));
- end += l;
- }
- }
- va_end(va);
- *end = 0;
- return out;
- }
- char* join_str_list(char* list[], char* joiner) {
- size_t list_len = 0;
- size_t total = 0;
- size_t jlen = strlen(joiner);
-
- // calculate total length
- for(int i = 0; list[i]; i++) {
- list_len++;
- total += strlen(list[i]);
- }
-
- if(total == 0) return strdup("");
-
- total += (list_len - 1) * jlen;
- char* out = malloc((total + 1) * sizeof(*out));
-
- char* end = out;
- for(int i = 0; list[i]; i++) {
- char* s = list[i];
- size_t l = strlen(s);
-
- if(i > 0) {
- memcpy(end, joiner, jlen);
- end += jlen;
- }
-
- if(s) {
- memcpy(end, s, l);
- end += l;
- }
-
- total += strlen(list[i]);
- }
-
- *end = 0;
-
- return out;
- }
- char* path_join_(size_t nargs, ...) {
- size_t total = 0;
- char* out, *end;
- size_t j_len;
- char* joiner = "/";
- int escape;
- if(nargs == 0) return NULL;
- // calculate total buffer length
- va_list va;
- va_start(va, nargs);
- for(size_t i = 0; i < nargs; i++) {
- char* s = va_arg(va, char*);
- if(s) total += strlen(s);
- }
- va_end(va);
- j_len = strlen(joiner);
- total += j_len * (nargs - 1);
- out = malloc((total + 1) * sizeof(char*));
- end = out;
- va_start(va, nargs);
- for(size_t i = 0; i < nargs; i++) {
- char* s = va_arg(va, char*);
- size_t l = strlen(s);
-
- if(s) {
- if(l > 1) {
- escape = s[l-2] == '\\' ? 1 : 0;
- }
- if(i > 0 && (s[0] == joiner[0])) {
- s++;
- l--;
- }
- if(i > 0 && i != nargs-1 && !escape && (s[l-1] == joiner[0])) {
- l--;
- }
- if(i > 0) {
- strcpy(end, joiner);
- end += j_len;
- }
- // should be strncpy, but GCC is so fucking stupid that it
- // has a warning about using strncpy to do exactly what
- // strncpy does if you read the fucking man page.
- // fortunately, we are already terminating our strings
- // manually so memcpy is a drop-in replacement here.
- memcpy(end, s, l);
- end += l;
- }
- }
- va_end(va);
- *end = 0;
- return out;
- }
- /* -END- string.c ----- */
- /* ----- strcache.c ----- */
- typedef unsigned long hash_t;
- hash_t strhash(char* str) {
- unsigned long h = 0;
- int c;
- while(c = *str++) {
- h = c + (h << 6) + (h << 16) - h;
- }
- return h;
- }
- hash_t strnhash(char* str, size_t n) {
- unsigned long h = 0;
- int c;
- while((c = *str++) && n--) {
- h = c + (h << 6) + (h << 16) - h;
- }
-
- return h;
- }
- typedef struct {
- hash_t hash;
- char* str;
- unsigned int len;
- int refs;
- } string_cache_bucket;
- typedef struct {
- int fill;
- int alloc;
- string_cache_bucket* buckets;
- } string_cache_t;
- string_cache_t string_cache;
- void string_cache_init(int alloc) {
- string_cache.fill = 0;
- string_cache.alloc = alloc ? alloc : 1024;
- string_cache.buckets = calloc(1, string_cache.alloc * sizeof(*string_cache.buckets));
- }
- int string_cache_find_bucket(string_cache_t* ht, hash_t hash, char* key) {
- int b = hash % ht->alloc;
-
- for(int i = 0; i < ht->alloc; i++) {
- // empty bucket
- if(ht->buckets[b].str == NULL) return b;
-
- // full bucket
- if(ht->buckets[b].hash == hash) {
- if(0 == strcmp(key, ht->buckets[b].str)) {
- return b;
- }
- }
-
- // probe forward on collisions
- b = (b + 1) % ht->alloc;
- }
-
- // should never reach here
- printf("oops. -1 bucket \n");
- return -1;
- }
- void string_cache_expand(string_cache_t* ht) {
- int old_alloc = ht->alloc;
- ht->alloc *= 2;
-
- string_cache_bucket* old = ht->buckets;
- ht->buckets = calloc(1, ht->alloc * sizeof(*ht->buckets));
-
- for(int i = 0, f = 0; i < old_alloc && f < ht->fill; i++) {
- if(!old[i].str) continue;
-
- int b = string_cache_find_bucket(ht, old[i].hash, old[i].str);
-
- ht->buckets[b] = old[i];
- f++;
- }
-
- free(old);
- }
- char* strcache(char* in) {
- hash_t hash = strhash(in);
- int b = string_cache_find_bucket(&string_cache, hash, in);
-
- if(!string_cache.buckets[b].str) {
- if(string_cache.fill > string_cache.alloc * .80) {
- string_cache_expand(&string_cache);
-
- // the bucket location has changed
- b = string_cache_find_bucket(&string_cache, hash, in);
- }
-
- string_cache.fill++;
-
- string_cache.buckets[b].str = strdup(in);
- string_cache.buckets[b].hash = hash;
- string_cache.buckets[b].refs = 1;
- string_cache.buckets[b].len = strlen(in);
- }
- else {
- string_cache.buckets[b].refs++;
- }
-
- return string_cache.buckets[b].str;
- }
- char* strncache(char* in, size_t n) {
- hash_t hash = strnhash(in, n);
- int b = string_cache_find_bucket(&string_cache, hash, in);
-
- if(!string_cache.buckets[b].str) {
- if(string_cache.fill > string_cache.alloc * .80) {
- string_cache_expand(&string_cache);
-
- // the bucket location has changed
- b = string_cache_find_bucket(&string_cache, hash, in);
- }
-
- string_cache.fill++;
-
- string_cache.buckets[b].str = strndup(in, n);
- string_cache.buckets[b].hash = hash;
- string_cache.buckets[b].refs = 1;
- string_cache.buckets[b].len = n;
- }
- else {
- string_cache.buckets[b].refs++;
- }
-
- return string_cache.buckets[b].str;
- }
- void struncache(char* in) {
- hash_t hash = strhash(in);
- int b = string_cache_find_bucket(&string_cache, hash, in);
-
- if(!string_cache.buckets[b].str) {
- // normal string, free it
- free(in);
- return;
- }
-
- string_cache.buckets[b].refs--;
- if(string_cache.buckets[b].refs == 0) {
- // just do nothing for now. deletion is a pain.
- }
- }
- /* -END- strcache.c ----- */
- /* ----- hash.c ----- */
- typedef struct {
- hash_t hash;
- char* key;
- void* value;
- } hashbucket;
- typedef struct hashtable {
- int fill;
- int alloc;
- hashbucket* buckets;
- } hashtable;
- void hash_init(hashtable* ht, int alloc) {
- ht->fill = 0;
- ht->alloc = alloc ? alloc : 128;
- ht->buckets = calloc(1, ht->alloc * sizeof(*ht->buckets));
- }
- hashtable* hash_new(int alloc) {
- hashtable* ht = malloc(sizeof(*ht));
- hash_init(ht, alloc);
- return ht;
- }
- int hash_find_bucket(hashtable* ht, hash_t hash, char* key) {
- int b = hash % ht->alloc;
-
- for(int i = 0; i < ht->alloc; i++) {
- // empty bucket
- if(ht->buckets[b].key == NULL) return b;
-
- // full bucket
- if(ht->buckets[b].hash == hash) {
- if(0 == strcmp(key, ht->buckets[b].key)) {
- return b;
- }
- }
-
- // probe forward on collisions
- b = (b + 1) % ht->alloc;
- }
-
- // should never reach here
- printf("oops. -1 bucket \n");
- return -1;
- }
- void hash_expand(hashtable* ht) {
- int old_alloc = ht->alloc;
- ht->alloc *= 2;
-
- hashbucket* old = ht->buckets;
- ht->buckets = calloc(1, ht->alloc * sizeof(*ht->buckets));
-
- for(int i = 0, f = 0; i < old_alloc && f < ht->fill; i++) {
- if(!old[i].key) continue;
-
- int b = hash_find_bucket(ht, old[i].hash, old[i].key);
-
- ht->buckets[b] = old[i];
- f++;
- }
-
- free(old);
- }
- void hash_insert(hashtable* ht, char* key, void* value) {
-
- hash_t hash = strhash(key);
- int b = hash_find_bucket(ht, hash, key);
-
- if(!ht->buckets[b].key) {
- if(ht->fill > ht->alloc * .80) {
- hash_expand(ht);
-
- // the bucket location has changed
- b = hash_find_bucket(ht, hash, key);
- }
-
- ht->fill++;
- }
-
- ht->buckets[b].key = strcache(key);
- ht->buckets[b].hash = hash;
- ht->buckets[b].value = value;
- }
- void* hash_find(hashtable* ht, char* key) {
- hash_t hash = strhash(key);
- int b = hash_find_bucket(ht, hash, key);
-
- return ht->buckets[b].value;
- }
- /* -END- hash.c ----- */
- /* ----- strlist.c ----- */
- typedef struct strlist {
- int len;
- int alloc;
- char** entries;
- } strlist;
- void strlist_init(strlist* sl) {
- sl->len = 0;
- sl->alloc = 32;
- sl->entries = malloc(sl->alloc * sizeof(*sl->entries));
- }
- strlist* strlist_new() {
- strlist* sl = malloc(sizeof(*sl));
- strlist_init(sl);
- return sl;
- }
- void strlist_push(strlist* sl, char* e) {
- check_alloc(sl);
- sl->entries[sl->len++] = e;
- sl->entries[sl->len] = 0;
- }
- /* -END- strlist.c ----- */
- /* ----- fs.c ----- */
- typedef struct realname_entry {
- char* realname;
- time_t mtime;
- } realname_entry;
- struct {
- hashtable names;
- int len;
- int alloc;
- struct realname_entry* entries;
- } realname_cache;
- hashtable mkdir_cache;
- #define FSU_EXCLUDE_HIDDEN (1<<0)
- #define FSU_NO_FOLLOW_SYMLINKS (1<<1)
- #define FSU_INCLUDE_DIRS (1<<2)
- #define FSU_EXCLUDE_FILES (1<<3)
- // return 0 to continue, nonzero to stop all directory scanning
- typedef int (*readDirCallbackFn)(char* /*fullPath*/, char* /*fileName*/, unsigned char /*type*/, void* /*data*/);
- // returns negative on error, nonzero if scanning was halted by the callback
- int recurse_dirs(
- char* path,
- readDirCallbackFn fn,
- void* data,
- int depth,
- unsigned int flags
- );
- void realname_cache_init();
- time_t realname_cache_add(char* fake_name, char* real_name);
- realname_entry* realname_cache_search_real(char* real_name);
- realname_entry* realname_cache_search(char* fake_name);
- char* realname_cache_find(char* fake_name);
- char* resolve_path(char* in, time_t* mtime_out);
- char* read_whole_file(char* path, size_t* srcLen);
- char* dir_name(char* path) {
- char* n = strdup(path);
- char* o = dirname(n);
- return strcache(o);
- }
- char* base_name(char* path) {
- char* n = strdup(path);
- char* o = basename(n);
- return strcache(o);
- }
- // does not handle escaped slashes
- int mkdirp(char* path, mode_t mode) {
-
- char* clean_path = strdup(path);
-
- // inch along the path creating each directory in line
- for(char* p = clean_path; *p; p++) {
- if(*p == '/') {
- *p = 0;
-
- if(mkdir(clean_path, mode)) {
- if(errno != EEXIST) goto FAIL;
- }
-
- *p = '/';
- }
- }
-
- // mop up the last dir
- if(mkdir(clean_path, mode)) {
- if(errno != EEXIST) goto FAIL;
- }
-
- free(clean_path);
- return 0;
-
- FAIL:
- free(clean_path);
- return -1;
- }
- void mkdirp_cached(char* path, mode_t mode) {
- void* there = hash_find(&mkdir_cache, path);
- if(!there) {
- hash_insert(&mkdir_cache, path, NULL);
- mkdirp(path, mode);
- }
- }
- // works like realpath(), except also handles ~/
- char* resolve_path(char* in, time_t* mtime_out) {
- int tmp_was_malloced = 0;
- char* out, *tmp;
-
- if(!in) return NULL;
-
- realname_entry* e = realname_cache_search(in);
- if(e) {
- if(mtime_out) *mtime_out = e->mtime;
- return strcache(e->realname);
- }
-
- // skip leading whitespace
- while(isspace(*in)) in++;
-
- // handle home dir shorthand
- if(in[0] == '~') {
- char* home = getenv("HOME");
-
- tmp_was_malloced = 1;
- tmp = malloc(sizeof(*tmp) * (strlen(home) + strlen(in) + 2));
-
- strcpy(tmp, home);
- strcat(tmp, "/"); // just in case
- strcat(tmp, in + 1);
- }
- else tmp = in;
-
- out = realpath(tmp, NULL);
-
- if(tmp_was_malloced) free(tmp);
-
- time_t t = 0;
- if(out) {
- // put it in the cache
- t = realname_cache_add(in, out);
- }
- else {
- // temporary
- struct stat st;
- if(!lstat(in, &st))
- t = st.st_mtim.tv_sec;
- }
-
- if(mtime_out) *mtime_out = t;
- return out ? out : in;
- }
- void realname_cache_init() {
- realname_cache.len = 0;
- realname_cache.alloc = 1024;
- realname_cache.entries = malloc(realname_cache.alloc * sizeof(*realname_cache.entries));
- hash_init(&realname_cache.names, 1024);
- }
- time_t realname_cache_add(char* fake_name, char* real_name) {
- realname_entry* e = hash_find(&realname_cache.names, fake_name);
- if(e) return e->mtime;
-
- e = hash_find(&realname_cache.names, real_name);
- if(!e) {
- struct stat st;
- lstat(real_name, &st);
-
- e = &realname_cache.entries[realname_cache.len];
- e->realname = strcache(real_name);
- e->mtime = st.st_mtim.tv_sec;
- realname_cache.len++;
-
- hash_insert(&realname_cache.names, real_name, e);
- }
-
- hash_insert(&realname_cache.names, fake_name, e);
-
- return e->mtime;
- }
- realname_entry* realname_cache_search_real(char* real_name) {
- for(int i = 0; i < realname_cache.len; i++) {
- if(0 == strcmp(real_name, realname_cache.entries[i].realname)) {
- return &realname_cache.entries[i];
- }
- }
-
- return NULL;
- }
- realname_entry* realname_cache_search(char* fake_name) {
- return hash_find(&realname_cache.names, fake_name);
- }
- char* realname_cache_find(char* fake_name) {
- realname_entry* r = realname_cache_search(fake_name);
- return r ? r->realname : NULL;
- }
- // returns negative on error, nonzero if scanning was halted by the callback
- int recurse_dirs(
- char* path,
- readDirCallbackFn fn,
- void* data,
- int depth,
- unsigned int flags
- ) {
-
- DIR* derp;
- struct dirent* result;
- int stop = 0;
-
- if(fn == NULL) {
- fprintf(stderr, "Error: readAllDir called with null function pointer.\n");
- return -1;
- }
-
- derp = opendir(path);
- if(derp == NULL) {
- fprintf(stderr, "Error opening directory '%s': %s\n", path, strerror(errno));
- return -1;
- }
-
-
- while((result = readdir(derp)) && !stop) {
- char* n = result->d_name;
- unsigned char type = DT_UNKNOWN;
- char* fullPath;
-
- // skip self and parent dir entries
- if(n[0] == '.') {
- if(n[1] == '.' && n[2] == 0) continue;
- if(n[1] == 0) continue;
-
- if(flags & FSU_EXCLUDE_HIDDEN) continue;
- }
- #ifdef _DIRENT_HAVE_D_TYPE
- type = result->d_type; // the way life should be
- #else
- // do some slow extra bullshit to get the type
- fullPath = path_join(path, n);
-
- struct stat upgrade_your_fs;
-
- lstat(fullPath, &upgrade_your_fs);
-
- if(S_ISREG(upgrade_your_fs.st_mode)) type = DT_REG;
- else if(S_ISDIR(upgrade_your_fs.st_mode)) type = DT_DIR;
- else if(S_ISLNK(upgrade_your_fs.st_mode)) type = DT_LNK;
- #endif
-
- if(flags & FSU_NO_FOLLOW_SYMLINKS && type == DT_LNK) {
- continue;
- }
-
- #ifdef _DIRENT_HAVE_D_TYPE
- fullPath = path_join(path, n);
- #endif
-
- if(type == DT_DIR) {
- if(flags & FSU_INCLUDE_DIRS) {
- stop = fn(fullPath, n, type, data);
- }
- if(depth != 0) {
- stop |= recurse_dirs(fullPath, fn, data, depth - 1, flags);
- }
- }
- else if(type == DT_REG) {
- if(!(flags & FSU_EXCLUDE_FILES)) {
- stop = fn(fullPath, n, type, data);
- }
- }
-
- free(fullPath);
- }
-
-
- closedir(derp);
-
- return stop;
- }
- char* read_whole_file(char* path, size_t* srcLen) {
- size_t fsize, total_read = 0, bytes_read;
- char* contents;
- FILE* f;
-
-
- f = fopen(path, "rb");
- if(!f) {
- fprintf(stderr, "Could not open file \"%s\"\n", path);
- return NULL;
- }
-
- fseek(f, 0, SEEK_END);
- fsize = ftell(f);
- rewind(f);
-
- contents = malloc(fsize + 1);
-
- while(total_read < fsize) {
- bytes_read = fread(contents + total_read, sizeof(char), fsize - total_read, f);
- total_read += bytes_read;
- }
-
- contents[fsize] = 0;
-
- fclose(f);
-
- if(srcLen) *srcLen = fsize;
-
- return contents;
- }
- /* -END- fs.c ----- */
- /* ----- pkgconfig.c ----- */
- char* pkg_config(char** packages, char* opts) {
- char* tmp;
-
- int num_pkgs = list_len(packages);
- if(num_pkgs == 0) return strdup("");
-
- char* pkgs = join_str_list(packages, " ");
-
- for(char* c = opts; *c; c++) {
- switch(*c) {
- case 'c':
- case 'C':
- case 'i':
- case 'I':
- tmp = strjoin(" ", "--cflags", pkgs);
- free(pkgs);
- pkgs = tmp;
- break;
-
- case 'l':
- case 'L':
- tmp = strjoin(" ", "--libs", pkgs);
- free(pkgs);
- pkgs = tmp;
- break;
- }
- }
-
- tmp = strjoin(" ", "pkg-config", pkgs);
- free(pkgs);
-
- FILE* f = popen(tmp, "r");
- free(tmp);
- if(!f) {
- fprintf(stderr, "Could not run command '%s'\n", tmp);
- exit(1);
- return NULL;
- }
-
- int len = 2048;
- int fill = 0;
- char* buffer = malloc(len * sizeof(*buffer));
- while(!feof(f)) {
- if(fill + 1 >= len) {
- len *= 2;
- buffer = realloc(buffer, len * sizeof(*buffer));
- }
- fill += fread(buffer + fill, 1, len - fill - 1, f);
- }
-
- buffer[fill] = 0;
- pclose(f);
-
- // strip out newlines and other garbage
- for(char* c = buffer; *c; c++) if(isspace(*c)) *c = ' ';
-
- return buffer;
- }
- /* -END- pkgconfig.c ----- */
- void parse_cli_opts(int argc, char** argv, objfile* obj) {
- for(int a = 1; a < argc; a++) {
- if(argv[a][0] == '-') {
- for(int i = 0; argv[a][i]; i++) {
-
- switch(argv[a][i]) {
- case 'd': // debug: -ggdb
- obj->mode_debug = 1;
- if(obj->mode_release == 1) {
- fprintf(stderr, "Debug and Release set at the same time.\n");
- }
- break;
-
- case 'p': // profiling: -pg
- obj->mode_profiling = 1;
- break;
-
- case 'r': // release: -O3
- obj->mode_release = 1;
- obj->mode_debug = 0;
- break;
-
- case 'c': // clean
- obj->clean_first = 1;
- break;
-
- case 'v': // verbose
- obj->verbose = 1;
- break;
- }
- }
- }
-
- }
-
-
- }
- void start_obj(objfile* obj) {
- unlink(obj->exe_path);
-
- obj->build_subdir = calloc(1, 20);
-
- if(obj->mode_debug) strcat(obj->build_subdir, "d");
- if(obj->mode_profiling) strcat(obj->build_subdir, "p");
- if(obj->mode_release) strcat(obj->build_subdir, "r");
- obj->build_dir = path_join(obj->base_build_dir, obj->build_subdir);
-
- // delete old build files if needed
- if(obj->clean_first) {
- printf("Cleaning directory %s/\n", obj->build_dir);
- system(sprintfdup("rm -rf %s/*", obj->build_dir));
- }
-
- // ensure the build dir exists
- mkdirp_cached(obj->build_dir, 0755);
-
-
- // flatten all the gcc options
- obj->gcc_opts_list = concat_lists(obj->ld_add, obj->common_cflags);
-
- if(obj->mode_debug) obj->gcc_opts_list = concat_lists(obj->gcc_opts_list, obj->debug_cflags);
- if(obj->mode_profiling) obj->gcc_opts_list = concat_lists(obj->gcc_opts_list, obj->profiling_cflags);
- if(obj->mode_release) obj->gcc_opts_list = concat_lists(obj->gcc_opts_list, obj->release_cflags);
-
- obj->gcc_opts_flat = join_str_list(obj->gcc_opts_list, " ");
- obj->gcc_include = pkg_config(obj->lib_headers_needed, "I");
- obj->gcc_libs = pkg_config(obj->libs_needed, "L");
-
- char* tmp = obj->gcc_opts_flat;
- obj->gcc_opts_flat = strjoin(" ", obj->gcc_opts_flat, obj->gcc_include);
- free(tmp);
-
- }
- /* ----- cprocs.c ----- */
- struct child_process_info {
- int pid;
- char state; // 'q'ueued, 'r'unning, 'd'ead
- int exit_status;
- int pty;
- int child_stdin;
- int child_stdout;
- int child_stderr;
- FILE* f_stdin;
- FILE* f_stdout;
- FILE* f_stderr;
-
- char* output_buffer;
- size_t buf_alloc;
- size_t buf_len;
- };
- struct child_process_info* exec_cmdline_pipe(char* cmdline);
- struct child_process_info* exec_process_pipe(char* exec_path, char* args[]);
- void read_cpi(struct child_process_info* cpi) {
- while(1) {
- if(cpi->buf_len > cpi->buf_alloc - 128) {
- cpi->buf_alloc *= 2;
- cpi->output_buffer = realloc(cpi->output_buffer, cpi->buf_alloc * sizeof(*cpi->output_buffer));
- }
-
- int ret = read(cpi->pty, cpi->output_buffer + cpi->buf_len, cpi->buf_alloc - cpi->buf_len - 1);
- if(ret > 0) cpi->buf_len += ret;
- else return;
- }
-
- return ;
- }
- char* printpct(float f) {
- static char buf[32];
- if(f != 100) sprintf(buf, "\e[1;33m %2.0f%%\e[0m", f);
- else sprintf(buf, "\e[1;32m100%%\e[0m");
-
- return buf;
- }
- int execute_mt(strlist* cmds, int threads, char* fmt, struct child_process_info*** cpis) {
- int ret = 0;
- int running = 0;
-
- struct child_process_info** procs = calloc(1, cmds->len * sizeof(*procs));
- if(cpis) *cpis = procs;
-
- if(fmt) printf(fmt, printpct(0)), fflush(stdout);
-
- int waiting = cmds->len;
- while(waiting > 0) {
- for(int i = 0; i < cmds->len; i++) {
-
- // keep the cores full
- if(!procs[i] && running < threads) {
- procs[i] = exec_cmdline_pipe(cmds->entries[i]);
- procs[i]->state = 'r';
- running++;
- }
-
- if(!procs[i] || procs[i]->state == 'd') continue;
-
-
- read_cpi(procs[i]);
-
- int status;
- // returns 0 if nothing happened, -1 on error, childpid if it exited
- int pid = waitpid(procs[i]->pid, &status, WNOHANG);
- if(pid != 0) {
- procs[i]->state = 'd';
- waiting--;
- running--;
-
- if(fmt) printf("\r"), printf(fmt, printpct((cmds->len - waiting) * 100.0f / (float)cmds->len)), fflush(stdout);
-
- read_cpi(procs[i]);
-
- procs[i]->output_buffer[procs[i]->buf_len] = 0;
- procs[i]->exit_status = WEXITSTATUS(status);
-
- if(!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
- // printf("error on pid %d/%d = %d\n", pid, procs[i]->pid, WEXITSTATUS(status));
- // printf("%s\n", procs[i]->output_buffer);
- ret = 1;
- }
- }
-
-
- }
-
-
- usleep(100);
- }
-
- if(fmt) {
- printf("\r");
- if(ret) printf(fmt, "\e[1;31mFAIL\e[0m");
- else printf(fmt, "\e[32mDONE\e[0m");
- fflush(stdout);
- }
-
- printf("\n");
-
-
- return ret;
- }
- struct child_process_info* exec_cmdline_pipe(char* cmdline) {
- // kinda janky. it doesn't handle quotes
- char** args = strsplit(" ", cmdline);
- struct child_process_info* cpi = exec_process_pipe(args[0], args);
- // printf("executing '%s'\n", cmdline);
- // for(char** s = args; *s; s++) printf("%d - '%s'\n", (s-args), *s);
- free_strpp(args);
-
- return cpi;
- }
- // effectively a better, asynchronous version of system()
- // redirects and captures the child process i/o
- struct child_process_info* exec_process_pipe(char* exec_path, char* args[]) {
-
-
- int master, slave; // pty
-
-
- errno = 0;
- if(openpty(&master, &slave, NULL, NULL, NULL) < 0) {
- fprintf(stderr, "Error opening new pty for '%s' [errno=%d]\n", exec_path, errno);
- return NULL;
- }
-
- errno = 0;
-
- int childPID = fork();
- if(childPID == -1) {
-
- fprintf(stderr, "failed to fork trying to execute '%s'\n", exec_path);
- perror(strerror(errno));
- return NULL;
- }
- else if(childPID == 0) { // child process
-
- setsid();
-
- // redirect standard fd's to the pipe fd's
- if(dup2(slave, fileno(stdin)) == -1) {
- printf("failed 1\n");
- exit(errno);
- }
- if(dup2(slave, fileno(stdout)) == -1) {
- printf("failed 2\n");
- exit(errno);
- }
- if(dup2(slave, fileno(stderr)) == -1) {
- printf("failed 3\n");
- exit(errno);
- }
-
- if(ioctl(slave, TIOCSCTTY, NULL) < 0) {
- fprintf(stderr, "ioctl TIOCSCTTY failed: %s, %d\n", exec_path, errno);
- }
-
- // close original fd's
- close(master);
- close(slave);
-
- // die when the parent does (linux only)
- prctl(PR_SET_PDEATHSIG, SIGHUP);
-
- // swap for the desired program
- execvp(exec_path, args); // never returns if successful
-
- fprintf(stderr, "failed to execute '%s'\n", exec_path);
- exit(1); // kill the forked process
- }
- else { // parent process
-
- // close the child-end of the pipes
- struct child_process_info* cpi;
- cpi = calloc(1, sizeof(*cpi));
- cpi->pid = childPID;
- cpi->pty = master;
-
- cpi->state = 'q';
- cpi->buf_alloc = 4096;
- cpi->output_buffer = malloc(cpi->buf_alloc * sizeof(*cpi->output_buffer));
-
- // set to non-blocking
- fcntl(master, F_SETFL, fcntl(master, F_GETFL) | FNDELAY | O_NONBLOCK);
-
- close(slave);
-
- // tcsetattr(STDIN_FILENO, TCSANOW, &master);
- // fcntl(master, F_SETFL, FNDELAY);
-
- // int status;
- // returns 0 if nothing happened, -1 on error
- // pid = waitpid(childPID, &status, WNOHANG);
-
- return cpi;
- }
-
- return NULL; // shouldn't reach here
- }
- /* -END- cprocs.c ----- */
- /* ----- rglob.c ----- */
- typedef struct rglob_entry {
- char type;
- char* full_path;
- char* file_name;
- // char* dir_name;
- } rglob_entry;
- typedef struct rglob {
- char* pattern;
- int len;
- int alloc;
- rglob_entry* entries;
-
- } rglob;
- int rglob_fn(char* full_path, char* file_name, unsigned char type, void* _results) {
- rglob* res = (rglob*)_results;
-
- if(0 == fnmatch(res->pattern, file_name, 0)) {
- check_alloc(res);
-
- res->entries[res->len].type = type;
- res->entries[res->len].full_path = strcache(full_path);
- res->entries[res->len].file_name = strcache(file_name);
- res->len++;
- }
-
- return 0;
- }
- void recursive_glob(char* base_path, char* pattern, int flags, rglob* results) {
-
- // to pass into recurse_dirs()
- results->pattern = pattern;
- results->len = 0;
- results->alloc = 32;
- results->entries = malloc(sizeof(*results->entries) * results->alloc);
-
- recurse_dirs(base_path, rglob_fn, results, -1, flags);
- }
- /* -END- rglob.c ----- */
- /* ----- gcc.c ----- */
- strlist compile_cache;
- int compile_cache_execute() {
- int ret = 0;
- struct child_process_info** cpis;
- // printf("compile cache length %d", compile_cache.len);
- ret = execute_mt(&compile_cache, g_nprocs, "Compiling... %s", &cpis);
-
- if(ret) {
- for(int i = 0; i < compile_cache.len; i++ ) {
- if(cpis[i]->exit_status) {
- printf("%.*s\n", (int)cpis[i]->buf_len, cpis[i]->output_buffer);
- }
- }
-
- }
-
- // TODO free compile cache
- // TODO free cpis
- return ret;
- }
- strlist* parse_gcc_dep_file(char* dep_file_path, time_t* newest_mtime) {
- size_t dep_src_len = 0;
- strlist* dep_list;
- time_t newest = 0;
-
- char* dep_src = read_whole_file(dep_file_path, &dep_src_len);
- if(!dep_src) return NULL;
-
- dep_list = strlist_new();
-
- // skip the first filename junk
- char* s = strchr(dep_src, ':');
- s++;
-
- int ret = 0;
-
- // gather dep strings, ignoring line continuations
- while(*s) {
- do {
- s = strskip(s, " \t\r\n");
- if(*s == '\\') {
- if(s[1] == '\r') s++;
- if(s[1] == '\n') s++;
- }
- } while(isspace(*s));
-
- int dlen = span_path(s);
- if(dlen == 0) break;
-
- time_t dep_mtime;
- char* dep_fake = strncache(s, dlen);
- char* dep_real = resolve_path(dep_fake, &dep_mtime);
- if(dep_mtime > newest) newest = dep_mtime;
-
- strlist_push(dep_list, dep_real);
-
- struncache(dep_fake);
- struncache(dep_real);
-
- s += dlen;
- }
-
- free(dep_src);
-
- if(newest_mtime) *newest_mtime = newest;
- return dep_list;
- }
- int gen_deps(char* src_path, char* dep_path, time_t src_mtime, time_t obj_mtime, objfile* obj) {
- time_t dep_mtime = 0;
- time_t newest_mtime = 0;
-
- char* real_dep_path = resolve_path(dep_path, &dep_mtime);
- if(dep_mtime < src_mtime) {
- //gcc -MM -MG -MT $1 -MF "build/$1.d" $1 $CFLAGS $LDADD
- // printf(" generating deps\n");
- char* cmd = sprintfdup("gcc -MM -MG -MT '' -MF %s %s %s", dep_path, src_path, obj->gcc_opts_flat);
- system(cmd);
- free(cmd);
- }
-
- strlist* deps = parse_gcc_dep_file(real_dep_path, &newest_mtime);
-
- // free or process deps
-
- return newest_mtime > obj_mtime;
- FAIL:
- return 0;
- }
- /* -END- gcc.c ----- */
|