Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
## About %%myproject%%
%%description%%

## WebAssembly Demo

Try the live WebAssembly demo:
- Main: [https://%%myorg%%.github.io/%%myproject%%/](https://%%myorg%%.github.io/%%myproject%%/)
- Develop: [https://%%myorg%%.github.io/%%myproject%%/develop/](https://%%myorg%%.github.io/%%myproject%%/develop/)

The `main` branch deploys to the root, `develop` to `/develop/`, and tags to `/tagname/`.


## More Details

Expand Down
59 changes: 59 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Build Intro WASM and Deploy to GitHub Pages

on:
pull_request:
release:
types: [published]
push:
branches: [main, develop]
tags: ['**']

permissions:
contents: write

jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Emscripten
uses: mymindstorm/setup-emsdk@v14
with:
version: 'latest'

- name: Install Ninja
run: sudo apt-get install -y ninja-build

- name: Configure CMake
run: emcmake cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release

- name: Build all WASM targets
run: emmake cmake --build build --target web-dist

- name: Prepare deployment
run: |
# web-dist target already created build/web-dist/
# Just copy it to dist/ for GitHub Pages action
cp -r build/web-dist dist

- name: Determine deploy path
id: deploy-path
if: github.event_name != 'pull_request' && github.event_name != 'release'
run: |
if [[ "$GITHUB_REF" == refs/tags/* ]]; then
echo "path=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
elif [[ "$GITHUB_REF" == refs/heads/main ]]; then
echo "path=." >> $GITHUB_OUTPUT
elif [[ "$GITHUB_REF" == refs/heads/develop ]]; then
echo "path=develop" >> $GITHUB_OUTPUT
fi

- name: Deploy to GitHub Pages
if: github.event_name != 'pull_request' && github.event_name != 'release'
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
destination_dir: ${{ steps.deploy-path.outputs.path }}
keep_files: true
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ project(
LANGUAGES CXX C)

include(cmake/PreventInSourceBuilds.cmake)
include(cmake/Emscripten.cmake)
include(ProjectOptions.cmake)


Expand Down Expand Up @@ -60,6 +61,11 @@ add_subdirectory(configured_files)
# Adding the src:
add_subdirectory(src)

# Create unified web deployment directory (for WASM builds)
if(EMSCRIPTEN)
myproject_create_web_dist()
endif()

# Don't even look at tests if we're not top level
if(NOT PROJECT_IS_TOP_LEVEL)
return()
Expand Down
33 changes: 20 additions & 13 deletions ProjectOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ include(CheckCXXSourceCompiles)


macro(myproject_supports_sanitizers)
if((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*" OR CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND NOT WIN32)
# Emscripten doesn't support sanitizers
if(EMSCRIPTEN)
set(SUPPORTS_UBSAN OFF)
set(SUPPORTS_ASAN OFF)
elseif((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*" OR CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND NOT WIN32)

message(STATUS "Sanity checking UndefinedBehaviorSanitizer, it should be supported on this platform")
set(TEST_PROGRAM "int main() { return 0; }")
Expand Down Expand Up @@ -166,19 +170,22 @@ macro(myproject_local_options)
""
"")

if(myproject_ENABLE_USER_LINKER)
include(cmake/Linker.cmake)
myproject_configure_linker(myproject_options)
endif()
# Linker and sanitizers not supported in Emscripten
if(NOT EMSCRIPTEN)
if(myproject_ENABLE_USER_LINKER)
include(cmake/Linker.cmake)
myproject_configure_linker(myproject_options)
endif()

include(cmake/Sanitizers.cmake)
myproject_enable_sanitizers(
myproject_options
${myproject_ENABLE_SANITIZER_ADDRESS}
${myproject_ENABLE_SANITIZER_LEAK}
${myproject_ENABLE_SANITIZER_UNDEFINED}
${myproject_ENABLE_SANITIZER_THREAD}
${myproject_ENABLE_SANITIZER_MEMORY})
include(cmake/Sanitizers.cmake)
myproject_enable_sanitizers(
myproject_options
${myproject_ENABLE_SANITIZER_ADDRESS}
${myproject_ENABLE_SANITIZER_LEAK}
${myproject_ENABLE_SANITIZER_UNDEFINED}
${myproject_ENABLE_SANITIZER_THREAD}
${myproject_ENABLE_SANITIZER_MEMORY})
endif()

set_target_properties(myproject_options PROPERTIES UNITY_BUILD ${myproject_ENABLE_UNITY_BUILD})

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ It includes
* a basic CLI example
* examples for fuzz, unit, and constexpr testing
* large GitHub action testing matrix
* WebAssembly build support with automatic GitHub Pages deployment

**Live Demo:** If you enable GitHub Pages in your project created from this template, you'll have a working example like this:
- Main: [https://cpp-best-practices.github.io/cmake_template/](https://cpp-best-practices.github.io/cmake_template/)
- Develop: [https://cpp-best-practices.github.io/cmake_template/develop/](https://cpp-best-practices.github.io/cmake_template/develop/)

The `main` branch deploys to the root, `develop` to `/develop/`, and tags to `/tagname/`.

It requires

Expand Down
Loading
Loading