diff options
author | Slendi <slendi@socopon.com> | 2024-01-13 20:11:17 +0200 |
---|---|---|
committer | Slendi <slendi@socopon.com> | 2024-01-13 20:11:17 +0200 |
commit | d96541da73c46fce85cacf371e76ac3871b35836 (patch) | |
tree | 924161adf104752b2cd68476c81351a30bf830d2 | |
parent | 7826c06f61682fa3dbd02e934803bacc67e87fba (diff) |
Add a way to change the compiler
Signed-off-by: Slendi <slendi@socopon.com>
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | cbuild.h | 3 | ||||
-rw-r--r-- | cbuild_impl.c | 33 | ||||
-rwxr-xr-x | test_project/build | bin | 0 -> 37136 bytes | |||
-rwxr-xr-x | test_project/build.c | 7 | ||||
-rw-r--r-- | test_project/src/coompiler.c | 11 | ||||
-rw-r--r-- | test_project/src/main.c | 3 |
7 files changed, 42 insertions, 17 deletions
@@ -1,3 +1,3 @@ test_project/hello -test_project/src/main.o +test_project/src/*.o @@ -43,6 +43,9 @@ typedef struct project { char *url; language_t language; + char *compiler; + char *linker; + target_header_t **targets; size_t target_count; } project_t; diff --git a/cbuild_impl.c b/cbuild_impl.c index be8385e..fdf7947 100644 --- a/cbuild_impl.c +++ b/cbuild_impl.c @@ -52,10 +52,11 @@ executable_t create_executable(char *name, char *sources) { .header = header, }; for (size_t off = 0; exe.header.sources[off]; - off += strlen(exe.header.sources) + 1) { + off += strlen(exe.header.sources + off) + 1) { char *path = exe.header.sources + off; + printf("Checking %s\n", path); if (!file_exists(path, NULL)) { - fprintf(stderr, "File %s does not exist\n", path); + fprintf(stderr, "File `%s` does not exist\n", path); exit(1); } } @@ -70,6 +71,10 @@ project_t create_project_ex(char *name, char *version, char *description, char *author, char *license, char *url, language_t language) { project_t project = { + .compiler = language == LANG_C ? (getenv("CC") ? getenv("CC") : "cc") + : (getenv("CXX") ? getenv("CXX") : "c++"), + .linker = + getenv("LD") ? getenv("LD") : (language == LANG_C ? "cc" : "c++"), .name = name, .version = version, .description = description, @@ -114,7 +119,7 @@ void build(project_t build) { executable_t *exe = (executable_t *)target; count_operations++; // Linking for (size_t off = 0; exe->header.sources[off]; - off += strlen(exe->header.sources) + 1) { + off += strlen(exe->header.sources + off) + 1) { sources_count_real++; time_t mtime_source; if (!file_exists(exe->header.sources + off, &mtime_source)) { @@ -124,8 +129,8 @@ void build(project_t build) { } char *dot_o = change_ext_to_dot_o(exe->header.sources + off); time_t mtime_dot_o = 0; - if (!file_exists(dot_o, &mtime_dot_o)) { - } + if (!file_exists(dot_o, &mtime_dot_o)) + mtime_dot_o = 0; free(dot_o); if (mtime_source > mtime_dot_o) { sources_count++; @@ -163,11 +168,9 @@ void build(project_t build) { if (target->type == EXECUTABLE) { executable_t *exe = (executable_t *)target; for (size_t off = 0; exe->header.sources[off]; - off += strlen(exe->header.sources) + 1) { + off += strlen(exe->header.sources + off) + 1) { char *source = exe->header.sources + off; char *dot_o = change_ext_to_dot_o(source); - fprintf(stderr, "\33[2K\r(%ld/%ld) Building \33[1m%s\33[0m", count++, - count_operations, source); time_t mtime_source; if (!file_exists(source, &mtime_source)) { @@ -175,13 +178,15 @@ void build(project_t build) { exit(1); } time_t mtime_dot_o = 0; - if (!file_exists(dot_o, &mtime_dot_o)) { - } + if (!file_exists(dot_o, &mtime_dot_o)) + mtime_dot_o = 0; if (mtime_source <= mtime_dot_o) { free(dot_o); continue; } + fprintf(stderr, "\33[2K\r(%ld/%ld) Building \33[1m%s\33[0m", count++, + count_operations, source); if (processes_alive == processing_units) { int status; waitpid(-1, &status, 0); @@ -209,7 +214,7 @@ void build(project_t build) { } else { pid_t pid = fork(); if (pid == 0) { - char *argv[] = {"gcc", "-c", source, "-o", dot_o, NULL}; + char *argv[] = {build.compiler, "-c", source, "-o", dot_o, NULL}; execvp(argv[0], argv); exit(0); } else if (pid == -1) { @@ -260,13 +265,13 @@ void build(project_t build) { fprintf(stderr, "\33[2K\r(%ld/%ld) Linking \33[1m%s\33[0m", count++, count_operations, exe->header.name); char *argv[4 + sources_count]; - argv[0] = "gcc"; + argv[0] = build.linker; argv[1] = "-o"; argv[2] = exe->header.name; argv[3] = NULL; size_t argv_off = 3; for (size_t off = 0; exe->header.sources[off]; - off += strlen(exe->header.sources) + 1) { + off += strlen(exe->header.sources + off) + 1) { char *source = exe->header.sources + off; char *dot_o = change_ext_to_dot_o(source); argv[argv_off++] = dot_o; @@ -353,7 +358,7 @@ void clean(project_t build) { if (target->type == EXECUTABLE) { executable_t *exe = (executable_t *)target; for (size_t off = 0; exe->header.sources[off]; - off += strlen(exe->header.sources) + 1) { + off += strlen(exe->header.sources + off) + 1) { char *source = exe->header.sources + off; char *dot_o = change_ext_to_dot_o(source); fprintf(stderr, "\33[2K\rCleaning \33[1m%s\33[0m", dot_o); diff --git a/test_project/build b/test_project/build Binary files differnew file mode 100755 index 0000000..0c7d54d --- /dev/null +++ b/test_project/build diff --git a/test_project/build.c b/test_project/build.c index b85b016..af1de08 100755 --- a/test_project/build.c +++ b/test_project/build.c @@ -1,5 +1,7 @@ #if 0 -tcc -run build.c $@ +gcc -O0 -ggdb -o build build.c +./build $@ +#tcc - run build.c $ @ exit #endif @@ -17,7 +19,8 @@ int main(int argc, char **argv) { for (int offset = 0; sources[offset] != '\0'; offset += strlen(sources) + 1) { fprintf(stderr, " - %s\n", sources + offset); } - executable_t exe = create_executable("hello", sources); + executable_t exe = + create_executable("hello", "src/coompiler.c\0src/main.c\0\0"); add_target(&proj, &exe); if (argc > 1) { if (strcmp(argv[1], "clean") == 0) { diff --git a/test_project/src/coompiler.c b/test_project/src/coompiler.c new file mode 100644 index 0000000..c1e3b0b --- /dev/null +++ b/test_project/src/coompiler.c @@ -0,0 +1,11 @@ +char const *compiler(void) { +#ifdef __TCC__ + return "TCC compiler"; +#elif __GNUC__ + return "GCC compiler"; +#elif __CLANG__ + return "CLANG compiler"; +#elif __MSVC__ + return "MSVC compiler"; +#endif +} diff --git a/test_project/src/main.c b/test_project/src/main.c index 420305a..9d0f0b4 100644 --- a/test_project/src/main.c +++ b/test_project/src/main.c @@ -1,6 +1,9 @@ #include <stdio.h> +char const *compiler(void); + int main(void) { puts("Hello, world!"); + puts(compiler()); return 0; } |