diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index bc0263649..d0822a910 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -385,16 +385,11 @@ void compiler_compile(void) if (cfiles) { - platform_compiler(active_target.csources, cfiles, active_target.cflags); for (int i = 0; i < cfiles; i++) { - char *filename = NULL; - bool split_worked = file_namesplit(active_target.csources[i], &filename, NULL); - assert(split_worked); - size_t len = strlen(filename); - // .c -> .o (quick hack to fix the name on linux) - filename[len - 1] = 'o'; - obj_files[output_file_count + i] = filename; + const char *file = active_target.csources[i]; + const char *obj = platform_compiler(file, active_target.cflags); + obj_files[output_file_count + i] = obj; } } diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 87d7181dc..e84d53b92 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -2980,7 +2980,7 @@ bool static_lib_linker(const char *output_file, const char **files, unsigned fil bool dynamic_lib_linker(const char *output_file, const char **files, unsigned file_count); bool linker(const char *output_file, const char **files, unsigned file_count); void platform_linker(const char *output_file, const char **files, unsigned file_count); -void platform_compiler(const char **files, unsigned file_count, const char* flags); +const char *platform_compiler(const char *file, const char* flags); const char *arch_to_linker_arch(ArchType arch); #define CAT(a,b) CAT2(a,b) // force expand diff --git a/src/compiler/linker.c b/src/compiler/linker.c index e006b6eee..e75738440 100644 --- a/src/compiler/linker.c +++ b/src/compiler/linker.c @@ -735,8 +735,30 @@ void platform_linker(const char *output_file, const char **files, unsigned file_ printf("Program linked to executable '%s'.\n", output_file); } -void platform_compiler(const char **files, unsigned file_count, const char *flags) +const char *platform_compiler(const char *file, const char *flags) { + const char *dir = active_target.object_file_dir; + if (!dir) dir = active_target.build_dir; + + char *filename = NULL; + bool split_worked = file_namesplit(file, &filename, NULL); + if (!split_worked) error_exit("Cannot compile '%s'", file); + size_t len = strlen(filename); + // Remove .cpp or .c + if (len > 5 && memcmp(filename + len - 4, ".cpp", 4) == 0) + { + len -= 4; + filename[len] = 0; + } + else if (len > 2 && memcmp(filename + len - 2, ".c", 2) == 0) + { + len -= 2; + filename[len] = 0; + } + const char *out_name = dir + ? str_printf("%s/%s%s", dir, filename, get_object_extension()) + : str_printf("%s%s", filename, get_object_extension()); + const char **parts = NULL; vec_add(parts, active_target.cc); @@ -750,17 +772,19 @@ void platform_compiler(const char **files, unsigned file_count, const char *flag { append_fpie_pic_options(platform_target.reloc_model, &parts); } + vec_add(parts, "-c"); if (flags) vec_add(parts, flags); - for (unsigned i = 0; i < file_count; i++) - { - vec_add(parts, files[i]); - } + vec_add(parts, file); + vec_add(parts, "-o"); + vec_add(parts, out_name); + const char *output = concat_string_parts(parts); if (system(output) != 0) { error_exit("Failed to compile c sources using command '%s'.\n", output); } + return out_name; } bool dynamic_lib_linker(const char *output_file, const char **files, unsigned file_count) diff --git a/src/version.h b/src/version.h index c37d200fb..d269ee8d8 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.4.5" \ No newline at end of file +#define COMPILER_VERSION "0.4.6" \ No newline at end of file