如何使用 cmake pack redhat 风格的 rpm,这是主要的和"-devel"?

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

本文关键字:-devel cmake 何使用 pack redhat rpm 风格      更新时间:2023-10-16

我尝试使用cmake来打包redhat风格的rpm。 经典的红帽式转速由 2 rpm 组成,一个是package-version.rpmrpm,另一个是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.3foo-devel-1.2.3.rpm包含lib/libfoo.soinclude/foo.h

但是现在,我只能得到foo-1.2.3-major.rpm(bin/demolib/libfoo.solib/libfoo.so.1lib/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
)

感谢大家