diff --git a/CMakeLists.txt b/CMakeLists.txt index a26fe2e2..bbb23dec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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( diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 00000000..121817df --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,25 @@ +# Copyright (c) 2023 Roberto Raggi +# +# 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) diff --git a/src/frontend/CMakeLists.txt b/src/frontend/CMakeLists.txt index 509c9409..d2cb9d25 100644 --- a/src/frontend/CMakeLists.txt +++ b/src/frontend/CMakeLists.txt @@ -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) diff --git a/src/frontend/cxx/cli.cc b/src/frontend/cxx/cli.cc index cc8946c6..b7a9c79b 100644 --- a/src/frontend/cxx/cli.cc +++ b/src/frontend/cxx/cli.cc @@ -214,6 +214,8 @@ auto CLI::positionals() const -> std::vector { } void CLI::parse(int& argc, char**& argv) { + app_name = argv[0]; + for (int i = 1; i < argc;) { const std::string arg(argv[i++]); diff --git a/src/frontend/cxx/cli.h b/src/frontend/cxx/cli.h index df5d96e0..94190219 100644 --- a/src/frontend/cxx/cli.h +++ b/src/frontend/cxx/cli.h @@ -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; diff --git a/src/frontend/cxx/frontend.cc b/src/frontend/cxx/frontend.cc index 3c3f4c54..30fdfb3e 100644 --- a/src/frontend/cxx/frontend.cc +++ b/src/frontend/cxx/frontend.cc @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -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); diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt new file mode 100644 index 00000000..844fb4a5 --- /dev/null +++ b/src/lib/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright (c) 2023 Roberto Raggi +# +# 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) diff --git a/src/lib/cxx/CMakeLists.txt b/src/lib/cxx/CMakeLists.txt new file mode 100644 index 00000000..562f46ef --- /dev/null +++ b/src/lib/cxx/CMakeLists.txt @@ -0,0 +1,21 @@ +# Copyright (c) 2023 Roberto Raggi +# +# 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) diff --git a/src/lib/cxx/include/stdarg.h b/src/lib/cxx/include/stdarg.h new file mode 100644 index 00000000..47622a63 --- /dev/null +++ b/src/lib/cxx/include/stdarg.h @@ -0,0 +1,4 @@ +#pragma once + +typedef __builtin_va_list va_list; +typedef __builtin_va_list __gnuc_va_list; diff --git a/src/lib/cxx/include/stddef.h b/src/lib/cxx/include/stddef.h new file mode 100644 index 00000000..69cf20bf --- /dev/null +++ b/src/lib/cxx/include/stddef.h @@ -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; diff --git a/src/parser/cxx/wasm32_wasi_toolchain.cc b/src/parser/cxx/wasm32_wasi_toolchain.cc index 6d964c37..d2a85dcd 100644 --- a/src/parser/cxx/wasm32_wasi_toolchain.cc +++ b/src/parser/cxx/wasm32_wasi_toolchain.cc @@ -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) { @@ -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() { diff --git a/src/parser/cxx/wasm32_wasi_toolchain.h b/src/parser/cxx/wasm32_wasi_toolchain.h index 922d3b68..0980a71b 100644 --- a/src/parser/cxx/wasm32_wasi_toolchain.h +++ b/src/parser/cxx/wasm32_wasi_toolchain.h @@ -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); @@ -39,6 +42,7 @@ class Wasm32WasiToolchain final : public Toolchain { void addPredefinedMacros() override; private: + std::string appdir_; std::string sysroot_; std::optional version_; };