_build.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // gcc -lutil build.c -o hacer && ./hacer
  2. #include "_build.inc.c"
  3. // link with -lutil
  4. char* sources[] = {
  5. "main.c",
  6. // "sti/sti.c",
  7. NULL,
  8. };
  9. // these are run through pkg-config
  10. char* libs_needed[] = {
  11. NULL,
  12. };
  13. char* ld_add[] = {
  14. "-lm",
  15. NULL,
  16. };
  17. // -ffast-math but without reciprocal approximations
  18. char* cflags[] = {
  19. "-std=gnu11",
  20. "-ggdb",
  21. "-ffunction-sections", "-fdata-sections",
  22. "-DLINUX",
  23. "-march=native",
  24. "-mtune=native",
  25. "-DSTI_C3DLAS_NO_CONFLICT",
  26. "-fno-math-errno",
  27. "-fexcess-precision=fast",
  28. "-fno-signed-zeros",
  29. "-fno-trapping-math",
  30. "-fassociative-math",
  31. "-ffinite-math-only",
  32. "-fno-rounding-math",
  33. "-fno-signaling-nans",
  34. "-include signal.h",
  35. "-pthread",
  36. "-Wall",
  37. "-Werror",
  38. "-Wextra",
  39. "-Wno-unused-result",
  40. "-Wno-unused-variable",
  41. "-Wno-unused-but-set-variable",
  42. "-Wno-unused-function",
  43. "-Wno-unused-label",
  44. "-Wno-unused-parameter",
  45. "-Wno-pointer-sign",
  46. "-Wno-missing-braces",
  47. "-Wno-maybe-uninitialized",
  48. "-Wno-implicit-fallthrough",
  49. "-Wno-sign-compare",
  50. "-Wno-char-subscripts",
  51. "-Wno-int-conversion",
  52. "-Wno-int-to-pointer-cast",
  53. "-Wno-unknown-pragmas",
  54. "-Wno-sequence-point",
  55. "-Wno-switch",
  56. "-Wno-parentheses",
  57. "-Wno-comment",
  58. "-Wno-strict-aliasing",
  59. "-Wno-endif-labels",
  60. "-Werror=implicit-function-declaration",
  61. "-Werror=uninitialized",
  62. "-Werror=return-type",
  63. NULL,
  64. };
  65. int compile_source(char* src_path, char* obj_path) {
  66. char* cmd = sprintfdup("gcc -c -o %s %s %s", obj_path, src_path, g_gcc_opts_flat);
  67. // printf("%s\n", cmd);
  68. strlist_push(&compile_cache, cmd);
  69. // exit(1);
  70. return 0;
  71. }
  72. void check_source(char* raw_src_path, strlist* objs) {
  73. time_t src_mtime, obj_mtime = 0, dep_mtime = 0;
  74. char* src_path = resolve_path(raw_src_path, &src_mtime);
  75. char* src_dir = dir_name(raw_src_path);
  76. char* base = base_name(src_path);
  77. char* build_dir = path_join("build", src_dir);
  78. char* obj_path = path_join(build_dir, base);
  79. // cheap and dirty
  80. size_t olen = strlen(obj_path);
  81. obj_path[olen-1] = 'o';
  82. strlist_push(objs, obj_path);
  83. char* dep_path = strcatdup(build_dir, "/", base, ".d");
  84. mkdirp_cached(build_dir, 0755);
  85. char* real_obj_path = resolve_path(obj_path, &obj_mtime);
  86. if(obj_mtime < src_mtime) {
  87. // printf(" objtime compile\n");
  88. compile_source(src_path, real_obj_path);
  89. return;
  90. }
  91. if(gen_deps(src_path, dep_path, src_mtime, obj_mtime)) {
  92. // printf(" deep dep compile\n");
  93. compile_source(src_path, real_obj_path);
  94. }
  95. //gcc -c -o $2 $1 $CFLAGS $LDADD
  96. }
  97. int main(int argc, char* argv[]) {
  98. string_cache_init(2048);
  99. realname_cache_init();
  100. strlist_init(&compile_cache);
  101. hash_init(&mkdir_cache, 128);
  102. g_nprocs = get_nprocs();
  103. char* tmp;
  104. char* exe_path = "switcher";
  105. unlink(exe_path);
  106. mkdirp_cached("build", 0755);
  107. g_gcc_opts_list = concat_lists(ld_add, cflags);
  108. g_gcc_opts_flat = join_str_list(g_gcc_opts_list, " ");
  109. g_gcc_include = pkg_config(libs_needed, "I");
  110. g_gcc_libs = pkg_config(libs_needed, "L");
  111. tmp = g_gcc_opts_flat;
  112. g_gcc_opts_flat = strjoin(" ", g_gcc_opts_flat, g_gcc_include);
  113. free(tmp);
  114. // rglob src;
  115. //recursive_glob("src", "*.[ch]", 0, &src);
  116. strlist objs;
  117. strlist_init(&objs);
  118. float source_count = list_len(sources);
  119. for(int i = 0; sources[i]; i++) {
  120. // printf("%i: checking %s\n", i, sources[i]);
  121. check_source(sources[i], &objs);
  122. printf("\rChecking dependencies... %s", printpct((i * 100) / source_count));
  123. }
  124. printf("\rChecking dependencies... \e[32mDONE\e[0m\n");
  125. fflush(stdout);
  126. if(compile_cache_execute()) {
  127. printf("\e[1;31mBuild failed.\e[0m\n");
  128. return 1;
  129. }
  130. char* objects_flat = join_str_list(objs.entries, " ");
  131. printf("Creating archive... "); fflush(stdout);
  132. if(system(sprintfdup("ar rcs build/tmp.a %s", objects_flat))) {
  133. printf(" \e[1;31mFAIL\e[0m\n");
  134. return 1;
  135. }
  136. else {
  137. printf(" \e[32mDONE\e[0m\n");
  138. }
  139. printf("Linking executable... "); fflush(stdout);
  140. char* cmd = sprintfdup("gcc -Wl,--gc-sections build/tmp.a -o %s %s %s", exe_path, g_gcc_libs, g_gcc_opts_flat);
  141. if(system(cmd)) {
  142. printf(" \e[1;31mFAIL\e[0m\n");
  143. return 1;
  144. }
  145. else {
  146. printf(" \e[32mDONE\e[0m\n");
  147. }
  148. // erase the build output if it succeeded
  149. printf("\e[F\e[K");
  150. printf("\e[F\e[K");
  151. printf("\e[F\e[K");
  152. printf("\e[F\e[K");
  153. printf("\e[32mBuild successful:\e[0m %s\n\n", exe_path);
  154. return 0;
  155. }