Skip to content

Commit fb367c9

Browse files
xuxin930xiaoxiang781216
authored andcommitted
cmake(enhance):Enhanced full WASM library and application compilation
1.add complete compilation FLAGS for wasm toolchain 2.wasm build no longer traverses the native directory, saving build time 3.implement OPT and AOT process actions for wasm files 4.create a bridge interface for navtie build and wasm build Signed-off-by: xuxin19 <[email protected]>
1 parent 2ad162d commit fb367c9

File tree

5 files changed

+364
-47
lines changed

5 files changed

+364
-47
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ if(CONFIG_APPS_DIR)
3434
include(nuttx_add_luamod)
3535
include(nuttx_add_wamrmod)
3636
include(nuttx_add_rust)
37+
include(nuttx_wasm_interface)
3738
nuttx_add_library(apps)
3839
if(NOT EXISTS {NUTTX_APPS_BINDIR}/dummy.c)
3940
file(TOUCH ${NUTTX_APPS_BINDIR}/dummy.c)
@@ -66,7 +67,6 @@ add_subdirectory(platform)
6667
add_subdirectory(sdr)
6768
add_subdirectory(system)
6869
add_subdirectory(testing)
69-
add_subdirectory(tools)
7070
add_subdirectory(videoutils)
7171
add_subdirectory(wireless)
7272

@@ -76,6 +76,7 @@ if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/external)
7676
add_subdirectory(external)
7777
endif()
7878

79+
add_subdirectory(tools) # after traversing all subdirectories
7980
add_subdirectory(builtin) # must be last
8081

8182
nuttx_generate_kconfig()

cmake/nuttx_wasm_interface.cmake

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# ##############################################################################
2+
# cmake/nuttx_wasm_interface.cmake
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
5+
# license agreements. See the NOTICE file distributed with this work for
6+
# additional information regarding copyright ownership. The ASF licenses this
7+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
8+
# use this file except in compliance with the License. You may obtain a copy of
9+
# the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations under
17+
# the License.
18+
#
19+
# ##############################################################################
20+
21+
# This is NuttX native wrapper for the WAMR interface.
22+
23+
if(NOT TARGET wasm_interface)
24+
add_custom_target(wasm_interface)
25+
endif()
26+
27+
# declare WAMS build directory and add INSTALL path
28+
function(wasm_add_application)
29+
30+
cmake_parse_arguments(
31+
APP "" "NAME;STACK_SIZE;INITIAL_MEMORY_SIZE;WAMR_MODE;INSTALL_NAME"
32+
"SRCS;WLDFLAGS;WCFLAGS;WINCLUDES" ${ARGN})
33+
34+
set_property(
35+
TARGET wasm_interface
36+
APPEND
37+
PROPERTY WASM_DIR ${CMAKE_CURRENT_LIST_DIR})
38+
39+
if(APP_INSTALL_NAME)
40+
add_custom_command(
41+
OUTPUT ${CMAKE_BINARY_DIR}/wasm/${APP_INSTALL_NAME}
42+
COMMAND ${CMAKE_COMMAND} -E touch_nocreate
43+
${CMAKE_BINARY_DIR}/wasm/${APP_INSTALL_NAME}
44+
DEPENDS apps)
45+
add_custom_target(wasm_gen_${APP_NAME}
46+
DEPENDS ${CMAKE_BINARY_DIR}/wasm/${APP_INSTALL_NAME})
47+
add_dynamic_rcraws(RAWS ${CMAKE_BINARY_DIR}/wasm/${APP_INSTALL_NAME}
48+
DEPENDS wasm_gen_${APP_NAME})
49+
endif()
50+
51+
endfunction()
52+
53+
function(wasm_add_library)
54+
set_property(
55+
TARGET wasm_interface
56+
APPEND
57+
PROPERTY WASM_DIR ${CMAKE_CURRENT_LIST_DIR})
58+
endfunction()

tools/CMakeLists.txt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,7 @@
2020
#
2121
# ##############################################################################
2222

23-
# Fake wasm_add_application function to suppress error from native build process
24-
function(wasm_add_application)
25-
26-
endfunction()
27-
28-
# Fake wasm_add_library function to suppress error from native build process
29-
function(wasm_add_library)
30-
31-
endfunction()
32-
33-
if(CONFIG_TOOLS_WASM_BUILD)
23+
if(CONFIG_TOOLS_WASM_BUILD OR CONFIG_INTERPRETERS_WAMR_BUILD_MODULES_FOR_NUTTX)
3424

3525
include(ExternalProject)
3626

@@ -45,14 +35,22 @@ if(CONFIG_TOOLS_WASM_BUILD)
4535
# Get parent dir of CMAKE_CURRENT_SOURCE_DIR
4636
get_filename_component(APPDIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
4737

38+
# Get the Wasm build dirs from the wasm_interface target
39+
get_property(
40+
WASM_DIRS
41+
TARGET wasm_interface
42+
PROPERTY WASM_DIR)
43+
44+
# ensure the Wasm build dirs are unique
45+
list(REMOVE_DUPLICATES WASM_DIRS)
4846
# Configure and build the Wasm based application
4947
add_custom_target(
5048
configure_wasm_build
5149
COMMAND
5250
${CMAKE_COMMAND} -B${CMAKE_BINARY_DIR}/Wasm
5351
${CMAKE_CURRENT_SOURCE_DIR}/Wasm -DAPPDIR=${APPDIR} -DTOPDIR=${TOPDIR}
54-
-DKCONFIG_FILE_PATH=${KCONFIG_FILE_PATH}
55-
-DWASI_SDK_PATH=$ENV{WASI_SDK_PATH})
52+
-DTOPBINDIR=${CMAKE_BINARY_DIR} -DKCONFIG_FILE_PATH=${KCONFIG_FILE_PATH}
53+
-DWASI_SDK_PATH=$ENV{WASI_SDK_PATH} -DWASM_DIRS=${WASM_DIRS})
5654

5755
add_custom_target(wasm_build COMMAND ${CMAKE_COMMAND} --build
5856
${CMAKE_BINARY_DIR}/Wasm)

tools/Wasm/CMakeLists.txt

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,24 @@
3232

3333
cmake_minimum_required(VERSION 3.5)
3434

35+
cmake_policy(SET CMP0079 NEW)
36+
37+
# Include the NuttX kconfig parser to shared the configuration between the NuttX
38+
# build and the Wasm build. And then parse the input KCONFIG_FILE_PATH to get
39+
# the configuration.
40+
include(${TOPDIR}/cmake/nuttx_kconfig.cmake)
41+
42+
# Parse the input KCONFIG_FILE_PATH to get the configuration.
43+
nuttx_export_kconfig(${KCONFIG_FILE_PATH})
44+
45+
# Include the WASI-SDK.cmake file to setup the necessary flags for building
3546
include(WASI-SDK.cmake)
3647

3748
project(WasmApps)
3849

50+
# Add the wasm_interface library to hold all the Wasm libraries.
51+
add_library(wasm_interface INTERFACE)
52+
3953
# Check whether the APPDIR is defined or not. If not, then set it to the parent
4054
# directory of the current CMakeLists.txt file.
4155
if(NOT DEFINED APPDIR)
@@ -47,18 +61,21 @@ if(NOT DEFINED TOPDIR)
4761
message(FATAL_ERROR "TOPDIR is not defined")
4862
endif()
4963

64+
# Check wether the TOPBINDIR is defined or not. If not, then raise an error.
65+
if(NOT DEFINED TOPBINDIR)
66+
message(FATAL_ERROR "TOPBINDIR is not defined")
67+
endif()
68+
69+
if(NOT EXISTS ${TOPBINDIR}/wasm)
70+
file(MAKE_DIRECTORY ${TOPBINDIR}/wasm)
71+
endif()
72+
5073
# Check wether the KCONFIG_FILE_PATH is defined or not. If not, then raise an
5174
# error.
5275
if(NOT DEFINED KCONFIG_FILE_PATH)
5376
message(FATAL_ERROR "KCONFIG_FILE_PATH is not defined")
5477
endif()
5578

56-
# Include the NuttX kconfig parser to shared the configuration between the NuttX
57-
# build and the Wasm build. And then parse the input KCONFIG_FILE_PATH to get
58-
# the configuration.
59-
include(${TOPDIR}/cmake/nuttx_kconfig.cmake)
60-
nuttx_export_kconfig(${KCONFIG_FILE_PATH})
61-
6279
# Provide FAR macro from command line since it is not supported in wasi-sdk, but
6380
# it is used in NuttX code.
6481
# ~~~
@@ -78,25 +95,11 @@ function(nuttx_add_library)
7895

7996
endfunction()
8097

81-
# Recursively find all the CMakeLists.txt files in the ${APPDIR} and add it by
82-
# add_subdirectory, but exclude the CMakeLists.txt file in the ${APPDIR}/tools
83-
# directory.
84-
file(GLOB_RECURSE WASM_APPS ${APPDIR}/*/CMakeLists.txt)
85-
list(FILTER WASM_APPS EXCLUDE REGEX ".*/tools/.*")
86-
87-
# Read and check if wasm_add_application is called in the CMakeLists.txt file in
88-
# WASM_APPS If true, then add the directory to the build process
89-
foreach(WASM_APP ${WASM_APPS})
90-
file(READ ${WASM_APP} WASM_APP_CONTENTS)
91-
string(FIND "${WASM_APP_CONTENTS}" "wasm_add_application" WASM_APP_FOUND)
92-
string(FIND "${WASM_APP_CONTENTS}" "wasm_add_library" WASM_LIB_FOUND)
93-
if(WASM_APP_FOUND GREATER -1 OR WASM_LIB_FOUND GREATER -1)
94-
get_filename_component(WASM_APP_DIR ${WASM_APP} DIRECTORY)
95-
# Add subdirectory to the build process and put the build directory in the
96-
# current build directory with the name same as the relative path of the
97-
# ${APPDIR}
98-
string(REPLACE ${APPDIR} "" WASM_APP_BUILD_DIR ${WASM_APP_DIR})
99-
add_subdirectory(${WASM_APP_DIR}
100-
${CMAKE_CURRENT_BINARY_DIR}/Wasm/${WASM_APP_BUILD_DIR})
101-
endif()
98+
# ~~~
99+
# Add all the Wasm apps to the build process.
100+
foreach(WASM_APP ${WASM_DIRS})
101+
string(REPLACE ${APPDIR} "" WASM_APP_BUILD_DIR ${WASM_APP})
102+
add_subdirectory(${WASM_APP}
103+
${CMAKE_CURRENT_BINARY_DIR}/Wasm/${WASM_APP_BUILD_DIR})
102104
endforeach()
105+
# ~~~

0 commit comments

Comments
 (0)