_build.c 4.5 KB

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