BuildLuajit.cmake 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. include(CMakeParseArguments)
  2. # BuildLuajit(TARGET targetname CONFIGURE_COMMAND ... BUILD_COMMAND ... INSTALL_COMMAND ...)
  3. # Reusable function to build luajit, wraps ExternalProject_Add.
  4. # Failing to pass a command argument will result in no command being run
  5. function(BuildLuajit)
  6. cmake_parse_arguments(_luajit
  7. ""
  8. "TARGET"
  9. "CONFIGURE_COMMAND;BUILD_COMMAND;INSTALL_COMMAND"
  10. ${ARGN})
  11. if(NOT _luajit_CONFIGURE_COMMAND AND NOT _luajit_BUILD_COMMAND
  12. AND NOT _luajit_INSTALL_COMMAND)
  13. message(FATAL_ERROR "Must pass at least one of CONFIGURE_COMMAND, BUILD_COMMAND, INSTALL_COMMAND")
  14. endif()
  15. if(NOT _luajit_TARGET)
  16. set(_luajit_TARGET "luajit")
  17. endif()
  18. ExternalProject_Add(${_luajit_TARGET}
  19. PREFIX ${DEPS_BUILD_DIR}
  20. URL ${LUAJIT_URL}
  21. DOWNLOAD_DIR ${DEPS_DOWNLOAD_DIR}/luajit
  22. DOWNLOAD_COMMAND ${CMAKE_COMMAND}
  23. -DPREFIX=${DEPS_BUILD_DIR}
  24. -DDOWNLOAD_DIR=${DEPS_DOWNLOAD_DIR}/luajit
  25. -DURL=${LUAJIT_URL}
  26. -DEXPECTED_SHA256=${LUAJIT_SHA256}
  27. -DTARGET=${_luajit_TARGET}
  28. -DUSE_EXISTING_SRC_DIR=${USE_EXISTING_SRC_DIR}
  29. -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/DownloadAndExtractFile.cmake
  30. CONFIGURE_COMMAND "${_luajit_CONFIGURE_COMMAND}"
  31. BUILD_IN_SOURCE 1
  32. BUILD_COMMAND "${_luajit_BUILD_COMMAND}"
  33. INSTALL_COMMAND "${_luajit_INSTALL_COMMAND}")
  34. endfunction()
  35. if(CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
  36. set(AMD64_ABI "LDFLAGS=-lpthread -lc++abi")
  37. else()
  38. set(AMD64_ABI "")
  39. endif()
  40. set(INSTALLCMD_UNIX ${MAKE_PRG} CFLAGS=-fPIC
  41. CFLAGS+=-DLUA_USE_APICHECK
  42. CFLAGS+=-DLUA_USE_ASSERT
  43. ${AMD64_ABI}
  44. CCDEBUG+=-g
  45. Q=
  46. install)
  47. if(UNIX)
  48. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  49. # Set MACOSX_DEPLOYMENT_TARGET (else luajit defaults to 10.4). #9050
  50. # https://github.com/LuaJIT/LuaJIT/blob/b025b01c5b9d23f6218c7d72b7aafa3f1ab1e08a/src/Makefile#L301-L303
  51. set(DEPLOYMENT_TARGET "MACOSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}")
  52. else()
  53. set(DEPLOYMENT_TARGET "")
  54. endif()
  55. BuildLuaJit(INSTALL_COMMAND ${INSTALLCMD_UNIX}
  56. CC=${DEPS_C_COMPILER} PREFIX=${DEPS_INSTALL_DIR}
  57. ${DEPLOYMENT_TARGET})
  58. elseif(MINGW AND CMAKE_CROSSCOMPILING)
  59. # Build luajit for the host
  60. BuildLuaJit(TARGET luajit_host
  61. CONFIGURE_COMMAND ""
  62. BUILD_COMMAND ""
  63. INSTALL_COMMAND ${INSTALLCMD_UNIX}
  64. CC=${HOST_C_COMPILER} PREFIX=${HOSTDEPS_INSTALL_DIR})
  65. # Build luajit for the target
  66. BuildLuaJit(
  67. # Similar to Unix + cross - fPIC
  68. INSTALL_COMMAND
  69. ${MAKE_PRG} PREFIX=${DEPS_INSTALL_DIR}
  70. BUILDMODE=static install
  71. TARGET_SYS=${CMAKE_SYSTEM_NAME}
  72. CROSS=${CROSS_TARGET}-
  73. HOST_CC=${HOST_C_COMPILER} HOST_CFLAGS=${HOST_C_FLAGS}
  74. HOST_LDFLAGS=${HOST_EXE_LINKER_FLAGS}
  75. FILE_T=luajit.exe
  76. Q=
  77. INSTALL_TSYMNAME=luajit.exe)
  78. elseif(MINGW)
  79. if(CMAKE_GENERATOR MATCHES "Ninja")
  80. set(LUAJIT_MAKE_PRG ${MAKE_PRG})
  81. else()
  82. set(LUAJIT_MAKE_PRG ${CMAKE_MAKE_PROGRAM})
  83. endif()
  84. BuildLuaJit(BUILD_COMMAND ${LUAJIT_MAKE_PRG} CC=${DEPS_C_COMPILER}
  85. PREFIX=${DEPS_INSTALL_DIR}
  86. CFLAGS+=-DLUA_USE_APICHECK
  87. CFLAGS+=-DLUA_USE_ASSERT
  88. CCDEBUG+=-g
  89. BUILDMODE=static
  90. # Build a DLL too
  91. COMMAND ${LUAJIT_MAKE_PRG} CC=${DEPS_C_COMPILER} BUILDMODE=dynamic
  92. INSTALL_COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/bin
  93. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/luajit.exe ${DEPS_INSTALL_DIR}/bin
  94. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.dll ${DEPS_INSTALL_DIR}/bin
  95. COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/lib
  96. # Luarocks searches for lua51.dll in lib
  97. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.dll ${DEPS_INSTALL_DIR}/lib
  98. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/libluajit.a ${DEPS_INSTALL_DIR}/lib
  99. COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/include/luajit-2.0
  100. COMMAND ${CMAKE_COMMAND} -DFROM_GLOB=${DEPS_BUILD_DIR}/src/luajit/src/*.h -DTO=${DEPS_INSTALL_DIR}/include/luajit-2.0 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CopyFilesGlob.cmake
  101. )
  102. elseif(MSVC)
  103. BuildLuaJit(
  104. BUILD_COMMAND ${CMAKE_COMMAND} -E chdir ${DEPS_BUILD_DIR}/src/luajit/src ${DEPS_BUILD_DIR}/src/luajit/src/msvcbuild.bat
  105. INSTALL_COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/bin
  106. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/luajit.exe ${DEPS_INSTALL_DIR}/bin
  107. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.dll ${DEPS_INSTALL_DIR}/bin
  108. COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/lib
  109. # Luarocks searches for lua51.lib
  110. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.lib ${DEPS_INSTALL_DIR}/lib/lua51.lib
  111. # Luv searches for luajit.lib
  112. COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.lib ${DEPS_INSTALL_DIR}/lib/luajit.lib
  113. COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/include/luajit-2.0
  114. COMMAND ${CMAKE_COMMAND} -DFROM_GLOB=${DEPS_BUILD_DIR}/src/luajit/src/*.h -DTO=${DEPS_INSTALL_DIR}/include/luajit-2.0 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CopyFilesGlob.cmake)
  115. else()
  116. message(FATAL_ERROR "Trying to build luajit in an unsupported system ${CMAKE_SYSTEM_NAME}/${CMAKE_C_COMPILER_ID}")
  117. endif()
  118. list(APPEND THIRD_PARTY_DEPS luajit)