_build.c 4.3 KB

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