如何使用主要和“-devel”的cmake pack redhat-style rpm?

How to use cmake pack redhat-style rpm, which is major and "-devel"?

我试过用cmake来打包redhat风格的rpm。 经典的 RedHat 风格的 rpm 由 2 个 rpm 组成,一个是 package-version.rpm,另一个是 package-devel-version.rpm。但是我失败了。

例如项目foo,其中包含

- bin/demo 
- lib/libfoo.so.1.2.3
- lib/libfoo.so.1 
- lib/libfoo.so
- include/foo.h

我希望 foo-1.2.3.rpm 包含 bin/demolib/libfoo.so.1lib/libfoo.so.1.2.3 并且 foo-devel-1.2.3.rpm 包含 lib/libfoo.soinclude/foo.h .

但是现在,我只能得到foo-1.2.3-major.rpm (bin/demo, lib/libfoo.so, lib/libfoo.so.1, lib/libfoo.so.1.2.3) 和foo-1.2.3-devel.rpm (include/foo.h).

# CMakeLists.txt 

project (foo)

include (GNUInstallDirs)

set(CPACK_PACKAGE_VERSION_MAJOR 1)
set(CPACK_PACKAGE_VERSION_MINOR 2)
set(CPACK_PACKAGE_VERSION_PATCH 3)
set(CPACK_PACKAGE_RELEASE 1)
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "The CMake Demo Project")
set(CPACK_GENERATOR RPM)
set(CPACK_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_COMPONENTS_ALL major devel)

include (CPackComponent)
include (CPack)
cpack_add_component(major DISPLAY_NAME Major)
cpack_add_component(devel DISPLAY_NAME Development)

add_executable (demo demo.cxx)

add_library (foo SHARED foo.cxx foo.h)
set_target_properties(foo PROPERTIES VERSION 1.2.3 SOVERSION 1)

install (TARGETS demo foo
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    COMPONENT major
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    COMPONENT major
    )
install (FILES foo.h
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    COMPONENT devel
    )

感谢@Tsyvarev,我取得了一些进步。 在 include(CPack) 之前添加行 set(CPACK_RPM_MAIN_COMPONENT major),我得到以下结果。 rpm 名称 foo-1.2.3.rpm 现在是正确的。

]$ rpm -ql --qf "%{name}-%{version}\n" -p foo-1.2.3-devel.rpm
foo-devel-1.2.3
/usr/include/foo.h

]$ rpm -ql --qf "%{name}-%{version}\n" -p foo-1.2.3.rpm
foo-1.2.3
/usr/bin/demo
/usr/lib64/libfoo.so
/usr/lib64/libfoo.so.1
/usr/lib64/libfoo.so.1.2.3

但是foo-1.2.3-devel.rpm的文件名还是不对,rpm里面的文件还是不对

我终于完成了我的CMakeLists.txt

# CMakeLists.txt 
cmake_minimum_required (VERSION 3.12)

set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "build type")

project (foo VERSION 1.2.3 HOMEPAGE_URL "http://null")

include (GNUInstallDirs)

set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "The CMake Demo Project")
set(CPACK_COMPONENTS_ALL major devel)
set(CPACK_GENERATOR RPM)
set(CPACK_RPM_PACKAGE_RELEASE 10)
set(CPACK_RPM_PACKAGE_RELEASE_DIST  ON)
set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_RPM_MAIN_COMPONENT major) # Which set the major package name to foo instead of foo-major
set(CPACK_RPM_FILE_NAME "RPM-DEFAULT") # Which makes the rpm name from foo-1.2.3-devel to foo-devel-1.2.3
set(CPACK_RPM_DEBUGINFO_PACKAGE ON)
set(CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE ON)

# you must include it here after set(CPACK...) and befor cpack_add_... 
include (CPack) 

cpack_add_component(major DESCRIPTION "Something for demo")
cpack_add_component(devel DESCRIPTION "For Development")

add_executable (demo demo.cxx)

add_library (foo SHARED foo.cxx foo.h)
set_target_properties(foo PROPERTIES VERSION 1.2.3 SOVERSION 1)

set(PUBLIC_HEADERS foo.h)

install (TARGETS demo foo
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    COMPONENT major
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    COMPONENT major
    NAMELINK_COMPONENT devel # Which separate libfoo.so into foo-devel rpm, but it requires cmake-3.12, RHEL-8 takes 3.11.
    )
install (FILES ${PUBLIC_HEADERS}
    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
    COMPONENT devel
    )
install (FILES README.md
    DESTINATION ${CMAKE_INSTALL_DOCDIR}-${PROJECT_VERSION}
    COMPONENT devel
    )

使用 cmake-3.11 的 RHEL-8 解决方法:

# install libfoo.so.1.2.3 and libfoo.so.1
install (TARGETS foo
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    COMPONENT major
    NAMELINK_SKIP
    )
# install libfoo.so
install (TARGETS foo
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    COMPONENT devel
    NAMELINK_ONLY
    )

感谢大家