Skip to content

Commit 9320597

Browse files
no1wudixiaoxiang781216
authored andcommitted
wamr: Add external module registration mechanism
This patch adds module registration mechanism for WAMR runtime. To register a module, these steps are required: 1. Include Module.mk in your module's Makefile 2. Define WAMR_MODULE_NAME as the module name 3. Implement bool wamr_module_WAMR_MODULE_NAME_register(void) function in your module's source file 4. Enable INTERPRETERS_WAMR_EXTERNAL_MODULE_REGISTRY in menuconfig Signed-off-by: Huang Qi <[email protected]>
1 parent df4cdba commit 9320597

File tree

9 files changed

+280
-2
lines changed

9 files changed

+280
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ if(CONFIG_APPS_DIR)
3030
# add apps cmake modules
3131
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
3232
include(nuttx_add_luamod)
33+
include(nuttx_add_wamrmod)
3334
nuttx_add_library(apps)
3435
if(NOT EXISTS {NUTTX_APPS_BINDIR}/dummy.c)
3536
file(TOUCH ${NUTTX_APPS_BINDIR}/dummy.c)

cmake/nuttx_add_wamrmod.cmake

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# ##############################################################################
2+
# cmake/nuttx_add_wamrmod.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+
set(WAMR_MODULE_DIR ${CMAKE_BINARY_DIR}/wamrmod)
22+
23+
if(NOT EXISTS {WAMR_MODULE_DIR})
24+
file(MAKE_DIRECTORY ${WAMR_MODULE_DIR})
25+
endif()
26+
27+
include(nuttx_parse_function_args)
28+
29+
# ~~~
30+
# nuttx_add_wamrmod
31+
#
32+
# Description:
33+
# Register custom module into WAMR runtime
34+
#
35+
# Example:
36+
# nuttx_add_wamrmod(
37+
# MODS
38+
# ${custom_mods}
39+
# )
40+
# ~~~
41+
42+
function(nuttx_add_wamrmod)
43+
44+
# parse arguments into variables
45+
46+
nuttx_parse_function_args(
47+
FUNC
48+
nuttx_add_wamrmod
49+
MULTI_VALUE
50+
MODS
51+
REQUIRED
52+
MODS
53+
ARGN
54+
${ARGN})
55+
56+
set(WAMR_MODULE_LIST ${WAMR_MODULE_DIR}/wamr_external_module_list.h)
57+
set(WAMR_MODULE_PROTO ${WAMR_MODULE_DIR}/wamr_external_module_proto.h)
58+
foreach(mod ${MODS})
59+
set(MOD_PDAT ${WAMR_MODULE_DIR}/${mod}.pdat)
60+
set(MOD_BDAT ${WAMR_MODULE_DIR}/${mod}.bdat)
61+
add_custom_command(
62+
OUTPUT ${MOD_PDAT} ${MOD_BDAT}
63+
COMMAND ${CMAKE_COMMAND} -E echo "wamr_module_${mod}_register," >>
64+
${WAMR_MODULE_LIST}
65+
COMMAND ${CMAKE_COMMAND} -E echo "bool wamr_module_${mod}_register(void);"
66+
>> ${WAMR_MODULE_PROTO}
67+
COMMAND ${CMAKE_COMMAND} -E touch ${MOD_PDAT}
68+
COMMAND ${CMAKE_COMMAND} -E touch ${MOD_BDAT}
69+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
70+
VERBATIM
71+
COMMENT "WAMR Module: Gen and Updating external module => ${mod}")
72+
add_custom_target(wamr_module_${mod} DEPENDS ${MOD_PDAT} ${MOD_BDAT})
73+
add_dependencies(apps_context wamr_module_${mod})
74+
endforeach()
75+
76+
endfunction()

interpreters/wamr/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.zip
22
wamr
33
wasm-micro-runtime-*
4+
wamr_external_module_list.h
5+
wamr_external_module_proto.h

interpreters/wamr/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,17 @@ if(CONFIG_INTERPRETERS_WAMR)
7171
DEPENDS
7272
wamr)
7373

74+
if(CONFIG_INTERPRETERS_WAMR_LIBC_NUTTX
75+
OR CONFIG_INTERPRETERS_WAMR_EXTERNAL_MODULE_REGISTRY)
76+
nuttx_append_source_file_properties(
77+
${WAMR_DIR}/product-mini/platforms/nuttx/main.c COMPILE_FLAGS
78+
-Dwasm_runtime_full_init=wamr_custom_init)
79+
target_sources(wamr PRIVATE wamr_custom_init.c)
80+
endif()
81+
82+
# Add ${CMAKE_BINARY_DIR}/wamrmod to the include directories
83+
if(CONFIG_INTERPRETERS_WAMR_EXTERNAL_MODULE_REGISTRY)
84+
target_include_directories(wamr PRIVATE ${CMAKE_BINARY_DIR}/wamrmod)
85+
endif()
86+
7487
endif()

interpreters/wamr/Kconfig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,12 @@ config INTERPRETERS_WAMR_CONFIGUABLE_BOUNDS_CHECKS
248248
disable bounds checks passing --disable-bounds-checks to
249249
iwasm.
250250

251-
endif # INTERPRETERS_WAMR
251+
config INTERPRETERS_WAMR_EXTERNAL_MODULE_REGISTRY
252+
bool "Enable external module registry"
253+
default n
254+
---help---
255+
This option enables the support for loading WASM modules from
256+
external libraries, which is useful when you want to extend the
257+
functionality of WAMR without modifying its source code.
258+
259+
endif

interpreters/wamr/Makefile

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ PROGNAME = iwasm
3939
PRIORITY = $(CONFIG_INTERPRETERS_WAMR_PRIORITY)
4040
STACKSIZE = $(CONFIG_INTERPRETERS_WAMR_STACKSIZE)
4141
MODULE = $(CONFIG_INTERPRETERS_WAMR)
42+
43+
endif
44+
45+
WAMR_NEED_CUSTOM_INIT = n
46+
47+
# Check if we need to build custom init code
48+
49+
ifeq ($(CONFIG_INTERPRETERS_WAMR_EXTERNAL_MODULE_REGISTRY),y)
50+
WAMR_NEED_CUSTOM_INIT = y
51+
endif
52+
53+
# If we need custom init code, add it to the build
54+
ifeq ($(WAMR_NEED_CUSTOM_INIT),y)
55+
# When the loadable module configuration is enabled,we need to use CELFFLAGS
56+
$(WAMR_UNPACK)/product-mini/platforms/nuttx/main.c_CFLAGS = -Dwasm_runtime_full_init=wamr_custom_init
57+
$(WAMR_UNPACK)/product-mini/platforms/nuttx/main.c_CELFFLAGS = -Dwasm_runtime_full_init=wamr_custom_init
58+
CSRCS += wamr_custom_init.c
4259
endif
4360

4461
$(WAMR_TARBALL):
@@ -51,11 +68,50 @@ $(WAMR_UNPACK): $(WAMR_TARBALL)
5168
$(Q) mv wasm-micro-runtime-$(WAMR_VERSION) $(WAMR_UNPACK)
5269
$(Q) touch $(WAMR_UNPACK)
5370

71+
# Register the external WAMR module
72+
73+
ifeq ($(CONFIG_INTERPRETERS_WAMR_EXTERNAL_MODULE_REGISTRY),y)
74+
75+
PDATLIST = $(strip $(call RWILDCARD, registry, *.pdat))
76+
BDATLIST = $(strip $(call RWILDCARD, registry, *.bdat))
77+
78+
registry$(DELIM).updated:
79+
$(Q) touch registry$(DELIM).updated
80+
81+
wamr_external_module_list.h: registry$(DELIM).updated
82+
ifeq ($(BDATLIST),)
83+
$(call DELFILE, wamr_external_module_list.h)
84+
$(Q) touch wamr_external_module_list.h
85+
else
86+
$(call CATFILE, wamr_external_module_list.h, $(BDATLIST))
87+
endif
88+
89+
wamr_external_module_proto.h: registry$(DELIM).updated
90+
ifeq ($(PDATLIST),)
91+
$(call DELFILE, wamr_external_module_proto.h)
92+
$(Q) touch wamr_external_module_proto.h
93+
else
94+
$(call CATFILE, wamr_external_module_proto.h, $(PDATLIST))
95+
endif
96+
97+
depend:: $(GLUECSRCS) wamr_external_module_list.h wamr_external_module_proto.h
98+
endif # CONFIG_INTERPRETERS_WAMR_EXTERNAL_MODULE_REGISTRY
99+
100+
clean::
101+
$(call DELFILE, wamr_external_module_list.h)
102+
$(call DELFILE, wamr_external_module_proto.h)
103+
104+
clean_context::
105+
$(call DELFILE, $(BDATLIST))
106+
$(call DELFILE, $(PDATLIST))
107+
54108
# Download and unpack tarball if no git repo found
55109
ifeq ($(wildcard $(WAMR_UNPACK)/.git),)
56110
context:: $(WAMR_UNPACK)
111+
endif
57112

58-
distclean::
113+
distclean:: clean clean_context
114+
ifeq ($(wildcard $(WAMR_UNPACK)/.git),)
59115
$(call DELDIR, $(WAMR_UNPACK))
60116
$(call DELFILE, $(WAMR_TARBALL))
61117
endif

interpreters/wamr/Module.mk

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
############################################################################
2+
# apps/interpreters/wamr/Module.mk
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of 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
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
WAMR_MODULE_REGISTRY = $(APPDIR)$(DELIM)interpreters$(DELIM)wamr$(DELIM)registry
22+
23+
define WAMR_MODULE_REGISTER
24+
$(Q) echo Register WAMR Module: $1
25+
$(Q) echo "$2," > "$(WAMR_MODULE_REGISTRY)$(DELIM)$1.bdat"
26+
$(Q) echo "bool $2(void);" > "$(WAMR_MODULE_REGISTRY)$(DELIM)$1.pdat"
27+
$(Q) touch "$(WAMR_MODULE_REGISTRY)$(DELIM).updated"
28+
endef
29+
30+
ifneq ($(WAMR_MODULE_NAME),)
31+
WAMR_MODULE_LIST := $(addprefix $(WAMR_MODULE_REGISTRY)$(DELIM),$(addsuffix .bdat,$(WAMR_MODULE_NAME)))
32+
$(WAMR_MODULE_LIST): $(DEPCONFIG) Makefile
33+
$(call WAMR_MODULE_REGISTER,$(firstword $(WAMR_MODULE_NAME)),$(firstword wamr_module_$(WAMR_MODULE_NAME)_register))
34+
$(eval WAMR_MODULE_NAME=$(filter-out $(firstword $(WAMR_MODULE_NAME)),$(WAMR_MODULE_NAME)))
35+
36+
register:: $(WAMR_MODULE_LIST)
37+
endif

interpreters/wamr/registry/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.updated
2+
*.pdat
3+
*.bdat

interpreters/wamr/wamr_custom_init.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/****************************************************************************
2+
* apps/interpreters/wamr/wamr_custom_init.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of 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
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <stdio.h>
26+
#include <stdlib.h>
27+
#include <stdbool.h>
28+
#include <stdint.h>
29+
#include <semaphore.h>
30+
#include <sys/param.h>
31+
#include <sys/types.h>
32+
#include <iconv.h>
33+
34+
#include "wasm_native.h"
35+
36+
#include "wamr_custom_init.h"
37+
38+
#ifdef CONFIG_INTERPRETERS_WAMR_EXTERNAL_MODULE_REGISTRY
39+
#include "wamr_external_module_proto.h"
40+
#endif
41+
42+
/****************************************************************************
43+
* Private Data
44+
****************************************************************************/
45+
46+
typedef bool (*module_register_t)(void);
47+
48+
#ifdef CONFIG_INTERPRETERS_WAMR_EXTERNAL_MODULE_REGISTRY
49+
static const module_register_t g_wamr_modules[] =
50+
{
51+
#include "wamr_external_module_list.h"
52+
};
53+
#endif
54+
55+
/****************************************************************************
56+
* Public Functions
57+
****************************************************************************/
58+
59+
bool wamr_custom_init(RuntimeInitArgs *init_args)
60+
{
61+
bool ret = wasm_runtime_full_init(init_args);
62+
63+
if (!ret)
64+
{
65+
return ret;
66+
}
67+
68+
/* Add extra init hook here */
69+
70+
#ifdef CONFIG_INTERPRETERS_WAMR_EXTERNAL_MODULE_REGISTRY
71+
for (int i = 0; i < nitems(g_wamr_modules); i++)
72+
{
73+
ret = g_wamr_modules[i]();
74+
if (!ret)
75+
{
76+
return ret;
77+
}
78+
}
79+
#endif
80+
81+
return ret;
82+
}

0 commit comments

Comments
 (0)