SHOW:
|
|
- or go back to the newest paste.
| 1 | # CMakeLists.txt file for LUA - 2026-02-06 | |
| 2 | # | |
| 3 | cmake_minimum_required(VERSION 3.15) | |
| 4 | ||
| 5 | # Rather than being hardcoded here in the "project" command, the LUA version | |
| 6 | # is derived from project files. Hence, we set the CMP0048 policy to suppress | |
| 7 | # CMake warning messages. | |
| 8 | # | |
| 9 | cmake_policy(SET CMP0048 NEW) | |
| 10 | ||
| 11 | project(lua C) | |
| 12 | ||
| 13 | include(GNUInstallDirs) | |
| 14 | ||
| 15 | # Reads macro from a header file which is expected to be a single-line define. | |
| 16 | # | |
| 17 | function(read_macro PATH MACRO OUTPUT) | |
| 18 | file(STRINGS ${PATH} _line LIMIT_COUNT 1 REGEX "^#define +${MACRO}")
| |
| 19 | string(REGEX REPLACE "^.+\"?([0-9])\"?.*" "\\1" _val "${_line}")
| |
| 20 | set(${OUTPUT} ${_val} PARENT_SCOPE)
| |
| 21 | endfunction(read_macro) | |
| 22 | ||
| 23 | # Obtain project version information from lua.h file. | |
| 24 | # | |
| 25 | read_macro("src/lua.h" "LUA_VERSION_MAJOR" LUA_VERSION_MAJOR)
| |
| 26 | read_macro("src/lua.h" "LUA_VERSION_MINOR" LUA_VERSION_MINOR)
| |
| 27 | read_macro("src/lua.h" "LUA_VERSION_RELEASE" LUA_VERSION_RELEASE)
| |
| 28 | set(LUA_VERSION "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_RELEASE}")
| |
| 29 | ||
| 30 | message(STATUS "LUA version is ${LUA_VERSION}")
| |
| 31 | ||
| 32 | # Define LUA_SUFFIX for Windows build. | |
| 33 | # | |
| 34 | set(LUA_SUFFIX) | |
| 35 | if(MSVC) | |
| 36 | set(LUA_SUFFIX "${LUA_VERSION_MAJOR}${LUA_VERSION_MINOR}")
| |
| 37 | message(STATUS "LUA suffix is ${LUA_SUFFIX}")
| |
| 38 | endif() | |
| 39 | ||
| 40 | include_directories(src ${CMAKE_CURRENT_BINARY_DIR})
| |
| 41 | ||
| 42 | # Define source files. | |
| 43 | # | |
| 44 | set(SRC_CORE src/lapi.c src/lcode.c src/lctype.c src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c | |
| 45 | src/lgc.c src/llex.c src/lmem.c src/lobject.c src/lopcodes.c src/lparser.c src/lstate.c | |
| 46 | src/lstring.c src/ltable.c src/ltm.c src/lundump.c src/lvm.c src/lzio.c) | |
| 47 | ||
| 48 | set(SRC_LIB src/lauxlib.c src/lbaselib.c src/lcorolib.c src/ldblib.c src/liolib.c src/lmathlib.c | |
| 49 | src/loslib.c src/lstrlib.c src/ltablib.c src/lutf8lib.c src/loadlib.c src/linit.c) | |
| 50 | ||
| 51 | set(SRC_LUA src/lua.c) | |
| 52 | set(SRC_LUAC src/luac.c) | |
| 53 | ||
| 54 | set(LUA_INCLUDES src/lua.h src/lualib.h src/lauxlib.h src/lua.hpp src/luaconf.h) | |
| 55 | ||
| 56 | # Check for maths library (needed for Linux builds). | |
| 57 | # | |
| 58 | include(CheckLibraryExists) | |
| 59 | set(LIBM_LIBRARY) | |
| 60 | CHECK_LIBRARY_EXISTS(m log2 "" HAVE_LIB_M) | |
| 61 | if(HAVE_LIB_M) | |
| 62 | set(LIBM_LIBRARY "m") | |
| 63 | endif() | |
| 64 | ||
| 65 | # Define static library build. | |
| 66 | # | |
| 67 | add_library(liblua-static ${SRC_CORE} ${SRC_LIB})
| |
| 68 | set_target_properties(liblua-static PROPERTIES OUTPUT_NAME lua) | |
| 69 | ||
| 70 | # Define shared library build. | |
| 71 | # | |
| 72 | add_library(liblua-shared SHARED ${SRC_CORE} ${SRC_LIB})
| |
| 73 | set_target_properties(liblua-shared PROPERTIES OUTPUT_NAME lua${LUA_SUFFIX}
| |
| 74 | VERSION "${LUA_VERSION}" SOVERSION "${LUA_VERSION_MAJOR}")
| |
| 75 | ||
| 76 | if(WIN32) | |
| 77 | target_compile_definitions(liblua-shared PRIVATE _CRT_SECURE_NO_WARNINGS LUA_BUILD_AS_DLL LUA_COMPAT_ALL) | |
| 78 | endif() | |
| 79 | ||
| 80 | # Define executables build. | |
| 81 | # | |
| 82 | add_executable(lua ${SRC_LUA})
| |
| 83 | target_link_libraries(lua liblua-shared ${LIBM_LIBRARY})
| |
| 84 | if(NOT WIN32) | |
| 85 | # Set runtime library path for the lua executable. | |
| 86 | # | |
| 87 | set_target_properties(lua PROPERTIES | |
| 88 | INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
| |
| 89 | BUILD_WITH_INSTALL_RPATH TRUE) | |
| 90 | endif() | |
| 91 | ||
| 92 | add_executable(luac ${SRC_LUAC})
| |
| 93 | target_link_libraries(luac liblua-static ${LIBM_LIBRARY})
| |
| 94 | ||
| 95 | # Installation | |
| 96 | # | |
| 97 | if(NOT BUNDLED_MODE) | |
| 98 | install( | |
| 99 | TARGETS lua | |
| 100 | RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
| |
| 101 | ) | |
| 102 | ||
| 103 | install( | |
| 104 | TARGETS luac | |
| 105 | RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
| |
| 106 | ) | |
| 107 | ||
| 108 | install( | |
| 109 | TARGETS liblua-static | |
| 110 | ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
| |
| 111 | LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
| |
| 112 | RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
| |
| 113 | ) | |
| 114 | ||
| 115 | install( | |
| 116 | TARGETS liblua-shared | |
| 117 | ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
| |
| 118 | LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
| |
| 119 | RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
| |
| 120 | ) | |
| 121 | ||
| 122 | install( | |
| 123 | FILES ${LUA_INCLUDES}
| |
| 124 | DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
| |
| 125 | ) | |
| 126 | endif() | |
| 127 |