Skip to content

Commit

Permalink
统一cmake的导出名称
Browse files Browse the repository at this point in the history
  • Loading branch information
czyt1988 committed Nov 29, 2023
1 parent 34967fa commit 3c91213
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 54 deletions.
9 changes: 5 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ else()
set(SARIBBON_PLATFORM "64")
endif()

# windows系统下,直接安装到当前文件夹下
if(WIN32)
set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/bin_qt${QT_VERSION}/SARibbon")
endif()


include(cmake/WinResource.cmake)

Expand All @@ -115,6 +112,10 @@ include(cmake/WinResource.cmake)
set(SARIBBON_BIN_NAME bin_qt${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH}_${SARIBBON_BUILD_TYPE}_${SARIBBON_PLATFORM})
set(SARIBBON_BIN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${SARIBBON_BIN_NAME})

# windows系统下,默认直接安装到当前文件夹下
if(WIN32)
set(CMAKE_INSTALL_PREFIX "${SARIBBON_BIN_DIR}/SARibbon")
endif()

add_subdirectory(src)
add_subdirectory(cmake)
Expand Down
93 changes: 47 additions & 46 deletions src/SARibbonBar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.5)
project(SARibbonBar LANGUAGES CXX VERSION ${SARIBBON_VERSION})
set(SARIBBONBAR_LIB_NAME SARibbonBar)
set(SARIBBON_LIB_NAME SARibbonBar)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

Expand Down Expand Up @@ -282,7 +282,7 @@ SET(SARIBBON_RESOURCE_FILES

################################################################
if(SARIBBON_USE_FRAMELESS_LIB)
add_library(${SARIBBONBAR_LIB_NAME} SHARED
add_library(${SARIBBON_LIB_NAME} SHARED
${SARIBBON_HEADER_FILES}
${SARIBBON_SOURCE_FILES}
${SARIBBON_RESOURCE_FILES}
Expand All @@ -292,21 +292,21 @@ if(SARIBBON_USE_FRAMELESS_LIB)
${FRAMELESS_SOURCE_FILES}
${FRAMELESS_RESOURCE_FILES}
)
setup_custom_moc_macros(TARGETS ${SARIBBONBAR_LIB_NAME})
setup_custom_moc_macros(TARGETS ${SARIBBON_LIB_NAME})
else()
add_library(${SARIBBONBAR_LIB_NAME} SHARED
add_library(${SARIBBON_LIB_NAME} SHARED
${SARIBBON_HEADER_FILES}
${SARIBBON_SOURCE_FILES}
${SARIBBON_RESOURCE_FILES}
${SACOLOR_HEADER_FILES}
${SACOLOR_SOURCE_FILES}
)
endif()
add_library(SARibbon ALIAS ${SARIBBONBAR_LIB_NAME})
add_library(SARibbon ALIAS ${SARIBBON_LIB_NAME})
#################################################
# Qt相关的依赖
#################################################
target_link_libraries(${SARIBBONBAR_LIB_NAME} PUBLIC
target_link_libraries(${SARIBBON_LIB_NAME} PUBLIC
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Gui
Qt${QT_VERSION_MAJOR}::Widgets)
Expand All @@ -319,73 +319,72 @@ target_link_libraries(${SARIBBONBAR_LIB_NAME} PUBLIC
# But it was again brought back as a private feature of QtGui in 6.2
if(SARIBBON_USE_FRAMELESS_LIB)
if(TARGET Qt5::X11Extras)
target_link_libraries(${SARIBBONBAR_LIB_NAME} PRIVATE
target_link_libraries(${SARIBBON_LIB_NAME} PRIVATE
Qt5::X11Extras
)
endif()

# frameless 依赖了qt-private相关的库
target_link_libraries(${SARIBBONBAR_LIB_NAME} PRIVATE
target_link_libraries(${SARIBBON_LIB_NAME} PRIVATE
Qt${QT_VERSION_MAJOR}::CorePrivate
Qt${QT_VERSION_MAJOR}::GuiPrivate
)

if(APPLE)
target_link_libraries(${SARIBBONBAR_LIB_NAME} PRIVATE
target_link_libraries(${SARIBBON_LIB_NAME} PRIVATE
"-framework Foundation"
"-framework Cocoa"
"-framework AppKit"
)
elseif(UNIX)
if(TARGET X11::xcb)
target_link_libraries(${SARIBBONBAR_LIB_NAME} PRIVATE
target_link_libraries(${SARIBBON_LIB_NAME} PRIVATE
X11::xcb
)
endif()
if(TARGET PkgConfig::GTK3)
target_link_libraries(${SARIBBONBAR_LIB_NAME} PRIVATE
target_link_libraries(${SARIBBON_LIB_NAME} PRIVATE
PkgConfig::GTK3
)
target_compile_definitions(${SARIBBONBAR_LIB_NAME} PRIVATE
target_compile_definitions(${SARIBBON_LIB_NAME} PRIVATE
GDK_VERSION_MIN_REQUIRED=GDK_VERSION_3_6
)
endif()
endif()
endif()
#################################################

set_target_properties(${SARIBBONBAR_LIB_NAME} PROPERTIES
set_target_properties(${SARIBBON_LIB_NAME} PROPERTIES
AUTOMOC ON
AUTOUIC ON
AUTORCC ON
CXX_EXTENSIONS OFF
DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX}
VERSION ${SARIBBON_VERSION}
EXPORT_NAME ${SARIBBONBAR_LIB_NAME}
ARCHIVE_OUTPUT_DIRECTORY "${SARIBBON_BIN_DIR}"
LIBRARY_OUTPUT_DIRECTORY "${SARIBBON_BIN_DIR}"
RUNTIME_OUTPUT_DIRECTORY "${SARIBBON_BIN_DIR}"
EXPORT_NAME ${SARIBBON_LIB_NAME}
)
# 构建库定义的宏
target_compile_definitions(${SARIBBONBAR_LIB_NAME} PRIVATE SA_RIBBON_BAR_MAKE_LIB)
target_compile_definitions(${SARIBBON_LIB_NAME} PRIVATE SA_RIBBON_BAR_MAKE_LIB)
# 使用SAColorWidgets但不作为库使用定义的宏
target_compile_definitions(${SARIBBONBAR_LIB_NAME} PRIVATE SA_COLOR_WIDGETS_MAKE_LIB)
target_compile_definitions(${SARIBBON_LIB_NAME} PRIVATE SA_COLOR_WIDGETS_MAKE_LIB)
if(SARIBBON_USE_FRAMELESS_LIB)
# 显示定义FRAMELESSHELPER_FEATURE_static_build为-1
target_compile_definitions(${SARIBBONBAR_LIB_NAME} PRIVATE FRAMELESSHELPER_FEATURE_static_build=-1)
target_compile_definitions(${SARIBBON_LIB_NAME} PRIVATE FRAMELESSHELPER_FEATURE_static_build=-1)
# 使用frameless,但也要导出符号,因此要定义导出宏
target_compile_definitions(${SARIBBONBAR_LIB_NAME} PRIVATE FRAMELESSHELPER_CORE_LIBRARY)
target_compile_definitions(${SARIBBONBAR_LIB_NAME} PRIVATE FRAMELESSHELPER_WIDGETS_LIBRARY)
target_compile_definitions(${SARIBBON_LIB_NAME} PRIVATE FRAMELESSHELPER_CORE_LIBRARY)
target_compile_definitions(${SARIBBON_LIB_NAME} PRIVATE FRAMELESSHELPER_WIDGETS_LIBRARY)
#使用frameless必须设置SARIBBON_USE_3RDPARTY_FRAMELESSHELPER宏为1
target_compile_definitions(${SARIBBONBAR_LIB_NAME} PUBLIC SARIBBON_USE_3RDPARTY_FRAMELESSHELPER=1)
target_compile_definitions(${SARIBBON_LIB_NAME} PUBLIC SARIBBON_USE_3RDPARTY_FRAMELESSHELPER=1)
else()
#不使用frameless必须设置SARIBBON_USE_3RDPARTY_FRAMELESSHELPER宏为0
target_compile_definitions(${SARIBBONBAR_LIB_NAME} PUBLIC SARIBBON_USE_3RDPARTY_FRAMELESSHELPER=0)
target_compile_definitions(${SARIBBON_LIB_NAME} PUBLIC SARIBBON_USE_3RDPARTY_FRAMELESSHELPER=0)
endif()

# INSTALL_INTERFACE的路径替换,不要出现引号
if(SARIBBON_USE_FRAMELESS_LIB)
target_include_directories(${SARIBBONBAR_LIB_NAME} PUBLIC
$<INSTALL_INTERFACE:"include">
target_include_directories(${SARIBBON_LIB_NAME} PUBLIC
$<INSTALL_INTERFACE:include>
$<INSTALL_INTERFACE:include/3rdparty/framelesshelper/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${FRAMELESS_HEADER_BASE}>
$<BUILD_INTERFACE:${FRAMELESS_HEADER_DIR}>
Expand All @@ -396,15 +395,13 @@ if(SARIBBON_USE_FRAMELESS_LIB)
$<BUILD_INTERFACE:${FRAMELESS_HEADER_DIR_WIDGETS_PRIVATE}>
)
else()
target_include_directories(${SARIBBONBAR_LIB_NAME} PUBLIC
$<INSTALL_INTERFACE:"include">
target_include_directories(${SARIBBON_LIB_NAME} PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)
endif()

# ------------------------------- install script -------------------------------

set(CMAKE_INSTALL_PREFIX ${SARIBBON_BIN_DIR})
message(STATUS "install dir ${SARIBBON_BIN_DIR}")

# 头文件
Expand All @@ -422,9 +419,11 @@ install(FILES

# 生成单一文件
# 单一文件是把整个SARibbon打包为一个h和一个cpp,方便集成
set(SARIBBON_AMALGAMATE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../SARibbon.h
${CMAKE_CURRENT_SOURCE_DIR}/../SARibbon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../SARibbon.pri)
set(SARIBBON_AMALGAMATE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/../SARibbon.h
${CMAKE_CURRENT_SOURCE_DIR}/../SARibbon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../SARibbon.pri
)
install(FILES
${SARIBBON_AMALGAMATE_FILES}
DESTINATION amalgamate
Expand Down Expand Up @@ -458,45 +457,47 @@ if(SARIBBON_USE_FRAMELESS_LIB)
endif()

include(CMakePackageConfigHelpers)
# SARibbonBarConfig.cmake.in中,会让此变量和“${CMAKE_CURRENT_LIST_DIR}/”进行拼接,也就是${CMAKE_CURRENT_LIST_DIR}/@SARIBBONBAR_LIB_INCLUDE_INSTALL_DIR@
# SARibbonBarConfig.cmake.in中,会让此变量和“${PACKAGE_PREFIX_DIR}/”进行拼接,也就是${PACKAGE_PREFIX_DIR}/@SARIBBON_LIB_INCLUDE_INSTALL_DIR@
# PACKAGE_PREFIX_DIR等于${CMAKE_CURRENT_LIST_DIR}/../..
# 最终变为${CMAKE_CURRENT_LIST_DIR}/../../include
set(SARIBBONBAR_LIB_INCLUDE_INSTALL_DIR "../../include")
# 注意此处是替换,不要出现引号
set(SARIBBON_LIB_INCLUDE_INSTALL_DIR include)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/SARibbonConfigVersion.cmake
${CMAKE_CURRENT_BINARY_DIR}/${SARIBBON_LIB_NAME}ConfigVersion.cmake
VERSION ${SARIBBON_VERSION}
COMPATIBILITY SameMajorVersion
)
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/SARibbonBarConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/SARibbonConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${SARIBBON_LIB_NAME}Config.cmake"
INSTALL_DESTINATION "lib/cmake"
PATH_VARS SARIBBONBAR_LIB_INCLUDE_INSTALL_DIR
PATH_VARS SARIBBON_LIB_INCLUDE_INSTALL_DIR
)
message("CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}")
install(TARGETS ${SARIBBONBAR_LIB_NAME}
EXPORT SARibbonBarTargets
install(TARGETS ${SARIBBON_LIB_NAME}
EXPORT ${SARIBBON_LIB_NAME}Targets
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
INCLUDES DESTINATION include
)
install(EXPORT SARibbonBarTargets
FILE SARibbonTargets.cmake
install(EXPORT ${SARIBBON_LIB_NAME}Targets
FILE ${SARIBBON_LIB_NAME}Targets.cmake
DESTINATION lib/cmake
)

install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/SARibbonConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/SARibbonConfigVersion.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${SARIBBON_LIB_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${SARIBBON_LIB_NAME}ConfigVersion.cmake"
DESTINATION lib/cmake
)

# 资源文件生成

if(WIN32)
create_win32_resource_version(
TARGET ${SARIBBONBAR_LIB_NAME}
FILENAME ${SARIBBONBAR_LIB_NAME}
TARGET ${SARIBBON_LIB_NAME}
FILENAME ${SARIBBON_LIB_NAME}
EXT "dll"
DESCRIPTION "Ribbon control library for Qt"
)
Expand Down
8 changes: 4 additions & 4 deletions src/SARibbonBar/SARibbonBarConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
# @PROJECT_NAME@_LIBRARIES, where to find the libs
@PACKAGE_INIT@

set (PackageName @SARIBBONBAR_LIB_NAME@)
set (@SARIBBONBAR_LIB_NAME@_VERSION @SARIBBON_VERSION@)
set (PackageName @SARIBBON_LIB_NAME@)
set (@SARIBBON_LIB_NAME@_VERSION @SARIBBON_VERSION@)

include ( ${CMAKE_CURRENT_LIST_DIR}/SARibbonTargets.cmake )
set_and_check ( ${PackageName}_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/@SARIBBONBAR_LIB_INCLUDE_INSTALL_DIR@ )
include ( ${CMAKE_CURRENT_LIST_DIR}/${PackageName}Targets.cmake )
set_and_check ( ${PackageName}_INCLUDE_DIR ${PACKAGE_PREFIX_DIR}/@SARIBBON_LIB_INCLUDE_INSTALL_DIR@ )
set ( ${PackageName}_LIBRARIES)
list ( APPEND ${PackageName}_LIBRARIES ${PackageName})

Expand Down

0 comments on commit 3c91213

Please sign in to comment.