init.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. void parse_cli_opts(int argc, char** argv, objfile* obj) {
  2. for(int a = 1; a < argc; a++) {
  3. if(argv[a][0] == '-') {
  4. for(int i = 0; argv[a][i]; i++) {
  5. switch(argv[a][i]) {
  6. case 'd': // debug: -ggdb
  7. obj->mode_debug = 1;
  8. if(obj->mode_release == 1) {
  9. fprintf(stderr, "Debug and Release set at the same time.\n");
  10. }
  11. break;
  12. case 'p': // profiling: -pg
  13. obj->mode_profiling = 1;
  14. break;
  15. case 'r': // release: -O3
  16. obj->mode_release = 1;
  17. obj->mode_debug = 0;
  18. break;
  19. case 'c': // clean
  20. obj->clean_first = 1;
  21. break;
  22. case 'v': // verbose
  23. obj->verbose = 1;
  24. break;
  25. }
  26. }
  27. }
  28. }
  29. }
  30. char* default_compile_source(char* src_path, char* obj_path, objfile* obj);
  31. void start_obj(objfile* obj) {
  32. obj->build_subdir = calloc(1, 20);
  33. if(obj->mode_debug) strcat(obj->build_subdir, "d");
  34. if(obj->mode_profiling) strcat(obj->build_subdir, "p");
  35. if(obj->mode_release) strcat(obj->build_subdir, "r");
  36. obj->build_dir = path_join(obj->base_build_dir, obj->build_subdir);
  37. if(!obj->archive_path) {
  38. obj->archive_path = path_join(obj->build_dir, "tmp.a");
  39. }
  40. // clean up old executables
  41. unlink(obj->exe_path);
  42. unlink(obj->archive_path);
  43. // delete old build files if needed
  44. if(obj->clean_first) {
  45. printf("Cleaning directory %s/\n", obj->build_dir);
  46. system(sprintfdup("rm -rf %s/*", obj->build_dir));
  47. }
  48. // ensure the build dir exists
  49. mkdirp_cached(obj->build_dir, 0755);
  50. // flatten all the gcc options
  51. obj->gcc_opts_list = concat_lists(obj->ld_add, obj->common_cflags);
  52. if(obj->mode_debug) obj->gcc_opts_list = concat_lists(obj->gcc_opts_list, obj->debug_cflags);
  53. if(obj->mode_profiling) obj->gcc_opts_list = concat_lists(obj->gcc_opts_list, obj->profiling_cflags);
  54. if(obj->mode_release) obj->gcc_opts_list = concat_lists(obj->gcc_opts_list, obj->release_cflags);
  55. obj->gcc_opts_flat = join_str_list(obj->gcc_opts_list, " ");
  56. obj->gcc_include = pkg_config(obj->lib_headers_needed, "I");
  57. obj->gcc_libs = pkg_config(obj->libs_needed, "L");
  58. char* tmp = obj->gcc_opts_flat;
  59. obj->gcc_opts_flat = strjoin(" ", obj->gcc_opts_flat, obj->gcc_include);
  60. free(tmp);
  61. // initialze the object file list
  62. strlist_init(&obj->objs);
  63. strlist_init(&obj->compile_cache);
  64. if(!obj->compile_source_cmd) obj->compile_source_cmd = default_compile_source;
  65. }