4

I use CMake install(DIRECTORIES...) form to copy headers on install:

install(DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}
  DESTINATION include
  FILES_MATCHING PATTERN "*.h")

However, this command does create empty directories (those where no headers are found). Thus, I want to find and delete those empty directories during the install process:

install(CODE "execute_process(
  COMMAND find -type d -empty -exec rmdir '{}' ';'
  WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}
  ERROR_FILE ${CMAKE_CURRENT_BINARY_DIR}/prune_empty_dirs.err)")

With the command above, the file prune_empty_dirs.err contains:

find: missing argument to `-exec'

I tried to escape the braces but it yields the same behavior. What am I doing wrong? Thanks,

piwi
  • 5,136
  • 2
  • 24
  • 48
  • You have a typo in WORKING_DIREC**OTIR** – ixSci Feb 22 '13 at 09:44
  • Thanks @ixSci I fixed it; however the typo is in the question not in my script! – piwi Feb 22 '13 at 09:48
  • 1
    did you try this command without '' or with double ''? `-exec rmdir {} ;` or with `-exec rmdir ''{}'' '';''` or `-exec \"rmdir {} ;\"` – ixSci Feb 22 '13 at 09:51
  • @ixSci I tried the possible ways of quoting you suggested, but they all give the same failure :-/ However, It did get to do what I seek by executing `bash` with the `find` command as argument to `-c`; thanks for your help – piwi Feb 22 '13 at 09:59
  • I'd suggest you to not create folders in a first place instead of removing them later. You can do it with FILE(GLOB) - get all files you need, then test output if it contains anything, and if it doesn't then just do not execute INSTALL(DIRECTORIES) – ixSci Feb 22 '13 at 10:02
  • I know, but I'm not confident with the `file(GLOB)` command; the documentation states: `We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.` – piwi Feb 22 '13 at 10:08

1 Answers1

2

I found the solution. It is not the "{}" rather than the delemiting ";". The following syntax worked for me (CMake 2.8.9):

--exec rm {} "\;"
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
user1995197
  • 91
  • 1
  • 5
  • Came across this in the review queue, looks dangerous, but I need to learn more about cmake. Can you explain a little further? – Russia Must Remove Putin Oct 15 '14 at 16:57
  • It seems that the solution is to escape the `;` but not surrounding it with quotes when invoking the `find` command within the `install()` code (otherwise the string is truncated). – piwi Oct 16 '14 at 07:45