Skip to content

Commit

Permalink
Changes to be able to build to webassembly with emscripten. (#201)
Browse files Browse the repository at this point in the history
* Changes to enable libcoro to be built with emscripten
* Removed the TL::expected dependency from submodules
* Tweaked a size test in test_task.cpp that failed in wasm as it
em++ seems to be adding padding.
  • Loading branch information
bruno-j-nicoletti authored Dec 1, 2023
1 parent d1be506 commit b3fb243
Show file tree
Hide file tree
Showing 12 changed files with 2,734 additions and 31 deletions.
4 changes: 1 addition & 3 deletions .githooks/readme-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ Completed 10000000 requests
Finished 10000000 requests


Server Software:
Server Software:
Server Hostname: 127.0.0.1
Server Port: 8888

Expand Down Expand Up @@ -412,8 +412,6 @@ This project uses git submodules, to properly checkout this project use:
This project depends on the following git sub-modules:
* [libc-ares](https://github.com/c-ares/c-ares) For async DNS resolver, this is a git submodule.
* [catch2](https://github.com/catchorg/Catch2) For testing, this is embedded in the `test/` directory.
* [expected](https://github.com/TartanLlama/expected) For results on operations that can fail, this is a git submodule in the `vendor/` directory.
* `-std=c++23` if it supports `__cpp_lib_expected` will use `std::expected` instead.

#### Building
mkdir Release && cd Release
Expand Down
49 changes: 47 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ jobs:
run: |
cd build-release-clang++-feature-networking-disabled
ctest -VV
build-fedora-32-to-37:
name: fedora-${{ matrix.fedora_version }}
runs-on: ubuntu-latest
Expand Down Expand Up @@ -281,4 +280,50 @@ jobs:
- name: test-release-cl
run: |
cd build-release-cl
ctest --build-config Release -VV
build-emscripten-3_1_45:
name: emscripten-3_1_45
runs-on: ubuntu-latest
container:
image: ubuntu:22.04
env:
TZ: America/New_York
DEBIAN_FRONTEND: noninteractive
steps:
- name: apt
run: |
apt-get update
apt-get -y upgrade
apt install -y build-essential software-properties-common
add-apt-repository ppa:ubuntu-toolchain-r/test
apt-get install -y \
cmake \
git \
ninja-build
- name: Checkout
uses: actions/checkout@v2
with:
submodules: recursive
- name: install-emsdk
run: |
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install 3.1.45
./emsdk activate 3.1.45
- name: build-release-emscripten
run: |
cd emsdk
. ./emsdk_env.sh
cd ..
mkdir build-release-emscripten
cd build-release-emscripten
emcmake cmake \
-GNinja \
-DCMAKE_BUILD_TYPE=Release \
..
ninja
- name: test-release-emscripten
run: |
cd emsdk
. ./emsdk_env.sh
cd ../build-release-emscripten
node --experimental-wasm-eh ./test/libcoro_test.js
7 changes: 2 additions & 5 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "vendor/c-ares/c-ares"]
path = vendor/c-ares/c-ares
url = https://github.com/c-ares/c-ares.git
[submodule "vendor/tartanllama/expected"]
path = vendor/tartanllama/expected
url = https://github.com/jstranik/expected.git
path = vendor/c-ares/c-ares
url = https://github.com/c-ares/c-ares.git
31 changes: 20 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ option(LIBCORO_EXTERNAL_DEPENDENCIES "Use Cmake find_package to resolve dependen
option(LIBCORO_BUILD_TESTS "Build the tests, Default=ON." ON)
option(LIBCORO_CODE_COVERAGE "Enable code coverage, tests must also be enabled, Default=OFF" OFF)
option(LIBCORO_BUILD_EXAMPLES "Build the examples, Default=ON." ON)
cmake_dependent_option(LIBCORO_FEATURE_PLATFORM "Include linux platform features, Default=ON." ON "NOT MSVC" OFF)
cmake_dependent_option(LIBCORO_FEATURE_NETWORKING "Include networking features, Default=ON." ON "NOT MSVC" OFF)
cmake_dependent_option(LIBCORO_FEATURE_SSL "Include SSL encryption features, Default=ON." ON "NOT MSVC" OFF)

cmake_dependent_option(LIBCORO_FEATURE_PLATFORM "Include linux platform features, Default=ON." ON "NOT EMSCRIPTEN; NOT MSVC" OFF)
cmake_dependent_option(LIBCORO_FEATURE_NETWORKING "Include networking features, Default=ON." ON "NOT EMSCRIPTEN; NOT MSVC" OFF)
cmake_dependent_option(LIBCORO_FEATURE_SSL "Include SSL encryption features, Default=ON." ON "NOT EMSCRIPTEN; NOT MSVC" OFF)

message("${PROJECT_NAME} LIBCORO_EXTERNAL_DEPENDENCIES = ${LIBCORO_EXTERNAL_DEPENDENCIES}")
message("${PROJECT_NAME} LIBCORO_BUILD_TESTS = ${LIBCORO_BUILD_TESTS}")
Expand All @@ -39,7 +40,6 @@ if(LIBCORO_EXTERNAL_DEPENDENCIES)
if(LIBCORO_FEATURE_NETWORKING)
find_package(c-ares CONFIG REQUIRED)
endif()
find_package(tl-expected CONFIG REQUIRED)
else()
if(NOT LIBCORO_BUILD_TESTS)
# Disable testing in expected
Expand All @@ -51,7 +51,6 @@ else()
set(CARES_INSTALL OFF CACHE INTERNAL "")
add_subdirectory(vendor/c-ares/c-ares)
endif()
add_subdirectory(vendor/tartanllama/expected)
endif()

set(LIBCORO_SOURCE_FILES
Expand Down Expand Up @@ -112,18 +111,28 @@ if(LIBCORO_FEATURE_NETWORKING)
endif()
endif()

if(DEFINED EMSCRIPTEN)
add_compile_options(-fwasm-exceptions)
add_compile_options(-pthread)
add_compile_options(-matomics)

add_link_options(-fwasm-exceptions)
add_link_options(-pthread)
add_link_options(-sPROXY_TO_PTHREAD)
add_link_options(-sALLOW_MEMORY_GROWTH)
add_link_options(-sEXIT_RUNTIME)
endif()

add_library(${PROJECT_NAME} STATIC ${LIBCORO_SOURCE_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX PREFIX "")
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
target_include_directories(${PROJECT_NAME} PUBLIC include)
if(MSVC)
# Not sure why this is only needed on Windows.
target_include_directories(${PROJECT_NAME} PUBLIC vendor/tartanllama/expected/include)
endif()

if(LIBCORO_FEATURE_PLATFORM)
target_link_libraries(${PROJECT_NAME} PUBLIC pthread tl::expected)
target_link_libraries(${PROJECT_NAME} PUBLIC pthread)
target_compile_definitions(${PROJECT_NAME} PUBLIC LIBCORO_FEATURE_PLATFORM)
endif()
endif()

if(LIBCORO_FEATURE_NETWORKING)
target_link_libraries(${PROJECT_NAME} PUBLIC c-ares::cares)
target_compile_definitions(${PROJECT_NAME} PUBLIC LIBCORO_FEATURE_NETWORKING)
Expand Down
4 changes: 3 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
target_compile_options(coro_task PUBLIC -Wall -Wextra -pipe)
target_compile_options(coro_generator PUBLIC -Wall -Wextra -pipe)
target_compile_options(coro_event PUBLIC -Wall -Wextra -pipe)
target_compile_options(coro_latch PUBLIC -Wall -Wextra -pipe)
if(LIBCORO_FEATURE_PLATFORM)
target_compile_options(coro_latch PUBLIC -Wall -Wextra -pipe)
endif()
target_compile_options(coro_mutex PUBLIC -Wall -Wextra -pipe)
target_compile_options(coro_thread_pool PUBLIC -Wall -Wextra -pipe)
target_compile_options(coro_semaphore PUBLIC -Wall -Wextra -pipe)
Expand Down
Loading

0 comments on commit b3fb243

Please sign in to comment.