Skip to content

Commit

Permalink
Add Emscripten support to CMakeLists as much as possible
Browse files Browse the repository at this point in the history
Only need to set Emscripten's toolchain file and all tests and benchmarks work normally.
  • Loading branch information
alugowski committed Jan 25, 2024
1 parent 0bb0dcd commit f3ea1b7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
15 changes: 2 additions & 13 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,9 @@ jobs:
test-amalgamation: true

- os: ubuntu-latest
description: "Emscripten"
description: "Emscripten 3.1"
emscripten-version: "3.1.52"
# needs:
# - use emscripten's cmake toolchain file so cmake uses em++ and such
# - must compile with -pthread for C++11 threads to work
#
# See https://emscripten.org/docs/porting/pthreads.html
#
# poolSTL test-suite specific options:
# - using -sNO_DISABLE_EXCEPTION_CATCHING because some tests throw
# - using high -sTOTAL_MEMORY because our benchmarks use large arrays
# - disable codecov because emscripten does not support it.
# - Consider -DCMAKE_EXE_LINKER_FLAGS="-sPTHREAD_POOL_SIZE=navigator.hardwareConcurrency"
cmake-flags: '-DCMAKE_TOOLCHAIN_FILE=$(em-config EMSCRIPTEN_ROOT)/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CXX_FLAGS="-pthread -sNO_DISABLE_EXCEPTION_CATCHING" -DCMAKE_EXE_LINKER_FLAGS="-sTOTAL_MEMORY=2048MB" -DPOOLSTL_TEST_COVERAGE=OFF'
cmake-flags: '-DCMAKE_TOOLCHAIN_FILE=$(em-config EMSCRIPTEN_ROOT)/cmake/Modules/Platform/Emscripten.cmake'

- os: ubuntu-latest
# default GCC, which has gcov
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ target_compile_features(poolSTL INTERFACE cxx_std_11)
find_package(Threads)
target_link_libraries(poolSTL INTERFACE Threads::Threads)

if (EMSCRIPTEN)
# Emscripten needs -pthread compiler and linker switch for C++11 threads.
# Anything that will link against poolSTL also needs to be compiled with -pthread.
add_compile_options("-pthread")
target_compile_options(poolSTL INTERFACE "-pthread")
target_link_options(poolSTL INTERFACE "-pthread")
endif()

###############################################

# Tests
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,15 @@ std::vector<int> vec = {5, 2, 1, 3, 0, 4};
std::sort(poolstl::par, vec.begin(), vec.end());
```

## Installation
# Installation

### Single File

Each [release](https://github.com/alugowski/poolSTL/releases/latest) publishes a single-file amalgamated `poolstl.hpp`. Simply copy this into your project.
**Note:** Some compilers (non-Apple Clang, GCC 8 and older) require `-lpthread` to use C++11 threads.
Each [release](https://github.com/alugowski/poolSTL/releases/latest) publishes a single-file amalgamated `poolstl.hpp`. Simply copy this into your project.

**Build requirements:**
- Clang and GCC 8 or older: require `-lpthread` to use C++11 threads.
- Emscripten: compile and link with `-pthread` to use C++11 threads. [See docs](https://emscripten.org/docs/porting/pthreads.html).

### CMake

Expand Down
8 changes: 8 additions & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,11 @@ else()
# MinGW also requires nothing to compile std::execution::par, but performance appears to be the same as sequential.
target_compile_definitions(poolstl_bench PUBLIC POOLSTL_BENCH_STD_PAR)
endif()

if (EMSCRIPTEN)
# Benchmark uses large arrays that require more than Emscripten's default 16MB.
target_link_options(poolstl_bench PUBLIC "-sTOTAL_MEMORY=2048MB")

# Recommended by docs, but does not appear to have an effect. Maybe new node.js does not need it?
# target_link_options(poolstl_bench PUBLIC "-sPTHREAD_POOL_SIZE=navigator.hardwareConcurrency")
endif()
12 changes: 11 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ endif()
# Code Coverage
option(POOLSTL_TEST_COVERAGE "Code-coverage" OFF)
if(POOLSTL_TEST_COVERAGE)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
if (EMSCRIPTEN)
# Clang's code coverage tools not supported on Emscripten (will produce link error).
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_options("--coverage")
add_link_options("-lgcov" "--coverage")
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
Expand All @@ -52,6 +54,14 @@ list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)


if (EMSCRIPTEN)
# Includes a test that throws exceptions, which are opt-in on Emscripten.
# See https://emscripten.org/docs/porting/exceptions.html
add_compile_options("-fexceptions")
add_link_options("-fexceptions")
endif()

# tests compiled with C++14 (Catch2 requires C++14)
add_executable(poolstl_test poolstl_test.cpp)
target_link_libraries(poolstl_test PRIVATE Catch2::Catch2WithMain poolSTL::poolSTL)
Expand Down

0 comments on commit f3ea1b7

Please sign in to comment.