Skip to content

Commit 61d3ce1

Browse files
authored
Merge pull request #128 from cpp-best-practices/develop
Develop
2 parents 2c18452 + 2fa1a63 commit 61d3ce1

File tree

13 files changed

+768
-23
lines changed

13 files changed

+768
-23
lines changed

.github/template/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
## About %%myproject%%
88
%%description%%
99

10+
## WebAssembly Demo
11+
12+
Try the live WebAssembly demo:
13+
- Main: [https://%%myorg%%.github.io/%%myproject%%/](https://%%myorg%%.github.io/%%myproject%%/)
14+
- Develop: [https://%%myorg%%.github.io/%%myproject%%/develop/](https://%%myorg%%.github.io/%%myproject%%/develop/)
15+
16+
The `main` branch deploys to the root, `develop` to `/develop/`, and tags to `/tagname/`.
17+
1018

1119
## More Details
1220

.github/workflows/wasm.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Build Intro WASM and Deploy to GitHub Pages
2+
3+
on:
4+
pull_request:
5+
release:
6+
types: [published]
7+
push:
8+
branches: [main, develop]
9+
tags: ['**']
10+
11+
permissions:
12+
contents: write
13+
14+
jobs:
15+
build-and-deploy:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Setup Emscripten
21+
uses: mymindstorm/setup-emsdk@v14
22+
with:
23+
version: 'latest'
24+
25+
- name: Install Ninja
26+
run: sudo apt-get install -y ninja-build
27+
28+
- name: Configure CMake
29+
run: emcmake cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
30+
31+
- name: Build all WASM targets
32+
run: emmake cmake --build build --target web-dist
33+
34+
- name: Prepare deployment
35+
run: |
36+
# web-dist target already created build/web-dist/
37+
# Just copy it to dist/ for GitHub Pages action
38+
cp -r build/web-dist dist
39+
40+
- name: Determine deploy path
41+
id: deploy-path
42+
if: github.event_name != 'pull_request' && github.event_name != 'release'
43+
run: |
44+
if [[ "$GITHUB_REF" == refs/tags/* ]]; then
45+
echo "path=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
46+
elif [[ "$GITHUB_REF" == refs/heads/main ]]; then
47+
echo "path=." >> $GITHUB_OUTPUT
48+
elif [[ "$GITHUB_REF" == refs/heads/develop ]]; then
49+
echo "path=develop" >> $GITHUB_OUTPUT
50+
fi
51+
52+
- name: Deploy to GitHub Pages
53+
if: github.event_name != 'pull_request' && github.event_name != 'release'
54+
uses: peaceiris/actions-gh-pages@v4
55+
with:
56+
github_token: ${{ secrets.GITHUB_TOKEN }}
57+
publish_dir: ./dist
58+
destination_dir: ${{ steps.deploy-path.outputs.path }}
59+
keep_files: true

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ project(
2323
LANGUAGES CXX C)
2424

2525
include(cmake/PreventInSourceBuilds.cmake)
26+
include(cmake/Emscripten.cmake)
2627
include(ProjectOptions.cmake)
2728

2829

@@ -60,6 +61,11 @@ add_subdirectory(configured_files)
6061
# Adding the src:
6162
add_subdirectory(src)
6263

64+
# Create unified web deployment directory (for WASM builds)
65+
if(EMSCRIPTEN)
66+
myproject_create_web_dist()
67+
endif()
68+
6369
# Don't even look at tests if we're not top level
6470
if(NOT PROJECT_IS_TOP_LEVEL)
6571
return()

ProjectOptions.cmake

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ include(CheckCXXSourceCompiles)
88

99

1010
macro(myproject_supports_sanitizers)
11-
if((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*" OR CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND NOT WIN32)
11+
# Emscripten doesn't support sanitizers
12+
if(EMSCRIPTEN)
13+
set(SUPPORTS_UBSAN OFF)
14+
set(SUPPORTS_ASAN OFF)
15+
elseif((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*" OR CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND NOT WIN32)
1216

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

169-
if(myproject_ENABLE_USER_LINKER)
170-
include(cmake/Linker.cmake)
171-
myproject_configure_linker(myproject_options)
172-
endif()
173+
# Linker and sanitizers not supported in Emscripten
174+
if(NOT EMSCRIPTEN)
175+
if(myproject_ENABLE_USER_LINKER)
176+
include(cmake/Linker.cmake)
177+
myproject_configure_linker(myproject_options)
178+
endif()
173179

174-
include(cmake/Sanitizers.cmake)
175-
myproject_enable_sanitizers(
176-
myproject_options
177-
${myproject_ENABLE_SANITIZER_ADDRESS}
178-
${myproject_ENABLE_SANITIZER_LEAK}
179-
${myproject_ENABLE_SANITIZER_UNDEFINED}
180-
${myproject_ENABLE_SANITIZER_THREAD}
181-
${myproject_ENABLE_SANITIZER_MEMORY})
180+
include(cmake/Sanitizers.cmake)
181+
myproject_enable_sanitizers(
182+
myproject_options
183+
${myproject_ENABLE_SANITIZER_ADDRESS}
184+
${myproject_ENABLE_SANITIZER_LEAK}
185+
${myproject_ENABLE_SANITIZER_UNDEFINED}
186+
${myproject_ENABLE_SANITIZER_THREAD}
187+
${myproject_ENABLE_SANITIZER_MEMORY})
188+
endif()
182189

183190
set_target_properties(myproject_options PROPERTIES UNITY_BUILD ${myproject_ENABLE_UNITY_BUILD})
184191

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ It includes
2020
* a basic CLI example
2121
* examples for fuzz, unit, and constexpr testing
2222
* large GitHub action testing matrix
23+
* WebAssembly build support with automatic GitHub Pages deployment
24+
25+
**Live Demo:** If you enable GitHub Pages in your project created from this template, you'll have a working example like this:
26+
- Main: [https://cpp-best-practices.github.io/cmake_template/](https://cpp-best-practices.github.io/cmake_template/)
27+
- Develop: [https://cpp-best-practices.github.io/cmake_template/develop/](https://cpp-best-practices.github.io/cmake_template/develop/)
28+
29+
The `main` branch deploys to the root, `develop` to `/develop/`, and tags to `/tagname/`.
2330

2431
It requires
2532

0 commit comments

Comments
 (0)