69

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?

Kevin
  • 16,549
  • 8
  • 60
  • 74
goocreations
  • 2,938
  • 8
  • 37
  • 59

3 Answers3

73

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/")
simont
  • 68,704
  • 18
  • 117
  • 136
  • 7
    This is also the approach suggested here: http://www.cmake.org/Wiki/CMake:How_To_Find_Libraries#Using_external_libraries_that_CMake_doesn.27t_yet_have_modules_for – Vincenzo Pii Apr 11 '14 at 08:46
  • 16
    That's not an answer to the question. The idea is not how users of *MyLib* could include his `FindMyLib.cmake`, but how to install it so end users could make use of it as is. What you are describing requires users of *MyLib* to copy the .cmake file under their own development directory tree... – Alexis Wilke Jun 10 '15 at 05:22
  • 1
    Is there some special reason for the capital M in Modules? – einpoklum Dec 25 '16 at 22:29
  • `list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}}/cmake/Modules/")` is even easier and more clear IMHO for CMake version 3 users. – Ryan Friedman Jun 16 '23 at 06:02
50

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.

Lucas Walter
  • 942
  • 3
  • 10
  • 23
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • 3
    You should mark this as the right answer @goocreations. Also, this is too nifty of a solution. – jackyalcine May 27 '13 at 06:30
  • 3
    This did not work for me (using cmake 3.3.1). According to the docs, " CMake searches for a file called Find.cmake in the CMAKE_MODULE_PATH followed by the CMake installation". However, I can't seem to find what that is. It searches the paths you listed above for Package Configurations files, not CMake-Find modules. – Joey Dumont Sep 08 '15 at 12:21
  • 1
    How is the value of `` determined by CMake? – nn0p Sep 07 '16 at 14:31
  • 2
    This is set by [`CMAKE_SYSTEM_PREFIX_PATH`](https://cmake.org/cmake/help/v3.6/variable/CMAKE_SYSTEM_PREFIX_PATH.html) or [`CMAKE_PREFIX_PATH`](https://cmake.org/cmake/help/v3.6/variable/CMAKE_PREFIX_PATH.html) – Fraser Sep 12 '16 at 19:53
  • I think these paths only work for the `Config.cmake` files, not for `find.cmake` – veio May 28 '20 at 11:53
16

The best way to allow

future developers can simply specify FIND_PACKAGE(MyLib) to use my library

is to write a package config file (-config.cmake) , not a Find module. The package config file should then be installed in one of the folders where the FindPackage module looks for (something like /lib/package/ or /lib/cmake/package the second being preferred)

The FindPackage module will automatically load the config file if it can find it there.

The CMake wiki has more detailed instructions at https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/Packaging

Lucas Walter
  • 942
  • 3
  • 10
  • 23
Triskeldeian
  • 590
  • 3
  • 18