From a2c521daccc6a6579f30ca124c3f7cb894048a2c Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Fri, 1 Nov 2024 19:01:56 +0100 Subject: [PATCH 1/5] chore: Remove leftover --- src/frontend/cxx/lsp_server.cc | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/frontend/cxx/lsp_server.cc b/src/frontend/cxx/lsp_server.cc index 6c26e0f8..d0379091 100644 --- a/src/frontend/cxx/lsp_server.cc +++ b/src/frontend/cxx/lsp_server.cc @@ -83,14 +83,7 @@ void Server::Text::computeLineStartOffsets() { } Server::Server(const CLI& cli) - : cli(cli), input(std::cin), output(std::cout), log(std::cerr) { - // create workers - const auto workerCount = 4; - - auto worker = [] { - - }; -} + : cli(cli), input(std::cin), output(std::cout), log(std::cerr) {} Server::~Server() {} @@ -138,7 +131,7 @@ auto Server::nextRequest() -> std::optional { if (it == headers.end()) { return std::nullopt; - }; + } const auto contentLength = std::stoi(it->second); From 59f860f370eeb4e60439ab27a6da9f4532106f4c Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Fri, 1 Nov 2024 19:05:58 +0100 Subject: [PATCH 2/5] fix: Set the offset of the EOF token to the end of the main file --- src/parser/cxx/preprocessor.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/parser/cxx/preprocessor.cc b/src/parser/cxx/preprocessor.cc index bd39b079..21006613 100644 --- a/src/parser/cxx/preprocessor.cc +++ b/src/parser/cxx/preprocessor.cc @@ -2905,8 +2905,14 @@ void Preprocessor::beginPreprocessing(std::string source, std::string fileName, void Preprocessor::endPreprocessing(std::vector &tokens) { if (tokens.empty()) return; - auto sourceFileId = tokens.back().fileId(); - tokens.emplace_back(TokenKind::T_EOF_SYMBOL, sourceFileId); + + // assume the main source file is the first one + const auto mainSourceFileId = 1; + + // place the EOF token at the end of the main source file + const auto offset = d->sourceFiles_[mainSourceFileId - 1]->source.size(); + auto &tk = tokens.emplace_back(TokenKind::T_EOF_SYMBOL, offset); + tk.setFileId(mainSourceFileId); } auto Preprocessor::continuePreprocessing(std::vector &tokens) -> Status { From f08f91e9511843bf4eb59815a35e61f3cfb60638 Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Fri, 1 Nov 2024 19:26:27 +0100 Subject: [PATCH 3/5] chore: Simplify APIs to resolve source locations --- .../cxx-gen-ast/src/gen_ast_encoder_cc.ts | 18 +++-- src/frontend/cxx/cxx_document.cc | 22 ++----- src/frontend/cxx/verify_diagnostics_client.cc | 21 +++--- src/js/cxx/api.cc | 41 ++++-------- src/parser/cxx/diagnostics_client.cc | 15 ++--- src/parser/cxx/flatbuffers/ast_encoder.cc | 18 +++-- src/parser/cxx/preprocessor.cc | 66 ++++++++----------- src/parser/cxx/preprocessor.h | 10 +-- src/parser/cxx/source_location.h | 9 +++ src/parser/cxx/translation_unit.cc | 14 ++-- src/parser/cxx/translation_unit.h | 10 ++- 11 files changed, 100 insertions(+), 144 deletions(-) diff --git a/packages/cxx-gen-ast/src/gen_ast_encoder_cc.ts b/packages/cxx-gen-ast/src/gen_ast_encoder_cc.ts index 7de53438..af46e941 100644 --- a/packages/cxx-gen-ast/src/gen_ast_encoder_cc.ts +++ b/packages/cxx-gen-ast/src/gen_ast_encoder_cc.ts @@ -264,36 +264,34 @@ auto ASTEncoder::encodeSourceLocation(const SourceLocation& loc) return {}; } - std::string_view fileName; - std::uint32_t line = 0, column = 0; - unit_->getTokenStartPosition(loc, &line, &column, &fileName); + const auto start = unit_->tokenStartPosition(loc); flatbuffers::Offset sourceLineOffset; - auto key = std::tuple(fileName, line); + auto key = std::tuple(start.fileName, start.line); if (sourceLines_.contains(key)) { sourceLineOffset = sourceLines_.at(key).o; } else { flatbuffers::Offset fileNameOffset; - if (sourceFiles_.contains(fileName)) { - fileNameOffset = sourceFiles_.at(fileName); + if (sourceFiles_.contains(start.fileName)) { + fileNameOffset = sourceFiles_.at(start.fileName); } else { - fileNameOffset = fbb_.CreateString(fileName); - sourceFiles_.emplace(fileName, fileNameOffset.o); + fileNameOffset = fbb_.CreateString(start.fileName); + sourceFiles_.emplace(start.fileName, fileNameOffset.o); } io::SourceLineBuilder sourceLineBuilder{fbb_}; sourceLineBuilder.add_file_name(fileNameOffset); - sourceLineBuilder.add_line(line); + sourceLineBuilder.add_line(start.line); sourceLineOffset = sourceLineBuilder.Finish(); sourceLines_.emplace(std::move(key), sourceLineOffset.o); } io::SourceLocationBuilder sourceLocationBuilder{fbb_}; sourceLocationBuilder.add_source_line(sourceLineOffset); - sourceLocationBuilder.add_column(column); + sourceLocationBuilder.add_column(start.column); auto offset = sourceLocationBuilder.Finish(); diff --git a/src/frontend/cxx/cxx_document.cc b/src/frontend/cxx/cxx_document.cc index 45817c13..c5947505 100644 --- a/src/frontend/cxx/cxx_document.cc +++ b/src/frontend/cxx/cxx_document.cc @@ -47,27 +47,17 @@ struct Diagnostics final : cxx::DiagnosticsClient { Vector diagnostics{messages}; void report(const cxx::Diagnostic& diag) override { - std::string_view fileName; - std::uint32_t line = 0; - std::uint32_t column = 0; - - preprocessor()->getTokenStartPosition(diag.token(), &line, &column, - &fileName); - - std::uint32_t endLine = 0; - std::uint32_t endColumn = 0; - - preprocessor()->getTokenEndPosition(diag.token(), &endLine, &endColumn, - nullptr); + auto start = preprocessor()->tokenStartPosition(diag.token()); + auto end = preprocessor()->tokenEndPosition(diag.token()); auto tmp = json::object(); auto d = diagnostics.emplace_back(); - int s = std::max(int(line) - 1, 0); - int sc = std::max(int(column) - 1, 0); - int e = std::max(int(endLine) - 1, 0); - int ec = std::max(int(endColumn) - 1, 0); + int s = std::max(int(start.line) - 1, 0); + int sc = std::max(int(start.column) - 1, 0); + int e = std::max(int(end.line) - 1, 0); + int ec = std::max(int(end.column) - 1, 0); d.message(diag.message()); d.range().start(lsp::Position(tmp).line(s).character(sc)); diff --git a/src/frontend/cxx/verify_diagnostics_client.cc b/src/frontend/cxx/verify_diagnostics_client.cc index 265d3dea..e558450b 100644 --- a/src/frontend/cxx/verify_diagnostics_client.cc +++ b/src/frontend/cxx/verify_diagnostics_client.cc @@ -20,6 +20,8 @@ #include "verify_diagnostics_client.h" +#include + namespace cxx { auto VerifyDiagnosticsClient::verify() const -> bool { return verify_; } @@ -46,11 +48,8 @@ void VerifyDiagnosticsClient::handleComment(Preprocessor* preprocessor, return; } - std::string_view fileName; - unsigned line = 0; - unsigned column = 0; - - preprocessor->getTokenStartPosition(token, &line, &column, &fileName); + const auto pos = preprocessor->tokenStartPosition(token); + auto line = pos.line; Severity severity = Severity::Error; @@ -67,7 +66,7 @@ void VerifyDiagnosticsClient::handleComment(Preprocessor* preprocessor, const auto& message = match[3]; expectedDiagnostics_.push_back( - {token, severity, std::string(fileName), message, line}); + {token, severity, std::string(pos.fileName), message, line}); } auto VerifyDiagnosticsClient::hasErrors() const -> bool { @@ -121,15 +120,11 @@ auto VerifyDiagnosticsClient::findDiagnostic(const ExpectedDiagnostic& expected) return false; } - unsigned line = 0; - unsigned column = 0; - std::string_view fileName; - - preprocessor()->getTokenStartPosition(d.token(), &line, &column, &fileName); + const auto pos = preprocessor()->tokenStartPosition(d.token()); - if (line != expected.line) return false; + if (pos.line != expected.line) return false; - if (fileName != expected.fileName) return false; + if (pos.fileName != expected.fileName) return false; if (d.message() != expected.message) return false; diff --git a/src/js/cxx/api.cc b/src/js/cxx/api.cc index ccf2b3d9..337ddd58 100644 --- a/src/js/cxx/api.cc +++ b/src/js/cxx/api.cc @@ -44,25 +44,15 @@ struct DiagnosticsClient final : cxx::DiagnosticsClient { val messages = val::array(); void report(const cxx::Diagnostic& diag) override { - std::string_view fileName; - std::uint32_t line = 0; - std::uint32_t column = 0; - - preprocessor()->getTokenStartPosition(diag.token(), &line, &column, - &fileName); - - std::uint32_t endLine = 0; - std::uint32_t endColumn = 0; - - preprocessor()->getTokenEndPosition(diag.token(), &endLine, &endColumn, - nullptr); + const auto start = preprocessor()->tokenStartPosition(diag.token()); + const auto end = preprocessor()->tokenEndPosition(diag.token()); val d = val::object(); - d.set("fileName", val(std::string(fileName))); - d.set("startLine", val(line)); - d.set("startColumn", val(column)); - d.set("endLine", val(endLine)); - d.set("endColumn", val(endColumn)); + d.set("fileName", val(std::string(start.fileName))); + d.set("startLine", val(start.line)); + d.set("startColumn", val(start.column)); + d.set("endLine", val(end.line)); + d.set("endColumn", val(end.column)); d.set("message", val(diag.message())); messages.call("push", d); } @@ -113,20 +103,15 @@ val getTokenLocation(std::intptr_t handle, std::intptr_t unitHandle) { cxx::SourceLocation loc(handle); - unsigned startLine = 0, startColumn = 0; - - unit->getTokenStartPosition(loc, &startLine, &startColumn); - - unsigned endLine = 0, endColumn = 0; - - unit->getTokenEndPosition(loc, &endLine, &endColumn); + const auto start = unit->tokenStartPosition(loc); + const auto end = unit->tokenEndPosition(loc); val result = val::object(); - result.set("startLine", startLine); - result.set("startColumn", startColumn); - result.set("endLine", endLine); - result.set("endColumn", endColumn); + result.set("startLine", start.line); + result.set("startColumn", start.column); + result.set("endLine", end.line); + result.set("endColumn", end.column); return result; } diff --git a/src/parser/cxx/diagnostics_client.cc b/src/parser/cxx/diagnostics_client.cc index 309e73e5..ae618dbe 100644 --- a/src/parser/cxx/diagnostics_client.cc +++ b/src/parser/cxx/diagnostics_client.cc @@ -22,6 +22,7 @@ // cxx #include +#include #include #include @@ -50,19 +51,15 @@ void DiagnosticsClient::report(const Diagnostic& diag) { break; } // switch - std::string_view fileName; - std::uint32_t line = 0; - std::uint32_t column = 0; + const auto pos = preprocessor_->tokenStartPosition(diag.token()); - preprocessor_->getTokenStartPosition(diag.token(), &line, &column, &fileName); - - if (!fileName.empty()) { - std::cerr << std::format("{}:{}:{}: {}\n", fileName, line, column, - diag.message()); + if (!pos.fileName.empty()) { + std::cerr << std::format("{}:{}:{}: {}\n", pos.fileName, pos.line, + pos.column, diag.message()); const auto textLine = preprocessor_->getTextLine(diag.token()); - const auto end = std::max(0, static_cast(column) - 1); + const auto end = std::max(0, static_cast(pos.column) - 1); std::string indent{textLine.substr(0, end)}; diff --git a/src/parser/cxx/flatbuffers/ast_encoder.cc b/src/parser/cxx/flatbuffers/ast_encoder.cc index f9525e24..6dfabd11 100644 --- a/src/parser/cxx/flatbuffers/ast_encoder.cc +++ b/src/parser/cxx/flatbuffers/ast_encoder.cc @@ -90,36 +90,34 @@ auto ASTEncoder::encodeSourceLocation(const SourceLocation& loc) return {}; } - std::string_view fileName; - std::uint32_t line = 0, column = 0; - unit_->getTokenStartPosition(loc, &line, &column, &fileName); + const auto start = unit_->tokenStartPosition(loc); flatbuffers::Offset sourceLineOffset; - auto key = std::tuple(fileName, line); + auto key = std::tuple(start.fileName, start.line); if (sourceLines_.contains(key)) { sourceLineOffset = sourceLines_.at(key).o; } else { flatbuffers::Offset fileNameOffset; - if (sourceFiles_.contains(fileName)) { - fileNameOffset = sourceFiles_.at(fileName); + if (sourceFiles_.contains(start.fileName)) { + fileNameOffset = sourceFiles_.at(start.fileName); } else { - fileNameOffset = fbb_.CreateString(fileName); - sourceFiles_.emplace(fileName, fileNameOffset.o); + fileNameOffset = fbb_.CreateString(start.fileName); + sourceFiles_.emplace(start.fileName, fileNameOffset.o); } io::SourceLineBuilder sourceLineBuilder{fbb_}; sourceLineBuilder.add_file_name(fileNameOffset); - sourceLineBuilder.add_line(line); + sourceLineBuilder.add_line(start.line); sourceLineOffset = sourceLineBuilder.Finish(); sourceLines_.emplace(std::move(key), sourceLineOffset.o); } io::SourceLocationBuilder sourceLocationBuilder{fbb_}; sourceLocationBuilder.add_source_line(sourceLineOffset); - sourceLocationBuilder.add_column(column); + sourceLocationBuilder.add_column(start.column); auto offset = sourceLocationBuilder.Finish(); diff --git a/src/parser/cxx/preprocessor.cc b/src/parser/cxx/preprocessor.cc index 21006613..bb19e9c5 100644 --- a/src/parser/cxx/preprocessor.cc +++ b/src/parser/cxx/preprocessor.cc @@ -1313,11 +1313,9 @@ void Preprocessor::Private::initialize() { "__LINE__", [this](const MacroExpansionContext &context) -> const TokList * { auto ts = context.ts; - unsigned line = 0; - preprocessor_->getTokenStartPosition(ts->tok->token(), &line, nullptr, - nullptr); - auto tk = - gen(TokenKind::T_INTEGER_LITERAL, string(std::to_string(line))); + const auto start = preprocessor_->tokenStartPosition(ts->tok->token()); + auto tk = gen(TokenKind::T_INTEGER_LITERAL, + string(std::to_string(start.line))); tk->sourceFile = ts->tok->sourceFile; tk->space = true; return cons(tk, ts->next); @@ -2936,15 +2934,8 @@ void Preprocessor::getPreprocessedText(const std::vector &tokens, if (lastFileId != std::numeric_limits::max()) { out << '\n'; } - const auto &sourceFile = *d->sourceFiles_[fileId - 1]; - std::uint32_t line = 0, column = 0; - getTokenStartPosition(token, &line, &column, nullptr); -#if true - out << std::format("# {} \"{}\"\n", line, sourceFile.fileName); -#else - out << std::format("# {} \"{}:{}:{}\"\n", line, sourceFile.fileName, line, - column); -#endif + const auto pos = tokenStartPosition(token); + out << std::format("# {} \"{}\"\n", pos.line, pos.fileName); lastFileId = fileId; atStartOfLine = true; } else if (token.startOfLine()) { @@ -2968,10 +2959,9 @@ void Preprocessor::getPreprocessedText(const std::vector &tokens, } if (atStartOfLine) { - std::uint32_t line = 0, column = 0; - getTokenStartPosition(token, &line, &column, nullptr); - if (column > 0) { - for (std::uint32_t i = 0; i < column - 1; ++i) { + const auto pos = tokenStartPosition(token); + if (pos.column > 0) { + for (std::uint32_t i = 0; i < pos.column - 1; ++i) { out << ' '; } } @@ -3059,44 +3049,42 @@ void Preprocessor::printMacros(std::ostream &out) const { } } -void Preprocessor::getTokenStartPosition(const Token &token, unsigned *line, - unsigned *column, - std::string_view *fileName) const { +auto Preprocessor::tokenStartPosition(const Token &token) const + -> SourcePosition { if (token.fileId() == 0) { - if (line) *line = 0; - if (column) *column = 0; - if (fileName) *fileName = std::string_view(); - return; + return {}; } auto &sourceFile = *d->sourceFiles_[token.fileId() - 1]; - sourceFile.getTokenStartPosition(token.offset(), line, column, fileName); + SourcePosition pos; + sourceFile.getTokenStartPosition(token.offset(), &pos.line, &pos.column, + &pos.fileName); + return pos; } -void Preprocessor::getTokenEndPosition(const Token &token, unsigned *line, - unsigned *column, - std::string_view *fileName) const { +auto Preprocessor::tokenEndPosition(const Token &token) const + -> SourcePosition { if (token.fileId() == 0) { - if (line) *line = 0; - if (column) *column = 0; - if (fileName) *fileName = std::string_view(); - return; + return {}; } auto &sourceFile = *d->sourceFiles_[token.fileId() - 1]; - sourceFile.getTokenStartPosition(token.offset() + token.length(), line, - column, fileName); + + SourcePosition pos; + sourceFile.getTokenStartPosition(token.offset() + token.length(), &pos.line, + &pos.column, &pos.fileName); + return pos; } auto Preprocessor::getTextLine(const Token &token) const -> std::string_view { if (token.fileId() == 0) return {}; const SourceFile *file = d->sourceFiles_[token.fileId() - 1].get(); - unsigned line = 0; - getTokenStartPosition(token, &line, nullptr, nullptr); + const auto pos = tokenStartPosition(token); std::string_view source = file->source; const auto &lines = file->lines; - const auto start = lines.at(line - 1); - const auto end = line < lines.size() ? lines.at(line) : source.length(); + const auto start = lines.at(pos.line - 1); + const auto end = + pos.line < lines.size() ? lines.at(pos.line) : source.length(); auto textLine = source.substr(start, end - start); while (!textLine.empty()) { auto ch = textLine.back(); diff --git a/src/parser/cxx/preprocessor.h b/src/parser/cxx/preprocessor.h index 1a9d542e..5fb5cf1f 100644 --- a/src/parser/cxx/preprocessor.h +++ b/src/parser/cxx/preprocessor.h @@ -37,6 +37,7 @@ class DiagnosticsClient; class CommentHandler; class Preprocessor; class PreprocessorDelegate; +class SourcePosition; class CommentHandler { public: @@ -135,12 +136,11 @@ class Preprocessor { void printMacros(std::ostream &out) const; - void getTokenStartPosition(const Token &token, unsigned *line, - unsigned *column, - std::string_view *fileName) const; + [[nodiscard]] auto tokenStartPosition(const Token &token) const + -> SourcePosition; - void getTokenEndPosition(const Token &token, unsigned *line, unsigned *column, - std::string_view *fileName) const; + [[nodiscard]] auto tokenEndPosition(const Token &token) const + -> SourcePosition; [[nodiscard]] auto getTextLine(const Token &token) const -> std::string_view; diff --git a/src/parser/cxx/source_location.h b/src/parser/cxx/source_location.h index d293403b..480f9e8e 100644 --- a/src/parser/cxx/source_location.h +++ b/src/parser/cxx/source_location.h @@ -20,7 +20,9 @@ #pragma once +#include #include +#include #include namespace cxx { @@ -71,6 +73,13 @@ class SourceLocation { } }; +class SourcePosition { + public: + std::string_view fileName; + std::uint32_t line = 0; + std::uint32_t column = 0; +}; + } // namespace cxx template <> diff --git a/src/parser/cxx/translation_unit.cc b/src/parser/cxx/translation_unit.cc index 7ec38ccf..5058937e 100644 --- a/src/parser/cxx/translation_unit.cc +++ b/src/parser/cxx/translation_unit.cc @@ -108,16 +108,14 @@ auto TranslationUnit::tokenText(SourceLocation loc) const } // switch } -void TranslationUnit::getTokenStartPosition(SourceLocation loc, unsigned* line, - unsigned* column, - std::string_view* fileName) const { - preprocessor_->getTokenStartPosition(tokenAt(loc), line, column, fileName); +auto TranslationUnit::tokenStartPosition(SourceLocation loc) const + -> SourcePosition { + return preprocessor_->tokenStartPosition(tokenAt(loc)); } -void TranslationUnit::getTokenEndPosition(SourceLocation loc, unsigned* line, - unsigned* column, - std::string_view* fileName) const { - preprocessor_->getTokenEndPosition(tokenAt(loc), line, column, fileName); +auto TranslationUnit::tokenEndPosition(SourceLocation loc) const + -> SourcePosition { + return preprocessor_->tokenEndPosition(tokenAt(loc)); } void TranslationUnit::parse(const ParserConfiguration& config) { diff --git a/src/parser/cxx/translation_unit.h b/src/parser/cxx/translation_unit.h index e4924ea1..9e8ed5d6 100644 --- a/src/parser/cxx/translation_unit.h +++ b/src/parser/cxx/translation_unit.h @@ -117,13 +117,11 @@ class TranslationUnit { [[nodiscard]] auto tokenText(SourceLocation loc) const -> const std::string&; - void getTokenStartPosition(SourceLocation loc, unsigned* line, - unsigned* column = nullptr, - std::string_view* fileName = nullptr) const; + [[nodiscard]] auto tokenStartPosition(SourceLocation loc) const + -> SourcePosition; - void getTokenEndPosition(SourceLocation loc, unsigned* line, - unsigned* column = nullptr, - std::string_view* fileName = nullptr) const; + [[nodiscard]] auto tokenEndPosition(SourceLocation loc) const + -> SourcePosition; [[nodiscard]] auto identifier(SourceLocation loc) const -> const Identifier*; From 04b1d37dbb219c10a378b3d48c15a2040a52073b Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Fri, 1 Nov 2024 19:45:49 +0100 Subject: [PATCH 4/5] chore: Add support for wasm32-wasi-pthread target --- .github/workflows/ci.yml | 2 ++ CMakePresets.json | 35 +++++++++++++++++++++++++++++------ scripts/cxx-wasmtime.mjs | 6 +++++- src/frontend/CMakeLists.txt | 8 ++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f2b91baf..8c25a3cd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -194,6 +194,7 @@ jobs: - name: Test run: | wasmtime \ + -W threads=y -S threads=y \ --dir ${{github.workspace}}/build.wasi/install::/ \ --dir tests::tests \ ${{github.workspace}}/build.wasi/install/usr/bin/cxx.wasm -v tests/manual/source.cc @@ -205,6 +206,7 @@ jobs: for i in src/parser/cxx/*.cc src/lsp/cxx/lsp/*.cc src/frontend/cxx/*.cc; do echo "Parsing $i" wasmtime \ + -W threads=y -S threads=y \ --dir=src::/src \ --dir=build.wasi/_deps::/build.wasi/_deps \ --dir=build.wasi/src/parser::build.wasi/src/parser \ diff --git a/CMakePresets.json b/CMakePresets.json index 0f4e6c24..f7a2eaee 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -28,9 +28,9 @@ "displayName": "emscripten", "generator": "Ninja", "binaryDir": "${sourceDir}/build.em", + "toolchainFile": "$env{EMSCRIPTEN_ROOT}/cmake/Modules/Platform/Emscripten.cmake", "cacheVariables": { "CMAKE_EXPORT_COMPILE_COMMANDS": "YES", - "CMAKE_TOOLCHAIN_FILE": "$env{EMSCRIPTEN_ROOT}/cmake/Modules/Platform/Emscripten.cmake", "CMAKE_BUILD_TYPE": "Release", "CMAKE_INSTALL_PREFIX": "${sourceDir}/build.em/install/usr", "CMAKE_INTERPROCEDURAL_OPTIMIZATION": "YES", @@ -48,9 +48,9 @@ "displayName": "wasi", "generator": "Ninja", "binaryDir": "${sourceDir}/build.wasi", + "toolchainFile": "$env{WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake", "cacheVariables": { "CMAKE_EXPORT_COMPILE_COMMANDS": "YES", - "CMAKE_TOOLCHAIN_FILE": "$env{WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake", "CMAKE_BUILD_TYPE": "Release", "CMAKE_INSTALL_PREFIX": "${sourceDir}/build.wasi/install/usr", "CMAKE_INTERPROCEDURAL_OPTIMIZATION": "YES", @@ -62,6 +62,11 @@ "lhs": "$env{WASI_SDK_PATH}", "rhs": "" } + }, + { + "name": "wasi-threads", + "inherits": "wasi", + "toolchainFile": "$env{WASI_SDK_PATH}/share/cmake/wasi-sdk-pthread.cmake" } ], "buildPresets": [ @@ -95,6 +100,15 @@ "name": "install-wasi", "configurePreset": "wasi", "targets": ["install"] + }, + { + "name": "build-wasi-threads", + "configurePreset": "wasi-threads" + }, + { + "name": "install-wasi-threads", + "configurePreset": "wasi-threads", + "targets": ["install"] } ], "testPresets": [ @@ -109,10 +123,6 @@ { "name": "emscripten", "configurePreset": "emscripten" - }, - { - "name": "wasi", - "configurePreset": "wasi" } ], "workflowPresets": [ @@ -166,6 +176,19 @@ "name": "install-wasi" } ] + }, + { + "name": "wasi-threads", + "steps": [ + { + "type": "configure", + "name": "wasi-threads" + }, + { + "type": "build", + "name": "install-wasi-threads" + } + ] } ] } diff --git a/scripts/cxx-wasmtime.mjs b/scripts/cxx-wasmtime.mjs index 306e4e2c..6ce9b1e7 100755 --- a/scripts/cxx-wasmtime.mjs +++ b/scripts/cxx-wasmtime.mjs @@ -28,6 +28,10 @@ const cxxWasiPrefixPath = path.join(workspacePath, "build.wasi/install"); const wasmtime = await which("wasmtime"); const cxxArgs = [ + "-W", + "threads=y", + "-S", + "threads=y", `--dir=${cxxWasiPrefixPath}/usr::/usr`, `--dir=${process.cwd()}::/`, `${cxxWasiPrefixPath}/usr/bin/cxx.wasm`, @@ -35,7 +39,7 @@ const cxxArgs = [ try { const result = await $`${wasmtime} ${cxxArgs} ${process.argv.slice( - 3, + 3 )}`.quiet(); echo`${result}`; diff --git a/src/frontend/CMakeLists.txt b/src/frontend/CMakeLists.txt index 8279a8cd..d2257eb4 100644 --- a/src/frontend/CMakeLists.txt +++ b/src/frontend/CMakeLists.txt @@ -45,6 +45,14 @@ endif() if (CMAKE_SYSTEM_NAME STREQUAL "WASI") set_target_properties(cxx PROPERTIES SUFFIX ".wasm") + + if (Threads_FOUND) + target_link_options(cxx PRIVATE + "SHELL:-Wl,--import-memory" + "SHELL:-Wl,--export-memory" + "SHELL:-Wl,--max-memory=134217728" + ) + endif() endif() if(CXX_INTERPROCEDURAL_OPTIMIZATION) From 6c3f8a3093b49b03068785b4aca7981d1f8da30f Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Fri, 1 Nov 2024 19:49:25 +0100 Subject: [PATCH 5/5] chore: Update emsdk base image to version 3.1.70 Signed-off-by: Roberto Raggi --- Dockerfile.emsdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile.emsdk b/Dockerfile.emsdk index c8f1839d..a0e60b3f 100644 --- a/Dockerfile.emsdk +++ b/Dockerfile.emsdk @@ -1,4 +1,4 @@ -FROM emscripten/emsdk:3.1.69 as em +FROM emscripten/emsdk:3.1.70 as em RUN apt-get update && apt-get install -y \ ninja-build \