Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake(enhance):Enhanced full WASM library and application compilation #2971

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ if(CONFIG_APPS_DIR)
include(nuttx_add_luamod)
include(nuttx_add_wamrmod)
include(nuttx_add_rust)
include(nuttx_wasm_interface)
nuttx_add_library(apps)
if(NOT EXISTS {NUTTX_APPS_BINDIR}/dummy.c)
file(TOUCH ${NUTTX_APPS_BINDIR}/dummy.c)
Expand Down Expand Up @@ -66,7 +67,6 @@ add_subdirectory(platform)
add_subdirectory(sdr)
add_subdirectory(system)
add_subdirectory(testing)
add_subdirectory(tools)
add_subdirectory(videoutils)
add_subdirectory(wireless)

Expand All @@ -76,6 +76,7 @@ if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/external)
add_subdirectory(external)
endif()

add_subdirectory(tools) # after traversing all subdirectories
add_subdirectory(builtin) # must be last

nuttx_generate_kconfig()
58 changes: 58 additions & 0 deletions cmake/nuttx_wasm_interface.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# ##############################################################################
# cmake/nuttx_wasm_interface.cmake
#
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses this
# file to you under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# ##############################################################################

# This is NuttX native wrapper for the WAMR interface.

if(NOT TARGET wasm_interface)
add_custom_target(wasm_interface)
endif()

# declare WAMS build directory and add INSTALL path
function(wasm_add_application)

cmake_parse_arguments(
APP "" "NAME;STACK_SIZE;INITIAL_MEMORY_SIZE;WAMR_MODE;INSTALL_NAME"
"SRCS;WLDFLAGS;WCFLAGS;WINCLUDES" ${ARGN})

set_property(
TARGET wasm_interface
APPEND
PROPERTY WASM_DIR ${CMAKE_CURRENT_LIST_DIR})

if(APP_INSTALL_NAME)
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/wasm/${APP_INSTALL_NAME}
COMMAND ${CMAKE_COMMAND} -E touch_nocreate
${CMAKE_BINARY_DIR}/wasm/${APP_INSTALL_NAME}
DEPENDS apps)
add_custom_target(wasm_gen_${APP_NAME}
DEPENDS ${CMAKE_BINARY_DIR}/wasm/${APP_INSTALL_NAME})
add_dynamic_rcraws(RAWS ${CMAKE_BINARY_DIR}/wasm/${APP_INSTALL_NAME}
DEPENDS wasm_gen_${APP_NAME})
endif()

endfunction()

function(wasm_add_library)
set_property(
TARGET wasm_interface
APPEND
PROPERTY WASM_DIR ${CMAKE_CURRENT_LIST_DIR})
endfunction()
24 changes: 11 additions & 13 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,7 @@
#
# ##############################################################################

# Fake wasm_add_application function to suppress error from native build process
function(wasm_add_application)

endfunction()

# Fake wasm_add_library function to suppress error from native build process
function(wasm_add_library)

endfunction()

if(CONFIG_TOOLS_WASM_BUILD)
if(CONFIG_TOOLS_WASM_BUILD OR CONFIG_INTERPRETERS_WAMR_BUILD_MODULES_FOR_NUTTX)

include(ExternalProject)

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

# Get the Wasm build dirs from the wasm_interface target
get_property(
WASM_DIRS
TARGET wasm_interface
PROPERTY WASM_DIR)

# ensure the Wasm build dirs are unique
list(REMOVE_DUPLICATES WASM_DIRS)
# Configure and build the Wasm based application
add_custom_target(
configure_wasm_build
COMMAND
${CMAKE_COMMAND} -B${CMAKE_BINARY_DIR}/Wasm
${CMAKE_CURRENT_SOURCE_DIR}/Wasm -DAPPDIR=${APPDIR} -DTOPDIR=${TOPDIR}
-DKCONFIG_FILE_PATH=${KCONFIG_FILE_PATH}
-DWASI_SDK_PATH=$ENV{WASI_SDK_PATH})
-DTOPBINDIR=${CMAKE_BINARY_DIR} -DKCONFIG_FILE_PATH=${KCONFIG_FILE_PATH}
-DWASI_SDK_PATH=$ENV{WASI_SDK_PATH} -DWASM_DIRS="${WASM_DIRS}")

add_custom_target(wasm_build COMMAND ${CMAKE_COMMAND} --build
${CMAKE_BINARY_DIR}/Wasm)
Expand Down
59 changes: 32 additions & 27 deletions tools/Wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,24 @@

cmake_minimum_required(VERSION 3.5)

cmake_policy(SET CMP0079 NEW)

# Include the NuttX kconfig parser to shared the configuration between the NuttX
# build and the Wasm build. And then parse the input KCONFIG_FILE_PATH to get
# the configuration.
include(${TOPDIR}/cmake/nuttx_kconfig.cmake)

# Parse the input KCONFIG_FILE_PATH to get the configuration.
nuttx_export_kconfig(${KCONFIG_FILE_PATH})

# Include the WASI-SDK.cmake file to setup the necessary flags for building
include(WASI-SDK.cmake)

project(WasmApps)

# Add the wasm_interface library to hold all the Wasm libraries.
add_library(wasm_interface INTERFACE)

# Check whether the APPDIR is defined or not. If not, then set it to the parent
# directory of the current CMakeLists.txt file.
if(NOT DEFINED APPDIR)
Expand All @@ -47,18 +61,21 @@ if(NOT DEFINED TOPDIR)
message(FATAL_ERROR "TOPDIR is not defined")
endif()

# Check wether the TOPBINDIR is defined or not. If not, then raise an error.
if(NOT DEFINED TOPBINDIR)
message(FATAL_ERROR "TOPBINDIR is not defined")
endif()

if(NOT EXISTS ${TOPBINDIR}/wasm)
file(MAKE_DIRECTORY ${TOPBINDIR}/wasm)
endif()

# Check wether the KCONFIG_FILE_PATH is defined or not. If not, then raise an
# error.
if(NOT DEFINED KCONFIG_FILE_PATH)
message(FATAL_ERROR "KCONFIG_FILE_PATH is not defined")
endif()

# Include the NuttX kconfig parser to shared the configuration between the NuttX
# build and the Wasm build. And then parse the input KCONFIG_FILE_PATH to get
# the configuration.
include(${TOPDIR}/cmake/nuttx_kconfig.cmake)
nuttx_export_kconfig(${KCONFIG_FILE_PATH})

# Provide FAR macro from command line since it is not supported in wasi-sdk, but
# it is used in NuttX code.
# ~~~
Expand All @@ -78,25 +95,13 @@ function(nuttx_add_library)

endfunction()

# Recursively find all the CMakeLists.txt files in the ${APPDIR} and add it by
# add_subdirectory, but exclude the CMakeLists.txt file in the ${APPDIR}/tools
# directory.
file(GLOB_RECURSE WASM_APPS ${APPDIR}/*/CMakeLists.txt)
list(FILTER WASM_APPS EXCLUDE REGEX ".*/tools/.*")

# Read and check if wasm_add_application is called in the CMakeLists.txt file in
# WASM_APPS If true, then add the directory to the build process
foreach(WASM_APP ${WASM_APPS})
file(READ ${WASM_APP} WASM_APP_CONTENTS)
string(FIND "${WASM_APP_CONTENTS}" "wasm_add_application" WASM_APP_FOUND)
string(FIND "${WASM_APP_CONTENTS}" "wasm_add_library" WASM_LIB_FOUND)
if(WASM_APP_FOUND GREATER -1 OR WASM_LIB_FOUND GREATER -1)
get_filename_component(WASM_APP_DIR ${WASM_APP} DIRECTORY)
# Add subdirectory to the build process and put the build directory in the
# current build directory with the name same as the relative path of the
# ${APPDIR}
string(REPLACE ${APPDIR} "" WASM_APP_BUILD_DIR ${WASM_APP_DIR})
add_subdirectory(${WASM_APP_DIR}
${CMAKE_CURRENT_BINARY_DIR}/Wasm/${WASM_APP_BUILD_DIR})
endif()
separate_arguments(WASM_DIRS)

# ~~~
# Add all the Wasm apps to the build process.
foreach(WASM_APP ${WASM_DIRS})
string(REPLACE ${APPDIR} "" WASM_APP_BUILD_DIR ${WASM_APP})
add_subdirectory(${WASM_APP}
${CMAKE_CURRENT_BINARY_DIR}/Wasm/${WASM_APP_BUILD_DIR})
endforeach()
# ~~~
Loading
Loading