]> Creatis software - bbtk.git/blob - kernel/cmake/CMakeCreateFindPackage.cmake
*** empty log message ***
[bbtk.git] / kernel / cmake / CMakeCreateFindPackage.cmake
1 #-----------------------------------------------------------------------------
2 # Macro CREATE_FIND_PACKAGE
3 # Creates and install the cmake files which allow 
4 # to find a library by the cmake command FIND_PACKAGE.
5 #
6 # Inputs :
7 # --------
8 #  LIBRARY_NAME : name of the library to find 
9 #
10 # The following variables **MUST** have been set previously :
11 #
12 # * ${LIBRARY_NAME}_BUILD_TREE_RELATIVE_INCLUDE_PATHS
13 #    The list of include paths 
14 #    when someone uses a *BUILD TREE* version of ${LIBRARY_NAME}.
15 #    NB : 
16 #     THE PATHS *MUST BE RELATIVE* TO THE ROOT DIR OF THE PROJECT **SOURCES** !
17 #    Assume your project architecture is :
18 #     install/ : directory in which CREATE_FIND_PACKAGE is invoked
19 #     src/part1/include/ : first include dir
20 #     src/part2/include/ : second include dir
21 #    Then you should set the var with :
22 #    SET(${LIBRARY_NAME}_BUILD_TREE_RELATIVE_INCLUDE_PATHS
23 #     src/part1/include
24 #     src/part2/include )
25 #    Which will result in actual include paths :
26 #     ${PROJECT_SOURCE_DIR}/src/part1/include;
27 #     ${PROJECT_SOURCE_DIR}/src/part2/include
28 # * ${LIBRARY_NAME}_BUILD_TREE_RELATIVE_LIBRARY_PATHS
29 #    Like the previous var but for the library paths.
30 #    NB : 
31 #     THE PATHS *MUST BE RELATIVE* TO THE ROOT DIR OF THE **BUILD TREE** 
32 #     THAT IS POINT TO THE FOLDERS WHERE THE LIBS WILL BE BUILD
33 #    Assume that your project architecture is :
34 #     src/part1/src/ : first source dir, in which the lib 'part1' is built
35 #     src/part2/src/ : first source dir, in which the lib 'part2' is built
36 #    Then you should set the var with 
37 #    SET(${LIBRARY_NAME}_BUILD_TREE_RELATIVE_LIBRARY_PATHS
38 #     src/part1/src
39 #     src/part2/src
40 #     )
41 #    Which will result in actual library paths :
42 #     ${PROJECT_BINARY_DIR}/src/part1/src
43 #     ${PROJECT_BINARY_DIR}/src/part2/src
44 # * ${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_INCLUDE_PATHS
45 #    The list of include paths 
46 #    when someone uses an *INSTALLED* version of ${LIBRARY_NAME} 
47 #    The paths *MUST BE RELATIVE* to INSTALL_PREFIX
48 #  #    A typical example is "include/${LIBRARY_NAME}"
49 # * ${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS
50 #    Like the previous var but for library paths.
51 #    A typical example is ".."
52 # * ${LIBRARY_NAME}_LIBRARIES
53 #    The list of libraries to link against when using ${LIBRARY_NAME}
54 #
55 # The following variables can be set optionally :
56 #
57 # * ${LIBRARY_NAME}_REQUIRED_C_FLAGS
58 # * ${LIBRARY_NAME}_REQUIRED_CXX_FLAGS
59 # * ${LIBRARY_NAME}_REQUIRED_LINK_FLAGS
60 # * ${LIBRARY_NAME}_MAJOR_VERSION
61 # * ${LIBRARY_NAME}_MINOR_VERSION
62 # * ${LIBRARY_NAME}_BUILD_VERSION
63 # * ${LIBRARY_NAME}_INSTALL_FOLDER : if set then install the stuff 
64 #   in INSTALL_PREFIX/lib/${LIBRARY_NAME}_INSTALL_FOLDER 
65 #   instead of INSTALL_PREFIX/lib/${LIBRARY_NAME}
66 #
67
68 # To provide a user defined 
69 # couple of Config/Use file (in addition to the standard one) use :
70 #  SET( ${LIBRARY_NAME}_HAS_ADDITIONAL_CONFIG_FILE  TRUE )
71 # and store the *ABSOLUTE* paths to the additional files in the vars :
72 #  ${LIBRARY_NAME}_ADDITIONAL_CONFIG_FILE
73 #  ${LIBRARY_NAME}_ADDITIONAL_USE_FILE
74 # (e.g. ${CMAKE_CURRENT_SOURCE_DIR}/MyConfig.cmake)
75 #
76 # Outputs :
77 # --------
78 # At cmake run-time, the macro creates the following files 
79 # in the current dir of the build tree (where the macro is invoked) :
80 #  Use${LIBRARY_NAME}.cmake
81 #  ${LIBRARY_NAME}Config.cmake
82 #  ${LIBRARY_NAME}BuildSettings.cmake
83 # And if the vars ${LIBRARY_NAME}_ADDITIONAL_CONFIG_FILE and
84 # ${LIBRARY_NAME}_ADDITIONAL_USE_FILE are set, it also creates :
85 #  Additional${LIBRARY_NAME}Config.cmake
86 #  AdditionalUse${LIBRARY_NAME}.cmake
87
88 # At install-time, the same files are installed 
89 # in INSTALL_PREFIX/lib/${LIBRARY_NAME}
90 # and the file :
91 #  Find${LIBRARY_NAME}.cmake
92 # is installed in ${CMAKE_ROOT}/Modules/
93 #
94 #
95 #-----------------------------------------------------------------------------
96
97 MACRO(CMAKE_CREATE_FIND_PACKAGE LIBRARY_NAME)
98
99   #---------------------------------------------------------------------------
100   # Sets the common values to build tree and install tree configs
101   SET(CFP_LIB_REQUIRED_C_FLAGS    ${${LIBRARY_NAME}_REQUIRED_C_FLAGS})
102   SET(CFP_LIB_REQUIRED_CXX_FLAGS  ${${LIBRARY_NAME}_REQUIRED_CXX_FLAGS})
103   SET(CFP_LIB_REQUIRED_LINK_FLAGS ${${LIBRARY_NAME}_REQUIRED_LINK_FLAGS})
104   SET(CFP_LIB_MAJOR_VERSION       ${${LIBRARY_NAME}_MAJOR_VERSION})
105   SET(CFP_LIB_MINOR_VERSION       ${${LIBRARY_NAME}_MINOR_VERSION})
106   SET(CFP_LIB_BUILD_VERSION       ${${LIBRARY_NAME}_BUILD_VERSION})
107   SET(CFP_LIB_VERSION             
108     ${CFP_LIB_MAJOR_VERSION}.${CFP_LIB_MINOR_VERSION}.${CFP_LIB_BUILD_VERSION})
109   SET(CFP_LIB_LIBRARIES ${${LIBRARY_NAME}_LIBRARIES})
110   IF (${LIBRARY_NAME}_HAS_ADDITIONAL_CONFIG_FILE)
111     SET(CFP_LIB_HAS_ADDITIONAL_CONFIG_FILE TRUE)
112   ELSE (${LIBRARY_NAME}_HAS_ADDITIONAL_CONFIG_FILE)
113     SET(CFP_LIB_HAS_ADDITIONAL_CONFIG_FILE FALSE)
114   ENDIF (${LIBRARY_NAME}_HAS_ADDITIONAL_CONFIG_FILE)
115   #---------------------------------------------------------------------------
116
117
118   #---------------------------------------------------------------------------
119   #-- BUILD TREE configuration 
120   SET(CFP_BUILD_TREE_CONFIGURATION TRUE)
121   # UseLIBRARY_NAME.cmake 
122   CONFIGURE_FILE(
123     ${CMakeCreateFindPackage_DIR}/CMakeCreateFindPackage_UseLibrary.cmake.in
124     ${PROJECT_BINARY_DIR}/Use${LIBRARY_NAME}.cmake
125     @ONLY IMMEDIATE
126     )
127   # LIBRARY_NAMEConfig.cmake 
128   SET(CFP_LIB_RELATIVE_INCLUDE_PATHS 
129     ${${LIBRARY_NAME}_BUILD_TREE_RELATIVE_INCLUDE_PATHS})
130   SET(CFP_LIB_RELATIVE_LIBRARY_PATHS 
131     ${${LIBRARY_NAME}_BUILD_TREE_RELATIVE_LIBRARY_PATHS})
132   CONFIGURE_FILE(
133     ${CMakeCreateFindPackage_DIR}/CMakeCreateFindPackage_LibraryConfig.cmake.in
134     ${PROJECT_BINARY_DIR}/${LIBRARY_NAME}Config.cmake
135     @ONLY IMMEDIATE
136     )
137   # LIBRARY_NAMEBuildSettings.cmake :
138   # Save the compiler settings so another project can import them.
139   INCLUDE(${CMAKE_ROOT}/Modules/CMakeExportBuildSettings.cmake)
140   CMAKE_EXPORT_BUILD_SETTINGS(${PROJECT_BINARY_DIR}/${LIBRARY_NAME}BuildSettings.cmake)
141   # Additional Config and Use files
142   IF(CFP_LIB_HAS_ADDITIONAL_CONFIG_FILE)
143     CONFIGURE_FILE(
144       ${${LIBRARY_NAME}_ADDITIONAL_CONFIG_FILE}
145       ${PROJECT_BINARY_DIR}/Additional${LIBRARY_NAME}Config.cmake
146       @ONLY IMMEDIATE
147       )
148     CONFIGURE_FILE(
149       ${${LIBRARY_NAME}_ADDITIONAL_USE_FILE}
150       ${PROJECT_BINARY_DIR}/AdditionalUse${LIBRARY_NAME}.cmake
151       @ONLY IMMEDIATE
152       )
153   ENDIF(CFP_LIB_HAS_ADDITIONAL_CONFIG_FILE)
154   #---------------------------------------------------------------------------
155   
156
157   #---------------------------------------------------------------------------
158   #-- INSTALL TREE configuration 
159   SET(CFP_BUILD_TREE_CONFIGURATION FALSE)
160   # Create work directory to put the configured files because 
161   # if the user invoked the macro at the root of the build tree the files 
162   # would overwrite those configured for the build tree
163   SET(CFP_WORK_DIR ${CMAKE_CURRENT_BINARY_DIR}/CMakeCreateFindPackage_stuff)
164   IF(NOT IS_DIRECTORY ${CFP_WORK_DIR})
165     FILE(MAKE_DIRECTORY ${CFP_WORK_DIR})
166   ENDIF(NOT IS_DIRECTORY ${CFP_WORK_DIR})
167   
168   #----------------------------------------------------------------------------
169   #INSTALLATION PATH
170   IF(WIN32)
171     SET(INSTALL_PATH .)
172   ELSE(WIN32)
173     IF(${LIBRARY_NAME}_INSTALL_FOLDER)
174       SET(INSTALL_PATH lib/${${LIBRARY_NAME}_INSTALL_FOLDER})
175     ELSE(${LIBRARY_NAME}_INSTALL_FOLDER)
176       SET(INSTALL_PATH lib/${LIBRARY_NAME})
177     ENDIF(${LIBRARY_NAME}_INSTALL_FOLDER)
178   ENDIF(WIN32)
179
180
181   # UseLIBRARY_NAME.cmake 
182   CONFIGURE_FILE(
183     ${CMakeCreateFindPackage_DIR}/CMakeCreateFindPackage_UseLibrary.cmake.in
184     ${CFP_WORK_DIR}/Use${LIBRARY_NAME}.cmake
185     @ONLY IMMEDIATE
186     )
187   INSTALL(
188     FILES 
189     ${CFP_WORK_DIR}/Use${LIBRARY_NAME}.cmake
190     DESTINATION ${INSTALL_PATH}
191     )
192   # LIBRARY_NAMEConfig.cmake 
193   SET(CFP_LIB_RELATIVE_INCLUDE_PATHS 
194     ${${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_INCLUDE_PATHS})
195   SET(CFP_LIB_RELATIVE_LIBRARY_PATHS 
196     ${${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS})
197   CONFIGURE_FILE(
198     ${CMakeCreateFindPackage_DIR}/CMakeCreateFindPackage_LibraryConfig.cmake.in
199     ${CFP_WORK_DIR}/${LIBRARY_NAME}Config.cmake
200     @ONLY IMMEDIATE
201     )
202   INSTALL(
203     FILES 
204     ${CFP_WORK_DIR}/${LIBRARY_NAME}Config.cmake
205     DESTINATION ${INSTALL_PATH}
206     )
207   # LIBRARY_NAMEBuildSettings.cmake :
208   CMAKE_EXPORT_BUILD_SETTINGS(${CFP_WORK_DIR}/${LIBRARY_NAME}BuildSettings.cmake)
209   INSTALL(
210     FILES 
211     ${CFP_WORK_DIR}/${LIBRARY_NAME}BuildSettings.cmake
212     DESTINATION  ${INSTALL_PATH}
213     )
214   # Additional Config and Use files
215   IF(CFP_LIB_HAS_ADDITIONAL_CONFIG_FILE)
216     CONFIGURE_FILE(
217       ${${LIBRARY_NAME}_ADDITIONAL_CONFIG_FILE}
218       ${CFP_WORK_DIR}/Additional${LIBRARY_NAME}Config.cmake
219       @ONLY IMMEDIATE
220       )
221     INSTALL(
222       FILES 
223       ${CFP_WORK_DIR}/Additional${LIBRARY_NAME}Config.cmake
224       DESTINATION ${INSTALL_PATH}
225       )
226     CONFIGURE_FILE(
227       ${${LIBRARY_NAME}_ADDITIONAL_USE_FILE}
228       ${CFP_WORK_DIR}/AdditionalUse${LIBRARY_NAME}.cmake
229       @ONLY IMMEDIATE
230       )
231     INSTALL(
232       FILES 
233       ${CFP_WORK_DIR}/AdditionalUse${LIBRARY_NAME}.cmake
234       DESTINATION  ${INSTALL_PATH}
235       )
236   ENDIF(CFP_LIB_HAS_ADDITIONAL_CONFIG_FILE)
237   # Specific to install tree
238   # FindLIBRARY_NAME.cmake in CMake/Modules dir
239   CONFIGURE_FILE(
240     ${CMakeCreateFindPackage_DIR}/CMakeCreateFindPackage_FindLibrary.cmake.in
241     ${CFP_WORK_DIR}/Find${LIBRARY_NAME}.cmake
242     @ONLY IMMEDIATE
243     )
244   INSTALL( 
245     FILES ${CFP_WORK_DIR}/Find${LIBRARY_NAME}.cmake
246     DESTINATION ${CMAKE_ROOT}/Modules 
247     )
248   #---------------------------------------------------------------------------
249   
250
251
252
253 ENDMACRO(CMAKE_CREATE_FIND_PACKAGE)
254 #-----------------------------------------------------------------------------