_build.c 7.4 KB

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