BuildLuajit.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # BuildLuajit(TARGET targetname CONFIGURE_COMMAND ... BUILD_COMMAND ... INSTALL_COMMAND ...)
  2. # Reusable function to build luajit, wraps ExternalProject_Add.
  3. # Failing to pass a command argument will result in no command being run
  4. function(BuildLuajit)
  5. cmake_parse_arguments(_luajit
  6. ""
  7. "TARGET"
  8. "CONFIGURE_COMMAND;BUILD_COMMAND;INSTALL_COMMAND;DEPENDS"
  9. ${ARGN})
  10. if(NOT _luajit_CONFIGURE_COMMAND AND NOT _luajit_BUILD_COMMAND
  11. AND NOT _luajit_INSTALL_COMMAND)
  12. message(FATAL_ERROR "Must pass at least one of CONFIGURE_COMMAND, BUILD_COMMAND, INSTALL_COMMAND")
  13. endif()
  14. if(NOT _luajit_TARGET)
  15. set(_luajit_TARGET "luajit")
  16. endif()
  17. ExternalProject_Add(${_luajit_TARGET}
  18. PREFIX ${DEPS_BUILD_DIR}
  19. URL ${LUAJIT_URL}
  20. DOWNLOAD_DIR ${DEPS_DOWNLOAD_DIR}/luajit
  21. DOWNLOAD_COMMAND ${CMAKE_COMMAND}
  22. -DPREFIX=${DEPS_BUILD_DIR}
  23. -DDOWNLOAD_DIR=${DEPS_DOWNLOAD_DIR}/luajit
  24. -DURL=${LUAJIT_URL}
  25. -DEXPECTED_SHA256=${LUAJIT_SHA256}
  26. -DTARGET=${_luajit_TARGET}
  27. -DUSE_EXISTING_SRC_DIR=${USE_EXISTING_SRC_DIR}
  28. -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/DownloadAndExtractFile.cmake
  29. CONFIGURE_COMMAND "${_luajit_CONFIGURE_COMMAND}"
  30. BUILD_IN_SOURCE 1
  31. BUILD_COMMAND "${_luajit_BUILD_COMMAND}"
  32. INSTALL_COMMAND "${_luajit_INSTALL_COMMAND}"
  33. DEPENDS "${_luajit_DEPENDS}")
  34. # Create symlink for development version manually.
  35. if(UNIX)
  36. add_custom_command(
  37. TARGET ${_luajit_TARGET}
  38. COMMAND ${CMAKE_COMMAND} -E create_symlink luajit-2.1.0-beta3 ${DEPS_BIN_DIR}/${_luajit_TARGET})
  39. endif()
  40. endfunction()
  41. check_c_compiler_flag(-fno-stack-check HAS_NO_STACK_CHECK)
  42. if(CMAKE_SYSTEM_NAME MATCHES "Darwin" AND HAS_NO_STACK_CHECK)
  43. set(NO_STACK_CHECK "CFLAGS+=-fno-stack-check")
  44. else()
  45. set(NO_STACK_CHECK "")
  46. endif()
  47. if(CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
  48. set(AMD64_ABI "LDFLAGS=-lpthread -lc++abi")
  49. else()
  50. set(AMD64_ABI "")
  51. endif()
  52. set(BUILDCMD_UNIX ${MAKE_PRG} CFLAGS=-fPIC
  53. CFLAGS+=-DLUA_USE_APICHECK
  54. CFLAGS+=-funwind-tables
  55. ${NO_STACK_CHECK}
  56. ${AMD64_ABI}
  57. CCDEBUG+=-g
  58. Q=)
  59. # Setting MACOSX_DEPLOYMENT_TARGET is mandatory for LuaJIT; use version set by
  60. # cmake.deps/CMakeLists.txt (either environment variable or current system version).
  61. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  62. set(DEPLOYMENT_TARGET "MACOSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}")
  63. endif()
  64. if((UNIX AND NOT APPLE) OR (APPLE AND NOT CMAKE_OSX_ARCHITECTURES))
  65. BuildLuaJit(INSTALL_COMMAND ${BUILDCMD_UNIX}
  66. CC=${DEPS_C_COMPILER} PREFIX=${DEPS_INSTALL_DIR}
  67. ${DEPLOYMENT_TARGET} install)
  68. elseif(CMAKE_OSX_ARCHITECTURES AND APPLE)
  69. # Passing multiple `-arch` flags to the LuaJIT build will cause it to fail.
  70. # To get a working universal build, we build each requested architecture slice
  71. # individually then `lipo` them all up.
  72. set(LUAJIT_SRC_DIR "${DEPS_BUILD_DIR}/src/luajit")
  73. foreach(ARCH IN LISTS CMAKE_OSX_ARCHITECTURES)
  74. set(STATIC_CC "${LUAJIT_C_COMPILER} -arch ${ARCH}")
  75. set(DYNAMIC_CC "${LUAJIT_C_COMPILER} -arch ${ARCH} -fPIC")
  76. set(TARGET_LD "${LUAJIT_C_COMPILER} -arch ${ARCH}")
  77. list(APPEND LUAJIT_THIN_EXECUTABLES "${LUAJIT_SRC_DIR}-${ARCH}/src/luajit")
  78. list(APPEND LUAJIT_THIN_STATIC_LIBS "${LUAJIT_SRC_DIR}-${ARCH}/src/libluajit.a")
  79. list(APPEND LUAJIT_THIN_DYLIBS "${LUAJIT_SRC_DIR}-${ARCH}/src/libluajit.so")
  80. list(APPEND LUAJIT_THIN_TARGETS "luajit-${ARCH}")
  81. # See https://luajit.org/install.html#cross.
  82. BuildLuaJit(TARGET "luajit-${ARCH}"
  83. BUILD_COMMAND ${BUILDCMD_UNIX}
  84. CC=${LUAJIT_C_COMPILER} STATIC_CC=${STATIC_CC}
  85. DYNAMIC_CC=${DYNAMIC_CC} TARGET_LD=${TARGET_LD}
  86. PREFIX=${DEPS_INSTALL_DIR}
  87. ${DEPLOYMENT_TARGET})
  88. endforeach()
  89. BuildLuaJit(
  90. CONFIGURE_COMMAND ${BUILDCMD_UNIX} CC=${LUAJIT_C_COMPILER} PREFIX=${DEPS_INSTALL_DIR} ${DEPLOYMENT_TARGET}
  91. COMMAND ${CMAKE_COMMAND} -E rm -f ${LUAJIT_SRC_DIR}/src/luajit ${LUAJIT_SRC_DIR}/src/libluajit.so ${LUAJIT_SRC_DIR}/src/libluajit.a
  92. BUILD_COMMAND lipo ${LUAJIT_THIN_EXECUTABLES} -create -output ${LUAJIT_SRC_DIR}/src/luajit
  93. COMMAND lipo ${LUAJIT_THIN_STATIC_LIBS} -create -output ${LUAJIT_SRC_DIR}/src/libluajit.a
  94. COMMAND lipo ${LUAJIT_THIN_DYLIBS} -create -output ${LUAJIT_SRC_DIR}/src/libluajit.so
  95. INSTALL_COMMAND ${BUILDCMD_UNIX} CC=${LUAJIT_C_COMPILER} PREFIX=${DEPS_INSTALL_DIR} ${DEPLOYMENT_TARGET} install
  96. DEPENDS ${LUAJIT_THIN_TARGETS}
  97. )
  98. elseif(MINGW)
  99. if(CMAKE_GENERATOR MATCHES "Ninja")
  100. set(LUAJIT_MAKE_PRG ${MAKE_PRG})
  101. else()
  102. set(LUAJIT_MAKE_PRG ${CMAKE_MAKE_PROGRAM})
  103. endif()
  104. BuildLuaJit(BUILD_COMMAND ${LUAJIT_MAKE_PRG} CC=${DEPS_C_COMPILER}
  105. PREFIX=${DEPS_INSTALL_DIR}
  106. CFLAGS+=-DLUA_USE_APICHECK
  107. CFLAGS+=-funwind-tables
  108. CCDEBUG+=-g
  109. BUILDMODE=static
  110. # Build a DLL too
  111. COMMAND ${LUAJIT_MAKE_PRG} CC=${DEPS_C_COMPILER} BUILDMODE=dynamic
  112. INSTALL_COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/bin
  113. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/luajit.exe ${DEPS_INSTALL_DIR}/bin
  114. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.dll ${DEPS_INSTALL_DIR}/bin
  115. COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/lib
  116. # Luarocks searches for lua51.dll in lib
  117. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.dll ${DEPS_INSTALL_DIR}/lib
  118. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/libluajit.a ${DEPS_INSTALL_DIR}/lib
  119. COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/include/luajit-2.1
  120. COMMAND ${CMAKE_COMMAND} -DFROM_GLOB=${DEPS_BUILD_DIR}/src/luajit/src/*.h -DTO=${DEPS_INSTALL_DIR}/include/luajit-2.1 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CopyFilesGlob.cmake
  121. COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/bin/lua/jit
  122. COMMAND ${CMAKE_COMMAND} -E copy_directory ${DEPS_BUILD_DIR}/src/luajit/src/jit ${DEPS_INSTALL_DIR}/bin/lua/jit
  123. )
  124. elseif(MSVC)
  125. BuildLuaJit(
  126. BUILD_COMMAND ${CMAKE_COMMAND} -E chdir ${DEPS_BUILD_DIR}/src/luajit/src ${DEPS_BUILD_DIR}/src/luajit/src/msvcbuild.bat
  127. INSTALL_COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/bin
  128. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/luajit.exe ${DEPS_INSTALL_DIR}/bin
  129. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.dll ${DEPS_INSTALL_DIR}/bin
  130. COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/lib
  131. # Luarocks searches for lua51.lib
  132. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.lib ${DEPS_INSTALL_DIR}/lib/lua51.lib
  133. # Luv searches for luajit.lib
  134. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.lib ${DEPS_INSTALL_DIR}/lib/luajit.lib
  135. COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/include/luajit-2.1
  136. COMMAND ${CMAKE_COMMAND} -DFROM_GLOB=${DEPS_BUILD_DIR}/src/luajit/src/*.h -DTO=${DEPS_INSTALL_DIR}/include/luajit-2.1 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CopyFilesGlob.cmake
  137. COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/bin/lua/jit
  138. COMMAND ${CMAKE_COMMAND} -E copy_directory ${DEPS_BUILD_DIR}/src/luajit/src/jit ${DEPS_INSTALL_DIR}/bin/lua/jit
  139. )
  140. else()
  141. message(FATAL_ERROR "Trying to build luajit in an unsupported system ${CMAKE_SYSTEM_NAME}/${CMAKE_C_COMPILER_ID}")
  142. endif()
  143. list(APPEND THIRD_PARTY_DEPS luajit)