_build.c 5.4 KB

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