Skip to content

Commit

Permalink
Add --output-dir #1155
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Mar 12, 2024
1 parent 2a95006 commit 146856f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- 'rgba' also available for swizzling.
- The name "subarray" has been replaced by the more well known name "slice' across the codebase.
- Improved alignment handling.
- Add `--output-dir` to command line. #1155

### Fixes
- Fixed issue in safe mode when converting enums.
Expand Down
1 change: 1 addition & 0 deletions src/build/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ typedef struct BuildOptions_
const char *testfn;
const char *cc;
const char *build_dir;
const char *output_dir;
const char *llvm_out;
const char *asm_out;
const char *obj_out;
Expand Down
8 changes: 8 additions & 0 deletions src/build/build_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ static void usage(void)
OUTPUT(" -D <name> - Add feature flag <name>.");
OUTPUT(" -U <name> - Remove feature flag <name>.");
OUTPUT(" --trust=<option> - Trust level: none (default), include ($include allowed), full ($exec / exec allowed).");
OUTPUT(" --output-dir <dir> - Override general output directory.");
OUTPUT(" --build-dir <dir> - Override build output directory.");
OUTPUT(" --obj-out <dir> - Override object file output directory.");
OUTPUT(" --script-dir <dir> - Override the base directory for $exec.");
Expand Down Expand Up @@ -961,6 +962,12 @@ static void parse_option(BuildOptions *options)
options->macos.min_version = next_arg();
return;
}
if (match_longopt("output-dir"))
{
if (at_end() || next_is_opt()) error_exit("error: --output-dir needs a directory.");
options->output_dir = next_arg();
return;
}
if (match_longopt("build-dir"))
{
if (at_end() || next_is_opt()) error_exit("error: --build-dir needs a directory.");
Expand Down Expand Up @@ -1126,6 +1133,7 @@ BuildOptions parse_arguments(int argc, const char *argv[])
.single_module = SINGLE_MODULE_NOT_SET,
.files = NULL,
.build_dir = NULL,
.output_dir = NULL,
.script_dir = NULL,

};
Expand Down
1 change: 1 addition & 0 deletions src/build/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ static void update_build_target_from_options(BuildTarget *target, BuildOptions *
target->emit_llvm = options->emit_llvm;
target->build_threads = options->build_threads;
target->emit_asm = options->emit_asm;
if (options->output_dir) target->output_dir = options->output_dir;
if (options->panicfn) target->panicfn = options->panicfn;
if (options->testfn) target->testfn = options->testfn;
if (options->benchfn) target->benchfn = options->benchfn;
Expand Down
28 changes: 25 additions & 3 deletions src/compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,15 @@ void compiler_parse(void)
compiler_parsing_time = bench_mark();
}

static void create_output_dir(const char *dir)
{
if (!file_exists(dir))
{
if (!dir_make(dir)) error_exit("Failed to create output directory %s.", dir);
}
if (!file_is_dir(dir)) error_exit("Output directory is not a directory %s.", dir);
}

void compiler_compile(void)
{
sema_analysis_run();
Expand Down Expand Up @@ -486,11 +495,16 @@ void compiler_compile(void)

if (output_exe)
{
if (active_target.output_dir) output_exe = file_append_path(active_target.output_dir, output_exe);
if (active_target.output_dir)
{
create_output_dir(active_target.output_dir);
output_exe = file_append_path(active_target.output_dir, output_exe);
}
if (file_is_dir(output_exe))
{
error_exit("Cannot create exe with the name '%s' - there is already a directory with that name.", output_exe);
}

bool system_linker_available = link_libc() && platform_target.os != OS_TYPE_WIN32;
bool use_system_linker = system_linker_available && active_target.arch_os_target == default_target;
switch (active_target.linker_type)
Expand Down Expand Up @@ -545,7 +559,11 @@ void compiler_compile(void)
}
else if (output_static)
{
if (active_target.output_dir) output_static = file_append_path(active_target.output_dir, output_static);
if (active_target.output_dir)
{
create_output_dir(active_target.output_dir);
output_static = file_append_path(active_target.output_dir, output_static);
}
if (file_is_dir(output_static))
{
error_exit("Cannot create a static library with the name '%s' - there is already a directory with that name.", output_exe);
Expand All @@ -561,7 +579,11 @@ void compiler_compile(void)
}
else if (output_dynamic)
{
if (active_target.output_dir) output_dynamic = file_append_path(active_target.output_dir, output_dynamic);
if (active_target.output_dir)
{
create_output_dir(active_target.output_dir);
output_dynamic = file_append_path(active_target.output_dir, output_dynamic);
}
if (file_is_dir(output_dynamic))
{
error_exit("Cannot create a dynamic library with the name '%s' - there is already a directory with that name.", output_exe);
Expand Down

0 comments on commit 146856f

Please sign in to comment.