_build.c 5.6 KB

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