Skip to content

Commit ea7fc76

Browse files
authored
feat: check for share directory relative to executable (#15)
this makes cpp2b more portable
1 parent 3bb968e commit ea7fc76

File tree

10 files changed

+154
-35
lines changed

10 files changed

+154
-35
lines changed

.github/workflows/main.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ env:
1010
# https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2404-Readme.md
1111
LLVM_VERSION: '18.1.3'
1212
CPP2B_LIBCXX_BUILD_ROOT: '/tmp/llvm-project/build'
13+
CPP2B_PROJECT_ROOT: '.'
1314

1415
jobs:
1516
typos-check:

build.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ setlocal enableextensions disabledelayedexpansion
6262
set "search=@CPP2B_PROJECT_ROOT@"
6363
set "replace=%root_dir%"
6464

65-
set "inputFile=%root_dir%share\cpp2b.cppm.tpl"
65+
set "inputFile=%root_dir%share\cpp2b\cpp2b.cppm.tpl"
6666
set "outputFile=%root_dir%.cache\cpp2\source\_build\cpp2b.ixx"
6767

6868
for /f "delims=" %%i in ('type "%inputFile%"') do (

build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ if [ -f "$ROOT_DIR/.cache/cpp2/source/_build/cpp2b.cppm" ]; then
181181
rm "$ROOT_DIR/.cache/cpp2/source/_build/cpp2b.cppm"
182182
fi
183183

184-
cat "$ROOT_DIR/share/cpp2b.cppm.tpl" | sed "s\`@CPP2B_PROJECT_ROOT@\`$ROOT_DIR\`g" > "$ROOT_DIR/.cache/cpp2/source/_build/cpp2b.cppm"
184+
cat "$ROOT_DIR/share/cpp2b/cpp2b.cppm.tpl" | sed "s\`@CPP2B_PROJECT_ROOT@\`$ROOT_DIR\`g" > "$ROOT_DIR/.cache/cpp2/source/_build/cpp2b.cppm"
185185

186186
$CPP2B_COMPILER \
187187
-stdlib=libc++ \
@@ -197,6 +197,7 @@ $CPPFRONT src/main.cpp2 -pure -import-std -l -format-colon-errors -o "$ROOT_DIR/
197197

198198
log_info "compiling..."
199199
$CPP2B_COMPILER \
200+
-g \
200201
-stdlib=libc++ \
201202
"$MODULES_DIR/cpp2b.pcm" \
202203
"$MODULES_DIR/dylib.pcm" \

install.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
@echo off
22

33
set cpp2b_dist=%~dp0dist\debug\cpp2b
4+
set cpp2b_share_dir=%~dp0share
45

56
call %~dp0build.cmd
67

78
xcopy /y /q %cpp2b_dist%.exe %USERPROFILE%\.local\bin\
89
xcopy /y /q %cpp2b_dist%.pdb %USERPROFILE%\.local\bin\
10+
xcopy /e /i /h /y %cpp2b_share_dir% %USERPROFILE%\.local\share
11+

install.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ cd $ROOT_DIR
1010

1111
./build.sh
1212
cp dist/debug/cpp2b ~/.local/bin/cpp2b
13+
rm -rf ~/.local/share/cpp2b
14+
cp -r share/cpp2b ~/.local/share
File renamed without changes.
File renamed without changes.

share/cpp2b.cppm.tpl renamed to share/cpp2b/cpp2b.cppm.tpl

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module;
44
# include <Windows.h>
55
#else
66
# include <stdlib.h>
7+
# include <unistd.h>
78
#endif
89

910
export module cpp2b;
@@ -22,7 +23,6 @@ export namespace cpp2b {
2223
enum class platform { linux, macos, windows };
2324
enum class compiler_type { gcc, clang, msvc };
2425
enum class compilation_type { debug, optimized, fast };
25-
enum class build_type { local, development, release };
2626

2727
constexpr 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

6756
export 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
}
File renamed without changes.

0 commit comments

Comments
 (0)