_build.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // gcc -lutil build.c -o hacer && ./hacer
  2. #include "_build.inc.c"
  3. // link with -lutil
  4. char* build_dir;
  5. char* sources[] = { "main.c",
  6. "app.c",
  7. "buffer.c",
  8. "buffer_drawing.c",
  9. "buffer_range.c",
  10. "buffer_raw.c",
  11. "buffer_settings.c",
  12. "buffer_undo.c",
  13. "bufferEditor.c",
  14. "bufferEditControl.c",
  15. "bufferLine.c",
  16. "c3dlas/c3dlas.c",
  17. "c3dlas/meshgen.c",
  18. "c_json/json.c",
  19. // "calcControl.c",
  20. "clipboard.c",
  21. "dumpImage.c",
  22. "fbo.c",
  23. "fcfg.c",
  24. "fileBrowser.c",
  25. "font.c",
  26. "fuzzyMatch.c",
  27. "fuzzyMatchControl.c",
  28. "grepOpenControl.c",
  29. "hexedit.c",
  30. "highlight.c",
  31. "input.c",
  32. "json_gl.c",
  33. "log.c",
  34. "mainControl.c",
  35. "mdi.c",
  36. "msg.c",
  37. "pass.c",
  38. "pcBuffer.c",
  39. "qsort_r.c",
  40. "settings.c",
  41. "shader.c",
  42. "statusBar.c",
  43. "sti/sti.c",
  44. "texture.c",
  45. "textureAtlas.c",
  46. "ui/buttons.c",
  47. "ui/commands.c",
  48. "ui/edits.c",
  49. "ui/gui.c",
  50. "ui/gui_settings.c",
  51. "ui/guiManager.c",
  52. "ui/imgui.c",
  53. "ui/opts_structs.c",
  54. "ui/scrollbars.c",
  55. // "ui/selects.c",
  56. // "ui/sliders.c",
  57. "ui/text.c",
  58. "units.c",
  59. "utilities.c",
  60. "window.c",
  61. NULL,
  62. };
  63. // these are run through pkg-config
  64. char* lib_headers_needed[] = {
  65. // freetype and fontconfig are dynamically loaded only when needed
  66. "freetype2", "fontconfig",
  67. "gl", "glu", "glew",
  68. "libpcre2-8",
  69. "libpng",
  70. "x11", "xfixes", "xrender",
  71. NULL
  72. };
  73. // these are run through pkg-config
  74. char* libs_needed[] = {
  75. "gl", "glu", "glew",
  76. "libpcre2-8",
  77. "libpng",
  78. "x11", "xfixes", "xrender",
  79. NULL,
  80. };
  81. char* ld_add[] = {
  82. "-lm", "-ldl", "-lutil",
  83. NULL,
  84. };
  85. char* debug_cflags[] = {
  86. "-ggdb",
  87. "-DDEBUG",
  88. "-O0",
  89. NULL
  90. };
  91. char* profiling_cflags[] = {
  92. "-pg",
  93. NULL
  94. };
  95. char* release_cflags[] = {
  96. "-DRELEASE",
  97. "-O3",
  98. "-Wno-array-bounds", // temporary, until some shit in sti gets fixed. only happens with -O3
  99. NULL
  100. };
  101. // -ffast-math but without reciprocal approximations
  102. char* cflags[] = {
  103. "-std=gnu11",
  104. "-ffunction-sections", "-fdata-sections",
  105. "-DLINUX",
  106. "-D_GNU_SOURCE",
  107. "-march=native",
  108. "-mtune=native",
  109. "-DSTI_C3DLAS_NO_CONFLICT",
  110. "-fno-math-errno",
  111. "-fexcess-precision=fast",
  112. "-fno-signed-zeros",
  113. "-fno-trapping-math",
  114. "-fassociative-math",
  115. "-ffinite-math-only",
  116. "-fno-rounding-math",
  117. "-fno-signaling-nans",
  118. "-include signal.h",
  119. "-include ./src/global.h",
  120. "-pthread",
  121. "-Wall",
  122. "-Werror",
  123. "-Wextra",
  124. "-Wno-unused-result",
  125. "-Wno-unused-variable",
  126. "-Wno-unused-but-set-variable",
  127. "-Wno-unused-function",
  128. "-Wno-unused-label",
  129. "-Wno-unused-parameter",
  130. "-Wno-pointer-sign",
  131. "-Wno-missing-braces",
  132. "-Wno-maybe-uninitialized",
  133. "-Wno-implicit-fallthrough",
  134. "-Wno-sign-compare",
  135. "-Wno-char-subscripts",
  136. "-Wno-int-conversion",
  137. "-Wno-int-to-pointer-cast",
  138. "-Wno-unknown-pragmas",
  139. "-Wno-sequence-point",
  140. "-Wno-switch",
  141. "-Wno-parentheses",
  142. "-Wno-comment",
  143. "-Wno-strict-aliasing",
  144. "-Wno-endif-labels",
  145. "-Werror=implicit-function-declaration",
  146. "-Werror=uninitialized",
  147. "-Werror=return-type",
  148. NULL,
  149. };
  150. void gen_optsstructs() {
  151. time_t exe_mtime;
  152. time_t cfg_mtime;
  153. time_t gen1_mtime;
  154. time_t gen2_mtime;
  155. char* exefile = "tools/settings/gen_opts_structs";
  156. char* cfgfile = "src/ui/opts_structs.sex";
  157. char* genfile1 = "src/ui/opts_structs.h";
  158. char* genfile2 = "src/ui/opts_structs.c";
  159. resolve_path(exefile, &exe_mtime);
  160. resolve_path(cfgfile, &cfg_mtime);
  161. resolve_path(genfile1, &gen1_mtime);
  162. resolve_path(genfile2, &gen2_mtime);
  163. if(cfg_mtime > gen1_mtime || cfg_mtime > gen2_mtime || !exe_mtime) {
  164. printf("Regenerating src/ui/opts_structs.[ch]\n");
  165. system("tools/settings/build.sh");
  166. system("tools/settings/gen_opts_structs src/ui/opts_structs.sex src/ui/opts_structs.h src/ui/opts_structs.c");
  167. }
  168. }
  169. int compile_source(char* src_path, char* obj_path) {
  170. char* cmd = sprintfdup("gcc -c -o %s %s %s", obj_path, src_path, g_gcc_opts_flat);
  171. // printf("%s\n", cmd);
  172. strlist_push(&compile_cache, cmd);
  173. // exit(1);
  174. return 0;
  175. }
  176. void check_source(char* raw_src_path, strlist* objs) {
  177. time_t src_mtime, obj_mtime = 0, dep_mtime = 0;
  178. char* src_path = resolve_path(raw_src_path, &src_mtime);
  179. char* src_dir = dir_name(raw_src_path);
  180. char* base = base_name(src_path);
  181. // char* build_base = "debug";
  182. char* src_build_dir = path_join(build_dir, src_dir);
  183. char* obj_path = path_join(src_build_dir, base);
  184. // cheap and dirty
  185. size_t olen = strlen(obj_path);
  186. obj_path[olen-1] = 'o';
  187. strlist_push(objs, obj_path);
  188. char* dep_path = strcatdup(src_build_dir, "/", base, ".d");
  189. mkdirp_cached(src_build_dir, 0755);
  190. char* real_obj_path = resolve_path(obj_path, &obj_mtime);
  191. if(obj_mtime < src_mtime) {
  192. // printf(" objtime compile\n");
  193. compile_source(src_path, real_obj_path);
  194. return;
  195. }
  196. if(gen_deps(src_path, dep_path, src_mtime, obj_mtime)) {
  197. // printf(" deep dep compile\n");
  198. compile_source(src_path, real_obj_path);
  199. }
  200. //gcc -c -o $2 $1 $CFLAGS $LDADD
  201. }
  202. struct {
  203. int debug;
  204. int profiling;
  205. int release;
  206. int clean;
  207. } g_options;
  208. int main(int argc, char* argv[]) {
  209. string_cache_init(2048);
  210. realname_cache_init();
  211. strlist_init(&compile_cache);
  212. hash_init(&mkdir_cache, 128);
  213. g_nprocs = get_nprocs();
  214. // defaults
  215. g_options.debug = 2;
  216. char* exe_path = "gpuedit";
  217. char* base_build_dir = "build";
  218. gen_optsstructs();
  219. char* tmp;
  220. int mode = 0;
  221. for(int a = 1; a < argc; a++) {
  222. if(argv[a][0] == '-') {
  223. for(int i = 0; argv[a][i]; i++) {
  224. switch(argv[a][i]) {
  225. case 'd': // debug: -ggdb
  226. g_options.debug = 1;
  227. if(g_options.release == 1) {
  228. fprintf(stderr, "Debug and Release set at the same time.\n");
  229. }
  230. break;
  231. case 'p': // profiling: -pg
  232. g_options.profiling = 1;
  233. break;
  234. case 'r': // release: -O3
  235. g_options.release = 1;
  236. g_options.debug = 0;
  237. break;
  238. case 'c': // clean
  239. g_options.clean = 1;
  240. break;
  241. }
  242. }
  243. }
  244. }
  245. // delete the old executable
  246. unlink(exe_path);
  247. char build_subdir[20] = {0};
  248. if(g_options.debug) strcat(build_subdir, "d");
  249. if(g_options.profiling) strcat(build_subdir, "p");
  250. if(g_options.release) strcat(build_subdir, "r");
  251. build_dir = path_join(base_build_dir, build_subdir);
  252. // delete old build files if needed
  253. if(g_options.clean) {
  254. printf("Cleaning directory %s/\n", build_dir);
  255. system(sprintfdup("rm -rf %s/*", build_dir));
  256. }
  257. mkdirp_cached(build_dir, 0755);
  258. // create the test files
  259. system("cp src/buffer.h ./testfile.h");
  260. system("cp src/buffer.c ./testfile.c");
  261. g_gcc_opts_list = concat_lists(ld_add, cflags);
  262. if(g_options.debug) g_gcc_opts_list = concat_lists(g_gcc_opts_list, debug_cflags);
  263. if(g_options.profiling) g_gcc_opts_list = concat_lists(g_gcc_opts_list, profiling_cflags);
  264. if(g_options.release) g_gcc_opts_list = concat_lists(g_gcc_opts_list, release_cflags);
  265. g_gcc_opts_flat = join_str_list(g_gcc_opts_list, " ");
  266. g_gcc_include = pkg_config(lib_headers_needed, "I");
  267. g_gcc_libs = pkg_config(libs_needed, "L");
  268. tmp = g_gcc_opts_flat;
  269. g_gcc_opts_flat = strjoin(" ", g_gcc_opts_flat, g_gcc_include);
  270. free(tmp);
  271. //printf("%s\n\n\n\n",g_gcc_opts_flat);
  272. // rglob src;
  273. //recursive_glob("src", "*.[ch]", 0, &src);
  274. strlist objs;
  275. strlist_init(&objs);
  276. float source_count = list_len(sources);
  277. for(int i = 0; sources[i]; i++) {
  278. // printf("%i: checking %s\n", i, sources[i]);
  279. char* t = path_join("src", sources[i]);
  280. check_source(t, &objs);
  281. free(t);
  282. printf("\rChecking dependencies... %s", printpct((i * 100) / source_count));
  283. }
  284. printf("\rChecking dependencies... \e[32mDONE\e[0m\n");
  285. fflush(stdout);
  286. if(compile_cache_execute()) {
  287. printf("\e[1;31mBuild failed.\e[0m\n");
  288. return 1;
  289. }
  290. char* objects_flat = join_str_list(objs.entries, " ");
  291. printf("Creating archive... "); fflush(stdout);
  292. if(system(sprintfdup("ar rcs %s/tmp.a %s", build_dir, objects_flat))) {
  293. printf(" \e[1;31mFAIL\e[0m\n");
  294. return 1;
  295. }
  296. else {
  297. printf(" \e[32mDONE\e[0m\n");
  298. }
  299. printf("Linking executable... "); fflush(stdout);
  300. char* cmd = sprintfdup("gcc -Wl,--gc-sections %s %s/tmp.a -o %s %s %s", g_options.profiling ? "-pg" : "", build_dir, exe_path, g_gcc_libs, g_gcc_opts_flat);
  301. if(system(cmd)) {
  302. printf(" \e[1;31mFAIL\e[0m\n");
  303. return 1;
  304. }
  305. else {
  306. printf(" \e[32mDONE\e[0m\n");
  307. }
  308. // erase the build output if it succeeded
  309. printf("\e[F\e[K");
  310. printf("\e[F\e[K");
  311. printf("\e[F\e[K");
  312. printf("\e[F\e[K");
  313. printf("\e[32mBuild successful:\e[0m %s\n\n", exe_path);
  314. return 0;
  315. }