_build.c 7.3 KB

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