From 81397f07261f87cab56f8468d978aade3e5130c2 Mon Sep 17 00:00:00 2001 From: Adversing <60707212+Adversing@users.noreply.github.com> Date: Wed, 26 Feb 2025 01:48:03 +0100 Subject: [PATCH] Add --print-env option to c3c (#1880) * Add `--build-env` for build environment information. --------- Co-authored-by: Christoffer Lerno --- releasenotes.md | 1 + src/build/build.h | 4 ++ src/build/build_options.c | 6 ++ src/compiler/compiler.c | 96 +++++++++++++++++++++++++++++++- src/compiler/compiler_internal.h | 6 +- 5 files changed, 111 insertions(+), 2 deletions(-) diff --git a/releasenotes.md b/releasenotes.md index f7d22ace7..41daf19c4 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -10,6 +10,7 @@ - Output into /.build/obj/ by default. - Output llvm/asm into llvm/ and asm/ 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. diff --git a/src/build/build.h b/src/build/build.h index 785f36873..6c5790d40 100644 --- a/src/build/build.h +++ b/src/build/build.h @@ -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); diff --git a/src/build/build_options.c b/src/build/build_options.c index b96bd5598..bb6ae32c5 100644 --- a/src/build/build_options.c +++ b/src/build/build_options.c @@ -92,6 +92,7 @@ static void usage(bool full) print_opt("-U ", "Remove feature flag ."); PRINTF(""); print_opt("--about", "Prints a short description of C3."); + print_opt("--build-env", "Prints build environment information."); print_opt("--libdir ", "Add this directory to the c3l library search paths."); print_opt("--lib ", "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."); diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 07ab8e6c5..a8c4f7873 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -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 -} \ No newline at end of file +} + +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"; + } +} diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index d6706cd40..8167a11c7 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -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); \ No newline at end of file +const char *default_c_compiler(void); + +void print_build_env(void); +const char *os_type_to_string(OsType os);