Skip to content

Commit 301700a

Browse files
committed
fix: Add option to install the WASI sysroot
1 parent 4c4e4c9 commit 301700a

File tree

5 files changed

+35
-21
lines changed

5 files changed

+35
-21
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
3232

3333
# option to install tools
3434
option(CXX_INSTALL_TOOLS "Install tools" ON)
35+
option(CXX_INSTALL_WASI_SYSROOT "Install wasi sysroot" OFF)
3536

3637
# if CMAKE_SYSTEM_NAME is WASI disable the exceptions
3738
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
@@ -55,6 +56,9 @@ FetchContent_Declare(
5556
GIT_SHALLOW 1
5657
)
5758

59+
# set FMT_INSTALL to OFF to avoid installing fmt
60+
set(FMT_INSTALL OFF CACHE BOOL "" FORCE)
61+
5862
FetchContent_MakeAvailable(fmt)
5963

6064
FetchContent_Declare(

src/frontend/cxx/cli.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ std::vector<CLIOptionDescr> options{
164164
CLIOptionDescrKind::kSeparated},
165165

166166
{"-toolchain", "<id>",
167-
"Set the toolchain to 'linux', 'darwin', 'wasm32', or 'windows'",
167+
"Set the toolchain to 'linux', 'darwin', 'wasm32', or 'windows'. Defaults "
168+
"to wasm32.",
168169
CLIOptionDescrKind::kSeparated},
169170

170171
{"-verify", "Verify the diagnostic messages", &CLI::opt_verify},

src/frontend/cxx/frontend.cc

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -228,40 +228,36 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {
228228
auto toolchainId = cli.getSingle("-toolchain");
229229

230230
if (!toolchainId) {
231-
#if defined(__APPLE__)
232-
toolchainId = "darwin";
233-
#elif defined(__wasi__)
234231
toolchainId = "wasm32";
235-
#elif defined(__linux__)
236-
toolchainId = "linux";
237-
#elif defined(_MSC_VER)
238-
toolchainId = "windows";
239-
#endif
240232
}
241233

242234
if (toolchainId == "darwin") {
243235
toolchain = std::make_unique<MacOSToolchain>(preprocesor);
244236
} else if (toolchainId == "wasm32") {
245237
auto wasmToolchain = std::make_unique<Wasm32WasiToolchain>(preprocesor);
246238

239+
fs::path app_dir;
240+
241+
#if __wasi__
242+
app_dir = fs::path("/");
243+
#elif __unix__
244+
char* app_name = realpath(cli.app_name.c_str(), nullptr);
245+
app_dir = fs::path(app_name).remove_filename().string();
246+
std::free(app_name);
247+
#endif
248+
249+
wasmToolchain->setAppdir(app_dir.string());
250+
247251
if (auto paths = cli.get("--sysroot"); !paths.empty()) {
248252
wasmToolchain->setSysroot(paths.back());
249253
} else {
254+
fs::path sysroot_dir;
250255
#if __wasi__
251-
wasmToolchain->setSysroot("/wasi-sysroot");
256+
sysroot_dir = fs::path("/wasi-sysroot");
252257
#elif __unix__
253-
char* app_name = realpath(cli.app_name.c_str(), nullptr);
254-
255-
const fs::path app_dir = fs::path(app_name).remove_filename();
256-
wasmToolchain->setAppdir(app_dir.string());
257-
258-
const auto sysroot_dir = app_dir / "../lib/wasi-sysroot";
259-
wasmToolchain->setSysroot(sysroot_dir.string());
260-
261-
if (app_name) {
262-
std::free(app_name);
263-
}
258+
sysroot_dir = app_dir / "../lib/wasi-sysroot";
264259
#endif
260+
wasmToolchain->setSysroot(sysroot_dir.string());
265261
}
266262

267263
toolchain = std::move(wasmToolchain);

src/lib/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ add_custom_target(link_wasi_sysroot ALL
2121
COMMAND ${CMAKE_COMMAND} -E create_symlink ${wasi_sysroot_SOURCE_DIR} wasi-sysroot)
2222

2323
add_subdirectory(cxx)
24+
25+
if (CXX_INSTALL_WASI_SYSROOT)
26+
install(
27+
DIRECTORY ${wasi_sysroot_SOURCE_DIR}/
28+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/wasi-sysroot)
29+
endif()

src/lib/cxx/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@
1919

2020
add_custom_target(link_cxx_include ALL
2121
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_SOURCE_DIR}/include include)
22+
23+
file(GLOB_RECURSE HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*")
24+
25+
install(
26+
FILES ${HEADER_FILES}
27+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cxx/include
28+
)

0 commit comments

Comments
 (0)