Skip to content

Commit 3d8dc5d

Browse files
xuxin930xiaoxiang781216
authored andcommitted
cmake:implement CMake build for NuttX Lua interpreter
add CMake module for register lua mod nsh> lua Lua 5.4.0 Copyright (C) 1994-2020 Lua.org, PUC-Rio > hello.say_hello() Hello World! > io.write("abs is =",math.abs(-10),"\n") abs is =10 > os.exit() nsh> Signed-off-by: xuxin19 <[email protected]>
1 parent 1f51bfe commit 3d8dc5d

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
# ~~~
2828

2929
if(CONFIG_APPS_DIR)
30+
# add apps cmake modules
31+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
32+
include(nuttx_add_luamod)
3033
nuttx_add_library(apps)
3134
if(NOT EXISTS {NUTTX_APPS_BINDIR}/dummy.c)
3235
file(TOUCH ${NUTTX_APPS_BINDIR}/dummy.c)

cmake/nuttx_add_luamod.cmake

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# ##############################################################################
2+
# cmake/nuttx_add_luamod.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(LUAMOD_DIR ${CMAKE_BINARY_DIR}/luamod)
22+
23+
if(NOT EXISTS {LUAMOD_DIR})
24+
file(MAKE_DIRECTORY ${LUAMOD_DIR})
25+
endif()
26+
27+
include(nuttx_parse_function_args)
28+
29+
# ~~~
30+
# nuttx_add_luamod
31+
#
32+
# Description:
33+
# Register custom Lua MOD to Lua interpreter
34+
#
35+
# Example:
36+
# nuttx_add_luamod(
37+
# MODS
38+
# ${custom_mods}
39+
#
40+
# ~~~
41+
42+
function(nuttx_add_luamod)
43+
44+
# parse arguments into variables
45+
46+
nuttx_parse_function_args(
47+
FUNC
48+
nuttx_add_luamod
49+
MULTI_VALUE
50+
MODS
51+
REQUIRED
52+
MODS
53+
ARGN
54+
${ARGN})
55+
56+
set(LUAMOD_LIST ${LUAMOD_DIR}/luamod_list.h)
57+
set(LUAMOD_PROTO ${LUAMOD_DIR}/luamod_proto.h)
58+
foreach(mod ${MODS})
59+
set(MOD_PDAT ${LUAMOD_DIR}/${mod}.pdat)
60+
set(MOD_BDAT ${LUAMOD_DIR}/${mod}.bdat)
61+
add_custom_command(
62+
OUTPUT ${MOD_PDAT} ${MOD_BDAT}
63+
COMMAND ${CMAKE_COMMAND} -E echo "{\"${mod}\", luaopen_${mod}}," >>
64+
${LUAMOD_LIST}
65+
COMMAND ${CMAKE_COMMAND} -E echo "int luaopen_${mod}(lua_State *L);" >>
66+
${LUAMOD_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 "Lua Mod:Gen and Updating lua mod => ${mod}")
72+
add_custom_target(lua_${mod} DEPENDS ${MOD_PDAT} ${MOD_BDAT})
73+
add_dependencies(apps_context lua_${mod})
74+
endforeach()
75+
76+
endfunction()

examples/lua_module/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ##############################################################################
2+
# apps/examples/lua_module/CMakeLists.txt
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+
if(CONFIG_EXAMPLES_LUA_MODULE)
22+
23+
target_sources(apps PRIVATE luamod_hello.c)
24+
25+
# register lua mod
26+
nuttx_add_luamod(MODS hello)
27+
endif()

interpreters/lua/CMakeLists.txt

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# ##############################################################################
2+
# apps/interpreters/lua/CMakeLists.txt
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+
if(CONFIG_INTERPRETERS_LUA)
22+
23+
# ############################################################################
24+
# Config and Fetch lua
25+
# ############################################################################
26+
27+
set(LUA_DIR ${CMAKE_CURRENT_LIST_DIR}/lua)
28+
29+
if(NOT EXISTS ${LUA_DIR})
30+
set(LUA_URL https://github.com/lua/lua/archive/refs/tags/)
31+
FetchContent_Declare(
32+
lua_fetch
33+
URL ${LUA_URL}/v${CONFIG_INTERPRETER_LUA_VERSION}.tar.gz SOURCE_DIR
34+
${CMAKE_CURRENT_LIST_DIR}/lua BINARY_DIR
35+
${CMAKE_BINARY_DIR}/apps/interpreters/lua/lua
36+
DOWNLOAD_NO_PROGRESS true
37+
TIMEOUT 30)
38+
39+
FetchContent_GetProperties(lua_fetch)
40+
41+
if(NOT lua_fetch_POPULATED)
42+
FetchContent_Populate(lua_fetch)
43+
endif()
44+
endif()
45+
46+
# ############################################################################
47+
# Flags
48+
# ############################################################################
49+
50+
set(CFLAGS -DLUA_MAXINPUT=${CONFIG_INTERPRETER_LUA_IOBUFSIZE}
51+
-DLUA_PROGNAME=\"lua\")
52+
if(CONFIG_INTERPRETER_LUA_32BITS)
53+
list(APPEND CFLAGS -DLUA_32BITS)
54+
endif()
55+
if(NOT "${CONFIG_INTERPRETER_LUA_PATH}" STREQUAL "")
56+
list(APPEND CFLAGS -DLUA_PATH_DEFAULT=\"${CONFIG_INTERPRETER_LUA_PATH}\")
57+
endif()
58+
59+
if(NOT "${CONFIG_INTERPRETER_LUA_CPATH}" STREQUAL "")
60+
list(APPEND CFLAGS -DLUA_CPATH_DEFAULT=\"${CONFIG_INTERPRETER_LUA_CPATH}\")
61+
endif()
62+
63+
if(CONFIG_SYSTEM_READLINE)
64+
set(LUA_SYSTEM_READLINE_DEF
65+
[=[
66+
#define lua_initreadline(L) ((void)L)
67+
#define lua_readline(L,b,p) ((void)L,fputs(p,stdout),fflush(stdout),readline_stream(b,LUA_MAXINPUT,stdin,stdout))
68+
#define lua_saveline(L,line) {(void)L;(void)line;}
69+
#define lua_freeline(L,line) {(void)L;(void)b;}
70+
]=])
71+
72+
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/def_lua_readline.h
73+
"${LUA_SYSTEM_READLINE_DEF}")
74+
list(APPEND CFLAGS "SHELL:-include \"system/readline.h\"")
75+
list(APPEND CFLAGS
76+
"SHELL:-include ${CMAKE_CURRENT_BINARY_DIR}/def_lua_readline.h")
77+
78+
endif()
79+
80+
# ############################################################################
81+
# Sources
82+
# ############################################################################
83+
84+
file(GLOB CORELIBS_SRCS ${LUA_DIR}/*lib.c)
85+
list(REMOVE_ITEM CORELIBS_SRCS ${LUA_DIR}/lauxlib.c)
86+
file(GLOB CSRCS ${LUA_DIR}/*.c)
87+
list(
88+
REMOVE_ITEM
89+
CSRCS
90+
${LUA_DIR}/onelua.c
91+
${LUA_DIR}/luac.c
92+
${LUA_DIR}/linit.c
93+
${LUA_DIR}/lua.c
94+
${CORELIBS_SRCS})
95+
list(APPEND CSRCS nuttx_linit.c)
96+
97+
if(CONFIG_INTERPRETER_LUA_CORELIBS)
98+
list(APPEND CSRCS ${CORELIBS_SRCS})
99+
100+
set(LUA_LIB_H_PATH ${LUA_DIR}/lualib.h)
101+
file(READ "${LUA_LIB_H_PATH}" FILE_CONTENTS)
102+
string(REGEX MATCHALL "#define[ \t]+LUA_[A-Z]+LIBNAME[ \t]+\"[^\"]+\""
103+
LIB_DEFINITIONS "${FILE_CONTENTS}")
104+
foreach(DEFINITION IN LISTS LIB_DEFINITIONS)
105+
string(REGEX REPLACE ".*\"([^\"]+)\"" "\\1" LIB_NAME "${DEFINITION}")
106+
# register lua core mod
107+
nuttx_add_luamod(MODS ${LIB_NAME})
108+
endforeach()
109+
endif()
110+
111+
# ############################################################################
112+
# Include Directory
113+
# ############################################################################
114+
115+
set(INCDIR ${LUAMOD_DIR})
116+
117+
# ############################################################################
118+
# Library Configuration
119+
# ############################################################################
120+
121+
set_property(
122+
TARGET nuttx
123+
APPEND
124+
PROPERTY NUTTX_INCLUDE_DIRECTORIES ${LUA_DIR})
125+
126+
# ############################################################################
127+
# Applications Configuration
128+
# ############################################################################
129+
130+
nuttx_add_application(
131+
MODULE
132+
${CONFIG_INTERPRETERS_LUA}
133+
NAME
134+
lua
135+
STACKSIZE
136+
${CONFIG_INTERPRETER_LUA_STACKSIZE}
137+
PRIORITY
138+
${CONFIG_INTERPRETER_LUA_PRIORITY}
139+
SRCS
140+
${LUA_DIR}/lua.c
141+
${CSRCS}
142+
INCLUDE_DIRECTORIES
143+
${INCDIR}
144+
COMPILE_FLAGS
145+
${CFLAGS})
146+
147+
endif()

0 commit comments

Comments
 (0)