1

I have a folder with mixed source (.cpp) and header (.h and .hpp) files. How do I write a regex expression in the CMake install command for installing only header files into a specific destination?

My search for an example on how to use a regex expression in the CMake install command did not succeed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amani
  • 16,245
  • 29
  • 103
  • 153

1 Answers1

6

From install documentation:

The FILES_MATCHING option may be given before the first match option to disable installation of files (but not directories) not matched by any expression.

Usage

cmake_minimum_required(VERSION 2.8)
project(foo)

install(
    DIRECTORY
    "./src"
    DESTINATION
    "include/foo"
    FILES_MATCHING
    PATTERN
    "*.hpp"
)

Example

> cmake -H. -B_builds -DCMAKE_INSTALL_PREFIX=`pwd`/_install
> cmake --build _builds/ --target install
> find src/ -type f
src/a.hpp
src/a.cpp
src/B/b.hpp
src/B/b.cpp

Only *.hpp files installed:

> find _install/ -type f
_install/include/foo/src/a.hpp
_install/include/foo/src/B/b.hpp
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Thanks, it works, but I also wanted to include header files with .h extension. I tried the pattern "*(.hpp|.h)" it never worked. Any idea with the right expression? – Amani Dec 27 '13 at 09:04
  • you can add several `PATTERN "..."` options, and also some `PATTERN "..." EXCLUDE` – Mizux Jan 12 '18 at 08:22