|
| 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