_build.c 4.5 KB

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