Adapt to suggestions

- build shared and/or static version controlled by `LUA_ENABLE_SHARED/STATIC`
- add alias Lua::Lib which is one of above, controlled by `BUILD_SHARED_LIBS`
- exclude the unneeded version if not toplevel
This commit is contained in:
melonedo 2022-04-29 21:23:19 +08:00
parent abf78dd2eb
commit 6f99e2d952
3 changed files with 47 additions and 12 deletions

View File

@ -3,8 +3,14 @@ project(lua LANGUAGES C VERSION 5.4.4)
option(LUA_SUPPORT_DL "Support dynamic loading of compiled modules" OFF) option(LUA_SUPPORT_DL "Support dynamic loading of compiled modules" OFF)
option(LUA_BUILD_AS_CXX "Build lua as C++" OFF) option(LUA_BUILD_AS_CXX "Build lua as C++" OFF)
option(LUA_ENABLE_SHARED "Build dynamic liblua" ON)
option(LUA_ENABLE_STATIC "Build static liblua" ON)
enable_language(CXX) enable_language(CXX)
if(NOT LUA_ENABLE_SHARED AND NOT LUA_ENABLE_STATIC)
message(FATAL_ERROR "You must enable at least one of static or dynamic version of liblua")
endif()
if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME}) if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME})
set(TOP_LEVEL TRUE) set(TOP_LEVEL TRUE)
else() else()

View File

@ -33,18 +33,48 @@ set(LUA_LIB_SRCS
"src/linit.c" "src/linit.c"
) )
set(TARGETS_TO_INSTALL lua_obj)
if(LUA_BUILD_AS_CXX) if(LUA_BUILD_AS_CXX)
set_source_files_properties(${LUA_LIB_SRCS} "src/lua.c" "src/luac.c" PROPERTIES LANGUAGE CXX ) set_source_files_properties(${LUA_LIB_SRCS} "src/lua.c" "src/luac.c" PROPERTIES LANGUAGE CXX )
endif() endif()
add_library(lua_obj OBJECT ${LUA_LIB_SRCS}) add_library(lua_obj OBJECT ${LUA_LIB_SRCS})
add_library(lua_lib) if(LUA_ENABLE_SHARED)
target_link_libraries(lua_lib PRIVATE lua_obj) add_library(lua_dynamic SHARED)
set_target_properties(lua_lib PROPERTIES target_link_libraries(lua_dynamic PUBLIC lua_obj)
OUTPUT_NAME "lua" set_target_properties(lua_dynamic PROPERTIES
VERSION "${PACKAGE_VERSION}" VERSION "${PACKAGE_VERSION}"
) )
list(APPEND TARGETS_TO_INSTALL lua_dynamic)
if(BUILD_SHARED_LIBS)
set(LUA_EXPORT_LIBRARY lua_dynamic)
add_library(Lua::Lib ALIAS lua_dynamic)
elseif(NOT TOP_LEVEL)
set_target_properties(lua_dynamic PROPERTIES
EXCLUDE_FROM_ALL ON
)
endif()
endif()
if(LUA_ENABLE_STATIC)
add_library(lua_static STATIC)
target_link_libraries(lua_static PUBLIC lua_obj)
set_target_properties(lua_static PROPERTIES
VERSION "${PACKAGE_VERSION}"
)
list(APPEND TARGETS_TO_INSTALL lua_static)
if(NOT BUILD_SHARED_LIBS)
set(LUA_EXPORT_LIBRARY lua_static)
add_library(Lua::Lib ALIAS lua_static)
elseif(NOT TOP_LEVEL)
set_target_properties(lua_static PROPERTIES
EXCLUDE_FROM_ALL ON
)
endif()
endif()
target_include_directories(lua_obj PUBLIC target_include_directories(lua_obj PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
@ -76,20 +106,19 @@ if(UNIX)
) )
endif() endif()
if (WIN32 AND BUILD_SHARED_LIBS) if (WIN32)
target_compile_definitions(lua_obj target_compile_definitions(lua_obj
PRIVATE LUA_BUILD_AS_DLL PRIVATE LUA_BUILD_AS_DLL
) )
endif() endif()
set(TARGETS_TO_INSTALL lua_lib)
if(LUA_BUILD_BINARY) if(LUA_BUILD_BINARY)
include(CheckIncludeFile) include(CheckIncludeFile)
CHECK_INCLUDE_FILE("readline/readline.h" HAVE_READLINE_READLINE_H) CHECK_INCLUDE_FILE("readline/readline.h" HAVE_READLINE_READLINE_H)
add_executable(lua "src/lua.c") add_executable(lua "src/lua.c")
# Can not use lua_dynamic because some symbols are not exported
target_link_libraries(lua PRIVATE lua_obj) target_link_libraries(lua PRIVATE lua_obj)
set_target_properties(lua PROPERTIES set_target_properties(lua PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR} RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
@ -127,6 +156,7 @@ write_basic_package_version_file(
install(EXPORT LuaTargets install(EXPORT LuaTargets
FILE LuaTargets.cmake FILE LuaTargets.cmake
DESTINATION "lib/cmake" DESTINATION "lib/cmake"
NAMESPACE Lua::
) )
configure_package_config_file( configure_package_config_file(

View File

@ -3,8 +3,7 @@
include("${CMAKE_CURRENT_LIST_DIR}/LuaTargets.cmake") include("${CMAKE_CURRENT_LIST_DIR}/LuaTargets.cmake")
set_and_check(LUA_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include") set_and_check(LUA_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")
set_and_check(LUA_LIBRARY "${PACKAGE_PREFIX_DIR}/lib/liblua.a") add_library(Lua::Lib ALIAS "Lua::@LUA_EXPORT_LIBRARY@")
set(LUA_LIBRARIES "${LUA_LIBRARY}") set(LUA_LIBRARIES "Lua::Lib")
add_library(Lua::Library ALIAS lua_static)
check_required_components(Lua) check_required_components(Lua)