build.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // gcc -lutil build.c -o hacer && ./hacer
  2. #include "build.inc.c"
  3. // link with -lutil
  4. char* sources[] = {
  5. "lexer.c",
  6. "../sti.c",
  7. "test.c",
  8. NULL,
  9. };
  10. char* ld_add[] = {
  11. "-lm",
  12. NULL,
  13. };
  14. // -ffast-math but without reciprocal approximations
  15. char* cflags[] = {
  16. "-std=gnu11",
  17. "-ggdb",
  18. "-DLINUX",
  19. "-march=native",
  20. "-mtune=native",
  21. "-fno-math-errno",
  22. "-fexcess-precision=fast",
  23. "-fno-signed-zeros",
  24. "-fno-trapping-math",
  25. "-fassociative-math",
  26. "-ffinite-math-only",
  27. "-fno-rounding-math",
  28. "-fno-signaling-nans",
  29. "-include signal.h",
  30. "-pthread",
  31. "-Wall",
  32. "-Werror",
  33. "-Wextra",
  34. "-Wno-unused-result",
  35. "-Wno-unused-variable",
  36. "-Wno-unused-but-set-variable",
  37. "-Wno-unused-function",
  38. "-Wno-unused-label",
  39. "-Wno-unused-parameter",
  40. "-Wno-pointer-sign",
  41. "-Wno-missing-braces",
  42. "-Wno-maybe-uninitialized",
  43. "-Wno-implicit-fallthrough",
  44. "-Wno-sign-compare",
  45. "-Wno-char-subscripts",
  46. "-Wno-int-conversion",
  47. "-Wno-int-to-pointer-cast",
  48. "-Wno-unknown-pragmas",
  49. "-Wno-sequence-point",
  50. "-Wno-switch",
  51. "-Wno-parentheses",
  52. "-Wno-comment",
  53. "-Wno-strict-aliasing",
  54. "-Wno-endif-labels",
  55. "-Werror=implicit-function-declaration",
  56. "-Werror=uninitialized",
  57. "-Werror=return-type",
  58. NULL,
  59. };
  60. int compile_source(char* src_path, char* obj_path) {
  61. char* cmd = sprintfdup("gcc -c -o %s %s %s", obj_path, src_path, g_gcc_opts_flat);
  62. // printf("%s\n", cmd);
  63. strlist_push(&compile_cache, cmd);
  64. return 0;
  65. }
  66. void check_source(char* raw_src_path, strlist* objs) {
  67. time_t src_mtime, obj_mtime = 0, dep_mtime = 0;
  68. char* src_path = resolve_path(raw_src_path, &src_mtime);
  69. char* src_dir = dir_name(raw_src_path);
  70. char* base = base_name(src_path);
  71. char* build_dir = path_join("build/deeper", src_dir);
  72. char* obj_path = path_join(build_dir, base);
  73. // cheap and dirty
  74. size_t olen = strlen(obj_path);
  75. obj_path[olen-1] = 'o';
  76. strlist_push(objs, obj_path);
  77. char* dep_path = strcatdup(build_dir, "/", base, ".d");
  78. mkdirp_cached(build_dir, 0755);
  79. char* real_obj_path = resolve_path(obj_path, &obj_mtime);
  80. if(obj_mtime < src_mtime) {
  81. printf(" objtime compile\n");
  82. compile_source(src_path, real_obj_path);
  83. return;
  84. }
  85. if(gen_deps(src_path, dep_path, src_mtime, obj_mtime)) {
  86. printf(" deep dep compile\n");
  87. compile_source(src_path, real_obj_path);
  88. }
  89. //gcc -c -o $2 $1 $CFLAGS $LDADD
  90. }
  91. int main(int argc, char* argv[]) {
  92. string_cache_init(2048);
  93. realname_cache_init();
  94. strlist_init(&compile_cache);
  95. hash_init(&mkdir_cache, 128);
  96. char* exe_path = "build/lexer_test";
  97. mkdirp_cached("build/deeper", 0755);
  98. g_gcc_opts_list = concat_lists(ld_add, cflags);
  99. g_gcc_opts_flat = join_str_list(g_gcc_opts_list, " ");
  100. // rglob src;
  101. //recursive_glob("src", "*.[ch]", 0, &src);
  102. strlist objs;
  103. strlist_init(&objs);
  104. for(int i = 0; sources[i]; i++) {
  105. printf("%i: checking %s\n", i, sources[i]);
  106. check_source(sources[i], &objs);
  107. }
  108. if(compile_cache_execute()) {
  109. printf("Build halted due to errors.\n");
  110. return 1;
  111. }
  112. char* objects_flat = join_str_list(objs.entries, " ");
  113. // gcc -o imcalc $objlist $CFLAGS $LDADD
  114. char* cmd = sprintfdup("gcc -o %s %s %s", exe_path, objects_flat, g_gcc_opts_flat);
  115. system(cmd);
  116. // printf("%d: %s\n", err, strerror(errno));
  117. return 0;
  118. }