Add --print-env option to c3c (#1880)

* Add `--build-env` for build environment information.

---------

Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
Adversing
2025-02-26 01:48:03 +01:00
committed by GitHub
parent 0c33b78a2f
commit 81397f0726
5 changed files with 111 additions and 2 deletions

View File

@@ -10,6 +10,7 @@
- Output into /.build/obj/<platform> by default.
- Output llvm/asm into llvm/<platform> and asm/<platform> by default.
- Add flag `--suppress-run`. For commands which may run executable after building, skip the run step. #1931
- Add `--build-env` for build environment information.
### Fixes
- Bug appearing when `??` was combined with boolean in some cases.

View File

@@ -592,6 +592,7 @@ typedef struct BuildOptions_
bool print_manifest_properties;
bool print_precedence;
bool print_linking;
bool print_env;
bool benchmarking;
bool testing;
} BuildOptions;
@@ -654,6 +655,7 @@ typedef struct
const char *ir_file_dir;
const char *asm_file_dir;
const char *script_dir;
bool is_non_project;
bool run_after_compile;
bool delete_after_run;
bool generate_benchmark_runner;
@@ -771,6 +773,7 @@ static const char *x86_cpu_set[8] = {
};
static BuildTarget default_build_target = {
.is_non_project = true,
.optlevel = OPTIMIZATION_NOT_SET,
.optsetting = OPT_SETTING_NOT_SET,
.memory_environment = MEMORY_ENV_NORMAL,
@@ -835,3 +838,4 @@ void resolve_libraries(BuildTarget *build_target);
void view_project(BuildOptions *build_options);
void add_target_project(BuildOptions *build_options);
void fetch_project(BuildOptions* options);
void print_build_env(void);

View File

@@ -92,6 +92,7 @@ static void usage(bool full)
print_opt("-U <name>", "Remove feature flag <name>.");
PRINTF("");
print_opt("--about", "Prints a short description of C3.");
print_opt("--build-env", "Prints build environment information.");
print_opt("--libdir <dir>", "Add this directory to the c3l library search paths.");
print_opt("--lib <name>", "Add this c3l library to the compilation.");
if (full)
@@ -753,6 +754,11 @@ static void parse_option(BuildOptions *options)
silence_deprecation = true;
return;
}
if (match_longopt("build-env"))
{
options->print_env = true;
return;
}
if (match_longopt("symtab"))
{
if (at_end() || next_is_opt()) error_exit("error: --symtab needs a valid integer.");

View File

@@ -87,6 +87,11 @@ void compiler_init(BuildOptions *build_options)
{
compiler.context.lib_dir = find_lib_dir();
}
if (build_options->print_env)
{
compiler.context.should_print_environment = true;
}
}
static void compiler_lex(void)
@@ -1328,6 +1333,11 @@ void compile()
unit->module = compiler.context.core_module;
compiler.context.core_unit = unit;
target_setup(&compiler.build);
if (compiler.context.should_print_environment)
{
print_build_env();
exit_compiler(COMPILER_SUCCESS_EXIT);
}
check_sanitizer_options(&compiler.build);
resolve_libraries(&compiler.build);
compiler.context.sources = compiler.build.sources;
@@ -1593,4 +1603,88 @@ const char *default_c_compiler(void)
#else
return cc = "cc";
#endif
}
}
static bool is_posix(OsType os)
{
switch (os)
{
case OS_TYPE_IOS:
case OS_TYPE_MACOSX:
case OS_TYPE_WATCHOS:
case OS_TYPE_TVOS:
case OS_TYPE_NETBSD:
case OS_TYPE_LINUX:
case OS_TYPE_KFREEBSD:
case OS_TYPE_FREE_BSD:
case OS_TYPE_SOLARIS:
return true;
case OS_TYPE_WIN32:
case OS_TYPE_WASI:
case OS_TYPE_EMSCRIPTEN:
return false;
default:
return false;
}
}
void print_build_env(void)
{
char path[PATH_MAX + 1];
printf("Version : %s\n", COMPILER_VERSION);
printf("Stdlib : %s\n", compiler.context.lib_dir);
printf("Exe name : %s\n", compiler_exe_name);
printf("Base path : %s\n", getcwd(path, PATH_MAX));
if (compiler.build.name && !compiler.build.is_non_project)
{
printf("Target name : %s\n", compiler.build.name);
}
printf("Output name : %s\n", compiler.build.output_name);
printf("System path : %s\n", getenv("PATH"));
printf("Arch/OS target : %s\n", arch_os_target[compiler.build.arch_os_target]);
printf("env::POSIX : %s\n", link_libc() && is_posix(compiler.platform.os) ? "true" : "false");
printf("env::WIN32 : %s\n", compiler.platform.os == OS_TYPE_WIN32 ? "true" : "false");
printf("env::LIBC : %s\n", link_libc() ? "true" : "false");
}
const char *os_type_to_string(OsType os)
{
switch (os)
{
case OS_TYPE_UNKNOWN: return "UNKNOWN";
case OS_TYPE_NONE: return "NONE";
case OS_TYPE_ANANAS: return "ANANAS";
case OS_TYPE_CLOUD_ABI: return "CLOUD_ABI";
case OS_TYPE_DRAGON_FLY: return "DRAGON_FLY";
case OS_TYPE_FUCHSIA: return "FUCHSIA";
case OS_TYPE_IOS: return "IOS";
case OS_TYPE_KFREEBSD: return "KFREEBSD";
case OS_TYPE_LINUX: return "LINUX";
case OS_TYPE_PS3: return "PS3";
case OS_TYPE_MACOSX: return "MACOSX";
case OS_TYPE_NETBSD: return "NETBSD";
case OS_TYPE_OPENBSD: return "OPENBSD";
case OS_TYPE_SOLARIS: return "SOLARIS";
case OS_TYPE_WIN32: return "WIN32";
case OS_TYPE_HAIKU: return "HAIKU";
case OS_TYPE_MINIX: return "MINIX";
case OS_TYPE_RTEMS: return "RTEMS";
case OS_TYPE_NACL: return "NACL";
case OS_TYPE_CNK: return "CNK";
case OS_TYPE_AIX: return "AIX";
case OS_TYPE_CUDA: return "CUDA";
case OS_TYPE_NVOPENCL: return "NVOPENCL";
case OS_TYPE_AMDHSA: return "AMDHSA";
case OS_TYPE_PS4: return "PS4";
case OS_TYPE_ELFIAMCU: return "ELFIAMCU";
case OS_TYPE_TVOS: return "TVOS";
case OS_TYPE_WATCHOS: return "WATCHOS";
case OS_TYPE_MESA3D: return "MESA3D";
case OS_TYPE_CONTIKI: return "CONTIKI";
case OS_TYPE_AMDPAL: return "AMDPAL";
case OS_TYPE_HERMITCORE: return "HERMITCORE";
case OS_TYPE_HURD: return "HURD";
case OS_TYPE_WASI: return "WASI";
case OS_TYPE_EMSCRIPTEN: return "EMSCRIPTEN";
default: return "UNKNOWN";
}
}

View File

@@ -1839,6 +1839,7 @@ typedef struct
typedef struct
{
bool should_print_environment;
HTable modules;
Module *core_module;
CompilationUnit *core_unit;
@@ -4117,4 +4118,7 @@ INLINE bool check_module_name(Path *path)
#endif
void assert_print_line(SourceSpan span);
const char *default_c_compiler(void);
const char *default_c_compiler(void);
void print_build_env(void);
const char *os_type_to_string(OsType os);