_build.c 5.6 KB

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