_build.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // gcc -lutil build.c -o hacer && ./hacer
  2. // link with -lutil
  3. char* build_dir;
  4. char* source_dir = "./"; // "src"
  5. char* exe_path = "namegen";
  6. char* base_build_dir = "build";
  7. #include "_build.inc.c"
  8. char* sources[] = {
  9. "preprocess.c",
  10. "sti/sti.c",
  11. "tree.c",
  12. NULL,
  13. };
  14. // these are run through pkg-config
  15. char* lib_headers_needed[] = {
  16. // freetype and fontconfig are dynamically loaded only when needed
  17. // "freetype2", "fontconfig",
  18. // "gl", "glu", "glew",
  19. // "libpcre2-8",
  20. // "libpng",
  21. // "x11", "xfixes",
  22. NULL
  23. };
  24. // these are run through pkg-config
  25. char* libs_needed[] = {
  26. // "gl", "glu", "glew",
  27. // "libpcre2-8",
  28. // "libpng",
  29. // "x11", "xfixes",
  30. NULL,
  31. };
  32. char* ld_add[] = {
  33. "-lm",
  34. // "-ldl", "-lutil",
  35. NULL,
  36. };
  37. char* debug_cflags[] = {
  38. "-ggdb",
  39. "-DDEBUG",
  40. "-O0",
  41. NULL
  42. };
  43. char* profiling_cflags[] = {
  44. "-pg",
  45. NULL
  46. };
  47. char* release_cflags[] = {
  48. "-DRELEASE",
  49. "-O3",
  50. // "-Wno-array-bounds", // temporary, until some shit in sti gets fixed. only happens with -O3
  51. NULL
  52. };
  53. // -ffast-math but without reciprocal approximations
  54. char* common_cflags[] = {
  55. "-std=gnu11",
  56. "-ffunction-sections", "-fdata-sections",
  57. "-DLINUX",
  58. "-march=native",
  59. "-mtune=native",
  60. "-fno-math-errno",
  61. "-fexcess-precision=fast",
  62. "-fno-signed-zeros",
  63. "-fno-trapping-math",
  64. "-fassociative-math",
  65. "-ffinite-math-only",
  66. "-fno-rounding-math",
  67. "-fno-signaling-nans",
  68. // "-include signal.h",
  69. "-pthread",
  70. "-Wall",
  71. "-Werror",
  72. "-Wextra",
  73. "-Wno-unused-result",
  74. "-Wno-unused-variable",
  75. "-Wno-unused-but-set-variable",
  76. "-Wno-unused-function",
  77. "-Wno-unused-label",
  78. "-Wno-unused-parameter",
  79. "-Wno-pointer-sign",
  80. "-Wno-missing-braces",
  81. "-Wno-maybe-uninitialized",
  82. "-Wno-implicit-fallthrough",
  83. "-Wno-sign-compare",
  84. "-Wno-char-subscripts",
  85. "-Wno-int-conversion",
  86. "-Wno-int-to-pointer-cast",
  87. "-Wno-unknown-pragmas",
  88. "-Wno-sequence-point",
  89. "-Wno-switch",
  90. "-Wno-parentheses",
  91. "-Wno-comment",
  92. "-Wno-strict-aliasing",
  93. "-Wno-endif-labels",
  94. "-Werror=implicit-function-declaration",
  95. "-Werror=uninitialized",
  96. "-Werror=return-type",
  97. NULL,
  98. };
  99. int compile_source(char* src_path, char* obj_path, objfile* obj) {
  100. char* cmd = sprintfdup("gcc -c -o %s %s %s", obj_path, src_path, obj->gcc_opts_flat);
  101. if(obj->verbose) puts(cmd);
  102. // printf("%s\n", cmd);
  103. strlist_push(&compile_cache, cmd);
  104. // exit(1);
  105. return 0;
  106. }
  107. void check_source(char* raw_src_path, strlist* objs, objfile* o) {
  108. time_t src_mtime, obj_mtime = 0, dep_mtime = 0;
  109. char* src_path = resolve_path(raw_src_path, &src_mtime);
  110. char* src_dir = dir_name(raw_src_path);
  111. char* base = base_name(src_path);
  112. // char* build_base = "debug";
  113. char* src_build_dir = path_join(o->build_dir, src_dir);
  114. char* obj_path = path_join(src_build_dir, base);
  115. // cheap and dirty
  116. size_t olen = strlen(obj_path);
  117. obj_path[olen-1] = 'o';
  118. strlist_push(objs, obj_path);
  119. char* dep_path = strcatdup(src_build_dir, "/", base, ".d");
  120. mkdirp_cached(src_build_dir, 0755);
  121. char* real_obj_path = resolve_path(obj_path, &obj_mtime);
  122. if(obj_mtime < src_mtime) {
  123. // printf(" objtime compile\n");
  124. compile_source(src_path, real_obj_path, o);
  125. return;
  126. }
  127. if(gen_deps(src_path, dep_path, src_mtime, obj_mtime, o)) {
  128. // printf(" deep dep compile\n");
  129. compile_source(src_path, real_obj_path, o);
  130. }
  131. //gcc -c -o $2 $1 $CFLAGS $LDADD
  132. }
  133. void global_init() {
  134. string_cache_init(2048);
  135. realname_cache_init();
  136. strlist_init(&compile_cache);
  137. hash_init(&mkdir_cache, 128);
  138. g_nprocs = get_nprocs();
  139. }
  140. int main(int argc, char* argv[]) {
  141. char* cmd;
  142. global_init();
  143. // defaults
  144. objfile* obj = calloc(1, sizeof(*obj));
  145. obj->mode_debug = 2;
  146. obj->exe_path = exe_path;
  147. obj->source_dir = "./"; // "src"
  148. obj->base_build_dir = "build";
  149. obj->sources = sources;
  150. obj->debug_cflags = debug_cflags;
  151. obj->release_cflags = release_cflags;
  152. obj->profiling_cflags = profiling_cflags;
  153. obj->common_cflags = common_cflags;
  154. obj->libs_needed = libs_needed;
  155. obj->lib_headers_needed = lib_headers_needed;
  156. obj->ld_add = ld_add;
  157. parse_cli_opts(argc, argv, obj);
  158. start_obj(obj);
  159. //---------------------------
  160. //
  161. // [ custom init code here]
  162. //
  163. //---------------------------
  164. //printf("%s\n\n\n\n",g_gcc_opts_flat);
  165. // rglob src;
  166. //recursive_glob("src", "*.[ch]", 0, &src);
  167. strlist objs;
  168. strlist_init(&objs);
  169. float source_count = list_len(obj->sources);
  170. for(int i = 0; obj->sources[i]; i++) {
  171. // printf("%i: checking %s\n", i, sources[i]);
  172. char* t = path_join(obj->source_dir, obj->sources[i]);
  173. check_source(t, &objs, obj);
  174. free(t);
  175. printf("\rChecking dependencies... %s", printpct((i * 100) / source_count));
  176. }
  177. printf("\rChecking dependencies... \e[32mDONE\e[0m\n");
  178. fflush(stdout);
  179. if(compile_cache_execute()) {
  180. printf("\e[1;31mBuild failed.\e[0m\n");
  181. return 1;
  182. }
  183. char* objects_flat = join_str_list(objs.entries, " ");
  184. cmd = sprintfdup("ar rcs %s/tmp.a %s", obj->build_dir, objects_flat);
  185. if(obj->verbose) puts(cmd);
  186. printf("Creating archive... "); fflush(stdout);
  187. if(system(cmd)) {
  188. printf(" \e[1;31mFAIL\e[0m\n");
  189. return 1;
  190. }
  191. else {
  192. printf(" \e[32mDONE\e[0m\n");
  193. }
  194. cmd = sprintfdup("gcc -Wl,--gc-sections %s %s/tmp.a -o %s %s %s",
  195. obj->mode_profiling ? "-pg" : "", obj->build_dir, obj->exe_path, obj->gcc_libs, obj->gcc_opts_flat
  196. );
  197. if(obj->verbose) puts(cmd);
  198. printf("Linking executable... "); fflush(stdout);
  199. if(system(cmd)) {
  200. printf(" \e[1;31mFAIL\e[0m\n");
  201. return 1;
  202. }
  203. else {
  204. printf(" \e[32mDONE\e[0m\n");
  205. }
  206. if(!obj->verbose) {
  207. // erase the build output if it succeeded
  208. printf("\e[F\e[K");
  209. printf("\e[F\e[K");
  210. printf("\e[F\e[K");
  211. printf("\e[F\e[K");
  212. }
  213. printf("\e[32mBuild successful:\e[0m %s\n\n", obj->exe_path);
  214. return 0;
  215. }