]> Creatis software - crea.git/blob - cmake/CREAMacro_InstallLibraryForCMake.cmake
3fd0288b42e8448740bc276833255a32dd8e5f3b
[crea.git] / cmake / CREAMacro_InstallLibraryForCMake.cmake
1 #-----------------------------------------------------------------------------
2 # Macro CREA_ADVANCED_INSTALL_LIBRARY_FOR_CMAKE
3 # Creates and install the cmake files which allow 
4 # to find the 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 the macro 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 CMAKE_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 "lib"
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 generated files 
64 #   in CMAKE_INSTALL_PREFIX/lib/${LIBRARY_NAME}_INSTALL_FOLDER 
65 #   instead of CMAKE_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 CMAKE_INSTALL_PREFIX/lib/${LIBRARY_NAME}_INSTALL_FOLDER 
90 # and the file :
91 #  Find${LIBRARY_NAME}.cmake
92 # is installed in ${CMAKE_ROOT}/Modules/
93 #
94 #
95 #-----------------------------------------------------------------------------
96
97 MACRO(CREA_ADVANCED_INSTALL_LIBRARY_FOR_CMAKE LIBRARY_NAME)
98
99   #---------------------------------------------------------------------------
100   # Sets the common values to build tree and install tree configs
101   SET(CILC_LIB_REQUIRED_C_FLAGS    ${${LIBRARY_NAME}_REQUIRED_C_FLAGS})
102   SET(CILC_LIB_REQUIRED_CXX_FLAGS  ${${LIBRARY_NAME}_REQUIRED_CXX_FLAGS})
103   SET(CILC_LIB_REQUIRED_LINK_FLAGS ${${LIBRARY_NAME}_REQUIRED_LINK_FLAGS})
104   SET(CILC_LIB_MAJOR_VERSION       ${${LIBRARY_NAME}_MAJOR_VERSION})
105   SET(CILC_LIB_MINOR_VERSION       ${${LIBRARY_NAME}_MINOR_VERSION})
106   SET(CILC_LIB_BUILD_VERSION       ${${LIBRARY_NAME}_BUILD_VERSION})
107   SET(CILC_LIB_VERSION             
108     ${CILC_LIB_MAJOR_VERSION}.${CILC_LIB_MINOR_VERSION}.${CILC_LIB_BUILD_VERSION})
109   SET(CILC_LIB_LIBRARIES ${${LIBRARY_NAME}_LIBRARIES})
110   IF (${LIBRARY_NAME}_HAS_ADDITIONAL_CONFIG_FILE)
111     SET(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE TRUE)
112   ELSE (${LIBRARY_NAME}_HAS_ADDITIONAL_CONFIG_FILE)
113     SET(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE FALSE)
114   ENDIF (${LIBRARY_NAME}_HAS_ADDITIONAL_CONFIG_FILE)
115   #---------------------------------------------------------------------------
116
117
118   #---------------------------------------------------------------------------
119   #-- BUILD TREE configuration 
120   SET(CILC_BUILD_TREE_CONFIGURATION TRUE)
121   # UseLIBRARY_NAME.cmake 
122   CONFIGURE_FILE(
123     ${CREA_CMAKE_DIR}/InstallLibraryForCMake_UseLibrary.cmake.in
124     ${PROJECT_BINARY_DIR}/Use${LIBRARY_NAME}.cmake
125     @ONLY IMMEDIATE
126     )
127   # LIBRARY_NAMEConfig.cmake 
128   SET(CILC_LIB_RELATIVE_INCLUDE_PATHS 
129     ${${LIBRARY_NAME}_BUILD_TREE_RELATIVE_INCLUDE_PATHS})
130   SET(CILC_LIB_RELATIVE_LIBRARY_PATHS 
131     ${${LIBRARY_NAME}_BUILD_TREE_RELATIVE_LIBRARY_PATHS})
132   CONFIGURE_FILE(
133     ${CREA_CMAKE_DIR}/InstallLibraryForCMake_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   # FCY : DEPREDCATED FUNCTION FOR CMAKE 2.8
140   IF( "${CMAKE_MINIMUM_REQUIRED_VERSION}" VERSION_LESS 2.7)
141   INCLUDE(${CMAKE_ROOT}/Modules/CMakeExportBuildSettings.cmake)
142   CMAKE_EXPORT_BUILD_SETTINGS(${PROJECT_BINARY_DIR}/${LIBRARY_NAME}BuildSettings.cmake)
143   ENDIF()
144   # Additional Config and Use files
145   IF(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE)
146     CONFIGURE_FILE(
147       ${${LIBRARY_NAME}_ADDITIONAL_CONFIG_FILE}
148       ${PROJECT_BINARY_DIR}/Additional${LIBRARY_NAME}Config.cmake
149       @ONLY IMMEDIATE
150       )
151     CONFIGURE_FILE(
152       ${${LIBRARY_NAME}_ADDITIONAL_USE_FILE}
153       ${PROJECT_BINARY_DIR}/AdditionalUse${LIBRARY_NAME}.cmake
154       @ONLY IMMEDIATE
155       )
156   ENDIF(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE)
157   #---------------------------------------------------------------------------
158   
159
160   #---------------------------------------------------------------------------
161   #-- INSTALL TREE configuration 
162
163
164   SET(CILC_BUILD_TREE_CONFIGURATION FALSE)
165   # Create work directory to put the configured files because 
166   # if the user invoked the macro at the root of the build tree the files 
167   # would overwrite those configured for the build tree
168   SET(CILC_WORK_DIR ${CMAKE_CURRENT_BINARY_DIR}/InstallLibraryForCMake_tmp)
169   IF(NOT IS_DIRECTORY ${CILC_WORK_DIR})
170     FILE(MAKE_DIRECTORY ${CILC_WORK_DIR})
171   ENDIF(NOT IS_DIRECTORY ${CILC_WORK_DIR})
172   
173   #----------------------------------------------------------------------------
174   #INSTALLATION PATH
175   IF(WIN32)
176     SET(INSTALL_PATH .)
177   ELSE(WIN32)
178     IF(${LIBRARY_NAME}_INSTALL_FOLDER)
179 ##EED      SET(INSTALL_PATH lib/${${LIBRARY_NAME}_INSTALL_FOLDER})
180       SET(INSTALL_PATH ${${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS}/${${LIBRARY_NAME}_INSTALL_FOLDER})
181     ELSE(${LIBRARY_NAME}_INSTALL_FOLDER)
182 ##EED      SET(INSTALL_PATH lib/${LIBRARY_NAME})
183            SET(INSTALL_PATH ${${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS}/${LIBRARY_NAME})
184     ENDIF(${LIBRARY_NAME}_INSTALL_FOLDER)
185   ENDIF(WIN32)
186
187   message(STATUS "Library Install Path ${INSTALL_PATH}")
188
189   # UseLIBRARY_NAME.cmake 
190   CONFIGURE_FILE(
191     ${CREA_CMAKE_DIR}/InstallLibraryForCMake_UseLibrary.cmake.in
192     ${CILC_WORK_DIR}/Use${LIBRARY_NAME}.cmake
193     @ONLY IMMEDIATE
194     )
195   INSTALL(
196     FILES 
197     ${CILC_WORK_DIR}/Use${LIBRARY_NAME}.cmake
198     DESTINATION ${INSTALL_PATH}
199     )
200   # LIBRARY_NAMEConfig.cmake 
201   SET(CILC_LIB_RELATIVE_INCLUDE_PATHS 
202     ${${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_INCLUDE_PATHS})
203   SET(CILC_LIB_RELATIVE_LIBRARY_PATHS 
204     ${${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS})
205   CONFIGURE_FILE(
206     ${CREA_CMAKE_DIR}/InstallLibraryForCMake_LibraryConfig.cmake.in
207     ${CILC_WORK_DIR}/${LIBRARY_NAME}Config.cmake
208     @ONLY IMMEDIATE
209     )
210   INSTALL(
211     FILES 
212     ${CILC_WORK_DIR}/${LIBRARY_NAME}Config.cmake
213     DESTINATION ${INSTALL_PATH}
214     )
215   # LIBRARY_NAMEBuildSettings.cmake :
216   # FCY : DEPREDCATED FUNCTION FOR CMAKE 2.8
217   IF( "${CMAKE_MINIMUM_REQUIRED_VERSION}" VERSION_LESS 2.7)
218   CMAKE_EXPORT_BUILD_SETTINGS(${CILC_WORK_DIR}/${LIBRARY_NAME}BuildSettings.cmake)
219   ENDIF()
220   INSTALL(
221     FILES 
222     ${CILC_WORK_DIR}/${LIBRARY_NAME}BuildSettings.cmake
223     DESTINATION  ${INSTALL_PATH}
224     )
225   # Additional Config and Use files
226   IF(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE)
227     CONFIGURE_FILE(
228       ${${LIBRARY_NAME}_ADDITIONAL_CONFIG_FILE}
229       ${CILC_WORK_DIR}/Additional${LIBRARY_NAME}Config.cmake
230       @ONLY IMMEDIATE
231       )
232     INSTALL(
233       FILES 
234       ${CILC_WORK_DIR}/Additional${LIBRARY_NAME}Config.cmake
235       DESTINATION ${INSTALL_PATH}
236       )
237     CONFIGURE_FILE(
238       ${${LIBRARY_NAME}_ADDITIONAL_USE_FILE}
239       ${CILC_WORK_DIR}/AdditionalUse${LIBRARY_NAME}.cmake
240       @ONLY IMMEDIATE
241       )
242     INSTALL(
243       FILES 
244       ${CILC_WORK_DIR}/AdditionalUse${LIBRARY_NAME}.cmake
245       DESTINATION  ${INSTALL_PATH}
246       )
247   ENDIF(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE)
248   # Specific to install tree
249   # FindLIBRARY_NAME.cmake in CMake/Modules dir
250   CONFIGURE_FILE(
251     ${CREA_CMAKE_DIR}/InstallLibraryForCMake_FindLibrary.cmake.in
252     ${CILC_WORK_DIR}/Find${LIBRARY_NAME}.cmake
253     @ONLY IMMEDIATE
254     )
255 #EED 02 mars 2011
256 #  INSTALL( 
257 #    FILES ${CILC_WORK_DIR}/Find${LIBRARY_NAME}.cmake
258 #    DESTINATION ${CMAKE_ROOT}/Modules 
259 #    )
260
261 # JPR 04 Mars 2011
262   INSTALL( 
263     FILES ${CILC_WORK_DIR}/Find${LIBRARY_NAME}.cmake
264     DESTINATION ${CMAKE_INSTALL_PREFIX}/share/cmake/Modules
265     )
266     
267  
268   #---------------------------------------------------------------------------
269   
270
271
272
273 ENDMACRO(CREA_ADVANCED_INSTALL_LIBRARY_FOR_CMAKE)
274 #-----------------------------------------------------------------------------
275
276
277
278
279 #-----------------------------------------------------------------------------
280 MACRO(CREA_INSTALL_LIBRARY_FOR_CMAKE LIBRARY_NAME1)
281   
282   # Sets the settings to default values
283   IF(NOT ${LIBRARY_NAME1}_INSTALL_FOLDER)
284     SET(${LIBRARY_NAME1}_INSTALL_FOLDER ${LIBRARY_NAME1})
285   ENDIF(NOT ${LIBRARY_NAME1}_INSTALL_FOLDER)
286
287   SET(${LIBRARY_NAME1}_LIBRARIES ${LIBRARY_NAME1})
288   
289   FILE(RELATIVE_PATH 
290     ${LIBRARY_NAME1}_BUILD_TREE_RELATIVE_INCLUDE_PATHS 
291     ${PROJECT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
292     )
293   IF ( ${PROJECT_BINARY_DIR} STREQUAL ${EXECUTABLE_OUTPUT_PATH} )
294     SET(CILFC_EXECUTABLE_OUTPUT_REL_PATH ".")
295   ELSE ( ${PROJECT_BINARY_DIR} STREQUAL ${EXECUTABLE_OUTPUT_PATH} )
296
297     FILE(RELATIVE_PATH 
298       CILFC_EXECUTABLE_OUTPUT_REL_PATH
299       ${PROJECT_BINARY_DIR} ${EXECUTABLE_OUTPUT_PATH})
300   ENDIF ( ${PROJECT_BINARY_DIR} STREQUAL ${EXECUTABLE_OUTPUT_PATH} )
301
302
303   IF(UNIX)
304     SET(${LIBRARY_NAME1}_BUILD_TREE_RELATIVE_LIBRARY_PATHS 
305       ${CILFC_EXECUTABLE_OUTPUT_REL_PATH})
306     SET(${LIBRARY_NAME1}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS ${CMAKE_CREA_LIB_PATH} )
307   ELSE(UNIX)
308     SET(${LIBRARY_NAME1}_BUILD_TREE_RELATIVE_LIBRARY_PATHS 
309       ${CILFC_EXECUTABLE_OUTPUT_REL_PATH} )
310 # LG 15/01/09 : Bug
311 #      ${CILFC_EXECUTABLE_OUTPUT_REL_PATH}/Debug
312 #     ${CILFC_EXECUTABLE_OUTPUT_REL_PATH}/Release)
313     SET(${LIBRARY_NAME1}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS bin)
314   ENDIF(UNIX)
315
316   IF(NOT ${LIBRARY_NAME1}_INSTALL_TREE_RELATIVE_INCLUDE_PATHS)
317     SET(${LIBRARY_NAME1}_INSTALL_TREE_RELATIVE_INCLUDE_PATHS include/${LIBRARY_NAME1})
318   ENDIF(NOT ${LIBRARY_NAME1}_INSTALL_TREE_RELATIVE_INCLUDE_PATHS)
319   
320   # Invoke the advanced macro
321   CREA_ADVANCED_INSTALL_LIBRARY_FOR_CMAKE(${LIBRARY_NAME1})
322
323
324 ENDMACRO(CREA_INSTALL_LIBRARY_FOR_CMAKE)
325 #-----------------------------------------------------------------------------