]> Creatis software - crea.git/blob - cmake/CREAMacro_InstallLibraryForCMake.cmake
Feature #1711
[crea.git] / cmake / CREAMacro_InstallLibraryForCMake.cmake
1 # ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
4 #                        pour la Santé)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and 
10 #  abiding by the rules of distribution of free software. You can  use, 
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
12 #  license as circulated by CEA, CNRS and INRIA at the following URL 
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability. 
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */ 
25
26
27 #-----------------------------------------------------------------------------
28 # Macro CREA_ADVANCED_INSTALL_LIBRARY_FOR_CMAKE
29 # Creates and install the cmake files which allow 
30 # to find the library by the cmake command FIND_PACKAGE.
31 #
32 # Inputs :
33 # --------
34 #  LIBRARY_NAME : name of the library to find 
35 #
36 # The following variables **MUST** have been set previously :
37 #
38 # * ${LIBRARY_NAME}_BUILD_TREE_RELATIVE_INCLUDE_PATHS
39 #    The list of include paths 
40 #    when someone uses a *BUILD TREE* version of ${LIBRARY_NAME}.
41 #    NB : 
42 #     THE PATHS *MUST BE RELATIVE* TO THE ROOT DIR OF THE PROJECT **SOURCES** !
43 #    Assume your project architecture is :
44 #     install/ : directory in which the macro is invoked
45 #     src/part1/include/ : first include dir
46 #     src/part2/include/ : second include dir
47 #    Then you should set the var with :
48 #    SET(${LIBRARY_NAME}_BUILD_TREE_RELATIVE_INCLUDE_PATHS
49 #     src/part1/include
50 #     src/part2/include )
51 #    Which will result in actual include paths :
52 #     ${PROJECT_SOURCE_DIR}/src/part1/include;
53 #     ${PROJECT_SOURCE_DIR}/src/part2/include
54 # * ${LIBRARY_NAME}_BUILD_TREE_RELATIVE_LIBRARY_PATHS
55 #    Like the previous var but for the library paths.
56 #    NB : 
57 #     THE PATHS *MUST BE RELATIVE* TO THE ROOT DIR OF THE **BUILD TREE** 
58 #     THAT IS POINT TO THE FOLDERS WHERE THE LIBS WILL BE BUILD
59 #    Assume that your project architecture is :
60 #     src/part1/src/ : first source dir, in which the lib 'part1' is built
61 #     src/part2/src/ : first source dir, in which the lib 'part2' is built
62 #    Then you should set the var with 
63 #    SET(${LIBRARY_NAME}_BUILD_TREE_RELATIVE_LIBRARY_PATHS
64 #     src/part1/src
65 #     src/part2/src
66 #     )
67 #    Which will result in actual library paths :
68 #     ${PROJECT_BINARY_DIR}/src/part1/src
69 #     ${PROJECT_BINARY_DIR}/src/part2/src
70 # * ${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_INCLUDE_PATHS
71 #    The list of include paths 
72 #    when someone uses an *INSTALLED* version of ${LIBRARY_NAME} 
73 #    The paths *MUST BE RELATIVE* to CMAKE_INSTALL_PREFIX
74 #  #    A typical example is "include/${LIBRARY_NAME}"
75 # * ${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS
76 #    Like the previous var but for library paths.
77 #    A typical example is "lib"
78 # * ${LIBRARY_NAME}_LIBRARIES
79 #    The list of libraries to link against when using ${LIBRARY_NAME}
80 #
81 # The following variables can be set optionally :
82 #
83 # * ${LIBRARY_NAME}_REQUIRED_C_FLAGS
84 # * ${LIBRARY_NAME}_REQUIRED_CXX_FLAGS
85 # * ${LIBRARY_NAME}_REQUIRED_LINK_FLAGS
86 # * ${LIBRARY_NAME}_MAJOR_VERSION
87 # * ${LIBRARY_NAME}_MINOR_VERSION
88 # * ${LIBRARY_NAME}_BUILD_VERSION
89 # * ${LIBRARY_NAME}_INSTALL_FOLDER : if set then install the generated files 
90 #   in CMAKE_INSTALL_PREFIX/lib/${LIBRARY_NAME}_INSTALL_FOLDER 
91 #   instead of CMAKE_INSTALL_PREFIX/lib/${LIBRARY_NAME}
92 #
93
94 # To provide a user defined 
95 # couple of Config/Use file (in addition to the standard one) use :
96 #  SET( ${LIBRARY_NAME}_HAS_ADDITIONAL_CONFIG_FILE  TRUE )
97 # and store the *ABSOLUTE* paths to the additional files in the vars :
98 #  ${LIBRARY_NAME}_ADDITIONAL_CONFIG_FILE
99 #  ${LIBRARY_NAME}_ADDITIONAL_USE_FILE
100 # (e.g. ${CMAKE_CURRENT_SOURCE_DIR}/MyConfig.cmake)
101 #
102 # Outputs :
103 # --------
104 # At cmake run-time, the macro creates the following files 
105 # in the current dir of the build tree (where the macro is invoked) :
106 #  Use${LIBRARY_NAME}.cmake
107 #  ${LIBRARY_NAME}Config.cmake
108 #  ${LIBRARY_NAME}BuildSettings.cmake
109 # And if the vars ${LIBRARY_NAME}_ADDITIONAL_CONFIG_FILE and
110 # ${LIBRARY_NAME}_ADDITIONAL_USE_FILE are set, it also creates :
111 #  Additional${LIBRARY_NAME}Config.cmake
112 #  AdditionalUse${LIBRARY_NAME}.cmake
113
114 # At install-time, the same files are installed 
115 # in CMAKE_INSTALL_PREFIX/lib/${LIBRARY_NAME}_INSTALL_FOLDER 
116 # and the file :
117 #  Find${LIBRARY_NAME}.cmake
118 # is installed in ${CMAKE_ROOT}/Modules/
119 #
120 #
121 #-----------------------------------------------------------------------------
122
123 MACRO(CREA_ADVANCED_INSTALL_LIBRARY_FOR_CMAKE LIBRARY_NAME)
124
125   #---------------------------------------------------------------------------
126   # Sets the common values to build tree and install tree configs
127   SET(CILC_LIB_REQUIRED_C_FLAGS    ${${LIBRARY_NAME}_REQUIRED_C_FLAGS})
128   SET(CILC_LIB_REQUIRED_CXX_FLAGS  ${${LIBRARY_NAME}_REQUIRED_CXX_FLAGS})
129   SET(CILC_LIB_REQUIRED_LINK_FLAGS ${${LIBRARY_NAME}_REQUIRED_LINK_FLAGS})
130   SET(CILC_LIB_MAJOR_VERSION       ${${LIBRARY_NAME}_MAJOR_VERSION})
131   SET(CILC_LIB_MINOR_VERSION       ${${LIBRARY_NAME}_MINOR_VERSION})
132   SET(CILC_LIB_BUILD_VERSION       ${${LIBRARY_NAME}_BUILD_VERSION})
133   SET(CILC_LIB_VERSION             
134     ${CILC_LIB_MAJOR_VERSION}.${CILC_LIB_MINOR_VERSION}.${CILC_LIB_BUILD_VERSION})
135   SET(CILC_LIB_LIBRARIES ${${LIBRARY_NAME}_LIBRARIES})
136   IF (${LIBRARY_NAME}_HAS_ADDITIONAL_CONFIG_FILE)
137     SET(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE TRUE)
138   ELSE (${LIBRARY_NAME}_HAS_ADDITIONAL_CONFIG_FILE)
139     SET(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE FALSE)
140   ENDIF (${LIBRARY_NAME}_HAS_ADDITIONAL_CONFIG_FILE)
141   #---------------------------------------------------------------------------
142
143
144   #---------------------------------------------------------------------------
145   #-- BUILD TREE configuration 
146   SET(CILC_BUILD_TREE_CONFIGURATION TRUE)
147   # UseLIBRARY_NAME.cmake 
148   CONFIGURE_FILE(
149     ${CREA_CMAKE_DIR}/InstallLibraryForCMake_UseLibrary.cmake.in
150     ${PROJECT_BINARY_DIR}/Use${LIBRARY_NAME}.cmake
151     @ONLY IMMEDIATE
152     )
153   # LIBRARY_NAMEConfig.cmake 
154   SET(CILC_LIB_RELATIVE_INCLUDE_PATHS 
155     ${${LIBRARY_NAME}_BUILD_TREE_RELATIVE_INCLUDE_PATHS})
156   SET(CILC_LIB_RELATIVE_LIBRARY_PATHS 
157     ${${LIBRARY_NAME}_BUILD_TREE_RELATIVE_LIBRARY_PATHS})
158   CONFIGURE_FILE(
159     ${CREA_CMAKE_DIR}/InstallLibraryForCMake_LibraryConfig.cmake.in
160     ${PROJECT_BINARY_DIR}/${LIBRARY_NAME}Config.cmake
161     @ONLY IMMEDIATE
162     )
163   # LIBRARY_NAMEBuildSettings.cmake :
164   # Save the compiler settings so another project can import them.
165   # FCY : DEPREDCATED FUNCTION FOR CMAKE 2.8
166   IF( "${CMAKE_MINIMUM_REQUIRED_VERSION}" VERSION_LESS 2.7)
167   INCLUDE(${CMAKE_ROOT}/Modules/CMakeExportBuildSettings.cmake)
168   CMAKE_EXPORT_BUILD_SETTINGS(${PROJECT_BINARY_DIR}/${LIBRARY_NAME}BuildSettings.cmake)
169   ENDIF()
170   # Additional Config and Use files
171   IF(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE)
172     CONFIGURE_FILE(
173       ${${LIBRARY_NAME}_ADDITIONAL_CONFIG_FILE}
174       ${PROJECT_BINARY_DIR}/Additional${LIBRARY_NAME}Config.cmake
175       @ONLY IMMEDIATE
176       )
177     CONFIGURE_FILE(
178       ${${LIBRARY_NAME}_ADDITIONAL_USE_FILE}
179       ${PROJECT_BINARY_DIR}/AdditionalUse${LIBRARY_NAME}.cmake
180       @ONLY IMMEDIATE
181       )
182   ENDIF(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE)
183   #---------------------------------------------------------------------------
184   
185
186   #---------------------------------------------------------------------------
187   #-- INSTALL TREE configuration 
188
189
190   SET(CILC_BUILD_TREE_CONFIGURATION FALSE)
191   # Create work directory to put the configured files because 
192   # if the user invoked the macro at the root of the build tree the files 
193   # would overwrite those configured for the build tree
194   SET(CILC_WORK_DIR ${CMAKE_CURRENT_BINARY_DIR}/InstallLibraryForCMake_tmp)
195   IF(NOT IS_DIRECTORY ${CILC_WORK_DIR})
196     FILE(MAKE_DIRECTORY ${CILC_WORK_DIR})
197   ENDIF(NOT IS_DIRECTORY ${CILC_WORK_DIR})
198   
199   #----------------------------------------------------------------------------
200   #INSTALLATION PATH
201   IF(WIN32)
202     SET(INSTALL_PATH .)
203   ELSE(WIN32)
204     IF(${LIBRARY_NAME}_INSTALL_FOLDER)
205 ##EED      SET(INSTALL_PATH lib/${${LIBRARY_NAME}_INSTALL_FOLDER})
206       SET(INSTALL_PATH ${${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS}/${${LIBRARY_NAME}_INSTALL_FOLDER})
207     ELSE(${LIBRARY_NAME}_INSTALL_FOLDER)
208 ##EED      SET(INSTALL_PATH lib/${LIBRARY_NAME})
209            SET(INSTALL_PATH ${${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS}/${LIBRARY_NAME})
210     ENDIF(${LIBRARY_NAME}_INSTALL_FOLDER)
211   ENDIF(WIN32)
212
213   message(STATUS "Library Install Path ${INSTALL_PATH}")
214
215   # UseLIBRARY_NAME.cmake 
216   CONFIGURE_FILE(
217     ${CREA_CMAKE_DIR}/InstallLibraryForCMake_UseLibrary.cmake.in
218     ${CILC_WORK_DIR}/Use${LIBRARY_NAME}.cmake
219     @ONLY IMMEDIATE
220     )
221   INSTALL(
222     FILES 
223     ${CILC_WORK_DIR}/Use${LIBRARY_NAME}.cmake
224     DESTINATION ${INSTALL_PATH}
225     )
226   # LIBRARY_NAMEConfig.cmake 
227   SET(CILC_LIB_RELATIVE_INCLUDE_PATHS 
228     ${${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_INCLUDE_PATHS})
229   SET(CILC_LIB_RELATIVE_LIBRARY_PATHS 
230     ${${LIBRARY_NAME}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS})
231   CONFIGURE_FILE(
232     ${CREA_CMAKE_DIR}/InstallLibraryForCMake_LibraryConfig.cmake.in
233     ${CILC_WORK_DIR}/${LIBRARY_NAME}Config.cmake
234     @ONLY IMMEDIATE
235     )
236   INSTALL(
237     FILES 
238     ${CILC_WORK_DIR}/${LIBRARY_NAME}Config.cmake
239     DESTINATION ${INSTALL_PATH}
240     )
241   # LIBRARY_NAMEBuildSettings.cmake :
242   # FCY : DEPREDCATED FUNCTION FOR CMAKE 2.8
243   IF( "${CMAKE_MINIMUM_REQUIRED_VERSION}" VERSION_LESS 2.7)
244   CMAKE_EXPORT_BUILD_SETTINGS(${CILC_WORK_DIR}/${LIBRARY_NAME}BuildSettings.cmake)
245   ENDIF()
246   INSTALL(
247     FILES 
248     ${CILC_WORK_DIR}/${LIBRARY_NAME}BuildSettings.cmake
249     DESTINATION  ${INSTALL_PATH}
250     )
251   # Additional Config and Use files
252   IF(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE)
253     CONFIGURE_FILE(
254       ${${LIBRARY_NAME}_ADDITIONAL_CONFIG_FILE}
255       ${CILC_WORK_DIR}/Additional${LIBRARY_NAME}Config.cmake
256       @ONLY IMMEDIATE
257       )
258     INSTALL(
259       FILES 
260       ${CILC_WORK_DIR}/Additional${LIBRARY_NAME}Config.cmake
261       DESTINATION ${INSTALL_PATH}
262       )
263     CONFIGURE_FILE(
264       ${${LIBRARY_NAME}_ADDITIONAL_USE_FILE}
265       ${CILC_WORK_DIR}/AdditionalUse${LIBRARY_NAME}.cmake
266       @ONLY IMMEDIATE
267       )
268     INSTALL(
269       FILES 
270       ${CILC_WORK_DIR}/AdditionalUse${LIBRARY_NAME}.cmake
271       DESTINATION  ${INSTALL_PATH}
272       )
273   ENDIF(CILC_LIB_HAS_ADDITIONAL_CONFIG_FILE)
274   # Specific to install tree
275   # FindLIBRARY_NAME.cmake in CMake/Modules dir
276   CONFIGURE_FILE(
277     ${CREA_CMAKE_DIR}/InstallLibraryForCMake_FindLibrary.cmake.in
278     ${CILC_WORK_DIR}/Find${LIBRARY_NAME}.cmake
279     @ONLY IMMEDIATE
280     )
281 #EED 02 mars 2011
282 #  INSTALL( 
283 #    FILES ${CILC_WORK_DIR}/Find${LIBRARY_NAME}.cmake
284 #    DESTINATION ${CMAKE_ROOT}/Modules 
285 #    )
286
287 # JPR 04 Mars 2011
288   INSTALL( 
289     FILES ${CILC_WORK_DIR}/Find${LIBRARY_NAME}.cmake
290     DESTINATION ${CMAKE_INSTALL_PREFIX}/share/cmake/Modules
291     )
292     
293  
294   #---------------------------------------------------------------------------
295   
296
297
298
299 ENDMACRO(CREA_ADVANCED_INSTALL_LIBRARY_FOR_CMAKE)
300 #-----------------------------------------------------------------------------
301
302
303
304
305 #-----------------------------------------------------------------------------
306 MACRO(CREA_INSTALL_LIBRARY_FOR_CMAKE LIBRARY_NAME1)
307   
308   # Sets the settings to default values
309   IF(NOT ${LIBRARY_NAME1}_INSTALL_FOLDER)
310     SET(${LIBRARY_NAME1}_INSTALL_FOLDER ${LIBRARY_NAME1})
311   ENDIF(NOT ${LIBRARY_NAME1}_INSTALL_FOLDER)
312
313   SET(${LIBRARY_NAME1}_LIBRARIES ${LIBRARY_NAME1})
314   
315   FILE(RELATIVE_PATH 
316     ${LIBRARY_NAME1}_BUILD_TREE_RELATIVE_INCLUDE_PATHS 
317     ${PROJECT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
318     )
319   IF ( ${PROJECT_BINARY_DIR} STREQUAL ${EXECUTABLE_OUTPUT_PATH} )
320     SET(CILFC_EXECUTABLE_OUTPUT_REL_PATH ".")
321   ELSE ( ${PROJECT_BINARY_DIR} STREQUAL ${EXECUTABLE_OUTPUT_PATH} )
322
323     FILE(RELATIVE_PATH 
324       CILFC_EXECUTABLE_OUTPUT_REL_PATH
325       ${PROJECT_BINARY_DIR} ${EXECUTABLE_OUTPUT_PATH})
326   ENDIF ( ${PROJECT_BINARY_DIR} STREQUAL ${EXECUTABLE_OUTPUT_PATH} )
327
328
329   IF(UNIX)
330     SET(${LIBRARY_NAME1}_BUILD_TREE_RELATIVE_LIBRARY_PATHS 
331       ${CILFC_EXECUTABLE_OUTPUT_REL_PATH})
332     SET(${LIBRARY_NAME1}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS ${CMAKE_CREA_LIB_PATH} )
333   ELSE(UNIX)
334     SET(${LIBRARY_NAME1}_BUILD_TREE_RELATIVE_LIBRARY_PATHS 
335       ${CILFC_EXECUTABLE_OUTPUT_REL_PATH} )
336 # LG 15/01/09 : Bug
337 #      ${CILFC_EXECUTABLE_OUTPUT_REL_PATH}/Debug
338 #     ${CILFC_EXECUTABLE_OUTPUT_REL_PATH}/Release)
339     SET(${LIBRARY_NAME1}_INSTALL_TREE_RELATIVE_LIBRARY_PATHS bin)
340   ENDIF(UNIX)
341
342   IF(NOT ${LIBRARY_NAME1}_INSTALL_TREE_RELATIVE_INCLUDE_PATHS)
343     SET(${LIBRARY_NAME1}_INSTALL_TREE_RELATIVE_INCLUDE_PATHS include/${LIBRARY_NAME1})
344   ENDIF(NOT ${LIBRARY_NAME1}_INSTALL_TREE_RELATIVE_INCLUDE_PATHS)
345   
346   # Invoke the advanced macro
347   CREA_ADVANCED_INSTALL_LIBRARY_FOR_CMAKE(${LIBRARY_NAME1})
348
349
350 ENDMACRO(CREA_INSTALL_LIBRARY_FOR_CMAKE)
351 #-----------------------------------------------------------------------------