Let's say I have the directory X which includes many files and I want to install it to some destination:
INSTALL(DIRECTORY x
Destination ${DEST}
)
When I run this command, the folder structure would look like this:
${DEST}/x/a
${DEST}/x/b
${DEST}/x/c
...
How can I make CMake to "Unpack" the directory structure and my output like this?
${DEST}/a
${DEST}/b
${DEST}/c
...
When you append a / to your directory x the contents of the directory is copied to the DESTINATION, i.e.
install(DIRECTORY x/
DESTINATION ${DEST}
)
The DESTINATION keyword has to be given upper case.
See CMake's install documentation for further details.
Related
I am building a project using ROS and thus, catkin_make to build my ROS nodes and libraries.
The problem I'm facing is:
I am using a git submodule in one package (package A) (and thus, I have a hierarchical include folder structure) and I have difficulties referencing a header file within that submodule.
In order to build the package B, which is dependent on package A, I have added the INCLUDE_DIRS statement to the catkin_package command in package A:
catkin_package(
INCLUDE_DIRS my-submodule/include
...
)
The content of that directory is:
my-submodule/my-header.h
(I have put the header files under a folder, named after the submodule, as many tutorials stated that within ROS you should use this convention).
The include statement in a file from package-B reads like this:
...
#include <my-submodule/my-header.h>
...
This works fine - package B is being built (as I am using one combined workspace to build this).
But: When I switch to the target system, where I only install package A, and then try to build package B (on that target system), it does not build because the include paths are not setup correctly.
The INSTALL statement for package A looks like this
install(DIRECTORY my-submodule/include
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
PATTERN ".svn" EXCLUDE
)
This is mainly, because the installed folder structure on the target system looks like this:
.../ros/include/my-package-A/include/my-submodule/my-header.h
So, the install process actually puts that submodule's include-path under the package-A-include path (which is a different path structure compared to when I build the packages directly in one combined workspace).
And the CFLAGS for compilation only set the include directory to the folder:
.../ros/include
And thus, breaking my include statement in my package-B file:
#include <my-submodule/my-header.h>
Do you have any idea how to solve this?
I am sure there are more people than me, trying to reference header files from a submodule within a package.
Assuming you have a file my-submodule/include/my-submodule/my-header.h inside your package A, then two small changes to your install() statement should fix this:
install(DIRECTORY my-submodule/include/
DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
PATTERN ".svn" EXCLUDE
)
First, add a slash to the path (.../include/ instead of .../include), which causes the contents of the include folder to be installed instead of the include folder itself (Otherwise you'll end up with ../ros/install/include/include/my-submodule/my-header.h)
Secondly, use ${CATKIN_GLOBAL_INCLUDE_DESTINATION} (which points to .../ros/install/include/) instead of ${CATKIN_PACKAGE_INCLUDE_DESTINATION} (which points to .../ros/install/my-package-A/include/) as destination.
The alternative would be to fix catkin, as
catkin_package(
INCLUDE_DIRS my-submodule/include
...
)
should theoretically already export my-submodule/include, so you can pick it up in package B with
find_package(catkin REQUIRED DEPENDS my-package-A)
catkin_package(
CATKIN_DEPENDS my-package-A
)
include_directories(${catkin_INCLUDE_DIRS})
Unfortunately, for some reason this is explicitely not possible when using catkin config --install. See https://answers.ros.org/question/335846/install_dirs-not-working-as-expected-when-using-install/.
assuming i have the next structure in my project:
src_dir\a
src_dir\b\b2
src_dir\c\c2\c3
and in each folders i have few types of files (.txt, .lib, .dll....)
i want to install only the dll files in directory X, so i tried:
install(
DIRECTORY src_dir
DESTINATION X/
COMPONENT DLLS
FILES_MATCHING PATTERN "*.dll"
)
it does work fine but it give me the full structure of my original structure (and i only want the dll files in X directory). the output it:
X/a/a.dll
X/b/b2/b.dll
X/c/c2/c3/c.dll
and i want it to be like that a.dll, b.dll and c.dll will be in X (without any sub-folders).
is there a way to do it without giving the full paths to every dll file i have in my project?
Thanks :)
You should be able to get the behaviour you want by listing each directory, not necessarily each DLL. If you include a trailing forward slash at the end of the DIRECTORY, it will omit the directory name when copying to the destination. I'd expect something like the following to achieve what you want:
install(
DIRECTORY src_dir/a/
DESTINATION X
COMPONENT DLLS
FILES_MATCHING PATTERN "*.dll"
)
install(
DIRECTORY src_dir/b/b2/
DESTINATION X
COMPONENT DLLS
FILES_MATCHING PATTERN "*.dll"
)
install(
DIRECTORY src_dir/c/c2/c3/
DESTINATION X
COMPONENT DLLS
FILES_MATCHING PATTERN "*.dll"
)
I'm trying to create an installation package with CMake and CPack. Everything works fine, but I would like to reduce the amount of code drastically by copying my resources folder entirely with one call instead of one for every subfolder.
So far, I do component wise installation the following way:
set(RESOURCES_CALIBRATION_DIR resources/calibration)
file(GLOB RESOURCES_CALIBRATION "${CMAKE_SOURCE_DIR}/${RESOURCES_CALIBRATION_DIR}/*")
install(FILES ${RESOURCES_CALIBRATION} DESTINATION ${RESOURCES_CALIBRATION_DIR} COMPONENT ResourcesCalibration)
set(RESOURCES_CURSORS_DIR resources/cursors)
file(GLOB RESOURCES_CURSORS "${CMAKE_SOURCE_DIR}/${RESOURCES_CURSORS_DIR}/*")
install(FILES ${RESOURCES_CURSORS} DESTINATION ${RESOURCES_CURSORS_DIR} COMPONENT ResourcesCursors)
...
... (repeat for every folder of my resources folder)
set(CPACK_COMPONENTS_ALL applications ResourcesCalibration ResourcesCursors ...)
set(CPACK_COMPONENT_RESOURCESCALIBRATION_GROUP "resources")
set(CPACK_COMPONENT_RESOURCESCURSORS_GROUP "resources")
...
...
Is there a clean way to copy / install the entire resources folder including all subfolders?
Command flow install(DIRECTORY) exists specifically for installing directory with its subdirectories and files.
install(DIRECTORY ${CMAKE_SOURCE_DIR}/resources/
DESTINATION resources
COMPONENT ResourcesCursors)
or even
install(DIRECTORY ${CMAKE_SOURCE_DIR}/resources
DESTINATION .
COMPONENT ResourcesCursors)
will copy resource directory in the source tree to the installation directory. See documentation on install for more info.
Is this variable always set to the directory containing the CMakeLists.txt file that I run cmake on?
For example, if I wish to run cmake on a CMakeLists.txt file that exists in the directory above my current directory, I would go: cmake ...
In this case, what is my CMAKE_SOURCE_DIR set to?
Assuming that you have 2 folders src and build where src contains your projects and build is the empty folder that you just created so you can deploy your out-of-tree build in it: CMAKE_SOURCE_DIR is the path to src where CMAKE_BINARY_DIR points to build.
Note that if you are doing an in-tree build, the 2 cache entries get the same value.
link: CMake Useful Variables .
EDIT
for further clarifications
<some location>/src/CMakeLists.txt ( so *src* is the root of your project )
<some location>/build
if you do something like
cd <some location>/build
cmake <some location>/src
you are making an out-of-tree build where CMAKE_SOURCE_DIR and CMAKE_BINARY_DIR get different values
I've created a list of files using:
file(GLOB_RECURSE DEPLOY_FILES "${PROJECT_SOURCE_DIR}/install/*")
I want to install all of these files in /usr/myproject/, but I want to maintain the file tree on the installed folder:
install/junk
install/other/junk2
install/other/junk3
If I use:
install(FILES ${DEPLOY_FILES} DESTINATION "usr/myproject")
All the files end up in /usr/myproject as:
/usr/myproject/junk
/usr/myproject/junk2
/usr/myproject/junk3
How can I make the install command keep track of relative paths?
I've worked around the issue by doing it manually in a for loop:
set(BASE "${PROJECT_SOURCE_DIR}/install")
foreach(ITEM ${DEPLOY_FILES})
get_filename_component(ITEM_PATH ${ITEM} PATH)
string(REPLACE ${BASE} "" ITEM_PATH ${ITEM_PATH})
install(FILES ${ITEM} DESTINATION "usr/myproject${ITEM_PATH}")
endforeach()
...but this is annoying to do. Surely there's an easier way?
(I can't see anything in the install documentation though...)
The simplest way to install everything from a given directory is:
install(DIRECTORY mydir/ DESTINATION dir/newname)
Trailing '/' is significant.
Without it mydir would be installed to newname/mydir.
From the CMake documentation:
The last component of each directory name is appended to the destination
directory but a trailing slash may be used to avoid this because it
leaves the last component empty.
I am assuming you have a list of files, let's say INCLUDE_FILES. Possibly a selection of files spread over a number of subdirectories, e.g. header files from across the source tree, as opposed to everything in a single subdir like in the other answers.
You can loop over the file list and use get_filename_component() to extract the directory part, then use that in a subsequent install() to set the DESTINATION subdirectory:
foreach ( file ${INCLUDE_FILES} )
get_filename_component( dir ${file} DIRECTORY )
install( FILES ${file} DESTINATION include/${dir} )
endforeach()
Done. ;-)
Edit: If all the files you want to install that way match a particular file pattern -- e.g. "all header files" -- then brno's answer has the edge over this one.
Use:
INSTALL( DIRECTORY <directory> DESTINATION usr/myproject )
(See here for details: http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command:install)
INSTALL( DIRECTORY ... ) preserves the directory structure. But, if you use install as <directory>, you would end up with usr/myproject/install/.... which is not what you want.
There are two ways to do this:
Use INSTALL( FILES .... DESTINATION usr/myproject) to install the files that lie directly in install/, then use INSTALL( DIRECTORY .... DESTINATION usr/myproject) and manually list the directories to install.
Use your globbing command in your original post, and then determine which entries are files, which are directories, move the directory entries to a separate list, feed the lists to INSTALL( FILES ...) and INSTALL( DIRECTORY ...), respectively.
file(GLOB DEPLOY_FILES_AND_DIRS "${PROJECT_SOURCE_DIR}/install/*")
foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
IF( IS_DIRECTORY "${ITEM}" )
LIST( APPEND DIRS_TO_DEPLOY "${ITEM}" )
ELSE()
LIST( APPEND FILES_TO_DEPLOY "${ITEM}" )
ENDIF()
endforeach()
INSTALL( FILES ${FILES_TO_DEPLOY} DESTINATION usr/myproject )
INSTALL( DIRECTORY ${DIRS_TO_DEPLOY} DESTINATION usr/myproject )
Note: Depending on the type of files you install, other INSTALL( ...) commands might be more appropriate (for example, INSTALL( TARGETS .... ) to install your libraries/executables.
Since globbing is not recommended, and running loops in CMakeLists.txt files is clunky, a pattern matching option of DIRECTORY did the trick for me.
install(DIRECTORY src/ DESTINATION include FILES_MATCHING PATTERN "*.h")
This took the entire folder structure inside src/ and reproduced it within <INSTALL_DIR>/include, header files only.