44# include <Windows .h >
55#else
66# include <stdlib .h >
7+ # include <unistd .h >
78#endif
89
910export module cpp2b;
@@ -22,7 +23,6 @@ export namespace cpp2b {
2223enum class platform { linux, macos, windows } ;
2324enum class compiler_type { gcc, clang, msvc } ;
2425enum class compilation_type { debug , optimized, fast } ;
25- enum class build_type { local, development, release } ;
2626
2727constexpr auto host_platform() -> platform {
2828#if defined(_WIN32)
@@ -51,17 +51,6 @@ constexpr auto compiler() -> compiler_type {
5151# error unknown compiler
5252#endif
5353}
54-
55- constexpr auto build() -> build_type {
56- return build_type::local;
57- }
58-
59- constexpr auto install_dir() -> const std::string_view {
60- if constexpr (build() == build_type::local) {
61- return R" _____cpp2b_____(@CPP2B_PROJECT_ROOT@)_____cpp2b_____" ;
62- }
63- }
64-
6554} // namespace cpp2b
6655
6756export namespace cpp2b::env {
@@ -183,4 +172,47 @@ public:
183172 return iterator(nullptr);
184173 }
185174};
175+
176+ std::filesystem::path executable_path() {
177+ static std::string executable_path_str;
178+
179+ if (! executable_path_str.empty()) {
180+ return executable_path_str;
181+ }
182+
183+ auto size = 260;
184+ auto buffer = std::vector<char >(size);
185+
186+ #if defined(_WIN32)
187+ for (;;) {
188+ DWORD len = GetModuleFileNameA(NULL, buffer.data(), size);
189+ if (len == 0) {
190+ executable_path_str = {} ;
191+ return executable_path_str;
192+ } else if (len < size - 1) {
193+ executable_path_str = std::string(buffer.data(), len);
194+ return executable_path_str;
195+ }
196+
197+ size += 260;
198+ buffer.resize(size);
199+ }
200+ #elif defined(__linux__)
201+ for (;;) {
202+ ssize_t len = readlink(" /proc/self/exe" , buffer.data(), size - 1);
203+ if (len < 0) {
204+ executable_path_str = {} ;
205+ return executable_path_str;
206+ } else if (len < size - 1) {
207+ executable_path_str = std::string(buffer.data(), len);
208+ return executable_path_str;
209+ }
210+
211+ size += 260;
212+ buffer.resize(size);
213+ }
214+ #else
215+ # error unhandled executable_path platform
216+ #endif
217+ }
186218}
0 commit comments