Skip to content

Commit 0db04e0

Browse files
authored
Fix some compile issues of samples (#690)
Fix compile errors of workloads due to emsdk version upgrade, enable BUILD_TARGET auto set for some samples, and call wasm_runtime_init_thread_env() for in thread routine which was created by pthread_create but not runtime in spawn-thread sample. Signed-off-by: Wenyong Huang <[email protected]>
1 parent 541f577 commit 0db04e0

File tree

23 files changed

+902
-304
lines changed

23 files changed

+902
-304
lines changed

samples/basic/CMakeLists.txt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,48 @@ set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
2121
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
2222

2323
# WAMR features switch
24-
set (WAMR_BUILD_TARGET "X86_64")
25-
set(CMAKE_BUILD_TYPE Debug)
24+
25+
# Set WAMR_BUILD_TARGET, currently values supported:
26+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
27+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
28+
if (NOT DEFINED WAMR_BUILD_TARGET)
29+
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
30+
set (WAMR_BUILD_TARGET "AARCH64")
31+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
32+
set (WAMR_BUILD_TARGET "RISCV64")
33+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
34+
# Build as X86_64 by default in 64-bit platform
35+
set (WAMR_BUILD_TARGET "X86_64")
36+
else ()
37+
# Build as X86_32 by default in 32-bit platform
38+
set (WAMR_BUILD_TARGET "X86_32")
39+
endif ()
40+
endif ()
41+
42+
if (NOT CMAKE_BUILD_TYPE)
43+
set (CMAKE_BUILD_TYPE Release)
44+
endif ()
45+
2646
set (WAMR_BUILD_INTERP 1)
2747
set (WAMR_BUILD_AOT 1)
2848
set (WAMR_BUILD_JIT 0)
2949
set (WAMR_BUILD_LIBC_BUILTIN 1)
50+
3051
if (NOT MSVC)
3152
set (WAMR_BUILD_LIBC_WASI 1)
3253
endif ()
33-
set (WAMR_BUILD_FAST_INTERP 0)
3454

3555
if (NOT MSVC)
3656
# linker flags
3757
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
3858
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
39-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
59+
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
4060
endif ()
4161
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
4262
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
43-
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
63+
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
4464
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
45-
endif ()
65+
endif ()
4666
endif ()
4767
endif ()
4868

samples/basic/src/main.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int main(int argc, char *argv_main[])
2424
static char global_heap_buf[512 * 1024];
2525
char *buffer, error_buf[128];
2626
int opt;
27-
char * wasm_path;
27+
char * wasm_path = NULL;
2828

2929
wasm_module_t module = NULL;
3030
wasm_module_inst_t module_inst = NULL;
@@ -148,7 +148,8 @@ int main(int argc, char *argv_main[])
148148
goto fail;
149149
}
150150

151-
float ret_val = *(float*)argv;
151+
float ret_val;
152+
memcpy(&ret_val, argv, sizeof(float));
152153
printf("Native finished calling wasm function generate_float(), returned a float value: %ff\n", ret_val );
153154

154155
// Next we will pass a buffer to the WASM function
@@ -157,7 +158,7 @@ int main(int argc, char *argv_main[])
157158
// must allocate buffer from wasm instance memory space (never use pointer from host runtime)
158159
wasm_buffer = wasm_runtime_module_malloc(module_inst, 100, (void**)&native_buffer);
159160

160-
*(float*)argv2 = ret_val; // the first argument
161+
memcpy(argv2, &ret_val, sizeof(float)); // the first argument
161162
argv2[1] = wasm_buffer; // the second argument is the wasm buffer address
162163
argv2[2] = 100; // the third argument is the wasm buffer size
163164
argv2[3] = 3; // the last argument is the digits after decimal point for converting float to string

samples/multi-module/CMakeLists.txt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,33 @@ set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
1515
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
1616

1717
# WAMR features switch
18-
set(WAMR_BUILD_TARGET "X86_64")
18+
19+
# Set WAMR_BUILD_TARGET, currently values supported:
20+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
21+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
22+
if (NOT DEFINED WAMR_BUILD_TARGET)
23+
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
24+
set (WAMR_BUILD_TARGET "AARCH64")
25+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
26+
set (WAMR_BUILD_TARGET "RISCV64")
27+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
28+
# Build as X86_64 by default in 64-bit platform
29+
set (WAMR_BUILD_TARGET "X86_64")
30+
else ()
31+
# Build as X86_32 by default in 32-bit platform
32+
set (WAMR_BUILD_TARGET "X86_32")
33+
endif ()
34+
endif ()
35+
36+
if (NOT CMAKE_BUILD_TYPE)
37+
set (CMAKE_BUILD_TYPE Release)
38+
endif ()
39+
1940
set(WAMR_BUILD_INTERP 1)
2041
set(WAMR_BUILD_AOT 0)
2142
set(WAMR_BUILD_JIT 0)
2243
set(WAMR_BUILD_LIBC_BUILTIN 1)
2344
set(WAMR_BUILD_LIBC_WASI 0)
24-
set(WAMR_BUILD_FAST_INTERP 0)
2545
set(WAMR_BUILD_MULTI_MODULE 1)
2646

2747
# compiling and linking flags

samples/multi-thread/CMakeLists.txt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,28 @@ set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
1515
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
1616

1717
# WAMR features switch
18-
set(WAMR_BUILD_TARGET "X86_64")
18+
19+
# Set WAMR_BUILD_TARGET, currently values supported:
20+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
21+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
22+
if (NOT DEFINED WAMR_BUILD_TARGET)
23+
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
24+
set (WAMR_BUILD_TARGET "AARCH64")
25+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
26+
set (WAMR_BUILD_TARGET "RISCV64")
27+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
28+
# Build as X86_64 by default in 64-bit platform
29+
set (WAMR_BUILD_TARGET "X86_64")
30+
else ()
31+
# Build as X86_32 by default in 32-bit platform
32+
set (WAMR_BUILD_TARGET "X86_32")
33+
endif ()
34+
endif ()
35+
36+
if (NOT CMAKE_BUILD_TYPE)
37+
set (CMAKE_BUILD_TYPE Release)
38+
endif ()
39+
1940
set(WAMR_BUILD_INTERP 1)
2041
set(WAMR_BUILD_AOT 1)
2142
set(WAMR_BUILD_JIT 0)

samples/ref-types/CMakeLists.txt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ else()
1010
enable_language (ASM_MASM)
1111
endif()
1212

13-
if(NOT CMAKE_BUILD_TYPE)
14-
set(CMAKE_BUILD_TYPE Debug)
15-
endif()
16-
1713
################ runtime settings ################
1814
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
1915
if (APPLE)
@@ -25,7 +21,27 @@ set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
2521
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
2622

2723
# WAMR features switch
28-
set(WAMR_BUILD_TARGET "X86_64")
24+
25+
# Set WAMR_BUILD_TARGET, currently values supported:
26+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
27+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
28+
if (NOT DEFINED WAMR_BUILD_TARGET)
29+
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
30+
set (WAMR_BUILD_TARGET "AARCH64")
31+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
32+
set (WAMR_BUILD_TARGET "RISCV64")
33+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
34+
# Build as X86_64 by default in 64-bit platform
35+
set (WAMR_BUILD_TARGET "X86_64")
36+
else ()
37+
# Build as X86_32 by default in 32-bit platform
38+
set (WAMR_BUILD_TARGET "X86_32")
39+
endif ()
40+
endif ()
41+
42+
if (NOT CMAKE_BUILD_TYPE)
43+
set (CMAKE_BUILD_TYPE Release)
44+
endif ()
2945

3046
if(NOT DEFINED WAMR_BUILD_INTERP)
3147
set(WAMR_BUILD_INTERP 1)

samples/spawn-thread/CMakeLists.txt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,28 @@ set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
1515
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
1616

1717
# WAMR features switch
18-
set(WAMR_BUILD_TARGET "X86_64")
18+
19+
# Set WAMR_BUILD_TARGET, currently values supported:
20+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
21+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
22+
if (NOT DEFINED WAMR_BUILD_TARGET)
23+
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
24+
set (WAMR_BUILD_TARGET "AARCH64")
25+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
26+
set (WAMR_BUILD_TARGET "RISCV64")
27+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
28+
# Build as X86_64 by default in 64-bit platform
29+
set (WAMR_BUILD_TARGET "X86_64")
30+
else ()
31+
# Build as X86_32 by default in 32-bit platform
32+
set (WAMR_BUILD_TARGET "X86_32")
33+
endif ()
34+
endif ()
35+
36+
if (NOT CMAKE_BUILD_TYPE)
37+
set (CMAKE_BUILD_TYPE Release)
38+
endif ()
39+
1940
set(WAMR_BUILD_INTERP 1)
2041
set(WAMR_BUILD_AOT 1)
2142
set(WAMR_BUILD_JIT 0)

samples/spawn-thread/src/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ void *thread(void* arg)
2323
wasm_function_inst_t func;
2424
uint32 argv[2];
2525

26+
if (!wasm_runtime_init_thread_env()) {
27+
printf("failed to initialize thread environment");
28+
return NULL;
29+
}
30+
2631
func = wasm_runtime_lookup_function(module_inst, "sum", NULL);
2732
if (!func) {
2833
printf("failed to lookup function sum");
34+
wasm_runtime_destroy_thread_env();
2935
return NULL;
3036
}
3137
argv[0] = thread_arg->start;
@@ -34,9 +40,11 @@ void *thread(void* arg)
3440
/* call the WASM function */
3541
if (!wasm_runtime_call_wasm(exec_env, func, 2, argv)) {
3642
printf("%s\n", wasm_runtime_get_exception(module_inst));
43+
wasm_runtime_destroy_thread_env();
3744
return NULL;
3845
}
3946

47+
wasm_runtime_destroy_thread_env();
4048
return (void *)(uintptr_t)argv[0];
4149
}
4250

samples/wasm-c-api/CMakeLists.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ endif()
1313
if(NOT CMAKE_BUILD_TYPE)
1414
set(CMAKE_BUILD_TYPE Release)
1515
endif()
16+
1617
################ runtime settings ################
18+
1719
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
1820
if (APPLE)
1921
add_definitions(-DBH_PLATFORM_DARWIN)
@@ -24,7 +26,23 @@ set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
2426
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
2527

2628
# WAMR features switch
27-
set(WAMR_BUILD_TARGET "X86_64")
29+
30+
# Set WAMR_BUILD_TARGET, currently values supported:
31+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
32+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
33+
if (NOT DEFINED WAMR_BUILD_TARGET)
34+
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
35+
set (WAMR_BUILD_TARGET "AARCH64")
36+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
37+
set (WAMR_BUILD_TARGET "RISCV64")
38+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
39+
# Build as X86_64 by default in 64-bit platform
40+
set (WAMR_BUILD_TARGET "X86_64")
41+
else ()
42+
# Build as X86_32 by default in 32-bit platform
43+
set (WAMR_BUILD_TARGET "X86_32")
44+
endif ()
45+
endif ()
2846

2947
if(NOT DEFINED WAMR_BUILD_INTERP)
3048
set(WAMR_BUILD_INTERP 1)

samples/workload/README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,42 @@ for details, the script includes below steps:
1414
[latest release](https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-linux.tar.gz)
1515
to */opt/wasi-sdk*
1616

17+
``` bash
18+
$ wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VER}/${WASI_SDK_FILE}
19+
$ tar zxf ${WASI_SDK_FILE} -C /opt
20+
$ ln -sf /opt/wasi-sdk-${WASI_SDK_VER}.0 /opt/wasi-sdk
21+
```
22+
1723
- **wabt**. Install
18-
[latest release](https://github.com/WebAssembly/wabt/releases/download/1.0.20/wabt-1.0.20-ubuntu.tar.gz)
19-
to */opt/wabt* or */opt/wabt-1.0.20*
24+
[latest release](https://github.com/WebAssembly/wabt/releases/download/1.0.23/wabt-1.0.23-ubuntu.tar.gz)
25+
to */opt/wabt*
26+
27+
``` bash
28+
$ wget https://github.com/WebAssembly/wabt/releases/download/${WABT_VER}/${WABT_FILE}
29+
$ tar zxf ${WABT_FILE} -C /opt
30+
$ ln -sf /opt/wabt-${WABT_VER} /opt/wabt
31+
```
2032

2133
- **emsdk**. Refer to [the guide](https://emscripten.org/docs/getting_started/downloads.html). Don't forget to activate
22-
emsdk and set up environment variables. Verify it with `echo ${EMSDK}`.
34+
emsdk and set up environment variables. Verify it with `echo ${EMSDK}`. Please be sure to install and activate the building
35+
of 2.0.12
36+
37+
``` bash
38+
$ cd /opt
39+
$ git clone https://github.com/emscripten-core/emsdk.git
40+
$ cd emsdk
41+
$ git pull
42+
$ ./emsdk install 2.0.12
43+
$ ./emsdk activate 2.0.12
44+
$ echo "source /opt/emsdk/emsdk_env.sh" >> "${HOME}"/.bashrc
45+
```
2346

2447
- **binaryen**. Install
25-
[latest release](https://github.com/WebAssembly/binaryen/releases/download/version_97/binaryen-version_97-x86_64-linux.tar.gz)
26-
to */opt/binaryen* or */opt/binaryen-version_97*
48+
[latest release](https://github.com/WebAssembly/binaryen/releases/download/version_101/binaryen-version_101-x86_64-linux.tar.gz)
49+
to */opt/binaryen*
50+
51+
``` bash
52+
$ wget https://github.com/WebAssembly/binaryen/releases/download/${BINARYEN_VER}/${BINARYEN_FILE}
53+
$ tar zxf ${BINARYEN_FILE} -C /opt
54+
$ ln -sf /opt/binaryen-${BINARYEN_VER} /opt/binaryen
55+
```

0 commit comments

Comments
 (0)