Skip to content

Commit

Permalink
Add "prepare" target in project.json #1577
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Nov 4, 2024
1 parent bdd6ed0 commit 7417072
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 8 additions & 4 deletions src/build/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

};


Expand Down
2 changes: 1 addition & 1 deletion src/build/project.c
Original file line number Diff line number Diff line change
Expand Up @@ -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."},
Expand Down
18 changes: 7 additions & 11 deletions src/build/project_manipulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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)
{
Expand Down
15 changes: 14 additions & 1 deletion src/compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ void compiler_compile(void)
break;
case TARGET_TYPE_OBJECT_FILES:
break;
case TARGET_TYPE_PREPARE:
break;
default:
UNREACHABLE
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7417072

Please sign in to comment.