1
0

fix: fix environ not found when building env.

- re-organize included headers for env.
- resolve environ not found when building env on macos. use our explicit extern declare to replace system header decl.
This commit is contained in:
2026-01-22 15:26:33 +08:00
parent 8989e909ad
commit 746d20a835

View File

@@ -1,40 +1,37 @@
#include "env.hpp" #include "env.hpp"
#include "macro/os_detector.hpp" #include "macro/os_detector.hpp"
#include "string/reinterpret.hpp"
#include "num/safe_op.hpp" #include "num/safe_op.hpp"
#include "num/safe_cast.hpp" #include "num/safe_cast.hpp"
#include <string>
#include <string_view>
#include <memory>
#include <type_traits>
#include <stdexcept>
#include <filesystem>
#if defined(YYCC_OS_WINDOWS) #if defined(YYCC_OS_WINDOWS)
// Windows headers
#include "encoding/windows.hpp" #include "encoding/windows.hpp"
#include "windows/winfct.hpp" #include "windows/winfct.hpp"
#include <memory> // For safely free gotten environment variable and commandline argument.
#include <type_traits> // Used by std::unique_ptr to get underlying pointer type.
#include "windows/import_guard_head.hpp" #include "windows/import_guard_head.hpp"
#include <Windows.h> #include <Windows.h>
#include <winbase.h> #include <winbase.h>
#include <processenv.h> // For getting environment variables and commandline argument. #include <processenv.h> // For getting environment variables and commandline argument.
#include <shellapi.h> // For getting commandline argument. #include <shellapi.h> // For getting commandline argument.
#include "windows/import_guard_tail.hpp" #include "windows/import_guard_tail.hpp"
#elif defined(YYCC_OS_LINUX) #else
#include <cstdlib> // POSIX headers
#include <cerrno> #include "string/reinterpret.hpp"
#include <cstdlib> // For POSIX environment variable operations.
#include <cerrno> // For POSIX errno.
#include <stdexcept> // For re-throw unexpected POSIX errno as STL exception.
#if defined(YYCC_OS_LINUX)
// Linux-only headers
#include <fstream> // For reading commandline argument. #include <fstream> // For reading commandline argument.
#include <unistd.h>
#include <sys/stat.h> // For reading symlink target.
#elif defined(YYCC_OS_MACOS) #elif defined(YYCC_OS_MACOS)
#include <cstdlib> // macOS-only headers
#include <cerrno>
#include <unistd.h>
#include <cstring>
#include <mach-o/dyld.h> // For getting current exe path. #include <mach-o/dyld.h> // For getting current exe path.
#include <crt_externs.h> // For getting commandline argument. #include <crt_externs.h> // For getting commandline argument.
#else #else
#error "Not supported OS" #error "Not supported OS"
#endif #endif
#endif
#define SAFECAST ::yycc::num::safe_cast #define SAFECAST ::yycc::num::safe_cast
#define SAFEOP ::yycc::num::safe_op #define SAFEOP ::yycc::num::safe_op
@@ -160,6 +157,7 @@ namespace yycc::env {
} }
#if defined(YYCC_OS_WINDOWS) #if defined(YYCC_OS_WINDOWS)
class EnvironmentStringsDeleter { class EnvironmentStringsDeleter {
public: public:
EnvironmentStringsDeleter() {} EnvironmentStringsDeleter() {}
@@ -170,6 +168,11 @@ namespace yycc::env {
} }
}; };
using SmartEnvironmentStrings = std::unique_ptr<std::remove_pointer_t<LPWCH>, EnvironmentStringsDeleter>; using SmartEnvironmentStrings = std::unique_ptr<std::remove_pointer_t<LPWCH>, EnvironmentStringsDeleter>;
#else
extern char** environ;
#endif #endif
VarResult<std::vector<VarPair>> get_vars() { VarResult<std::vector<VarPair>> get_vars() {