[CMake] What are the best practices for project installation hierarchy?

Egor Tensin egor.tensin at gmail.com
Sat Jun 21 14:14:20 EDT 2014


Hello,

I'm bothered with my approach to `install`ing CMake-enabled project
components. The whole source tree is separated into "libs" and
"examples" subdirectories, with a lot of libraries under the "libs"
subdirectory and library usage examples under the "examples"
subdirectory.

For example:
.
├── examples
│   ├── CMakeLists.txt
│   ├── lib1
│   │   ├── CMakeLists.txt
│   │   ├── example1.cpp
│   │   └── example2.cpp
│   └── lib2
│       ├── CMakeLists.txt
│       └── example1.cpp
└── libs
    ├── CMakeLists.txt
    ├── lib1
    └── lib2

I would like to install the corresponding targets like this:

.
├── bin
│   └── examples
│       ├── lib1
│       │   ├── example1.exe
│       │   └── example2.exe
│       └── lib2
│           └── example1.exe
└── lib
    ├── lib1.lib
    └── lib2.lib

At the moment I'm just defining a custom variable (say,
INSTALL_PREFIX) in the highest-level CMakeLists.txt, and basically in
each of the underlying CMakeLists.txt files append something to the
variable. For example, in libs/CMakeLists.txt I use:

set(INSTALL_PREFIX "${INSTALL_PREFIX}/lib")

and in examples/CMakeLists.txt:

set(INSTALL_PREFIX "${INSTALL_PREFIX}/bin")

and in examples/lib1/CMakeLists.txt:

set(INSTALL_PREFIX "${INSTALL_PREFIX}/lib1")

I'm wondering if it's idiomatic, because at the moment I'm facing a
few issues using this approach. For example, if I build & install only
examples/lib1/example1.cpp, it gets installed to "lib1/" instead of
"bin/examples/lib1".

Could you please share your best practices on installing project
components or give me an advice on how to fix the issue(s) I'm facing?

Thank you,
Egor Tensin.


More information about the CMake mailing list