From c3ebf51295fbd502f83b5c7068e21d12c84dbce6 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 26 Dec 2024 02:15:45 +0100 Subject: [PATCH] Add `"name"` project property to override the name of the resulting binary. #1719 --- releasenotes.md | 1 + src/build/build.h | 1 + src/build/builder.c | 1 + src/build/project.c | 4 ++++ src/build/project.h | 1 - src/build/project_manipulation.c | 1 + src/compiler/compiler.c | 25 ++++++++++++------------- src/compiler/module.c | 6 +++++- 8 files changed, 25 insertions(+), 15 deletions(-) diff --git a/releasenotes.md b/releasenotes.md index c9a6c6445..6c210f72d 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -9,6 +9,7 @@ - Deprecate cast-style conversion from integer <-> enum. - Make deprecation an error in test mode. - Add `--win-vs-dirs` to override VS detection dirs. +- Add `"name"` project property to override the name of the resulting binary. #1719 ### Fixes - Fix case trying to initialize a `char[*]*` from a String. diff --git a/src/build/build.h b/src/build/build.h index 0f648d99f..5e299f88e 100644 --- a/src/build/build.h +++ b/src/build/build.h @@ -556,6 +556,7 @@ typedef struct Library **library_list; LibraryTarget **ccompiling_libraries; const char *name; + const char *output_name; const char *version; const char *langrev; const char **source_dirs; diff --git a/src/build/builder.c b/src/build/builder.c index 572bf496f..5a597167e 100644 --- a/src/build/builder.c +++ b/src/build/builder.c @@ -521,6 +521,7 @@ void init_default_build_target(BuildTarget *target, BuildOptions *options) *target = default_build_target; target->source_dirs = options->files; target->name = options->output_name; + target->output_name = options->output_name; update_build_target_from_options(target, options); } diff --git a/src/build/project.c b/src/build/project.c index 8ae1c4329..de5fd3706 100644 --- a/src/build/project.c +++ b/src/build/project.c @@ -101,6 +101,7 @@ const char* project_target_keys[][2] = { {"macos-sdk-version", "Set the MacOS SDK compiled for." }, {"macossdk", "Set the directory for the MacOS SDK for cross compilation."}, {"memory-env", "Set the memory environment: normal, small, tiny, none."}, + {"name", "Set the name to be different from the target name."}, {"no-entry", "Do not generate (or require) a main function."}, {"opt", "Optimization setting: O0, O1, O2, O3, O4, O5, Os, Oz."}, {"optlevel", "Code optimization level: none, less, more, max."}, @@ -247,6 +248,9 @@ static void load_into_build_target(const char *filename, JSONObject *json, const target->feature.panic_level = (PanicLevel)get_valid_bool(filename, target_name, json, "panic-msg", target->feature.panic_level); + // Overridden name + target->output_name = get_optional_string(filename, target_name, json, "name"); + // Single module target->single_module = (SingleModule) get_valid_bool(filename, target_name, json, "single-module", target->single_module); diff --git a/src/build/project.h b/src/build/project.h index 8c49c330f..882b27054 100644 --- a/src/build/project.h +++ b/src/build/project.h @@ -2,7 +2,6 @@ #include "../utils/json.h" const char** get_project_dependency_directories(); - const char** get_project_dependencies(); static void print_vec(const char *header, const char **vec, bool opt); diff --git a/src/build/project_manipulation.c b/src/build/project_manipulation.c index 05fd2bfd4..b63e55d0f 100644 --- a/src/build/project_manipulation.c +++ b/src/build/project_manipulation.c @@ -209,6 +209,7 @@ static void view_target(const char *filename, const char *name, JSONObject *targ print_opt_str("\tName", name); TARGET_VIEW_MANDATORY_STRING("Type", "type"); TARGET_VIEW_STRING("Target language target", "langrev"); + TARGET_VIEW_STRING("Target output name", "name"); TARGET_VIEW_STRING_ARRAY("Warnings used", "warnings"); TARGET_VIEW_STRING_ARRAY("Additional c3l library search paths", "dependency-search-paths"); TARGET_VIEW_STRING_ARRAY("c3l library search paths (override)", "dependency-search-paths-override"); diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 0170235b0..cc6791efe 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -36,6 +36,13 @@ static double compiler_link_time; const char* c3_suffix_list[3] = { ".c3", ".c3t", ".c3i" }; +static const char *out_name(void) +{ + if (compiler.build.output_name) return compiler.build.output_name; + if (compiler.build.name) return compiler.build.name; + return NULL; +} + void compiler_init(BuildOptions *build_options) { // Process --path @@ -150,12 +157,8 @@ void **tilde_gen(Module** modules, unsigned module_count) const char *build_base_name(void) { - const char *name; - if (compiler.build.name) - { - name = compiler.build.name; - } - else + const char *name = out_name(); + if (!name) { Module **modules = compiler.context.module_list; Module *main_module = (modules[0] == compiler.context.core_module && vec_size(modules) > 1) ? modules[1] : modules[0]; @@ -176,13 +179,9 @@ const char *build_base_name(void) static const char *exe_name(void) { - ASSERT0(compiler.build.name || compiler.context.main || compiler.build.no_entry); - const char *name; - if (compiler.build.name || compiler.build.no_entry) - { - name = compiler.build.name ? compiler.build.name : "out"; - } - else + ASSERT0(compiler.build.output_name || compiler.build.name || compiler.context.main || compiler.build.no_entry); + const char *name = out_name(); + if (!name) { Path *path = compiler.context.main->unit->module->name; size_t first = 0; diff --git a/src/compiler/module.c b/src/compiler/module.c index b41181154..19446647f 100644 --- a/src/compiler/module.c +++ b/src/compiler/module.c @@ -43,7 +43,11 @@ void scratch_buffer_append_module(Module *module, bool is_export) const char *module_create_object_file_name(Module *module) { - if (compiler.build.single_module == SINGLE_MODULE_ON && compiler.build.name) return compiler.build.name; + if (compiler.build.single_module == SINGLE_MODULE_ON) + { + if (compiler.build.output_name) return compiler.build.output_name; + if (compiler.build.name) return compiler.build.name; + } scratch_buffer_clear(); char c; const char *name = module->name->module;