Skip to content

Commit

Permalink
fix: Link the wasi sysroot in the build directory
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoraggi committed Jun 10, 2023
1 parent aeee93a commit e82a63d
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 16 deletions.
6 changes: 1 addition & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ set_target_properties(nlohmann_json::nlohmann_json PROPERTIES INTERFACE_INCLUDE_
enable_testing()

add_subdirectory(tools/kwgen)
add_subdirectory(src/parser)
add_subdirectory(src/ir)
add_subdirectory(src/llvm)
add_subdirectory(src/js)
add_subdirectory(src/frontend)
add_subdirectory(src)
add_subdirectory(tests)

configure_package_config_file(
Expand Down
25 changes: 25 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2023 Roberto Raggi <roberto.raggi@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

add_subdirectory(lib)
add_subdirectory(parser)
add_subdirectory(ir)
add_subdirectory(llvm)
add_subdirectory(js)
add_subdirectory(frontend)
3 changes: 2 additions & 1 deletion src/frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ if(EMSCRIPTEN)
endif()

if (CMAKE_SYSTEM_NAME STREQUAL "WASI")
target_compile_definitions(cxx PUBLIC JSON_HAS_FILESYSTEM=0)
target_compile_definitions(cxx PRIVATE JSON_HAS_FILESYSTEM=0)
target_compile_definitions(cxx PRIVATE CXX_NO_FILESYSTEM)
endif()

if (CXX_INSTALL_TOOLS)
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/cxx/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ auto CLI::positionals() const -> std::vector<std::string> {
}

void CLI::parse(int& argc, char**& argv) {
app_name = argv[0];

for (int i = 1; i < argc;) {
const std::string arg(argv[i++]);

Expand Down
1 change: 1 addition & 0 deletions src/frontend/cxx/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class CLI {
public:
CLI();

std::string app_name;
bool opt_ast_dump = false;
bool opt_ir_dump = false;
bool opt_dM = false;
Expand Down
17 changes: 17 additions & 0 deletions src/frontend/cxx/frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <cxx/macos_toolchain.h>
#include <cxx/preprocessor.h>
#include <cxx/private/format.h>
#include <cxx/private/path.h>
#include <cxx/recursive_ast_visitor.h>
#include <cxx/scope.h>
#include <cxx/symbol_printer.h>
Expand Down Expand Up @@ -245,6 +246,22 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {

if (auto paths = cli.get("--sysroot"); !paths.empty()) {
wasmToolchain->setSysroot(paths.back());
} else {
#if __wasi__
wasmToolchain->setSysroot("/wasi-sysroot");
#elif __unix__
char* app_name = realpath(cli.app_name.c_str(), nullptr);

const fs::path app_dir = fs::path(app_name).remove_filename();
wasmToolchain->setAppdir(app_dir.string());

const auto sysroot_dir = app_dir / "../lib/wasi-sysroot";
wasmToolchain->setSysroot(sysroot_dir.string());

if (app_name) {
std::free(app_name);
}
#endif
}

toolchain = std::move(wasmToolchain);
Expand Down
23 changes: 23 additions & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2023 Roberto Raggi <roberto.raggi@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

add_custom_target(link_wasi_sysroot ALL
COMMAND ${CMAKE_COMMAND} -E create_symlink ${wasi_sysroot_SOURCE_DIR} wasi-sysroot)

add_subdirectory(cxx)
21 changes: 21 additions & 0 deletions src/lib/cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2023 Roberto Raggi <roberto.raggi@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

add_custom_target(link_cxx_include ALL
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_SOURCE_DIR}/include include)
4 changes: 4 additions & 0 deletions src/lib/cxx/include/stdarg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

typedef __builtin_va_list va_list;
typedef __builtin_va_list __gnuc_va_list;
13 changes: 13 additions & 0 deletions src/lib/cxx/include/stddef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

typedef long int ptrdiff_t;
typedef long unsigned int size_t;
typedef long unsigned int rsize_t;

typedef struct {
long long __clang_max_align_nonce1
__attribute__((__aligned__(__alignof__(long long))));

long double __clang_max_align_nonce2
__attribute__((__aligned__(__alignof__(long double))));
} max_align_t;
21 changes: 11 additions & 10 deletions src/parser/cxx/wasm32_wasi_toolchain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

namespace cxx {

const std::string& Wasm32WasiToolchain::appdir() const { return appdir_; }

void Wasm32WasiToolchain::setAppdir(std::string appdir) {
appdir_ = std::move(appdir);
}

const std::string& Wasm32WasiToolchain::sysroot() const { return sysroot_; }

void Wasm32WasiToolchain::setSysroot(std::string sysroot) {
Expand All @@ -34,16 +40,11 @@ void Wasm32WasiToolchain::setSysroot(std::string sysroot) {
void Wasm32WasiToolchain::addSystemIncludePaths() {
addSystemIncludePath(fmt::format("{}/include", sysroot_));

for (int version : {17, 16, 15, 14, 13, 12, 11, 10}) {
const auto path =
fs::path(fmt::format("/usr/lib/clang/{}/include", version));

if (exists(path)) {
version_ = version;
addSystemIncludePath(path.string());
break;
}
}
#if __wasi__
addSystemIncludePath(fmt::format("/usr/lib/cxx/include", appdir_));
#else
addSystemIncludePath(fmt::format("{}/../lib/cxx/include", appdir_));
#endif
}

void Wasm32WasiToolchain::addSystemCppIncludePaths() {
Expand Down
4 changes: 4 additions & 0 deletions src/parser/cxx/wasm32_wasi_toolchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Wasm32WasiToolchain final : public Toolchain {
public:
using Toolchain::Toolchain;

const std::string& appdir() const;
void setAppdir(std::string appdir);

const std::string& sysroot() const;
void setSysroot(std::string sysroot);

Expand All @@ -39,6 +42,7 @@ class Wasm32WasiToolchain final : public Toolchain {
void addPredefinedMacros() override;

private:
std::string appdir_;
std::string sysroot_;
std::optional<int> version_;
};
Expand Down

0 comments on commit e82a63d

Please sign in to comment.