Packaging directory with cpack for rpm - cmake

I am trying to create a rpm package with directory with lot of files using cmake
http://www.rpm.org/max-rpm/s1-rpm-inside-files-list.html
To make this situation a bit easier, if the %files list contains a path to a directory, RPM will automatically package every file in that directory, as well as every file in each subdirectory. Shell-style globbing can also be used in the %files list.
So with cmake I am using the following command:
INSTALL(DIRECTORY my_dir DESTINATION foo)
and I end up with a spec file with all the files (30k lines) instead of something like
%file
my_dir
Did I miss something on my cmake/cpack command or the is no other way to do it?
(using a tar and an extracting it is not suitable)

Related

Zip creation using CMake with no relative path

I want to bundle the build artifacts into a zip to deliver them on the delivery server. That for I integrated the zip creation into the CMake routine.
# zip the final release files into the build artifact
# call the zipping with 'cmake --build . --target zip' or 'make zip'
string(TIMESTAMP DATE_STRING %Y%m%d)
add_custom_target(zip
COMMAND ${CMAKE_COMMAND} -E tar "cfv" "${PROJECT_NAME}-${PROJECT_VERSION}-${CMAKE_CXX_COMPILER_ID}-${CMAKE_CXX_COMPILER_VERSION}-${DATE_STRING}.zip" "--format=zip" "--"
"${PROJECT_SOURCE_DIR}/bin/release/*")
The artifacts are bundeld into the bin/release folder in the ProjectSourceDir.
My problem is, that the zip file has the relative path to the files from my build directory. So all files are located under ../bin/release/ which is not what I want. I want to have the files in the "home folder" of the zip file.
I've found the solution to prepare the zip folder in my build folder, but thats something I think is very unpractical or looks dirty to me.
A WORKING_DIRECTORY can be specified to the add_custom command.
add_custom_target(zip
COMMAND ${CMAKE_COMMAND} -E tar cvf ${PROJECT_SOURCE_DIR}/${PROJECT_NAME}-${PROJECT_VERSION}-${CMAKE_CXX_COMPILER_ID}-${CMAKE_CXX_COMPILER_VERSION}-${DATE_STRING}.zip --format=zip .
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/bin/release
)
You don't need all that extra quoting as given in the example.

Setting path for CMake files

Is there any way to specify where CMake should create its build files? I mean some kind of set function. I want to run CMake in root directory and get its files in /build directory.
Files I want to put to build directory:
CMakeFiles/
cmake_install.cmake
CMakeCache.txt
Makefile
You can specify the build directory using the -B flag. You can invoke cmake from anywhere by using the -H flag, which provides the source directory (with the CMakeLists.txt file).
cmake -H/path/to/source -B/path/to/build

During build, how to create a directory for a file which is generated by some COMMAND?

I'm writing a custom command in CMake to convert a file to binary format at build time. However, the tool I'm using requires directory for a file to be pre-existed; it doesn't create a directory automatically.
Currently my CMake macro looks like this:
MACRO(ConvertToBinary _name)
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_BINARY_DIR}/${_name}.bin
COMMAND ${EXE_DIR}/toBinary -i ${CMAKE_CURRENT_SOURCE_DIR}/${_name} -o ${CMAKE_BINARY_DIR}/${_name}.bin
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_name}
)
ENDMACRO(ConvertToBinary)
This macro doesn't work for _name parameter equal, e.g., to "test/sample.txt", because the tool is run with test/ folder under ${CMAKE_BINARY_DIR} being not existed.
How to create the directory before the tool, generating the file, is run?
I have tried to overcome the problem by pre-creating the file (and directory) with the help of CMake. So the tool will be run with the directory created, and can override the output file. I have tried the following:
MACRO(ConvertToBinary _name)
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_BINARY_DIR}/${_name}.bin
FILE(WRITE ${CMAKE_BINARY_DIR}/${_name}.bin "Dummy")
COMMAND ${EXE_DIR}/toBinary -i ${CMAKE_CURRENT_SOURCE_DIR}/${_name} -o ${CMAKE_BINARY_DIR}/${_name}.bin
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_name}
)
ENDMACRO(ConvertToBinary)
But it did not work. I can't find information on how to create file at build time with CMake, so any help is appreciated.
In CMake you may extract directory component of the file and create this directory.
Below code can be used for a tool, which generates a file but doesn't create directory for it.
# Assume some tool generates file '${file}', but don't create directory for it.
#
# Extract directory component for a file
get_filename_component(_dir ${file} DIRECTORY)
add_custom_command(OUTPUT ${file}
COMMAND ${CMAKE_COMMAND} -E make_directory ${_dir} # Create output directory
COMMAND <tool-which-generates-file> ... ${file}
)
See also that question about ways for create a directory in CMake.

run a shell command (ctags) in cmake and make

I'm coding a c++ project in vim.
I'd like to run a ctags command (ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .) to generate references when I run make.
I think the way to do it is to use add_custom_command but I get confused on how to integrate it into CMakeLists.txt .
The most basic way to do this is:
set_source_files_properties( tags PROPERTIES GENERATED true)
add_custom_command ( OUTPUT tags
COMMAND ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} )
add_executable ( MyProjectOutput tags )
The first line tells CMake that tags will be generated. The add_custom_command is CMake will generate tags when needed, and finally, some target needs to depend on tags. The default working directory is in the build tree, so WORKING_DIRECTORY must be set to your source tree. This is equivalent a Makefile entry:
tags:
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
MyProjectOutput: tags
# Whatever here...
New solution:
I think CMake changed since the previous answer was given.
Here is the lines I added in my CMakeLists.txt (tested with version 2.8.12):
# Add "tags" target and make my_project depending on this target.
set_source_files_properties(tags PROPERTIES GENERATED true)
add_custom_target(tags
COMMAND ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
add_dependencies(my_project tags)
And it works perfectly now!
Daniel's and Creak's answers got me started, but I ended up with a more complex solution that I thought I'd share:
# Add a top-level "tags" target which includes all files in both
# the build and source versions of src/*.
set_source_files_properties(tags PROPERTIES GENERATED true)
add_custom_target(tags
COMMAND ctags -R --c++-kinds=+p --fields=+iaS --extra=+q
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ln -sf ${CMAKE_CURRENT_BINARY_DIR}/tags ${CMAKE_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
# ...but only make it a dependency of the project if the ctags program
# is available, else it will fail to build on Windows.
find_program(CTAGS_PATH ctags)
if(CTAGS_PATH)
message(STATUS "Found ctags: ${CTAGS_PATH}")
add_dependencies(MyProjecct tags)
endif(CTAGS_PATH)
It does several things that the simpler solutions do not:
It only adds "tags" as a dependency of the primary build product (MyProject) if there is actually a ctags program on the system. We don't want to break the build just because this is Windows, or because ctags simply hasn't been installed yet on the build system.
It extracts symbols from source files in both the build and source directories. This matters in a couple of cases.
First, you might be using configure_file() and come from an Autotools background, so you've named your true source files *.in, which means ctags -R won't scan them. You need it to scan the generated versions in the build directory. For example, you might have src/mainheader.h.in in your source tree, with the project version number automatically subbed into it as build/src/mainheader.h.
Second, some of your "source" files might be generated by other tools. In my current project, I have a couple of C++ header files that are generated by Perl scripts. I want symbols from both the generated headers and the Perl scripts in the tags file.
It works in a subdirectory.
In the project I'm working on right now, the primary build product is made from src/* relative to the project root, and I only want symbols from that subtree in the tags file. I don't want it to include symbols from the unit tests, the examples, or the utility scripts.
Because it is designed to run in a subdirectory, it creates a symlink to the src/tags file in the top of the build directory, so that vi -t TagName works. (I'm assuming here that if ctags exists, ln does, too.)

How to set the CMAKE_PREFIX_PATH?

I have a problem with the global environmental variable CMAKE_PREFIX_PATH. I already set this and I can see it is set when I type env, but when I run cmake . to build HipHop, it tells me that the variable isn't set.
Is there a way I can hard-code this into the makefiles?
Try to run cmake -DCMAKE_PREFIX_PATH=/your/path .
CMAKE_PREFIX_PATH works as a build directive, rather than as an environment variable. Moreover, you may perform the build into a dedicated temporary directory (it's cleaner, because when done, you can remove that temporary directory and you get back a clean pristine source tree).
$ mkdir -p tmpbuild && cd tmpbuild
$ cmake -DCMAKE_PREFIX_PATH=~/deliveries/hiphop ..
$ make install
$ cd ..
On MacOS it's different. I had to use:
make -i CMAKE_PREFIX_PATH="/the/path"
This was while installing VMQT, and this error was shown:
CMake Error at CMakeLists.txt:87 (find_package): By not providing
"FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has asked CMake
to find a package configuration file provided by "OpenCV", but CMake
did not find one.
Could not find a package configuration file provided by "OpenCV"
with any of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If
"OpenCV" provides a separate development package or SDK, be sure it
has been installed.
Used this to solve it: make -i CMAKE_PREFIX_PATH="/opt/homebrew/Cellar/opencv/4.6.0_1/lib/cmake/opencv4/"