Skip to content
This repository was archived by the owner on Nov 20, 2020. It is now read-only.

Commit b9a70a6

Browse files
committed
Updated CMake macros. Clean up of CMakeLists.txt
1 parent 3011a2c commit b9a70a6

File tree

2 files changed

+68
-164
lines changed

2 files changed

+68
-164
lines changed

CMakeLists.txt

+37-33
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Please note that the package source code is licensed under its own license.
66

77
project ( lua C )
8-
cmake_minimum_required ( VERSION 2.6 )
8+
cmake_minimum_required ( VERSION 2.8 )
99
include ( cmake/dist.cmake )
1010
include ( lua )
1111

@@ -16,14 +16,14 @@ set ( LUA_CPATH "LUA_CPATH" CACHE STRING "Environment variable to use as package
1616
set ( LUA_INIT "LUA_INIT" CACHE STRING "Environment variable for initial script." )
1717

1818
option ( LUA_ANSI "Use only ansi features." OFF )
19-
option ( LUA_USE_RELATIVE_LOADLIB "Use modified loadlib.c with support for relative paths on posix systems." ON)
19+
option ( LUA_USE_RELATIVE_LOADLIB "Use modified loadlib.c with support for relative paths on posix systems."
20+
ON )
2021
set ( LUA_IDSIZE 60 CACHE NUMBER "gives the maximum size for the description of the source." )
2122
set ( LUA_PROMPT "> " CACHE STRING "Is the default prompt used by stand-alone Lua." )
2223
set ( LUA_PROMPT2 ">> " CACHE STRING "Is the default continuation prompt used by stand-alone Lua." )
23-
set ( LUA_MAXINPUT 512 CACHE NUMBER "Is the maximum length for an input line in the stand-alone interpreter.")
24+
set ( LUA_MAXINPUT 512 CACHE NUMBER "Is the maximum length for an input line in the stand-alone interpreter." )
2425

2526
#2DO: LUAI_* and LUAL_* settings, for now defaults are used.
26-
2727
set ( LUA_DIRSEP "/" )
2828
set ( LUA_MODULE_SUFFIX ${CMAKE_SHARED_MODULE_SUFFIX} )
2929
set ( LUA_LDIR ${INSTALL_LMOD} )
@@ -32,58 +32,57 @@ set ( LUA_CDIR ${INSTALL_CMOD} )
3232
if ( LUA_USE_RELATIVE_LOADLIB )
3333
# This will set up relative paths to lib
3434
string ( REGEX REPLACE "[^!/]+" ".." LUA_DIR "!/${INSTALL_BIN}/" )
35-
else ()
35+
else ( )
3636
# Direct path to installation
37-
set ( LUA_DIR ${CMAKE_INSTALL_PREFIX} CACHE STRING "Destination from which modules will be resolved. See INSTALL_LMOD and INSTALL_CMOD.")
38-
endif ()
37+
set ( LUA_DIR ${CMAKE_INSTALL_PREFIX} CACHE STRING "Destination from which modules will be resolved. See INSTALL_LMOD and INSTALL_CMOD." )
38+
endif ( )
3939

4040
set ( LUA_PATH_DEFAULT "./?.lua;${LUA_DIR}${LUA_LDIR}/?.lua;${LUA_DIR}${LUA_LDIR}/?/init.lua;./?/init.lua" )
4141
set ( LUA_CPATH_DEFAULT "./?${LUA_MODULE_SUFFIX};${LUA_DIR}${LUA_CDIR}/?${LUA_MODULE_SUFFIX};${LUA_DIR}${LUA_CDIR}/loadall${LUA_MODULE_SUFFIX}" )
4242

4343
if ( WIN32 AND NOT CYGWIN )
4444
# Windows systems
4545
option ( LUA_WIN "Windows specific build." ON )
46-
option ( LUA_BUILD_WLUA "Build wLua interpretter without console output." ON)
46+
option ( LUA_BUILD_WLUA "Build wLua interpretter without console output." ON )
4747
option ( LUA_BUILD_AS_DLL "Build Lua library as Dll." ON )
4848
# Paths (Double escapes needed)
49-
set ( LUA_DIRSEP "\\\\" )
50-
string ( REPLACE "/" ${LUA_DIRSEP} LUA_DIR "${LUA_DIR}" )
49+
set ( LUA_DIRSEP "\\\\" ) string ( REPLACE " /" ${LUA_DIRSEP} LUA_DIR "${LUA_DIR}" )
5150
string ( REPLACE "/" ${LUA_DIRSEP} LUA_LDIR "${LUA_LDIR}" )
5251
string ( REPLACE "/" ${LUA_DIRSEP} LUA_CDIR "${LUA_CDIR}" )
5352
string ( REPLACE "/" ${LUA_DIRSEP} LUA_PATH_DEFAULT "${LUA_PATH_DEFAULT}" )
5453
string ( REPLACE "/" ${LUA_DIRSEP} LUA_CPATH_DEFAULT "${LUA_CPATH_DEFAULT}" )
55-
else ()
54+
else ( )
5655
# Posix systems (incl. Cygwin)
5756
option ( LUA_USE_POSIX "Use POSIX functionality." ON )
5857
option ( LUA_USE_DLOPEN "Use dynamic linker to load modules." ON )
5958
option ( LUA_USE_MKSTEMP "Use mkstep." ON )
6059
option ( LUA_USE_ISATTY "Use tty." ON )
6160
option ( LUA_USE_POPEN "Use popen." ON )
62-
option ( LUA_USE_ULONGJMP "Use ulongjmp" ON)
63-
endif ()
61+
option ( LUA_USE_ULONGJMP "Use ulongjmp" ON )
62+
endif ( )
6463

6564
## SETUP
6665
# Optional libraries
6766
find_package ( Readline )
6867
if ( READLINE_FOUND )
6968
option ( LUA_USE_READLINE "Use readline in the Lua CLI." ON )
70-
endif ()
69+
endif ( )
7170

7271
find_package ( Curses )
7372
if ( CURSES_FOUND )
7473
option ( LUA_USE_CURSES "Use curses in the Lua CLI." ON )
75-
endif ()
74+
endif ( )
7675

7776
# Setup needed variables and libraries
7877
if ( LUA_USE_POSIX )
7978
# On POSIX Lua links to standard math library "m"
8079
list ( APPEND LIBS m )
81-
endif ()
80+
endif ( )
8281

8382
if ( LUA_USE_DLOPEN )
8483
# Link to dynamic linker library "dl"
8584
list ( APPEND LIBS dl )
86-
endif ()
85+
endif ( )
8786

8887
if ( LUA_WIN )
8988
# Add extra rc files to the windows build
@@ -92,37 +91,41 @@ if ( LUA_WIN )
9291
set ( LUA_DLL_RC src/lua_dll.rc )
9392
set ( LUA_RC src/lua.rc )
9493
set ( LUAC_RC src/luac.rc )
95-
endif ()
96-
endif ()
94+
endif ( )
95+
endif ( )
9796

9897
if ( LUA_USE_READLINE )
9998
# Add readline
10099
include_directories ( ${READLINE_INCLUDE_DIR} )
101100
list ( APPEND LIBS ${READLINE_LIBRARY} )
102-
endif ()
101+
endif ( )
103102

104103
if ( LUA_USE_CURSES )
105104
# Add curses
106105
include_directories ( ${CURSES_INCLUDE_DIR} )
107106
list ( APPEND LIBS ${CURSES_LIBRARY} )
108-
endif ()
107+
endif ( )
109108

110109
## SOURCES
111110
# Generate luaconf.h
112111
configure_file ( src/luaconf.h.in ${CMAKE_CURRENT_BINARY_DIR}/luaconf.h )
113112

114113
# Sources and headers
115114
include_directories ( src ${CMAKE_CURRENT_BINARY_DIR} )
116-
set ( SRC_LIB src/lapi.c src/lcode.c src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c src/lgc.c src/llex.c src/lmem.c src/lobject.c src/lopcodes.c src/lparser.c src/lstate.c src/lstring.c src/ltable.c src/ltm.c src/lundump.c src/lvm.c src/lzio.c src/lauxlib.c src/lbaselib.c src/ldblib.c src/liolib.c src/lmathlib.c src/loslib.c src/ltablib.c src/lstrlib.c src/linit.c )
115+
set ( SRC_LIB src/lapi.c src/lcode.c src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c
116+
src/lgc.c src/llex.c src/lmem.c src/lobject.c src/lopcodes.c src/lparser.c src/lstate.c
117+
src/lstring.c src/ltable.c src/ltm.c src/lundump.c src/lvm.c src/lzio.c src/lauxlib.c
118+
src/lbaselib.c src/ldblib.c src/liolib.c src/lmathlib.c src/loslib.c src/ltablib.c
119+
src/lstrlib.c src/linit.c )
117120
set ( SRC_LUA src/lua.c )
118121
set ( SRC_LUAC src/luac.c src/print.c )
119122

120123
if ( LUA_USE_RELATIVE_LOADLIB )
121124
# Use modified loadlib
122125
list ( APPEND SRC_LIB src/loadlib_rel.c )
123-
else ()
126+
else ( )
124127
list ( APPEND SRC_LIB src/loadlib.c )
125-
endif ()
128+
endif ( )
126129

127130
## BUILD
128131
# Create dynamic library
@@ -138,11 +141,11 @@ add_executable ( lua ${SRC_LUA} ${LUA_RC} )
138141
target_link_libraries ( lua liblua )
139142

140143
# On windows a variant of the lua interpreter without console output needs to be built
141-
if(LUA_BUILD_WLUA)
144+
if ( LUA_BUILD_WLUA )
142145
add_executable ( wlua WIN32 ${SRC_LUA} ${LUA_RC} )
143-
target_link_libraries ( wlua liblua )
146+
target_link_libraries ( wlua liblua )
144147
install_executable ( wlua )
145-
endif()
148+
endif ( )
146149

147150
add_executable ( luac ${SRC_LUAC} ${LUAC_RC} )
148151
target_link_libraries ( luac liblua_static )
@@ -151,13 +154,13 @@ install_executable ( lua luac )
151154
install_library ( liblua )
152155
install_data ( README.md COPYRIGHT HISTORY )
153156
install_lua_module ( strict etc/strict.lua )
154-
install_header (src/lua.h src/lualib.h src/lauxlib.h etc/lua.hpp ${CMAKE_CURRENT_BINARY_DIR}/luaconf.h )
157+
install_header ( src/lua.h src/lualib.h src/lauxlib.h etc/lua.hpp ${CMAKE_CURRENT_BINARY_DIR}/luaconf.h )
155158
install_doc ( doc/ )
156159
install_foo ( etc/ )
157160
install_test ( test/ )
158161

159162
## TESTS
160-
set(LUA lua)
163+
set ( LUA lua )
161164

162165
add_lua_test ( test/bisect.lua )
163166
add_lua_test ( test/cf.lua )
@@ -168,10 +171,11 @@ add_lua_test ( test/fib.lua 20 )
168171
add_lua_test ( test/fibfor.lua )
169172
#add_lua_test ( test/globals.lua ) # Requires input
170173
add_lua_test ( test/hello.lua )
171-
file(READ test/life.lua _data) # life.lua test, with reduced run-time.
172-
string(REPLACE "40,20" "20,15" _data "${_data}")
173-
string(REPLACE 2000 20 _data "${_data}")
174-
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/test/life-quick.lua "${_data}")
174+
file ( READ test/life.lua _data )
175+
# life.lua test, with reduced run-time.
176+
string ( REPLACE "40,20" "20,15" _data "${_data}" )
177+
string ( REPLACE 2000 20 _data "${_data}" )
178+
file ( WRITE ${CMAKE_CURRENT_BINARY_DIR}/test/life-quick.lua "${_data}" )
175179
add_lua_test ( ${CMAKE_CURRENT_BINARY_DIR}/test/life-quick.lua )
176180
#add_lua_test ( test/luac.lua ) # Requires input
177181
add_lua_test ( test/printf.lua )

cmake/lua.cmake

+31-131
Original file line numberDiff line numberDiff line change
@@ -29,141 +29,39 @@ macro ( _append_path basepath path result )
2929
endmacro ()
3030

3131
# install_lua_executable ( target source )
32-
# Automatically generate a binary wrapper for lua application and install it
33-
# The wrapper and the source of the application will be placed into /bin
32+
# Automatically generate a binary if srlua package is available
33+
# The application or its source will be placed into /bin
3434
# If the application source did not have .lua suffix then it will be added
3535
# USE: lua_executable ( sputnik src/sputnik.lua )
3636
macro ( install_lua_executable _name _source )
3737
get_filename_component ( _source_name ${_source} NAME_WE )
38-
if ( NOT SKIP_LUA_WRAPPER )
39-
enable_language ( C )
38+
# Find srlua and glue
39+
find_program( SRLUA_EXECUTABLE NAMES srlua )
40+
find_program( GLUE_EXECUTABLE NAMES glue )
4041

41-
find_package ( Lua REQUIRED )
42-
include_directories ( ${LUA_INCLUDE_DIR} )
43-
44-
set ( _wrapper ${CMAKE_CURRENT_BINARY_DIR}/${_name}.c )
45-
set ( _code
46-
"// Not so simple executable wrapper for Lua apps
47-
#include <stdio.h>
48-
#include <signal.h>
49-
#include <lua.h>
50-
#include <lauxlib.h>
51-
#include <lualib.h>
52-
53-
lua_State *L\;
54-
55-
static int getargs (lua_State *L, char **argv, int n) {
56-
int narg\;
57-
int i\;
58-
int argc = 0\;
59-
while (argv[argc]) argc++\;
60-
narg = argc - (n + 1)\;
61-
luaL_checkstack(L, narg + 3, \"too many arguments to script\")\;
62-
for (i=n+1\; i < argc\; i++)
63-
lua_pushstring(L, argv[i])\;
64-
lua_createtable(L, narg, n + 1)\;
65-
for (i=0\; i < argc\; i++) {
66-
lua_pushstring(L, argv[i])\;
67-
lua_rawseti(L, -2, i - n)\;
68-
}
69-
return narg\;
70-
}
71-
72-
static void lstop (lua_State *L, lua_Debug *ar) {
73-
(void)ar\;
74-
lua_sethook(L, NULL, 0, 0)\;
75-
luaL_error(L, \"interrupted!\")\;
76-
}
77-
78-
static void laction (int i) {
79-
signal(i, SIG_DFL)\;
80-
lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1)\;
81-
}
82-
83-
static void l_message (const char *pname, const char *msg) {
84-
if (pname) fprintf(stderr, \"%s: \", pname)\;
85-
fprintf(stderr, \"%s\\n\", msg)\;
86-
fflush(stderr)\;
87-
}
88-
89-
static int report (lua_State *L, int status) {
90-
if (status && !lua_isnil(L, -1)) {
91-
const char *msg = lua_tostring(L, -1)\;
92-
if (msg == NULL) msg = \"(error object is not a string)\"\;
93-
l_message(\"${_source_name}\", msg)\;
94-
lua_pop(L, 1)\;
95-
}
96-
return status\;
97-
}
98-
99-
static int traceback (lua_State *L) {
100-
if (!lua_isstring(L, 1))
101-
return 1\;
102-
lua_getfield(L, LUA_GLOBALSINDEX, \"debug\")\;
103-
if (!lua_istable(L, -1)) {
104-
lua_pop(L, 1)\;
105-
return 1\;
106-
}
107-
lua_getfield(L, -1, \"traceback\")\;
108-
if (!lua_isfunction(L, -1)) {
109-
lua_pop(L, 2)\;
110-
return 1\;
111-
}
112-
lua_pushvalue(L, 1)\;
113-
lua_pushinteger(L, 2)\;
114-
lua_call(L, 2, 1)\;
115-
return 1\;
116-
}
117-
118-
static int docall (lua_State *L, int narg, int clear) {
119-
int status\;
120-
int base = lua_gettop(L) - narg\;
121-
lua_pushcfunction(L, traceback)\;
122-
lua_insert(L, base)\;
123-
signal(SIGINT, laction)\;
124-
status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base)\;
125-
signal(SIGINT, SIG_DFL)\;
126-
lua_remove(L, base)\;
127-
if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0)\;
128-
return status\;
129-
}
130-
131-
int main (int argc, char **argv) {
132-
L=lua_open()\;
133-
lua_gc(L, LUA_GCSTOP, 0)\;
134-
luaL_openlibs(L)\;
135-
lua_gc(L, LUA_GCRESTART, 0)\;
136-
int narg = getargs(L, argv, 0)\;
137-
lua_setglobal(L, \"arg\")\;
138-
139-
// Script
140-
char script[500] = \"./${_source_name}.lua\"\;
141-
lua_getglobal(L, \"_PROGDIR\")\;
142-
if (lua_isstring(L, -1)) {
143-
sprintf( script, \"%s/${_source_name}.lua\", lua_tostring(L, -1))\;
144-
}
145-
lua_pop(L, 1)\;
146-
147-
// Run
148-
int status = luaL_loadfile(L, script)\;
149-
lua_insert(L, -(narg+1))\;
150-
if (status == 0)
151-
status = docall(L, narg, 0)\;
152-
else
153-
lua_pop(L, narg)\;
154-
155-
report(L, status)\;
156-
lua_close(L)\;
157-
return status\;
158-
};
159-
")
160-
file ( WRITE ${_wrapper} ${_code} )
161-
add_executable ( ${_name} ${_wrapper} )
162-
target_link_libraries ( ${_name} ${LUA_LIBRARY} )
163-
install ( TARGETS ${_name} DESTINATION ${INSTALL_BIN} )
42+
if ( NOT SKIP_LUA_WRAPPER AND SRLUA_EXECUTABLE AND GLUE_EXECUTABLE )
43+
# Generate binary gluing the lua code to srlua
44+
add_custom_command(
45+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_name}
46+
COMMAND ${GLUE_EXECUTABLE}
47+
ARGS ${SRLUA_EXECUTABLE} ${_source} ${CMAKE_CURRENT_BINARY_DIR}/${_name}
48+
DEPENDS ${_source}
49+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
50+
VERBATIM
51+
)
52+
# Make sure we have a target associated with the binary
53+
add_custom_target(${_name} ALL
54+
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_name}
55+
)
56+
# Install with run permissions
57+
install ( PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${_name} DESTINATION ${INSTALL_BIN} COMPONENT Runtime)
58+
else()
59+
# Add .lua suffix and install as is
60+
install ( PROGRAMS ${_source} DESTINATION ${INSTALL_BIN}
61+
RENAME ${_source_name}.lua
62+
COMPONENT Runtime
63+
)
16464
endif()
165-
install ( PROGRAMS ${_source} DESTINATION ${INSTALL_BIN}
166-
RENAME ${_source_name}.lua )
16765
endmacro ()
16866

16967
macro ( _lua_module_helper is_install _name )
@@ -206,7 +104,9 @@ macro ( _lua_module_helper is_install _name )
206104

207105
if ( ${is_install} )
208106
install ( FILES ${_first_source} DESTINATION ${INSTALL_LMOD}/${_module_dir}
209-
RENAME ${_module_filename} )
107+
RENAME ${_module_filename}
108+
COMPONENT Runtime
109+
)
210110
endif ()
211111
else () # Lua C binary module
212112
enable_language ( C )
@@ -227,7 +127,7 @@ macro ( _lua_module_helper is_install _name )
227127
set_target_properties ( ${_target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
228128
"${_module_dir}" PREFIX "" OUTPUT_NAME "${_module_filenamebase}" )
229129
if ( ${is_install} )
230-
install ( TARGETS ${_target} DESTINATION ${INSTALL_CMOD}/${_module_dir})
130+
install ( TARGETS ${_target} DESTINATION ${INSTALL_CMOD}/${_module_dir} COMPONENT Runtime)
231131
endif ()
232132
endif ()
233133
endmacro ()

0 commit comments

Comments
 (0)