Aug 28, 2011

[Cmake] How Find_package works

usually include directory should be like this:
include_directories(${BZIP_INCLUDE_DIRS})


libraries are like this:
set(LIBS ${LIBS} ${LibXML++_LIBRARIES})


the package is found , the variable looks like this:
BZIP2_FOUND


---
the library/executable 's link libraries:
target_link_libraries(exampleProgram ${LIBS})


our own package.cmake files:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
set this before find_package, of course.
also read:
http://vsdmars.blogspot.com/2011/08/cmakekde-cmakeliststxt-for-building-kde.html


find package's components:
find_package(Qt COMPONENTS QtOpenGL QtXml REQUIRED)


we can omit the REQUIRED keyword, if the package is optional for your project. In this case, you can use the __FOUND variable (e.g. Qt_QtXml_FOUND) 


Same:

find_package(Qt COMPONENTS QtOpenGL QtXml REQUIRED)
find_package(Qt REQUIRED COMPONENTS QtOpenGL QtXml)
find_package(Qt REQUIRED QtOpenGL QtXml)




If you only require some components of a package, and want to use others only, if they are available, you can call find_package twice:
find_package(Qt COMPONENTS QtXml REQUIRED)
find_package(Qt COMPONENTS QtOpenGL)
Alternatively, you can invoke find_package once with all components, but without theREQUIRED keyword and then explicitly check the required components:
find_package(Qt COMPONENTS QtOpenGL QtXml)
if ( NOT Qt_FOUND OR NOT QtXml_FOUND )
  message(FATAL_ERROR "Package Qt and component QtXml required, but not found!")
endif( NOT Qt_FOUND OR NOT QtXml_FOUND )
---
Find_Package steps:
First CMake checks all directories in ${CMAKE_MODULE_PATH}, then it looks in its own module directory /share/cmake-x.y/Modules/.
If no such file is found, it looks for Config.cmake or -config.cmake, which are supposed to be installed by libraries (but there are currently not yet many libraries which install them) and that don't do detection, but rather just contain hardcoded values for the installed library.


No matter which mode is used, if the package has been found, a set of variables will be defined:
  • _FOUND
  • _INCLUDE_DIRS or _INCLUDES
  • _LIBRARIES or _LIBRARIES or _LIBS
  • _DEFINITIONS
All this takes place in the Find.cmake file.

Now, in the CMakeLists.txt file in the top level directory of your code (the client code that is actually going to make use of the library , we check for the variable _FOUND to see whether the package has been found or not. For most packages the resulting variables use the name of the package all uppercased, e.g. LIBFOO_FOUND, for some packages the exact case of the package is used, e.g. LibFoo_FOUND. If this variable is found, then, we pass the_INCLUDE_DIRS to the include_directories() command and _LIBRARIES to the target_link_libraries() command of CMake.

pkg-config is used.







No comments:

Post a Comment

Note: Only a member of this blog may post a comment.