3 # Copyright 2007-2008 Antoine Chavasse <a.chavasse@gmail.com>
5 # This file is part of Fail.
7 # Fail is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 3
9 # as published by the Free Software Foundation.
11 # Fail is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # This file builds lua. The idea is that the lua subdirectory contains
21 # a vanilla lua distribution and that this files takes chagres of applying
22 # all the required changes before building it (mainly copying the files and
23 # renameing them with a cpp suffix to compile lua as c++, but also changing
24 # the parameters to dlopen in the library loader so throwing from a lua module
26 include( CheckFunctionExists )
28 check_function_exists( mkstemp HAVE_MKSTEMP )
30 set( CMAKE_REQUIRED_INCLUDES readline/readline.h )
31 set( CMAKE_REQUIRED_LIBRARIES readline )
32 check_function_exists( readline HAVE_READLINE )
35 add_definitions( -DLUA_USE_MKSTEMP )
39 add_definitions( -DLUA_USE_READLINE )
40 endif( HAVE_READLINE )
43 add_definitions( -DLUA_USE_LINUX )
46 # We copy all the lua files into the build directory, so we can:
47 # - rename .c files into .cpp, so lua gets compiled as C++ (and so will use c++ exceptions to handle errors instead of setjmp/longjmp)
48 # - replace luaconf.h with our own
49 # The benefit is the ability to drop in a new version of lua in the source tree without having to to any change to the files directly.
50 file( GLOB lua_header_files ${PROJECT_SOURCE_DIR}/libs/lua/src/*.h )
51 file( GLOB lua_source_files ${PROJECT_SOURCE_DIR}/libs/lua/src/*.c )
53 foreach( file ${lua_header_files} )
54 string( REPLACE ${PROJECT_SOURCE_DIR}/libs/lua/src/ "" filename ${file} )
58 ${PROJECT_BINARY_DIR}/libs/lua/src/${filename}
62 foreach( file ${lua_source_files} )
63 string( REPLACE ${PROJECT_SOURCE_DIR}/libs/lua/src/ "" filename ${file} )
67 ${PROJECT_BINARY_DIR}/libs/lua/src/${filename}pp
71 # Replace the luaconf file
73 ${PROJECT_SOURCE_DIR}/libs/luaconf.h
74 ${PROJECT_BINARY_DIR}/libs/lua/src/luaconf.h
77 # Replace RTLD_NOW with RTLD_GLBOAL | RTLD_NOW in loadlib.c to make
78 # lua friendly to C++ shared objects. Otherwise std::type_info comparisons
79 # fail, and therefore so do dynamic_cast and exception handling.
80 # (cf http://gcc.gnu.org/faq.html#dso)
81 file( READ ${PROJECT_SOURCE_DIR}/libs/lua/src/loadlib.c loadlibsrc )
82 string( REPLACE RTLD_NOW "RTLD_GLOBAL | RTLD_NOW" loadlibsrc "${loadlibsrc}" )
83 file( WRITE ${PROJECT_BINARY_DIR}/libs/lua/src/loadlib.cpp "${loadlibsrc}" )
117 foreach( file ${lua_lib_files} )
119 # ${PROJECT_SOURCE_DIR}/libs/lua/src/${file}.c
120 # ${PROJECT_BINARY_DIR}/libs/lua/src/${file}.cpp
123 list( APPEND lua_cpp_files ${PROJECT_BINARY_DIR}/libs/lua/src/${file}.cpp )
127 include_directories( ${LUA_INCLUDE_DIR} )
129 add_library( fail-lua SHARED ${lua_cpp_files} )
133 install( TARGETS fail-lua DESTINATION lib )
136 ${PROJECT_SOURCE_DIR}/libs/lua/src/lua.c
137 ${PROJECT_BINARY_DIR}/libs/lua/src/lua.cpp
140 add_executable( fail-lua-exe ${PROJECT_BINARY_DIR}/libs/lua/src/lua.cpp )
142 set_target_properties( fail-lua-exe PROPERTIES OUTPUT_NAME fail-lua )
145 target_link_libraries( fail-lua-exe fail-lua )
148 target_link_libraries( fail-lua-exe dl )
152 target_link_libraries( fail-lua-exe readline )
153 endif( HAVE_READLINE )
155 install( TARGETS fail-lua-exe DESTINATION bin )