I need to create a RPM of my software expecting the following output: <my_sw_name>-<version>-<release>.rpm.
The default for <release> shall be 0.
In my CMakeLists.txt I am using the following command:
cmake_minimum_required(VERSION 3.20)
...
set(SW_RELEASE 0)
set(CPACK_GENERATOR "RPM")
set(CPACK_RPM_PACKAGE_RELEASE ${SW_RELEASE})
set(CPACK_RPM_PACKAGE_RELEASE_DIST OFF)
set(CPACK_RPM_FILE_NAME "RPM-DEFAULT")
...
Nevertheless, the generated RPM filename is <my_sw_name>-<version>-1.rpm, and the Release: field in the RPM file is set to 1.
Is this a bug in CPack or did I miss something?
To any future users who aren't aware, It's conventional to start the release number for RPM packages at 1 and not at 0. And here's a link to the CPackRPM docs in case anyone needs it.
Reading through :/Modules/Internal/CPack/CPackRPM.cmake for CMake version 3.24.1, I think the culprit is the following code at line 965 inside the function cpack_rpm_generate_package:
if(NOT CPACK_RPM_PACKAGE_RELEASE)
set(CPACK_RPM_PACKAGE_RELEASE "1")
endif()
The string 0 in CMake is falsy. Ex,
if(NOT 0)
message("zero is falsy") # this will be reached and printed
endif()
Ie. your 0 causes the expression NOT CPACK_RPM_PACKAGE_RELEASE to evaluate to FALSE, and then your 0 gets clobbered to a 1.
If you want the value 0 to be supported, you could file an issue on the CMake gitlab repo requesting it. It would probably look like changing it to this:
if(NOT DEFINED CPACK_RPM_PACKAGE_RELEASE)
set(CPACK_RPM_PACKAGE_RELEASE "1")
endif()
If this is truly the cause and you do file an issue to kitware, please either add a link to it as a comment under this answer, or to the end of the body of your question so future readers can find it.
Related
subject pretty much says it all:
I downloaded yaml-cpp version 0.6.3.
I need to compile on linux x86_64, target linux x86_32 (build on 64 bit, use result on 32-bit)
I have been trying to add a new "YAML_BUILD_32BIT" option - similar to the existing YAML_BUILD_SHARED_LIBS option.
When I detect YAML_BUILD_32BIT is set: I try to add "-m32" to a bunch of cmake variables.
My problem is that this list of variables seems endless or not well defined.
"yaml_cxx_flags" are passed to the compile and link steps for the yaml-cpp library code...but not to build the google 'mock' code. Similarly, I found other variables that I can also set, so that google-mock is compiled with -m32 as well...but then the yaml-cpp mock tests do not see the flag...and so on and so on.
I think I am missing something very fundamental. I expect that there will be a single variable I need to update...maybe 2 or 3. I don't expect to keep finding more and more.
--
Adding more specifics:
To CMakeLists.txt:
added line (immediately after the similar line which creates the YAML_BUILD_SHARED_LIBS flag)
option(YAML_BUILD_32BIT "Build with '-m32'" OFF)
then a bit later (immediately after the YAML_BUILD_SHARED_LIBS if/else):
if(YAML_BUILD_32BIT)
# seem to need this one for the shared lib link of yaml-cpp lib
# CXX_FLAGS passed to both compile and link
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
# seem to need this one, to get flag passed to gmock build
set(FLAG_TESTED "${FLAG_TESTED} -m32")
# this one passed to compile and link of testcase
set(yaml_cxx_flags "${yaml_cxx_flags} -m32")
endif()
and made "FLAG_TESTED" addive, on immediately following line:
set(FLAG_TESTED "-Wextra -Wshadow -Weffc++ -pedantic -pedantic-errors ${FLAG_TESTED}")
Given the above, then configuring with:
# using cmake/3.19.3
cmake -G "Unix Makefiles" -DYAML_BUILD_SHARED_LIBS=ON -DYAML_BUILD_32BIT=ON"
... and then building with 'make VERBOSE=1', I see that 'gmock-all.cc.o' did not receive the -m32 flag. (gmock-all.cc.o is only the first such file in my log..there are others.)
If I remove other of the lines in my CMakeLists.txt which attempted to add flags - then other compile commands or other link commands don't see -m32 and will fail.
As I said: I think there is something fundamental that I have misunderstood. I suspect that it is much easier to configure a 32-bit build than I am making it.
With some help from a coworker, I ended up doing the following:
top-level CMakeLists.txt file (near line 28, immediately following definition of YAML_BUILD_SHARED_LIBS variable):
option(YAML_BUILD_32BIT "Build with '-m32'" OFF)
if(YAML_BUILD_32BIT)
add_compile_options(-m32)
add_link_options(-m32)
endif()
in .../test/CMakeLists.txt (near line 10):
if(YAML_BUILD_32BIT)
set(GTEST_EXTRA_FLAGS "-DCMAKE_CXX_FLAGS=-m32")
endif()
then add new flag to "ExternalProject_Add(..." call (near line .../test/CMakeLists.txt:22):
ExternalProject_Add(
googletest_project
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.8.0"
INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/prefix"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-DBUILD_GMOCK=ON
-Dgtest_force_shared_crt=ON
${GTEST_EXTRA_FLAGS} # <- this line added
)
The above has the effect of passing the extra "-m32" flag the embedded gmocktest project.
Given the above changes, the cmake command line above generates something that will build successfully (at least on RHEL-7, with gcc/5.2.0)
Hope this can help somebody else.
Henry
Myproject/CMakeLists.txt:
find_package(Foo 2.0 EXACT REQUIRED)
Foo/CMakeLists.txt:
set(Foo_SOVERSION 1)
set(Foo_VERSION ${Foo_SOVERSION}.8)
Why does cmake Myproject not fail? It only notifies
-- Found Foo: /usr/local/lib/libfoo.so (Required is exact version "2.0")
and happily proceeds. How to enforce termination if an exact requirement is not met?
If you are also writing the FindFoo.cmake macro, then you can add to the macro something like following to enforce version checking.
if(FOO_FIND_VERSION)
# Add a version check logic and set FOO_FOUND as true conditionally
else()
set(FOO_FOUND TRUE)
endif()
Refer https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html#find-modules for more details.
In my project, I use cmake to construct the building system, I need to build an external project(here, I take zeromq for example) with ExternalProject_add, then pack the compiled binaries in a RPM package, but I need the generated RPM to have correct "PROVIDES" information to tell which libraries it provides, just like below
libzmq.so.5()(64bit)
zeromq = 4.1.2-1.el7
zeromq(x86-64) = 4.1.2-1.el7
But somehow, with setting CPACK_RPM_PACKAGE_AUTOPROV to 1, the built RPM still doesn't have correct 'PROVIDES' information, I will get 'PROVIDES' information below, without the provided libraries information
zeromq = 4.1.2-1
zeromq(x86-64) = 4.1.2-1
the CMakeLists.txt(just some key content) for this is
cmake_minimum_required (VERSION 3.4.0 FATAL_ERROR)
set(COMP zeromq)
set(CompVersion 4.1.2)
set(CompURL http://download.zeromq.org/zeromq-${CompVersion}.tar.gz)
set(CompMD5 159c0c56a895472f02668e692d122685)
project(${COMP} VERSION ${CompVersion})
include(ExternalProject)
ExternalProject_add(${COMP}
PREFIX ${COMP}
URL ${CompURL}
URL_MD5 ${CompMD5}
CONFIGURE_COMMAND <SOURCE_DIR>/configure --without-libsodium --prefix=${CMAKE_INSTALL_PREFIX})
install(FILES ${CMAKE_INSTALL_PREFIX}/lib/libzmq.so.5
${CMAKE_INSTALL_PREFIX}/lib/libzmq.so
${CMAKE_INSTALL_PREFIX}/lib/libzmq.so.5.0.0
DESTINATION lib64)
string(REPLACE "." ";" VERSION_LIST ${PROJECT_VERSION})
list(LENGTH VERSION_LIST VERSION_LIST_LENGTH)
list(GET VERSION_LIST 0 CPACK_PACKAGE_VERSION_MAJOR)
list(GET VERSION_LIST 1 CPACK_PACKAGE_VERSION_MINOR)
if(VERSION_LIST_LENGTH GREATER 2)
list(GET VERSION_LIST 2 CPACK_PACKAGE_VERSION_PATCH)
endif()
set(CPACK_GENERATOR "RPM")
set(CPACK_PACKAGE_VENDOR "Test")
set(CPACK_RPM_PACKAGE_GROUP "3rd-party-software")
set(CPACK_RPM_PACKAGE_AUTOPROV 1)
set(CPACK_RPM_PACKAGE_AUTOREQ 0)
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION
/usr/lib
/usr/lib64)
set(CPACK_RPM_COMPONENT_INSTALL OFF)
include(CPack)
Does someone know why this "CPACK_RPM_PACKAGE_AUTOPROV" option doesn't take effect? how can I make it auto generates these 'PROVIDES' information in the RPM? thanks for your time and it will be very appreciated if you can provide some hints.
CPACK_RPM_PACKAGE_AUTOPROV variable, by its description:
May be used to enable (1, yes) or disable (0, no) automatic listing of shared libraries that are provided by the package.
affects only on targets created by add_library(SHARED).
Neither CMake nor CPack tries to deduce file's type from their extension, that's why manually installed files (via install(FILES)) are not affected.
For add given files to PROVIDES list, use variable CPACK_RPM_PACKAGE_PROVIDES:
May be used to set RPM dependencies (provides). The provided package list of an RPM file could be printed with:
rpm -qp --provides file.rpm
Using install(PROGRAMS instead of install(FILES generates correct provides in the rpm for .so files (at least with cmake 3.13). As per the documentation:
The PROGRAMS form is identical to the FILES form except that the default permissions for the installed file also include OWNER_EXECUTE, GROUP_EXECUTE, and WORLD_EXECUTE. This form is intended to install programs that are not targets, such as shell scripts.
Maybe the documentation could add that it's suitable for shared libs too.
If you want to stick to CPACK_RPM_PACKAGE_PROVIDES, beware that the variable has to be a comma separated list.
(Maybe another possible addition to the documentation).
I start using CMake to build my c++ source files, I see a strange comportament when I build inicially:
'cmake ../' will gerate the directory structure
'make' will build all
any successive make command will build nothing, as expected
'cmake ../' will apparent do nothing
'make' WILL REBUILD all
any successive make command will build nothing, as expected
There is my CMakelists.txt:
cmake_minimum_required(VERSION 2.6)
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_FLAGS "-Wall -pipe")
set(var_target CommonBase)
set(var_path_source base)
project(Prj_${var_target})
file(GLOB_RECURSE var_sources ${var_path_source}/*.cpp)
add_library(${var_target} SHARED ${var_sources})
install(TARGETS ${var_target} DESTINATION ${PROJECT_SOURCE_DIR}/install)
Looking better, at first 'cmake ../' command the file 'CMakeFiles/CommonBase.dir/depend.make' is empty, and the successive make command will insert the list of file dependencies
There is something wrong with my CMakelists.txt?
Thanks
There are a couple of issues here.
The actual cause of your problem is having the line set(CMAKE_CXX_FLAGS ...) before the project command.
The project command does quite a lot of work the first time it is run, and actually clears out this variable as a side-effect. So on your first run of CMake, the compiler flags are empty, and thereafter always contain what you set them to. (It's only the second time you run CMake which causes make to recompile all, not subsequent runs of CMake).
Try wrapping your project call with messages to see the effect:
message("CMAKE_CXX_FLAGS - ${CMAKE_CXX_FLAGS}")
project(Prj_${var_target})
message("CMAKE_CXX_FLAGS - ${CMAKE_CXX_FLAGS}")
Delete your CMakeCache.txt file (in your build root), then just run cmake .. repeatedly.
To fix this, move your set(CMAKE_CXX_FLAGS ...) to after the project command.
The second issue is that it's not recommended to set CMAKE_CXX_COMPILER in a CMakeLists.txt. Have a look at the comment below "Setting default compiler in CMake", and also the link there to CMake's FAQ entry How do I use a different compiler?
I'm attempting to debug a command line CMake failure. The same CMake file works in Qt Creator, with the arguments in the Qt Creator window matching what I have entered on the command line.
This makes me think Qt Creator is adding some extra arguments, which makes sense since the generator drop down has several options that specify architecture and CMake version.
Is there a way to get the CMake command that Qt Creator executed to produce the desired result, specifically the arguments passed to the CMake executable?
I found one post that talks about viewing the CMakeCache files to do some forensics, but this only proves there are differences, it doesn't quickly show me what arguments to change.
Try adding the following block to the end of your CMakeLists.txt and running CMake from Qt Creator again. The CMake output should list all variables that have been passed via the -D command line argument.
get_cmake_property(CacheVars CACHE_VARIABLES)
foreach(CacheVar ${CacheVars})
get_property(CacheVarHelpString CACHE ${CacheVar} PROPERTY HELPSTRING)
if(CacheVarHelpString STREQUAL "No help, variable specified on the command line.")
get_property(CacheVarType CACHE ${CacheVar} PROPERTY TYPE)
if(CacheVarType STREQUAL "UNINITIALIZED")
set(CacheVarType)
else()
set(CacheVarType :${CacheVarType})
endif()
set(CMakeArgs "${CMakeArgs} -D${CacheVar}${CacheVarType}=\"${${CacheVar}}\"")
endif()
endforeach()
message("CMakeArgs: ${CMakeArgs}")
For more info, see this answer.
This won't show what generator was selected (if any) via the -G arg. To find that, you need to look for CMAKE_GENERATOR:INTERNAL=... in your CMakeCache.txt
If this doesn't help you identify the overall problem, you should probably heed #arrowdodger's advice and post more details about the errors you're getting and your two build environments. For example, an error could be caused simply by running CMake from a subdirectory of the source tree.