Add --sources build option to add additional files to compile. #2097

This commit is contained in:
Christoffer Lerno
2025-06-10 14:09:15 +02:00
parent 496d23e93f
commit bbd9f6dc96
5 changed files with 31 additions and 10 deletions

View File

@@ -18,6 +18,7 @@
- Add printf format to `$assert` and `$error` #2183.
- Make accepting arguments for `main` a bit more liberal, accepting `main(int argc, ZString* argv)`
- Make `$echo` and `@sprintf` correctly stringify compile time initializers and slices.
- Add `--sources` build option to add additional files to compile. #2097
### Fixes
- `-2147483648`, MIN literals work correctly.

View File

@@ -151,5 +151,10 @@ int get_valid_enum_from_string(const char *str, const char *target, const char *
void check_json_keys(const char *valid_keys[][2], size_t key_count, const char *deprecated_keys[], size_t deprecated_key_count, JSONObject *json, const char *target_name, const char *option);
long get_valid_integer(BuildParseContext context, JSONObject *table, const char *key, bool mandatory);
INLINE void append_strings_to_strings(const char*** list_of_strings_ptr, const char **strings_to_append)
{
FOREACH(const char *, string, strings_to_append) vec_add(*list_of_strings_ptr, string);
}
#define APPEND_STRING_LIST(list__, string__) \
get_list_append_strings(context, json, list__, string__, string__ "-override")

View File

@@ -43,6 +43,7 @@ const char *arch_os_target[ARCH_OS_TARGET_LAST + 1];
#define EOUTPUT(string, ...) fprintf(stderr, string "\n", ##__VA_ARGS__) // NOLINT
#define PRINTF(string, ...) fprintf(stdout, string "\n", ##__VA_ARGS__) // NOLINT
#define FAIL_WITH_ERR(string, ...) do { fprintf(stderr, "Error: " string "\n\n", ##__VA_ARGS__); usage(false); exit_compiler(EXIT_FAILURE); } while (0) /* NOLINT */
#define FAIL_WITH_ERR_LONG(string, ...) do { fprintf(stderr, "Error: " string "\n\n", ##__VA_ARGS__); usage(true); exit_compiler(EXIT_FAILURE); } while (0) /* NOLINT */
#define PROJECT_FAIL_WITH_ERR(string, ...) do { fprintf(stderr, "Error: " string "\n\n", ##__VA_ARGS__); project_usage(); exit_compiler(EXIT_FAILURE); } while (0) /* NOLINT */
static void usage(bool full)
@@ -96,6 +97,7 @@ static void usage(bool full)
print_opt("--run-dir <dir>", "Set the directory from where to run the binary (only for run and compile-run).");
print_opt("--libdir <dir>", "Add this directory to the c3l library search paths.");
print_opt("--lib <name>", "Add this c3l library to the compilation.");
print_opt("--sources <file1> [<file2> ...]", "Add these additional sources to the compilation.");
if (full)
{
print_opt("--validation=<option>", "Strictness of code validation: lenient (default), strict, obnoxious (very strict)");
@@ -721,6 +723,20 @@ static void parse_option(BuildOptions *options)
options->ansi = parse_opt_select(Ansi, argopt, on_off);
return;
}
if (match_longopt("sources"))
{
if (at_end() || next_is_opt())
{
FAIL_WITH_ERR_LONG("'--sources' expected at least one source file.");
}
do
{
next_arg();
append_file(options);
} while(!(at_end() || next_is_opt()));
return;
}
if (match_longopt("use-old-slice-copy"))
{
options->old_slice_copy = true;
@@ -728,7 +744,7 @@ static void parse_option(BuildOptions *options)
}
if (match_longopt("test-filter"))
{
if (at_end() || next_is_opt()) error_exit("error: --test-filter needs an argument.");
if (at_end() || next_is_opt()) FAIL_WITH_ERR_LONG("error: --test-filter needs an argument.");
options->test_filter = next_arg();
return;
}
@@ -770,13 +786,13 @@ static void parse_option(BuildOptions *options)
}
if (match_longopt("symtab"))
{
if (at_end() || next_is_opt()) error_exit("error: --symtab needs a valid integer.");
if (at_end() || next_is_opt()) FAIL_WITH_ERR_LONG("error: --symtab needs a valid integer.");
const char *symtab_string = next_arg();
int symtab = atoi(symtab_string);
if (symtab < 1024) error_exit("Expected the --symtab size to be valid positive integer >= 1024.");
if (symtab < 1024) FAIL_WITH_ERR_LONG("Expected the --symtab size to be valid positive integer >= 1024.");
if (symtab > MAX_SYMTAB_SIZE)
{
error_exit("The symtab size is too large. The maximum size is %d.", (int)MAX_SYMTAB_SIZE);
FAIL_WITH_ERR_LONG("The symtab size is too large. The maximum size is %d.", (int)MAX_SYMTAB_SIZE);
}
options->symtab_size = next_highest_power_of_2(symtab);
return;

View File

@@ -523,6 +523,11 @@ static void update_build_target_from_options(BuildTarget *target, BuildOptions *
target->header_file_dir = target->output_dir ? target->output_dir : target->build_dir;
}
if (options->files)
{
append_strings_to_strings(&target->source_dirs, options->files);
}
switch (options->compile_option)
{
case COMPILE_NORMAL:

View File

@@ -113,12 +113,6 @@ const char *get_cflags(BuildParseContext context, JSONObject *json, const char *
return cflags ? cflags : original_flags;
}
INLINE void append_strings_to_strings(const char*** list_of_strings_ptr, const char **strings_to_append)
{
FOREACH(const char *, string, strings_to_append) vec_add(*list_of_strings_ptr, string);
}
void get_list_append_strings(BuildParseContext context, JSONObject *json, const char ***list_ptr, const char *base, const char *override)
{
const char **value = get_optional_string_array(context, json, context.target ? override : base);