Skip to content

Commit 23e9dc6

Browse files
committed
Renamed and fixed profiles
1 parent 98d7b4f commit 23e9dc6

File tree

23 files changed

+104
-67
lines changed

23 files changed

+104
-67
lines changed

examples/cross_build/wasm/bindings/CMakeLists.txt renamed to examples/cross_build/emscripten/bindings/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ cmake_minimum_required(VERSION 3.15)
22
project(wasm_example CXX)
33

44
find_package(Eigen3 REQUIRED)
5+
find_package(ZLIB REQUIRED)
6+
find_package(fmt REQUIRED)
57
add_executable(wasm_example main.cpp)
68

7-
target_link_libraries(${PROJECT_NAME} PRIVATE Eigen3::Eigen)
9+
target_link_libraries(${PROJECT_NAME} PRIVATE Eigen3::Eigen ZLIB::ZLIB fmt::fmt)
810

911
# Set the executable suffix to .html in order to generate a html page by
1012
# Emscripten (there is no way of setting this from a user toolchain or

examples/cross_build/wasm/bindings/README.md renamed to examples/cross_build/emscripten/bindings/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# WASM project with bindings and conan dependency
22

3-
43
## Build and run
54

65
To compile the project:
@@ -12,7 +11,21 @@ $ conan build . -pr:h ../profiles/wasm32 --build=missing
1211
To open a WASM webpage locally, most of the browsers will complain due to security reasons as WASM must be loaded asynchronous
1312

1413
The easiest way of opening the generated webpage (should be in `build/release-wasm/wasm_example.html`) is by running a local server.
15-
This can be done via `emrun` command (needs to be downloaded):
14+
This can be done via `emrun` command:
15+
16+
`emrun` is packaged with `emskd` recipe so it should be available by activating build environment:
17+
18+
**POSIX**
19+
```sh
20+
$ source build/release-wasm/generators/conanbuild.sh
21+
```
22+
23+
**Windows**
24+
```sh
25+
$ build\release-wasm\generators\conanbuild.bat
26+
```
27+
28+
By this time, `emrun`, `node`, and other JS/WASM tools should be available in the path:
1629

1730
```sh
1831
$ emrun --browser <browser_name> build/release-wasm/wasm_example.html
File renamed without changes.

examples/cross_build/wasm/bindings/conanfile.py renamed to examples/cross_build/emscripten/bindings/conanfile.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import os
2-
31
from conan import ConanFile
42
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
5-
from conan.tools.files import copy
63

74

85
class WasmExampleRecipe(ConanFile):
@@ -16,21 +13,23 @@ def layout(self):
1613

1714
def requirements(self):
1815
self.requires("eigen/3.4.0")
16+
self.requires("zlib/1.3.1")
17+
self.requires("fmt/11.1.4")
1918

2019
def generate(self):
2120
deps = CMakeDeps(self)
2221
deps.generate()
2322
tc = CMakeToolchain(self)
2423

24+
# HEAPxx values need to be exported explicitly since Emscripten 4.0.7
25+
# https://github.com/emscripten-core/emscripten/blob/main/ChangeLog.md#407---041525
2526
tc.extra_exelinkflags.append(
2627
"-sEXPORTED_FUNCTIONS=['_malloc','_free'] \
27-
-sEXPORTED_RUNTIME_METHODS=['ccall','cwrap','getValue','setValue'] \
28-
-sENVIRONMENT=web \
28+
-sEXPORTED_RUNTIME_METHODS=['ccall','cwrap','getValue','setValue','HEAPF32'] \
2929
-sALLOW_MEMORY_GROWTH=1 \
3030
-sNO_EXIT_RUNTIME=1 \
3131
--shell-file ${CMAKE_SOURCE_DIR}/shell.html"
3232
)
33-
3433
tc.generate()
3534

3635
def build(self):

examples/cross_build/wasm/bindings/main.cpp renamed to examples/cross_build/emscripten/bindings/main.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include <Eigen/Core>
22
#include <cstdint>
33
#include <emscripten/emscripten.h>
4+
#include <fmt/printf.h>
45
#include <iostream>
56
#include <string>
7+
#include <zlib.h>
68

79
#ifdef __cplusplus
810
#define EXTERN extern "C"
@@ -36,18 +38,22 @@ EXTERN EMSCRIPTEN_KEEPALIVE void addOne(int32_t *input, int32_t *output) {
3638
}
3739

3840
EXTERN EMSCRIPTEN_KEEPALIVE float sumArray(const float *data, int32_t size) {
39-
// print data input
40-
std::cout << "Data input: ";
41+
fmt::print("Data input: ");
4142
for (int i = 0; i < size; ++i) {
42-
std::cout << data[i] << " ";
43+
fmt::print("{} ", data[i]);
4344
}
4445
std::cout << std::endl;
4546
Eigen::Map<const Eigen::ArrayXf> vec(data, size);
4647
return vec.sum();
4748
}
4849

50+
EXTERN EMSCRIPTEN_KEEPALIVE void getZlibVersion() {
51+
fmt::print("Zlib version being used: {}\n", zlibVersion());
52+
}
53+
4954
int main() {
5055
std::cout << "Hello World!" << std::endl;
5156
auto data = new float[5]{1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
5257
std::cout << sumArray(data, 5) << std::endl;
58+
fmt::print(zlibVersion());
5359
}

examples/cross_build/wasm/bindings/shell.html renamed to examples/cross_build/emscripten/bindings/shell.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!doctype html>
22
<html lang="en-us">
33
<body>
4-
<h1>Conan C++ WebAssembly Example</h1>
4+
<h1>Conan C++ Emscripten Example</h1>
55
<br />
66
<div id="status">Downloading...</div>
77
<div>
@@ -14,6 +14,9 @@ <h1>Conan C++ WebAssembly Example</h1>
1414
<button onclick="printMessage()">Print Message</button>
1515
<hr />
1616

17+
<button onclick="getZlibVersion()">Print Zlib version</button>
18+
<hr />
19+
1720
<!-- Example of a simple invocation to fibonachi -->
1821
<input type="number" id="fibInput" placeholder="e.g., 10" />
1922
<button onclick="fibExample()">Compute Fibonacci</button>
@@ -103,6 +106,9 @@ <h1>Conan C++ WebAssembly Example</h1>
103106
["Hello from C++ WebAssembly!"],
104107
);
105108
};
109+
const getZlibVersion = () => {
110+
Module.ccall("getZlibVersion", null, [], []);
111+
};
106112

107113
// Example of a simple invocation to fibonachi
108114
const fibExample = () => {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include(base)
2+
3+
[settings]
4+
arch=asm.js
5+
6+
[conf]
7+
tools.build:exelinkflags+=['-sMAXIMUM_MEMORY=2GB', '-sINITIAL_MEMORY=64MB']
8+
tools.build:sharedlinkflags+=['-sMAXIMUM_MEMORY=2GB', '-sINITIAL_MEMORY=64MB']
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Note: this profile uses emsdk package from Conan Center Index
2+
# To use a local emsdk installation could be done by:
3+
# a) Define a platform_tool_requires ensuring CC, CXX, etc are defined correctly in PATH
4+
# [platform_tool_requires]
5+
# emsdk/<version>
6+
# b) Define compiler_executables
7+
# tools.build:compiler_executables={'c':'emcc', 'cpp':'em++'}
8+
# c) Define buildenv
9+
# [buildenv]
10+
# CC=<path/to/emcc>
11+
# CXX=<path/to/em++>
12+
# AR ..
13+
14+
[settings]
15+
build_type=Release
16+
compiler=emcc
17+
compiler.cppstd=17
18+
compiler.libcxx=libc++
19+
compiler.version=4.0.9
20+
os=Emscripten
21+
22+
[tool_requires]
23+
emsdk/4.0.9
24+
ninja/[*]
25+
26+
[conf]
27+
tools.build:exelinkflags=['-sALLOW_MEMORY_GROWTH=1']
28+
tools.build:sharedlinkflags=['-sALLOW_MEMORY_GROWTH=1']
29+
30+
# Set Ninja as default generator as it is faster and will sove issues on Windows
31+
tools.cmake.cmaketoolchain:generator=Ninja
32+
33+
# Verbosity to see emcc invocations
34+
tools.build:verbosity=verbose
35+
tools.compilation:verbosity=verbose
36+
37+
# Distinguish between architectures
38+
tools.cmake.cmake_layout:build_folder_vars=['settings.build_type', 'settings.arch']
39+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include(base)
2+
3+
[settings]
4+
arch=wasm
5+
6+
[conf]
7+
tools.build:exelinkflags+=['-sMAXIMUM_MEMORY=4GB', '-sINITIAL_MEMORY=64MB']
8+
tools.build:sharedlinkflags+=['-sMAXIMUM_MEMORY=4GB', '-sINITIAL_MEMORY=64MB']
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include(base)
2+
3+
[settings]
4+
arch=wasm64
5+
6+
[conf]
7+
# In this early stage of wasm64, ALLOW_MEMORY_GROWTH is not having effect. Also it may not be the most efficient solution.
8+
# wasm64 for now needs to declare INITIAL_MEMORY as the maximum memory
9+
tools.build:exelinkflags+=['-sMAXIMUM_MEMORY=16GB', '-sINITIAL_MEMORY=16GB']
10+
tools.build:sharedlinkflags+=['-sMAXIMUM_MEMORY=16GB', '-sINITIAL_MEMORY=16GB']

0 commit comments

Comments
 (0)