_build.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. "cproc.c",
  11. "../src/git.c",
  12. "../src/sys.c",
  13. "main.c",
  14. "../src/sti/sti.c",
  15. "../src/strlist.c",
  16. NULL,
  17. };
  18. // these are run through pkg-config
  19. char* lib_headers_needed[] = {
  20. // "openssl",
  21. NULL
  22. };
  23. // these are run through pkg-config
  24. char* libs_needed[] = {
  25. // "openssl",
  26. NULL,
  27. };
  28. char* ld_add[] = {
  29. "-lm",
  30. "-lutil",
  31. NULL,
  32. };
  33. char* debug_cflags[] = {
  34. "-ggdb",
  35. "-DDEBUG",
  36. "-O0",
  37. NULL
  38. };
  39. char* profiling_cflags[] = {
  40. "-pg",
  41. NULL
  42. };
  43. char* release_cflags[] = {
  44. "-DRELEASE",
  45. "-O3",
  46. // "-Wno-array-bounds", // temporary, until some shit in sti gets fixed. only happens with -O3
  47. NULL
  48. };
  49. // -ffast-math but without reciprocal approximations
  50. char* common_cflags[] = {
  51. "-std=gnu11",
  52. "-ffunction-sections", "-fdata-sections",
  53. "-DLINUX",
  54. "-D_GNU_SOURCE",
  55. "-march=native",
  56. "-mtune=native",
  57. // "-include src/memtricks.h",
  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. /*
  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. /*
  107. void check_source(char* raw_src_path, strlist* objs, objfile* o) {
  108. time_t src_mtime, obj_mtime = 0, dep_mtime = 0;
  109. char* src_path = resolve_path(raw_src_path, &src_mtime);
  110. char* src_dir = dir_name(raw_src_path);
  111. char* base = base_name(src_path);
  112. // char* build_base = "debug";
  113. char* src_build_dir = path_join(o->build_dir, src_dir);
  114. char* obj_path = path_join(src_build_dir, base);
  115. // cheap and dirty
  116. size_t olen = strlen(obj_path);
  117. obj_path[olen-1] = 'o';
  118. strlist_push(objs, obj_path);
  119. char* dep_path = strcatdup(src_build_dir, "/", base, ".d");
  120. mkdirp_cached(src_build_dir, 0755);
  121. char* real_obj_path = resolve_path(obj_path, &obj_mtime);
  122. if(obj_mtime < src_mtime) {
  123. // printf(" objtime compile\n");
  124. compile_source(src_path, real_obj_path, o);
  125. return;
  126. }
  127. if(gen_deps(src_path, dep_path, src_mtime, obj_mtime, o)) {
  128. // printf(" deep dep compile\n");
  129. compile_source(src_path, real_obj_path, o);
  130. }
  131. //gcc -c -o $2 $1 $CFLAGS $LDADD
  132. }
  133. */
  134. void global_init() {
  135. string_cache_init(2048);
  136. realname_cache_init();
  137. // strlist_init(&compile_cache);
  138. hash_init(&mkdir_cache, 128);
  139. g_nprocs = get_nprocs();
  140. }
  141. int main(int argc, char* argv[]) {
  142. char* cmd;
  143. global_init();
  144. // defaults
  145. objfile* obj = calloc(1, sizeof(*obj));
  146. // obj->verbose = 1;
  147. obj->mode_debug = 2;
  148. obj->exe_path = "git_shell";
  149. obj->source_dir = "./"; // "src"
  150. obj->base_build_dir = "build";
  151. obj->sources = sources;
  152. obj->debug_cflags = debug_cflags;
  153. obj->release_cflags = release_cflags;
  154. obj->profiling_cflags = profiling_cflags;
  155. obj->common_cflags = common_cflags;
  156. obj->libs_needed = libs_needed;
  157. obj->lib_headers_needed = lib_headers_needed;
  158. obj->ld_add = ld_add;
  159. parse_cli_opts(argc, argv, obj);
  160. start_obj(obj);
  161. //---------------------------
  162. //
  163. // [ custom init code here]
  164. //
  165. //---------------------------
  166. //printf("%s\n\n\n\n",g_gcc_opts_flat);
  167. // rglob src;
  168. //recursive_glob("src", "*.[ch]", 0, &src);
  169. strlist objs;
  170. strlist_init(&objs);
  171. float source_count = list_len(obj->sources);
  172. for(int i = 0; obj->sources[i]; i++) {
  173. char* t = path_join(obj->source_dir, obj->sources[i]);
  174. check_source(t, obj);
  175. free(t);
  176. printf("\rChecking dependencies... %s", printpct((i * 100) / source_count));
  177. }
  178. printf("\rChecking dependencies... \e[32mDONE\e[0m\n");
  179. fflush(stdout);
  180. if(compile_cache_execute(obj)) {
  181. printf("\e[1;31mBuild failed.\e[0m\n");
  182. return 1;
  183. }
  184. char* objects_flat = join_str_list(obj->objs.entries, " ");
  185. cmd = sprintfdup("ar rcs %s/tmp.a %s", obj->build_dir, objects_flat);
  186. if(obj->verbose) puts(cmd);
  187. printf("Creating archive... "); fflush(stdout);
  188. if(system(cmd)) {
  189. printf(" \e[1;31mFAIL\e[0m\n");
  190. return 1;
  191. }
  192. else {
  193. printf(" \e[32mDONE\e[0m\n");
  194. }
  195. cmd = sprintfdup("gcc -Wl,--gc-sections %s %s/tmp.a -o %s %s %s",
  196. obj->mode_profiling ? "-pg" : "", obj->build_dir, obj->exe_path, obj->gcc_libs, obj->gcc_opts_flat
  197. );
  198. if(obj->verbose) puts(cmd);
  199. printf("Linking executable... "); fflush(stdout);
  200. if(system(cmd)) {
  201. printf(" \e[1;31mFAIL\e[0m\n");
  202. return 1;
  203. }
  204. else {
  205. printf(" \e[32mDONE\e[0m\n");
  206. }
  207. if(!obj->verbose) {
  208. // erase the build output if it succeeded
  209. printf("\e[F\e[K");
  210. printf("\e[F\e[K");
  211. printf("\e[F\e[K");
  212. printf("\e[F\e[K");
  213. }
  214. printf("\e[32mBuild successful:\e[0m %s\n\n", obj->exe_path);
  215. return 0;
  216. }