@@ -29,141 +29,39 @@ macro ( _append_path basepath path result )
29
29
endmacro ()
30
30
31
31
# 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
34
34
# If the application source did not have .lua suffix then it will be added
35
35
# USE: lua_executable ( sputnik src/sputnik.lua )
36
36
macro ( install_lua_executable _name _source )
37
37
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 )
40
41
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
+ )
164
64
endif ()
165
- install ( PROGRAMS ${_source} DESTINATION ${INSTALL_BIN}
166
- RENAME ${_source_name} .lua )
167
65
endmacro ()
168
66
169
67
macro ( _lua_module_helper is_install _name )
@@ -206,7 +104,9 @@ macro ( _lua_module_helper is_install _name )
206
104
207
105
if ( ${is_install} )
208
106
install ( FILES ${_first_source} DESTINATION ${INSTALL_LMOD} /${_module_dir}
209
- RENAME ${_module_filename} )
107
+ RENAME ${_module_filename}
108
+ COMPONENT Runtime
109
+ )
210
110
endif ()
211
111
else () # Lua C binary module
212
112
enable_language ( C )
@@ -227,7 +127,7 @@ macro ( _lua_module_helper is_install _name )
227
127
set_target_properties ( ${_target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
228
128
"${_module_dir} " PREFIX "" OUTPUT_NAME "${_module_filenamebase} " )
229
129
if ( ${is_install} )
230
- install ( TARGETS ${_target} DESTINATION ${INSTALL_CMOD} /${_module_dir} )
130
+ install ( TARGETS ${_target} DESTINATION ${INSTALL_CMOD} /${_module_dir} COMPONENT Runtime )
231
131
endif ()
232
132
endif ()
233
133
endmacro ()
0 commit comments