_build.c 5.8 KB

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