Skip to content

Commit b0ef3ce

Browse files
committed
add aot mode
1 parent dc33e69 commit b0ef3ce

5 files changed

Lines changed: 131 additions & 54 deletions

File tree

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,80 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required (VERSION 3.14)
4+
cmake_minimum_required(VERSION 3.14)
55

66
include(CheckPIESupported)
77

8-
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
9-
project (custom_section)
8+
if(NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
9+
project(custom_section)
1010
else()
11-
project (custom_section C ASM)
11+
project(custom_section C ASM)
1212
endif()
1313

14-
################ runtime settings ################
15-
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
16-
if (APPLE)
14+
# ############### runtime settings ################
15+
string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
16+
17+
if(APPLE)
1718
add_definitions(-DBH_PLATFORM_DARWIN)
18-
endif ()
19-
20-
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
21-
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
22-
23-
if (NOT DEFINED WAMR_BUILD_TARGET)
24-
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
25-
set (WAMR_BUILD_TARGET "AARCH64")
26-
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
27-
set (WAMR_BUILD_TARGET "RISCV64")
28-
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
29-
set (WAMR_BUILD_TARGET "X86_64")
30-
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
31-
set (WAMR_BUILD_TARGET "X86_32")
32-
else ()
19+
endif()
20+
21+
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
22+
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
23+
24+
if(NOT DEFINED WAMR_BUILD_TARGET)
25+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
26+
set(WAMR_BUILD_TARGET "AARCH64")
27+
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
28+
set(WAMR_BUILD_TARGET "RISCV64")
29+
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
30+
set(WAMR_BUILD_TARGET "X86_64")
31+
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
32+
set(WAMR_BUILD_TARGET "X86_32")
33+
else()
3334
message(SEND_ERROR "Unsupported build target platform!")
34-
endif ()
35-
endif ()
35+
endif()
36+
endif()
3637

37-
if (NOT CMAKE_BUILD_TYPE)
38-
set (CMAKE_BUILD_TYPE Debug)
39-
endif ()
38+
if(NOT CMAKE_BUILD_TYPE)
39+
set(CMAKE_BUILD_TYPE Debug)
40+
endif()
4041

41-
set (WAMR_BUILD_INTERP 1)
42-
set (WAMR_BUILD_AOT 1)
43-
set (WAMR_BUILD_JIT 0)
44-
set (WAMR_BUILD_LIBC_BUILTIN 1)
45-
set (WAMR_BUILD_LOAD_CUSTOM_SECTION 1)
42+
set(WAMR_BUILD_INTERP 1)
43+
set(WAMR_BUILD_AOT 1)
44+
set(WAMR_BUILD_JIT 0)
45+
set(WAMR_BUILD_LIBC_BUILTIN 1)
46+
set(WAMR_BUILD_LOAD_CUSTOM_SECTION 1)
4647

47-
if (NOT MSVC)
48-
set (WAMR_BUILD_LIBC_WASI 1)
49-
endif ()
48+
if(NOT MSVC)
49+
set(WAMR_BUILD_LIBC_WASI 1)
50+
endif()
5051

51-
if (NOT MSVC)
52-
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
53-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
54-
endif ()
55-
endif ()
52+
if(NOT MSVC)
53+
if(NOT(CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
54+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
55+
endif()
56+
endif()
5657

57-
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
58-
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
58+
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
59+
include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
5960

6061
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
6162

62-
################ application related ################
63+
# ############### application related ################
6364
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
64-
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
65+
include(${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
6566

66-
add_executable (custom_section
67+
add_executable(custom_section
6768
src/main.c
6869
src/native_impl.c
6970
${UNCOMMON_SHARED_SOURCE}
7071
)
7172

7273
check_pie_supported()
73-
set_target_properties (custom_section PROPERTIES POSITION_INDEPENDENT_CODE ON)
74+
set_target_properties(custom_section PROPERTIES POSITION_INDEPENDENT_CODE ON)
7475

75-
if (APPLE)
76-
target_link_libraries (custom_section vmlib -lm -ldl -lpthread)
77-
else ()
78-
target_link_libraries (custom_section vmlib -lm -ldl -lpthread -lrt)
79-
endif ()
76+
if(APPLE)
77+
target_link_libraries(custom_section vmlib -lm -ldl -lpthread)
78+
else()
79+
target_link_libraries(custom_section vmlib -lm -ldl -lpthread -lrt)
80+
endif()

samples/custom_section/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ The "custom_section" sample project
66
==============
77

88
This sample demonstrates how to:
9+
910
- embed a separate binary payload into a Wasm custom section through a `.s` file
1011
- load that Wasm module with `WAMR_BUILD_LOAD_CUSTOM_SECTION=1`
1112
- export native APIs that resolve a custom section name to a host-side handle
1213
- print the custom section bytes later through a second native API
14+
- optionally compile the Wasm module to AoT while preserving the `demo` custom section
1315

1416
The Wasm application is built from:
17+
1518
- `wasm-apps/custom_section.c`
1619
- `wasm-apps/custom_section_payload.s`
1720
- `wasm-apps/custom_section_payload.bin`
@@ -20,15 +23,23 @@ The assembler file emits a section named `.custom_section.demo`, which becomes a
2023

2124
Build this sample
2225
==============
26+
2327
Execute the `build.sh` script. The host executable and the Wasm app are generated in `out`.
2428

2529
```sh
2630
./build.sh
2731
```
2832

33+
Build the AoT variant only when needed by passing `--aot`. This preserves the `demo` custom section in the generated AoT file by calling `wamrc --emit-custom-sections=demo`.
34+
35+
```sh
36+
./build.sh --aot
37+
```
38+
2939
Run the sample
3040
==========================
31-
Enter the output directory and run the sample:
41+
42+
Enter the output directory and run the Wasm sample directly:
3243

3344
```sh
3445
cd ./out/
@@ -41,4 +52,10 @@ Or run the helper script from `samples/custom_section`:
4152
./run.sh
4253
```
4354

55+
To run the AoT artifact instead, pass `--aot` to the helper script:
56+
57+
```sh
58+
./run.sh --aot
59+
```
60+
4461
When running under WSL, use the Linux workspace path layout, for example `/home/tl/projects/wasm-micro-runtime/...`, not a Windows drive path.

samples/custom_section/build.sh

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ set -e
55
CURR_DIR=$PWD
66
OUT_DIR=${PWD}/out
77
WASM_APPS=${PWD}/wasm-apps
8+
WAMR_ROOT_DIR=${PWD}/../..
9+
WAMRC_CMD=${WAMR_ROOT_DIR}/wamr-compiler/build/wamrc
10+
BUILD_AOT=0
11+
12+
if [ $# -gt 1 ]; then
13+
echo "Usage: $0 [--aot]"
14+
exit 1
15+
fi
16+
17+
if [ $# -eq 1 ]; then
18+
if [ "$1" = "--aot" ]; then
19+
BUILD_AOT=1
20+
else
21+
echo "Usage: $0 [--aot]"
22+
exit 1
23+
fi
24+
fi
825

926
rm -rf ${OUT_DIR}
1027
mkdir -p ${OUT_DIR}/wasm-apps
@@ -29,5 +46,18 @@ cd ${WASM_APPS}
2946
custom_section.c \
3047
custom_section_payload.s
3148

32-
printf 'build custom_section.wasm success\n'
49+
printf '\nbuild custom_section.wasm success\n'
50+
51+
if [ ${BUILD_AOT} -eq 1 ]; then
52+
if [ ! -x ${WAMRC_CMD} ]; then
53+
echo "Error: wamrc not found at ${WAMRC_CMD}"
54+
echo "Please build wamrc first under ${WAMR_ROOT_DIR}/wamr-compiler"
55+
exit 1
56+
fi
57+
58+
printf '\n##################### build aot app\n'
59+
${WAMRC_CMD} --emit-custom-sections=demo -o ${OUT_DIR}/wasm-apps/custom_section.aot ${OUT_DIR}/wasm-apps/custom_section.wasm
60+
printf '\nbuild custom_section.aot success\n'
61+
fi
62+
3363
cd ${CURR_DIR}

samples/custom_section/run.sh

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,30 @@
22

33
set -e
44

5-
out/custom_section -f out/wasm-apps/custom_section.wasm
5+
if [ $# -gt 1 ]; then
6+
echo "Usage: $0 [--aot]"
7+
exit 1
8+
fi
9+
10+
APP=out/wasm-apps/custom_section.wasm
11+
12+
if [ $# -eq 1 ]; then
13+
if [ "$1" = "--aot" ]; then
14+
APP=out/wasm-apps/custom_section.aot
15+
else
16+
echo "Usage: $0 [--aot]"
17+
exit 1
18+
fi
19+
fi
20+
21+
if [ ! -f ${APP} ]; then
22+
echo "Error: ${APP} not found"
23+
if [ "$APP" = "out/wasm-apps/custom_section.aot" ]; then
24+
echo "Run ./build.sh --aot first"
25+
else
26+
echo "Run ./build.sh first"
27+
fi
28+
exit 1
29+
fi
30+
31+
out/custom_section -f ${APP}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
This payload lives in a Wasm custom section.
22
It is linked by custom_section_payload.s with .incbin.
3+
4+
The payload is read by custom_section.c, which is compiled to a Wasm module and executed by the host embedder.
5+
It can be arbitrarily data, and the host can resolve it with `wasm_runtime_get_custom_section` and print/use it later through a handle-based native API.

0 commit comments

Comments
 (0)