diff --git a/build.cmd b/build.cmd index aaef964..9cf66c6 100644 --- a/build.cmd +++ b/build.cmd @@ -83,7 +83,7 @@ cl /nologo ^ /std:c++latest /W4 /MDd /EHsc ^ /reference "%modules_dir%\std.ifc" ^ /reference "%modules_dir%\std.compat.ifc" ^ - /c "%root_dir%.cache\cpp2\source\_build\cpp2b.ixx" > NUL + /c "%root_dir%.cache\cpp2\source\_build\cpp2b.ixx" popd if %ERRORLEVEL% neq 0 ( diff --git a/share/cpp2b.cppm.tpl b/share/cpp2b.cppm.tpl index 2918168..556d95c 100644 --- a/share/cpp2b.cppm.tpl +++ b/share/cpp2b.cppm.tpl @@ -6,13 +6,17 @@ module; # include #endif -extern char **environ; - export module cpp2b; import std; import std.compat; +#ifdef _MSC_VER + extern char **_environ; +#else + extern char **environ; +#endif + export namespace cpp2b { enum class platform { linux, macos, windows }; @@ -69,15 +73,33 @@ inline auto set_var(const std::string& name, const std::string& value) -> void { #endif } +inline auto set_var(auto name, auto value) -> void { + return cpp2b::env::set_var(std::string{name}, std::string{value}); +} + inline auto get_var(const std::string& name) -> std::optional { +#if defined(_MSC_VER) + auto val = std::string{""}; + val.resize(GetEnvironmentVariableA(name.c_str(), val.data(), 1) - 1); + + if(!val.empty()) { + GetEnvironmentVariableA(name.c_str(), val.data(), val.size() + 1); + return val; + } +#else auto val = std::getenv(name.c_str()); if(val != nullptr) { return std::string{val}; } +#endif return {}; } +inline auto get_var(auto name) -> std::optional { + return cpp2b::env::get_var(std::string{name}); +} + export class vars { public: struct entry { @@ -150,7 +172,11 @@ public: }; iterator begin() const noexcept { +#ifdef _MSC_VER + return iterator(_environ); +#else return iterator(environ); +#endif } iterator end() const noexcept { diff --git a/src/main.cpp2 b/src/main.cpp2 index 25ac4ce..3e8a7fd 100644 --- a/src/main.cpp2 +++ b/src/main.cpp2 @@ -278,7 +278,7 @@ build_cpp1_module: (name: std::string, sources, module_deps) = { } has_env: (var_name: *const char) -> bool = { - return std::getenv(var_name) != nullptr; + return cpp2b::env::get_var(var_name).has_value(); } has_msvc_env_vars: () -> bool = {