From 741707273dce487debf79e0f018ba02eb5ddc0ee Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Mon, 4 Nov 2024 22:46:13 +0100 Subject: [PATCH] Add "prepare" target in project.json #1577 --- releasenotes.md | 1 + src/build/build.h | 12 ++++++++---- src/build/project.c | 2 +- src/build/project_manipulation.c | 18 +++++++----------- src/compiler/compiler.c | 15 ++++++++++++++- src/compiler/compiler.h | 1 + 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/releasenotes.md b/releasenotes.md index d39f8e3ae..77a7215fe 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -22,6 +22,7 @@ - `string::new_struct_to_str` and `io::struct_to_format` to dump struct data. - `io::print` will now print structs. - Improve error message when using `void` aliases as variable storage type. +- Add a target type: "prepare" which doesn't compile anything (but may run `exec`) ### Fixes - `Unsupported int[*] $x = { 1, 2, 3, 4 }` #1489. diff --git a/src/build/build.h b/src/build/build.h index af5243879..9326578cc 100644 --- a/src/build/build.h +++ b/src/build/build.h @@ -374,23 +374,27 @@ typedef enum TARGET_TYPE_OBJECT_FILES, TARGET_TYPE_BENCHMARK, TARGET_TYPE_TEST, + TARGET_TYPE_PREPARE, } TargetType; -static const char *targets[6] = { +static const char *targets[7] = { [TARGET_TYPE_EXECUTABLE] = "executable", [TARGET_TYPE_STATIC_LIB] = "static-lib", [TARGET_TYPE_DYNAMIC_LIB] = "dynamic-lib", [TARGET_TYPE_BENCHMARK] = "benchmark", [TARGET_TYPE_TEST] = "test", - [TARGET_TYPE_OBJECT_FILES] = "object-files" + [TARGET_TYPE_OBJECT_FILES] = "object-files", + [TARGET_TYPE_PREPARE] = "prepare", }; -static const char *target_desc[6] = { +static const char *target_desc[7] = { [TARGET_TYPE_EXECUTABLE] = "Executable", [TARGET_TYPE_STATIC_LIB] = "Static library", [TARGET_TYPE_DYNAMIC_LIB] = "Dynamic library", [TARGET_TYPE_BENCHMARK] = "benchmark suite", [TARGET_TYPE_TEST] = "test suite", - [TARGET_TYPE_OBJECT_FILES] = "object files" + [TARGET_TYPE_OBJECT_FILES] = "object files", + [TARGET_TYPE_PREPARE] = "prepare" + }; diff --git a/src/build/project.c b/src/build/project.c index 0acd8a795..5de759996 100644 --- a/src/build/project.c +++ b/src/build/project.c @@ -121,7 +121,7 @@ const char* project_target_keys[][2] = { {"target", "Compile for a particular architecture + OS target."}, {"testfn", "Override the test function."}, {"trap-on-wrap", "Make signed and unsigned integer overflow generate a panic rather than wrapping."}, - {"type", "Type of output, one of 'executable', 'static-lib', 'dynamic-lib', 'benchmark', 'test', 'object-files'." }, + {"type", "Type of output, one of 'executable', 'static-lib', 'dynamic-lib', 'benchmark', 'test', 'object-files' and 'prepare'." }, {"use-stdlib", "Include the standard library (default: true)."}, {"vendor", "Vendor specific extensions, ignored by c3c."}, {"version", "Version using semantic versioning."}, diff --git a/src/build/project_manipulation.c b/src/build/project_manipulation.c index e4b3988e3..05fd2bfd4 100644 --- a/src/build/project_manipulation.c +++ b/src/build/project_manipulation.c @@ -283,13 +283,11 @@ void fetch_project(BuildOptions* options) const char** deps_dirs = get_project_dependency_directories(); int num_lib = vec_size(deps_dirs); if (num_lib > 0) options->vendor_download_path = deps_dirs[0]; - } - } - const char** libdirs = get_project_dependency_directories(); - const char** deps = get_project_dependencies(); + const char **libdirs = get_project_dependency_directories(); + const char **deps = get_project_dependencies(); const char *filename; JSONObject *project_json = read_project(&filename); @@ -306,23 +304,21 @@ void fetch_project(BuildOptions* options) error_exit("Invalid data in target '%s'", key); } - const char** target_deps = get_optional_string_array(filename, key, target, "dependencies"); + const char **target_deps = get_optional_string_array(filename, key, target, "dependencies"); - FOREACH(const char*, dep, target_deps) { + FOREACH(const char*, dep, target_deps) + { vec_add(deps, dep); } } - } // dependency check tree while (vec_size(deps) > 0) { - FOREACH(const char*, dir, libdirs) { - - const char* dep = deps[vec_size(deps)-1]; + const char *dep = VECLAST(deps); if (file_exists(file_append_path(dir, str_printf("%s.c3l", dep)))) { vec_pop(deps); @@ -332,7 +328,7 @@ void fetch_project(BuildOptions* options) printf("Fetching missing library '%s'...", dep); fflush(stdout); - const char* error = vendor_fetch_single(dep, options->vendor_download_path); + const char *error = vendor_fetch_single(dep, options->vendor_download_path); if (!error) { diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 5e5b62f87..4369af770 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -506,6 +506,8 @@ void compiler_compile(void) break; case TARGET_TYPE_OBJECT_FILES: break; + case TARGET_TYPE_PREPARE: + break; default: UNREACHABLE } @@ -843,6 +845,17 @@ void compile_clean(BuildOptions *options) void compile_file_list(BuildOptions *options) { init_build_target(&compiler.build, options); + if (compiler.build.type == TARGET_TYPE_PREPARE) + { + if (options->command != COMMAND_BUILD) + { + error_exit("The target is a 'prepare' target, and only 'build' can be used with it."); + } + printf("] Running prepare target '%s'.\n", options->target_select); + execute_scripts(); + printf("] Completed.\n."); + return; + } if (options->command == COMMAND_CLEAN_RUN) { clean_obj_files(); @@ -1116,7 +1129,7 @@ static int jump_buffer_size() UNREACHABLE } -static void execute_scripts(void) +void execute_scripts(void) { if (!vec_size(compiler.build.exec)) return; if (compiler.build.trust_level < TRUST_FULL) diff --git a/src/compiler/compiler.h b/src/compiler/compiler.h index dd684ba38..89386ee34 100644 --- a/src/compiler/compiler.h +++ b/src/compiler/compiler.h @@ -10,6 +10,7 @@ void compile(); void compile_target(BuildOptions *options); void compile_file_list(BuildOptions *options); void compile_clean(BuildOptions *options); +void execute_scripts(void); void init_build_target(BuildTarget *build_target, BuildOptions *build_options); void init_default_build_target(BuildTarget *target, BuildOptions *options); void symtab_init(uint32_t max_size);