BuildLua.cmake 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. include(CMakeParseArguments)
  2. # BuildLua(CONFIGURE_COMMAND ... BUILD_COMMAND ... INSTALL_COMMAND ...)
  3. # Reusable function to build lua, wraps ExternalProject_Add.
  4. # Failing to pass a command argument will result in no command being run
  5. function(BuildLua)
  6. cmake_parse_arguments(_lua
  7. ""
  8. ""
  9. "CONFIGURE_COMMAND;BUILD_COMMAND;INSTALL_COMMAND"
  10. ${ARGN})
  11. if(NOT _lua_CONFIGURE_COMMAND AND NOT _lua_BUILD_COMMAND
  12. AND NOT _lua_INSTALL_COMMAND)
  13. message(FATAL_ERROR "Must pass at least one of CONFIGURE_COMMAND, BUILD_COMMAND, INSTALL_COMMAND")
  14. endif()
  15. ExternalProject_Add(lua
  16. PREFIX ${DEPS_BUILD_DIR}
  17. URL ${LUA_URL}
  18. DOWNLOAD_DIR ${DEPS_DOWNLOAD_DIR}/lua
  19. DOWNLOAD_COMMAND ${CMAKE_COMMAND}
  20. -DPREFIX=${DEPS_BUILD_DIR}
  21. -DDOWNLOAD_DIR=${DEPS_DOWNLOAD_DIR}/lua
  22. -DURL=${LUA_URL}
  23. -DEXPECTED_SHA256=${LUA_SHA256}
  24. -DTARGET=lua
  25. -DUSE_EXISTING_SRC_DIR=${USE_EXISTING_SRC_DIR}
  26. -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/DownloadAndExtractFile.cmake
  27. CONFIGURE_COMMAND "${_lua_CONFIGURE_COMMAND}"
  28. BUILD_IN_SOURCE 1
  29. BUILD_COMMAND "${_lua_BUILD_COMMAND}"
  30. INSTALL_COMMAND "${_lua_INSTALL_COMMAND}")
  31. endfunction()
  32. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  33. set(LUA_TARGET linux)
  34. elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  35. set(LUA_TARGET macosx)
  36. elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
  37. set(LUA_TARGET freebsd)
  38. elseif(CMAKE_SYSTEM_NAME MATCHES "BSD")
  39. set(CMAKE_LUA_TARGET bsd)
  40. elseif(CMAKE_SYSTEM_NAME MATCHES "^MINGW")
  41. set(CMAKE_LUA_TARGET mingw)
  42. else()
  43. if(UNIX)
  44. set(LUA_TARGET posix)
  45. else()
  46. set(LUA_TARGET generic)
  47. endif()
  48. endif()
  49. set(LUA_CFLAGS "-O0 -g3 -fPIC")
  50. set(LUA_LDFLAGS "")
  51. if(CLANG_ASAN_UBSAN)
  52. set(LUA_CFLAGS "${LUA_CFLAGS} -fsanitize=address")
  53. set(LUA_CFLAGS "${LUA_CFLAGS} -fno-omit-frame-pointer")
  54. set(LUA_CFLAGS "${LUA_CFLAGS} -fno-optimize-sibling-calls")
  55. set(LUA_LDFLAGS "${LUA_LDFLAGS} -fsanitize=address")
  56. endif()
  57. set(LUA_CONFIGURE_COMMAND
  58. sed -e "/^CC/s@gcc@${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}@"
  59. -e "/^CFLAGS/s@-O2@${LUA_CFLAGS}@"
  60. -e "/^MYLDFLAGS/s@$@${LUA_LDFLAGS}@"
  61. -e "s@-lreadline@@g"
  62. -e "s@-lhistory@@g"
  63. -e "s@-lncurses@@g"
  64. -i ${DEPS_BUILD_DIR}/src/lua/src/Makefile &&
  65. sed -e "/#define LUA_USE_READLINE/d"
  66. -e "s@\\(#define LUA_ROOT[ ]*\"\\)/usr/local@\\1${DEPS_INSTALL_DIR}@"
  67. -i ${DEPS_BUILD_DIR}/src/lua/src/luaconf.h)
  68. set(LUA_INSTALL_TOP_ARG "INSTALL_TOP=${DEPS_INSTALL_DIR}")
  69. set(LUA_BUILD_COMMAND
  70. ${MAKE_PRG} ${LUA_INSTALL_TOP_ARG} ${LUA_TARGET})
  71. set(LUA_INSTALL_COMMAND
  72. ${MAKE_PRG} ${LUA_INSTALL_TOP_ARG} install)
  73. message(STATUS "Lua target is ${LUA_TARGET}")
  74. BuildLua(CONFIGURE_COMMAND ${LUA_CONFIGURE_COMMAND}
  75. BUILD_COMMAND ${LUA_BUILD_COMMAND}
  76. INSTALL_COMMAND ${LUA_INSTALL_COMMAND})
  77. list(APPEND THIRD_PARTY_DEPS lua)
  78. set(BUSTED ${DEPS_INSTALL_DIR}/bin/busted)
  79. set(BUSTED_LUA ${BUSTED}-lua)
  80. add_custom_command(OUTPUT ${BUSTED_LUA}
  81. COMMAND sed -e 's/^exec/exec $$LUA_DEBUGGER/' -e 's/jit//g' < ${BUSTED} > ${BUSTED_LUA} && chmod +x ${BUSTED_LUA}
  82. DEPENDS lua busted ${BUSTED})
  83. add_custom_target(busted-lua
  84. DEPENDS ${DEPS_INSTALL_DIR}/bin/busted-lua)
  85. list(APPEND THIRD_PARTY_DEPS busted-lua)