Merge pull request #15 from melonedo/master

Support dynamic library
This commit is contained in:
Walter Schell 2022-05-12 21:18:09 -04:00 committed by GitHub
commit 14f98e5fdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 26 deletions

View File

@ -4,6 +4,7 @@ enable_testing()
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)
enable_language(CXX) enable_language(CXX)
if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME}) if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME})

View File

@ -33,42 +33,77 @@ set(LUA_LIB_SRCS
"src/linit.c" "src/linit.c"
) )
set(TARGETS_TO_INSTALL lua_internal lua_include)
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_static STATIC ${LUA_LIB_SRCS}) add_library(lua_internal INTERFACE)
set_target_properties(lua_static PROPERTIES
OUTPUT_NAME "lua" add_library(lua_include INTERFACE)
VERSION "${PACKAGE_VERSION}"
) target_include_directories(lua_include INTERFACE
target_include_directories(lua_static PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include> $<INSTALL_INTERFACE:include>
) )
target_link_libraries(lua_internal INTERFACE lua_include)
if(LUA_ENABLE_SHARED)
add_library(lua_shared SHARED ${LUA_LIB_SRCS})
target_link_libraries(lua_shared PRIVATE lua_internal PUBLIC lua_include)
set_target_properties(lua_shared PROPERTIES
VERSION "${PACKAGE_VERSION}"
)
if(WIN32)
target_compile_definitions(lua_shared PRIVATE LUA_BUILD_AS_DLL)
endif()
list(APPEND TARGETS_TO_INSTALL lua_shared)
if(BUILD_SHARED_LIBS)
add_library(Lua::Library ALIAS lua_shared)
elseif(NOT TOP_LEVEL)
set_target_properties(lua_shared PROPERTIES
EXCLUDE_FROM_ALL ON
)
endif()
endif()
add_library(lua_static STATIC ${LUA_LIB_SRCS})
target_link_libraries(lua_static PRIVATE lua_internal PUBLIC lua_include)
set_target_properties(lua_static PROPERTIES
VERSION "${PACKAGE_VERSION}"
)
list(APPEND TARGETS_TO_INSTALL lua_static)
if(NOT BUILD_SHARED_LIBS OR NOT LUA_ENABLE_SHARED)
add_library(Lua::Library ALIAS lua_static)
endif()
if(UNIX) if(UNIX)
set(LUA_DEFINITIONS)
if(NOT EMSCRIPTEN) if(NOT EMSCRIPTEN)
find_library(LIBM m) find_library(LIBM m)
#TODO: Redo this with find_package #TODO: Redo this with find_package
if(NOT LIBM) if(NOT LIBM)
message(FATAL_ERROR "libm not found and is required by lua") message(FATAL_ERROR "libm not found and is required by lua")
endif() endif()
target_link_libraries(lua_static INTERFACE m) target_link_libraries(lua_internal INTERFACE m)
list(APPEND LUA_DEFINITIONS LUA_USE_POSIX) target_compile_definitions(lua_internal
INTERFACE LUA_USE_POSIX
)
if(LUA_SUPPORT_DL) if(LUA_SUPPORT_DL)
target_compile_definitions(lua_static PRIVATE "LUA_USE_DLOPEN") target_compile_definitions(lua_internal INTERFACE "LUA_USE_DLOPEN")
target_link_libraries(lua_static INTERFACE dl) target_link_libraries(lua_internal INTERFACE dl)
endif() endif()
endif() endif()
target_compile_definitions(lua_static target_compile_options(lua_internal
PUBLIC ${LUA_DEFINITIONS} INTERFACE "-Wall" "-Wextra"
) )
target_compile_options(lua_static elseif(Win32)
PRIVATE "-Wall" "-Wextra" target_compile_options(lua_internal
INTERFACE "/Wall"
) )
endif() endif()
@ -76,36 +111,37 @@ 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")
target_link_libraries(lua PUBLIC lua_static) # Can not use lua_shared because some symbols are not exported
target_link_libraries(lua PRIVATE lua_static)
set_target_properties(lua PROPERTIES set_target_properties(lua PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR} RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
) )
if (HAVE_READLINE_READLINE_H) if (HAVE_READLINE_READLINE_H)
target_compile_definitions(lua PUBLIC "LUA_USE_READLINE") target_compile_definitions(lua PRIVATE "LUA_USE_READLINE")
target_link_libraries(lua PUBLIC readline) target_link_libraries(lua PUBLIC readline)
endif() endif()
list(APPEND TARGETS_TO_INSTALL lua)
endif() endif()
if(LUA_BUILD_COMPILER) if(LUA_BUILD_COMPILER)
add_executable(luac "src/luac.c") add_executable(luac "src/luac.c")
target_link_libraries(luac PUBLIC lua_static) target_link_libraries(luac PRIVATE lua_static)
set_target_properties(luac PROPERTIES set_target_properties(luac PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR} RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
) )
list(APPEND TARGETS_TO_INSTALL luac)
endif() endif()
install(TARGETS lua_static install(TARGETS ${TARGETS_TO_INSTALL}
EXPORT LuaTargets EXPORT LuaTargets
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include
) )
install(DIRECTORY include/ TYPE INCLUDE) install(DIRECTORY include/ TYPE INCLUDE)
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
get_target_property(LUA_EXPORT_LIBRARY Lua::Library ALIASED_TARGET)
write_basic_package_version_file( write_basic_package_version_file(
LuaConfigVersion.cmake LuaConfigVersion.cmake
VERSION ${PACKAGE_VERSION} VERSION ${PACKAGE_VERSION}
@ -115,6 +151,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::Library ALIAS "Lua::@LUA_EXPORT_LIBRARY@")
set(LUA_LIBRARIES "${LUA_LIBRARY}") set(LUA_LIBRARIES "Lua::Library")
add_library(Lua::Library ALIAS lua_static)
check_required_components(Lua) check_required_components(Lua)