BuildLuv.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. include(CMakeParseArguments)
  2. # BuildLuv(PATCH_COMMAND ... CONFIGURE_COMMAND ... BUILD_COMMAND ... INSTALL_COMMAND ...)
  3. # Reusable function to build luv, wraps ExternalProject_Add.
  4. # Failing to pass a command argument will result in no command being run
  5. function(BuildLuv)
  6. cmake_parse_arguments(_luv
  7. ""
  8. ""
  9. "PATCH_COMMAND;CONFIGURE_COMMAND;BUILD_COMMAND;INSTALL_COMMAND"
  10. ${ARGN})
  11. if(NOT _luv_CONFIGURE_COMMAND AND NOT _luv_BUILD_COMMAND
  12. AND NOT _luv_INSTALL_COMMAND)
  13. message(FATAL_ERROR "Must pass at least one of CONFIGURE_COMMAND, BUILD_COMMAND, INSTALL_COMMAND")
  14. endif()
  15. ExternalProject_Add(lua-compat-5.3
  16. PREFIX ${DEPS_BUILD_DIR}
  17. URL ${LUA_COMPAT53_URL}
  18. DOWNLOAD_DIR ${DEPS_DOWNLOAD_DIR}/lua-compat-5.3
  19. DOWNLOAD_COMMAND ${CMAKE_COMMAND}
  20. -DPREFIX=${DEPS_BUILD_DIR}
  21. -DDOWNLOAD_DIR=${DEPS_DOWNLOAD_DIR}/lua-compat-5.3
  22. -DURL=${LUA_COMPAT53_URL}
  23. -DEXPECTED_SHA256=${LUA_COMPAT53_SHA256}
  24. -DTARGET=lua-compat-5.3
  25. -DUSE_EXISTING_SRC_DIR=${USE_EXISTING_SRC_DIR}
  26. -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/DownloadAndExtractFile.cmake
  27. PATCH_COMMAND ""
  28. CONFIGURE_COMMAND ""
  29. BUILD_COMMAND ""
  30. INSTALL_COMMAND "")
  31. ExternalProject_Add(luv-static
  32. PREFIX ${DEPS_BUILD_DIR}
  33. DEPENDS lua-compat-5.3
  34. URL ${LUV_URL}
  35. DOWNLOAD_DIR ${DEPS_DOWNLOAD_DIR}/luv
  36. DOWNLOAD_COMMAND ${CMAKE_COMMAND}
  37. -DPREFIX=${DEPS_BUILD_DIR}
  38. -DDOWNLOAD_DIR=${DEPS_DOWNLOAD_DIR}/luv
  39. -DURL=${LUV_URL}
  40. -DEXPECTED_SHA256=${LUV_SHA256}
  41. -DTARGET=luv-static
  42. # The source is shared with BuildLuarocks (with USE_BUNDLED_LUV).
  43. -DSRC_DIR=${DEPS_BUILD_DIR}/src/luv
  44. -DUSE_EXISTING_SRC_DIR=${USE_EXISTING_SRC_DIR}
  45. -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/DownloadAndExtractFile.cmake
  46. PATCH_COMMAND "${_luv_PATCH_COMMAND}"
  47. CONFIGURE_COMMAND "${_luv_CONFIGURE_COMMAND}"
  48. BUILD_COMMAND "${_luv_BUILD_COMMAND}"
  49. INSTALL_COMMAND "${_luv_INSTALL_COMMAND}")
  50. endfunction()
  51. set(LUV_SRC_DIR ${DEPS_BUILD_DIR}/src/luv)
  52. set(LUV_INCLUDE_FLAGS
  53. "-I${DEPS_INSTALL_DIR}/include -I${DEPS_INSTALL_DIR}/include/luajit-2.0")
  54. # Replace luv default rockspec with the alternate one under the "rockspecs"
  55. # directory
  56. set(LUV_PATCH_COMMAND
  57. ${CMAKE_COMMAND} -E copy_directory ${LUV_SRC_DIR}/rockspecs ${LUV_SRC_DIR})
  58. set(LUV_CONFIGURE_COMMAND_COMMON
  59. ${CMAKE_COMMAND} ${LUV_SRC_DIR}
  60. -DCMAKE_GENERATOR=${CMAKE_GENERATOR}
  61. -DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR}
  62. -DLUA_BUILD_TYPE=System
  63. -DWITH_SHARED_LIBUV=ON
  64. -DBUILD_SHARED_LIBS=OFF
  65. -DBUILD_MODULE=OFF)
  66. if(USE_BUNDLED_LUAJIT)
  67. list(APPEND LUV_CONFIGURE_COMMAND_COMMON -DWITH_LUA_ENGINE=LuaJit)
  68. elseif(USE_BUNDLED_LUA)
  69. list(APPEND LUV_CONFIGURE_COMMAND_COMMON -DWITH_LUA_ENGINE=Lua)
  70. else()
  71. find_package(LuaJit)
  72. if(LUAJIT_FOUND)
  73. list(APPEND LUV_CONFIGURE_COMMAND_COMMON -DWITH_LUA_ENGINE=LuaJit)
  74. else()
  75. list(APPEND LUV_CONFIGURE_COMMAND_COMMON -DWITH_LUA_ENGINE=Lua)
  76. endif()
  77. endif()
  78. if(USE_BUNDLED_LIBUV)
  79. set(LUV_CONFIGURE_COMMAND_COMMON
  80. ${LUV_CONFIGURE_COMMAND_COMMON}
  81. -DCMAKE_PREFIX_PATH=${DEPS_INSTALL_DIR})
  82. endif()
  83. if(MINGW AND CMAKE_CROSSCOMPILING)
  84. get_filename_component(TOOLCHAIN ${CMAKE_TOOLCHAIN_FILE} REALPATH)
  85. set(LUV_CONFIGURE_COMMAND
  86. ${LUV_CONFIGURE_COMMAND_COMMON}
  87. # Pass toolchain
  88. -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN}
  89. "-DCMAKE_C_FLAGS:STRING=${LUV_INCLUDE_FLAGS} -D_WIN32_WINNT=0x0600"
  90. # Hack to avoid -rdynamic in Mingw
  91. -DCMAKE_SHARED_LIBRARY_LINK_C_FLAGS="")
  92. elseif(MSVC)
  93. set(LUV_CONFIGURE_COMMAND
  94. ${LUV_CONFIGURE_COMMAND_COMMON}
  95. -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
  96. # Same as Unix without fPIC
  97. "-DCMAKE_C_FLAGS:STRING=${CMAKE_C_COMPILER_ARG1} ${LUV_INCLUDE_FLAGS}"
  98. # Make sure we use the same generator, otherwise we may
  99. # accidentaly end up using different MSVC runtimes
  100. -DCMAKE_GENERATOR=${CMAKE_GENERATOR}
  101. # Use static runtime
  102. -DCMAKE_C_FLAGS_DEBUG="-MTd"
  103. -DCMAKE_C_FLAGS_RELEASE="-MT")
  104. else()
  105. set(LUV_CONFIGURE_COMMAND
  106. ${LUV_CONFIGURE_COMMAND_COMMON}
  107. -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
  108. "-DCMAKE_C_FLAGS:STRING=${CMAKE_C_COMPILER_ARG1} ${LUV_INCLUDE_FLAGS} -fPIC")
  109. endif()
  110. if(CMAKE_GENERATOR MATCHES "Unix Makefiles" AND
  111. (CMAKE_SYSTEM_NAME MATCHES ".*BSD" OR CMAKE_SYSTEM_NAME MATCHES "DragonFly"))
  112. set(LUV_BUILD_COMMAND ${CMAKE_COMMAND}
  113. "-DLUA_COMPAT53_DIR=${DEPS_BUILD_DIR}/src/lua-compat-5.3"
  114. "-DCMAKE_MAKE_PROGRAM=gmake" --build .)
  115. else()
  116. set(LUV_BUILD_COMMAND ${CMAKE_COMMAND}
  117. "-DLUA_COMPAT53_DIR=${DEPS_BUILD_DIR}/src/lua-compat-5.3" --build .)
  118. endif()
  119. set(LUV_INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install)
  120. BuildLuv(PATCH_COMMAND ${LUV_PATCH_COMMAND}
  121. CONFIGURE_COMMAND ${LUV_CONFIGURE_COMMAND}
  122. BUILD_COMMAND ${LUV_BUILD_COMMAND}
  123. INSTALL_COMMAND ${LUV_INSTALL_COMMAND})
  124. list(APPEND THIRD_PARTY_DEPS luv-static)
  125. if(USE_BUNDLED_LUAJIT)
  126. add_dependencies(luv-static luajit)
  127. endif()
  128. if(USE_BUNDLED_LIBUV)
  129. add_dependencies(luv-static libuv)
  130. endif()