CMakeLists.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #
  2. # Copyright (c) 2008-2014 the Urho3D project.
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. # Based on src/Makefile from http://luajit.org
  23. # The cross-compiling logic is ported to CMake as verbatim as possible although currently Urho3D does not support all of them
  24. # Makefile: Compiler options
  25. if (NOT MSVC)
  26. # Since the assembler part does NOT maintain a frame pointer, it's pointless
  27. # to slow down the C part by not omitting it. Debugging, tracebacks and
  28. # unwinding are not affected -- the assembler part has frame unwind
  29. # information and GCC emits it where needed (x64) or with -g (see CCDEBUG).
  30. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fomit-frame-pointer")
  31. endif ()
  32. # Makefile: Build mode
  33. # When LuaJIT is enabled for Urho3D, always build as static library to be linked against in main Urho3D executable or library CMake target
  34. # Use -DURHO3D_LUAJIT_AMALG=1 to compile LuaJIT core as one huge C file and allows GCC to generate faster and shorter code
  35. # Use -Dxxxxx=0/1/2 as CMake build option to turn off/on the features and debugging support below
  36. # Makefile: Features
  37. #
  38. # Permanently disable the FFI extension to reduce the size of the LuaJIT
  39. # executable. But please consider that the FFI library is compiled-in,
  40. # but NOT loaded by default. It only allocates any memory, if you actually
  41. # make use of it.
  42. if (LUAJIT_DISABLE_FFI)
  43. add_definitions (-DLUAJIT_DISABLE_FFI)
  44. endif ()
  45. # Features from Lua 5.2 that are unlikely to break existing code are
  46. # enabled by default. Some other features that *might* break some existing
  47. # code (e.g. __pairs or os.execute() return values) can be enabled here.
  48. # Note: this does not provide full compatibility with Lua 5.2 at this time.
  49. if (LUAJIT_ENABLE_LUA52COMPAT)
  50. add_definitions (-DLUAJIT_ENABLE_LUA52COMPAT)
  51. endif ()
  52. # Disable the JIT compiler, i.e. turn LuaJIT into a pure interpreter.
  53. if (LUAJIT_DISABLE_JIT)
  54. add_definitions (-DLUAJIT_DISABLE_JIT)
  55. endif ()
  56. # Some architectures (e.g. PPC) can use either single-number (1) or
  57. # dual-number (2) mode. Uncomment one of these lines to override the
  58. # default mode. Please see LJ_ARCH_NUMMODE in lj_arch.h for details.
  59. if (DEFINED LUAJIT_NUMMODE)
  60. add_definitions (-DLUAJIT_NUMMODE=${LUAJIT_NUMMODE})
  61. endif ()
  62. # Makefile: Debugging support
  63. # Note that most of these are NOT suitable for benchmarking or release mode!
  64. #
  65. # Use the system provided memory allocator (realloc) instead of the
  66. # bundled memory allocator. This is slower, but sometimes helpful for
  67. # debugging. It's helpful for Valgrind's memcheck tool, too. This option
  68. # cannot be enabled on x64, since the built-in allocator is mandatory.
  69. if (LUAJIT_USE_SYSMALLOC)
  70. add_definitions (-DLUAJIT_USE_SYSMALLOC)
  71. endif ()
  72. # This define is required to run LuaJIT under Valgrind. The Valgrind
  73. # header files must be installed. You should enable debug information, too.
  74. # Use --suppressions=lj.supp to avoid some false positives.
  75. if (LUAJIT_USE_VALGRIND)
  76. add_definitions (-DLUAJIT_USE_VALGRIND)
  77. endif ()
  78. # This is the client for the GDB JIT API. GDB 7.0 or higher is required
  79. # to make use of it. See lj_gdbjit.c for details. Enabling this causes
  80. # a non-negligible overhead, even when not running under GDB.
  81. if (LUAJIT_USE_GDBJIT)
  82. add_definitions (-DLUAJIT_USE_GDBJIT)
  83. endif ()
  84. # Turn on assertions for the Lua/C API to debug problems with lua_* calls.
  85. # This is rather slow -- use only while developing C libraries/embeddings.
  86. if (LUA_USE_APICHECK)
  87. add_definitions (-DLUA_USE_APICHECK)
  88. endif ()
  89. # Turn on assertions for the whole LuaJIT VM. This significantly slows down
  90. # everything. Use only if you suspect a problem with LuaJIT itself.
  91. if (LUA_USE_ASSERT)
  92. add_definitions (-DLUA_USE_ASSERT)
  93. endif ()
  94. # Makefile: Flags and options for host and target
  95. if (MSVC)
  96. if (LUAJIT_DISABLE_FFI)
  97. set (MSVC_HASFFI 0)
  98. else ()
  99. set (MSVC_HASFFI 1)
  100. endif ()
  101. set (TARGET_TESTARCH "LJ_HASFFI ${MSVC_HASFFI}\n")
  102. if (LUAJIT_DISABLE_JIT)
  103. set (MSVC_HASJIT 0)
  104. else ()
  105. set (MSVC_HASJIT 1)
  106. endif ()
  107. set (TARGET_TESTARCH "${TARGET_TESTARCH} LJ_HASJIT ${MSVC_HASJIT}\n")
  108. if (URHO3D_64BIT)
  109. set (MSVC_ARCH_BITS 64)
  110. set (MSVC_TARGET_ARCH X64)
  111. else ()
  112. set (MSVC_ARCH_BITS 32)
  113. set (MSVC_TARGET_ARCH X86)
  114. endif ()
  115. set (TARGET_TESTARCH "${TARGET_TESTARCH} LJ_ARCH_BITS ${MSVC_ARCH_BITS}\n")
  116. set (TARGET_TESTARCH "${TARGET_TESTARCH} LJ_TARGET_${MSVC_TARGET_ARCH} 1\n")
  117. # More assumptions
  118. set (TARGET_TESTARCH "${TARGET_TESTARCH} LJ_ARCH_HASFPU 1\n")
  119. set (TARGET_TESTARCH "${TARGET_TESTARCH} LJ_ABI_SOFTFP 0\n")
  120. else ()
  121. get_directory_property (TARGET_TCFLAGS COMPILE_DEFINITIONS)
  122. string (REPLACE ";" ";-D" TARGET_TCFLAGS ";${TARGET_TCFLAGS}")
  123. execute_process (COMMAND egrep -V RESULT_VARIABLE EGREP_EXIT_CODE OUTPUT_QUIET ERROR_QUIET)
  124. if (EGREP_EXIT_CODE EQUAL 0)
  125. execute_process (COMMAND ${CMAKE_C_COMPILER} ${TARGET_TCFLAGS} -E ${CMAKE_CURRENT_SOURCE_DIR}/src/lj_arch.h -dM
  126. COMMAND egrep "LJ_|MIPSEL|__SSE__" OUTPUT_VARIABLE TARGET_TESTARCH ERROR_QUIET)
  127. else ()
  128. execute_process (COMMAND ${CMAKE_C_COMPILER} ${TARGET_TCFLAGS} -E ${CMAKE_CURRENT_SOURCE_DIR}/src/lj_arch.h -dM OUTPUT_VARIABLE TARGET_TESTARCH ERROR_QUIET)
  129. endif ()
  130. endif ()
  131. # Makefile: Build mode handling
  132. # Urho3D only builds static LuaJIT library
  133. # Makefile: Make targets
  134. # These host tools must be built natively and would be used when cross-compiling
  135. if (CMAKE_CROSSCOMPILING OR IOS)
  136. # Cross-compiling
  137. If (IOS)
  138. set (BUILDVM_X buildvm-ios)
  139. elseif (ANDROID)
  140. set (BUILDVM_X buildvm-android-${ANDROID_NDK_ABI_NAME})
  141. elseif (RASPI)
  142. set (BUILDVM_X buildvm-raspi)
  143. elseif (MINGW)
  144. set (BUILDVM_X buildvm-mingw)
  145. else ()
  146. message (FATAL_ERROR "Unsupported cross-compiling target")
  147. endif ()
  148. execute_process (COMMAND ${PROJECT_ROOT_DIR}/Bin/${BUILDVM_X} RESULT_VARIABLE BUILDVM_EXIT_CODE OUTPUT_QUIET ERROR_QUIET)
  149. if (NOT BUILDVM_EXIT_CODE EQUAL 1)
  150. file (WRITE ${PROJECT_ROOT_DIR}/Bin/${BUILDVM_X}-arch.txt ${TARGET_TESTARCH})
  151. message (FATAL_ERROR
  152. "The configuration cannot be done now because the target-specific '${BUILDVM_X}' tool has not been built yet. "
  153. "However, the specific target architecture information is now saved. "
  154. "Reconfigure the desktop native build to incorporate the saved information by calling the respective batch/shell script for native build (it might be the same script as this one). "
  155. "Then run make or the equivalent command to actually build the missing tool natively. "
  156. "Finally, after the tool is built, come back to call this batch/shell script again to complete this configuration.")
  157. endif ()
  158. else ()
  159. # Not cross-compiling
  160. set (WARNING "# This is a generated file. DO NOT EDIT!")
  161. # minilua
  162. configure_file (CMakeLists.txt-minilua.in ${CMAKE_CURRENT_BINARY_DIR}/generated/minilua/CMakeLists.txt @ONLY)
  163. add_subdirectory (${CMAKE_CURRENT_BINARY_DIR}/generated/minilua generated/minilua)
  164. # buildvm
  165. configure_file (CMakeLists.txt-buildvm.in ${CMAKE_CURRENT_BINARY_DIR}/generated/buildvm/CMakeLists.txt @ONLY)
  166. add_subdirectory (${CMAKE_CURRENT_BINARY_DIR}/generated/buildvm generated/buildvm)
  167. set (BUILDVM_DEP buildvm)
  168. set (BUILDVM_X buildvm)
  169. # Also build variants for the supported target architectures
  170. file (GLOB VARIANT_ARCHS ${PROJECT_ROOT_DIR}/Bin/buildvm-*-arch.txt)
  171. foreach (VARIANT_ARCH ${VARIANT_ARCHS})
  172. string (REGEX REPLACE "^.*buildvm(-.*)-arch\\.txt$" \\1 VARIANT ${VARIANT_ARCH})
  173. file (READ ${VARIANT_ARCH} TARGET_TESTARCH)
  174. configure_file (CMakeLists.txt-buildvm.in ${CMAKE_CURRENT_BINARY_DIR}/generated/buildvm${VARIANT}/CMakeLists.txt @ONLY)
  175. add_subdirectory (${CMAKE_CURRENT_BINARY_DIR}/generated/buildvm${VARIANT} generated/buildvm${VARIANT})
  176. endforeach ()
  177. endif ()
  178. # Add definitions specific for target C Compiler
  179. if (NOT MSVC)
  180. # Large file support
  181. add_definitions (-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE)
  182. # Buffer overflows check
  183. add_definitions (-U_FORTIFY_SOURCE)
  184. endif ()
  185. # Define target name for LuaJIT library
  186. set (TARGET_NAME LuaJIT)
  187. # Macro for generating source file
  188. macro (generate_source name mode)
  189. set (GEN_SRC ${CMAKE_CURRENT_BINARY_DIR}/generated/${name})
  190. set (GEN_SRCS ${GEN_SRCS} ${GEN_SRC})
  191. add_custom_command (OUTPUT ${GEN_SRC}
  192. COMMAND ${PROJECT_ROOT_DIR}/Bin/${BUILDVM_X} -m ${mode} -o ${GEN_SRC} ${ARGN}
  193. DEPENDS ${BUILDVM_DEP} ${ARGN}
  194. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  195. COMMENT "Generating buildvm output: ${name}")
  196. endmacro ()
  197. # Define generated source files
  198. file (MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/generated)
  199. if (WIN32)
  200. set (LJVM_MODE peobj)
  201. set (LJVM_BOUT lj_vm.obj)
  202. else ()
  203. set (LJVM_BOUT lj_vm.s)
  204. enable_language (ASM)
  205. if (TARGET_LJARCH STREQUAL x64)
  206. if (URHO3D_64BIT)
  207. set (CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -m64")
  208. else ()
  209. set (CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -m32")
  210. endif ()
  211. endif ()
  212. if (APPLE)
  213. set (LJVM_MODE machasm)
  214. else ()
  215. set (LJVM_MODE elfasm)
  216. endif ()
  217. endif ()
  218. set (LJLIB_C src/lib_base.c src/lib_math.c src/lib_bit.c src/lib_string.c src/lib_table.c
  219. src/lib_io.c src/lib_os.c src/lib_package.c src/lib_debug.c src/lib_jit.c src/lib_ffi.c)
  220. generate_source (${LJVM_BOUT} ${LJVM_MODE})
  221. foreach (MODE bcdef ffdef libdef recdef)
  222. generate_source (lj_${MODE}.h ${MODE} ${LJLIB_C})
  223. endforeach ()
  224. generate_source (vmdef.lua vmdef ${LJLIB_C})
  225. generate_source (lj_folddef.h folddef src/lj_opt_fold.c)
  226. # Define source files
  227. if (URHO3D_LUAJIT_AMALG)
  228. set (LJCORE_C src/ljamalg.c)
  229. else ()
  230. set (LJCORE_C src/lj_gc.c src/lj_err.c src/lj_char.c src/lj_bc.c src/lj_obj.c
  231. src/lj_str.c src/lj_tab.c src/lj_func.c src/lj_udata.c src/lj_meta.c src/lj_debug.c
  232. src/lj_state.c src/lj_dispatch.c src/lj_vmevent.c src/lj_vmmath.c src/lj_strscan.c
  233. src/lj_api.c src/lj_lex.c src/lj_parse.c src/lj_bcread.c src/lj_bcwrite.c src/lj_load.c
  234. src/lj_ir.c src/lj_opt_mem.c src/lj_opt_fold.c src/lj_opt_narrow.c
  235. src/lj_opt_dce.c src/lj_opt_loop.c src/lj_opt_split.c src/lj_opt_sink.c
  236. src/lj_mcode.c src/lj_snap.c src/lj_record.c src/lj_crecord.c src/lj_ffrecord.c
  237. src/lj_asm.c src/lj_trace.c src/lj_gdbjit.c
  238. src/lj_ctype.c src/lj_cdata.c src/lj_cconv.c src/lj_ccall.c src/lj_ccallback.c
  239. src/lj_carith.c src/lj_clib.c src/lj_cparse.c
  240. src/lj_lib.c src/lj_alloc.c src/lib_aux.c
  241. ${LJLIB_C} src/lib_init.c)
  242. endif ()
  243. set (SOURCE_FILES ${LJCORE_C} ${GEN_SRCS})
  244. # Define dependency libs
  245. set (INCLUDE_DIRS_ONLY ${CMAKE_CURRENT_BINARY_DIR}/generated)
  246. # Setup target
  247. setup_library ()
  248. # On Android platform, use 'adb push' to copy the tool(s) from android-Bin to Android (virtual) device; use 'adb shell' to login into a remote shell to execute the tool in the (virtual) device
  249. # The tools are not built on iOS platform as there is no (easy) way to execute them on the iOS device
  250. if (NOT IOS AND URHO3D_TOOLS)
  251. # Define target name for LuaJIT interpreter cum compiler
  252. set (TARGET_NAME luajit_interpreter) # Note: intended target name is 'luajit' which clashes with 'LuaJIT' library target above for case-insensitive platform
  253. # Define source files
  254. set (SOURCE_FILES src/luajit.c)
  255. # Define dependency libs
  256. set (LINK_LIBS_ONLY LuaJIT)
  257. # Setup target
  258. setup_executable (NODEPS)
  259. adjust_target_name () # Adjust to intended target output name
  260. # Define post build steps
  261. set (LUAJIT_DEP_DIR ${PROJECT_ROOT_DIR}/Bin/Data/LuaScripts/jit)
  262. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
  263. COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/src/jit ${LUAJIT_DEP_DIR}
  264. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/generated/vmdef.lua ${LUAJIT_DEP_DIR}
  265. COMMENT "Copying dependency files for luajit standalone executable")
  266. # Install dependency files required by luajit
  267. install (DIRECTORY ${LUAJIT_DEP_DIR} DESTINATION ${DEST_RUNTIME_DIR}/Data/LuaScripts ${DEST_PERMISSIONS})
  268. endif ()
  269. # Add Data/LuaScripts directory into the LuaJIT module search path
  270. set (LUA_RDIR Data/LuaScripts/) # Relative directory
  271. set (LUA_IDIR ${CMAKE_INSTALL_PREFIX}/${DEST_RUNTIME_DIR}/Data/LuaScripts/) # Installation directory
  272. add_definitions (-DLUA_RDIR="${LUA_RDIR}" -DLUA_IDIR="${LUA_IDIR}")