Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install your custom CMake-Find module

Tags:

cmake

cpack

I configure and package my library using CMake and CPack. I have written my own find-module: FindMyLib.cmake.

How do I tell CMake/CPack to add this file to the CMake module directory, so that future developers can simply specify FIND_PACKAGE(MyLib) to use my library?

like image 265
goocreations Avatar asked May 26 '12 11:05

goocreations


People also ask

Where do I put CMake modules?

Using Modules For CMake, these sections are called cmake-modules and can be found in the Modules subdirectory of your installation.

How does CMake find package?

CMake searches for a file called Find<package>. cmake in the CMAKE_MODULE_PATH followed by the CMake installation. If the file is found, it is read and processed by CMake. It is responsible for finding the package, checking the version, and producing any needed messages.

What is CMake module path?

Semicolon-separated list of directories specifying a search path for CMake modules to be loaded by the include() or find_package() commands before checking the default modules that come with CMake. By default it is empty, it is intended to be set by the project.


2 Answers

You can set CMAKE_MODULE_PATH and distribute your custom FindFoo.cmake with your project. For example:

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") 
like image 133
simont Avatar answered Sep 19 '22 11:09

simont


The CMake module directory is part of the install tree of CMake itself, and as such you shouldn't be trying to add anything there.

The CMake module directory contains modules which have been written or at least reviewed by Kitware, and adding your own there would give the impression to users of your project that this was the case for your project also.

You'd be better to just install FindMyLib.cmake to one of the places searched by find_package:

<prefix>/                                               (Windows) <prefix>/(cmake|CMake)/                                 (Windows) <prefix>/<name>*/                                       (Windows) <prefix>/<name>*/(cmake|CMake)/                         (Windows) <prefix>/(lib/<arch>|lib|share)/cmake/<name>*/          (Unix) <prefix>/(lib/<arch>|lib|share)/<name>*/                (Unix) <prefix>/(lib/<arch>|lib|share)/<name>*/(cmake|CMake)/  (Unix) <prefix>/<name>.framework/Resources/                    (Apple) <prefix>/<name>.framework/Resources/CMake/              (Apple) <prefix>/<name>.framework/Versions/*/Resources/         (Apple) <prefix>/<name>.framework/Versions/*/Resources/CMake/   (Apple) <prefix>/<name>.app/Contents/Resources/                 (Apple) <prefix>/<name>.app/Contents/Resources/CMake/           (Apple) 


See the documentation for find_package for the full details of how find_package searches. Also the CMake packaging tutorial is useful in this case.

like image 24
Fraser Avatar answered Sep 21 '22 11:09

Fraser