3

I'm using CMake 2.8.2 version. The project is using lots of external files and custom libraries (unavailable through find_package) and there is a long cascade of elements like the one below:

find_path(XXX_INCLUDE_DIR XXX.h /XXX/include)
if (XXX_INCLUDE_DIR)
  message(STATUS "Includes (XXX) found in ${XXX_INCLUDE_DIR}")
else()
  message(FATAL_ERROR "Includes (XXX) not found")
endif()

There is over 20 things like this in the script - it doesn't look good. According to the documentation, unfortunately, neither find_path nor find_library have a REQUIRED option which would do the job here (just like it does with find_package - if not found, the script stops). Do you have an idea how can I shorten the CMake script code? Something like

find_path(XXX_INCLUDE_DIR XXX.h /XXX/include REQUIED)

or something similar would be great.

ducin
  • 25,621
  • 41
  • 157
  • 256

2 Answers2

6

Put them in your custom FindXXX.cmake modules. Read the docs and look at FindPNG.cmake for an example. Put them into <project>/cmake/FindXXX.cmake (or similar), and then add the directory containing these files to the CMAKE_MODULE_PATH and use find_package(), e.g.

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

find_package(XXX REQUIRED)
Michael Wild
  • 24,977
  • 3
  • 43
  • 43
1

You probably want to use either a macro or a function.

andybauer
  • 835
  • 6
  • 12