]> Creatis software - cpPlugins.git/blob - cmake/cpPlgFunctions.cmake
Merge branch 'sandbox' of ssh://git.creatis.insa-lyon.fr/cpPlugins into sandbox
[cpPlugins.git] / cmake / cpPlgFunctions.cmake
1 ## =========================================================================
2 ## @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 ## =========================================================================
4
5 ## -------------------------------------------------------------------------
6 function(BuildLibrary lib typ)
7 set(options RECURRENT INSTALL_ALL INSTALL_BIN INSTALL_DEV)
8 set(oneValueArgs)
9 set(multiValueArgs SOURCE VERSION LINKS)
10 cmake_parse_arguments(
11   BuildLibrary "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}
12   )
13
14 ## -- Get sources
15 set(_files)
16 foreach(_s ${BuildLibrary_SOURCE})
17
18   ## -- Canonicalize path
19   get_filename_component(_p "${_s}" ABSOLUTE)
20
21   ## -- Check type of input
22   if(IS_DIRECTORY ${_p})
23     if(BuildLibrary_RECURRENT)
24       file(GLOB_RECURSE _f "${_p}/*")
25     else(BuildLibrary_RECURRENT)
26       file(GLOB _f "${_p}/*")
27     endif(BuildLibrary_RECURRENT)
28     foreach(_x ${_f})
29       if(NOT IS_DIRECTORY ${_x})
30         list(APPEND _files ${_x})
31       endif(NOT IS_DIRECTORY ${_x})
32     endforeach(_x)
33   else(IS_DIRECTORY ${_p})
34     list(APPEND _files ${_p})
35   endif(IS_DIRECTORY ${_p})
36
37 endforeach(_s)
38
39 ## -- Process sources
40 set(_cpp)
41 set(_hpp)
42 set(_qui)
43 foreach(_f ${_files})
44
45   ## -- Separate filename from extension
46   string(REGEX REPLACE "\\.[^.]*$" "" _name ${_f})
47   string(REPLACE ${_name} "" _ext ${_f})
48   set(_out_name ${_name})
49   set(_out_ext ${_ext})
50
51   if(_out_ext)
52
53     ## -- Process .in files
54     string(COMPARE EQUAL "${_ext}" ".in" _in_cmp)
55     if(_in_cmp)
56       string(REPLACE ${PROJECT_SOURCE_DIR} ${PROJECT_BINARY_DIR} _out ${_name})
57       configure_file(${_f} ${_out} @ONLY)
58       string(REGEX REPLACE "\\.[^.]*$" "" _out_name ${_out})
59       string(REPLACE ${_out_name} "" _out_ext ${_out})
60     endif(_in_cmp)
61
62     ## -- Now, get real extension
63     string(SUBSTRING ${_out_ext} 0 2 _ext_cmp)
64
65     ## -- Process .c?? files
66     string(COMPARE EQUAL "${_ext_cmp}" ".c" _c_cmp)
67     if(_c_cmp)
68       list(APPEND _cpp ${_out_name}${_out_ext})
69     endif(_c_cmp)
70
71     ## -- Process .h?? files
72     string(COMPARE EQUAL "${_ext_cmp}" ".h" _h_cmp)
73     if(_h_cmp)
74       list(APPEND _hpp ${_out_name}${_out_ext})
75     endif(_h_cmp)
76
77     ## -- Process .ui files
78     string(COMPARE EQUAL "${_out_ext}" ".ui" _u_cmp)
79     if(_u_cmp)
80       list(APPEND _qui ${_out_name}${_out_ext})
81     endif(_u_cmp)
82
83   endif(_out_ext)
84
85 endforeach(_f)
86
87 ## -- Process Qt ui files
88 list(LENGTH _qui _qui_len)
89 if(${_qui_len} GREATER 0)
90   qt5_wrap_ui(_qui_hpp ${_qui})
91 endif(${_qui_len} GREATER 0)
92
93 if(_cpp)
94
95   ## -- Real build
96   add_library(${lib} ${typ} ${_cpp} ${_hpp} ${_qui_hpp})
97
98   ## -- Header creation
99   generate_export_header(${lib})
100
101   ## -- Put version strings
102   if(BuildLibrary_VERSION)
103     list(GET BuildLibrary_VERSION 0 _a)
104     list(GET BuildLibrary_VERSION 1 _b)
105     list(GET BuildLibrary_VERSION 2 _c)
106     set_property(TARGET ${lib} PROPERTY VERSION "${_a}.${_b}.${_c}")
107     set_property(TARGET ${lib} PROPERTY SOVERSION ${_a})
108     set_property(TARGET ${lib} PROPERTY INTERFACE_${lib}_MAJOR_VERSION ${_a})
109     set_property(
110       TARGET ${lib} APPEND PROPERTY COMPATIBLE_INTERFACE_STRING ${_a}
111       )
112   endif(BuildLibrary_VERSION)
113
114   ## -- Link library
115   if(BuildLibrary_LINKS)
116     target_link_libraries(${lib} PUBLIC ${BuildLibrary_LINKS})
117   endif(BuildLibrary_LINKS)
118
119 endif(_cpp)
120
121 ## -- Installation rules
122 if(BuildLibrary_INSTALL_ALL)
123   set(BuildLibrary_INSTALL_BIN ${BuildLibrary_INSTALL_ALL})
124   set(BuildLibrary_INSTALL_DEV ${BuildLibrary_INSTALL_ALL})
125 endif(BuildLibrary_INSTALL_ALL)
126
127 ## -- Installation rules
128 if(_cpp)
129   if(BuildLibrary_INSTALL_BIN)
130     install(
131       TARGETS ${lib}
132       EXPORT "${targets_export_name}"
133       LIBRARY DESTINATION "lib"
134       ARCHIVE DESTINATION "lib"
135       RUNTIME DESTINATION "bin"
136       INCLUDES DESTINATION "${include_install_dir}"
137       )
138   endif(BuildLibrary_INSTALL_BIN)
139 endif(_cpp)
140 if(BuildLibrary_INSTALL_DEV)
141   string(TOLOWER ${lib} _lower_lib)
142   set(_install_hdr ${_hpp})
143   if(_cpp)
144     list(
145       APPEND _install_hdr
146       ${CMAKE_CURRENT_BINARY_DIR}/${_lower_lib}_export.h
147       )
148   endif(_cpp)
149   set(_install_dirs)
150   foreach(_h ${_install_hdr})
151     string(REPLACE ${CMAKE_CURRENT_SOURCE_DIR} "" _h_name ${_h})
152     string(COMPARE EQUAL "${_h_name}" "${_h}" _h_cmp)
153     if(_h_cmp)
154       string(REPLACE ${CMAKE_CURRENT_BINARY_DIR} "" _h_name ${_h})
155     endif(_h_cmp)
156     set(_h_out ${include_install_dir}/${lib}${_h_name})
157     get_filename_component(_h_dir ${_h_out} DIRECTORY)
158     install(
159       FILES "${_h}"
160       DESTINATION "${_h_dir}"
161       )
162   endforeach(_h)
163 endif(BuildLibrary_INSTALL_DEV)
164
165 endfunction()
166
167 ## -------------------------------------------------------------------------
168 function(BuildApplication app)
169
170 set(options INSTALL RECURRENT)
171 set(oneValueArgs)
172 set(multiValueArgs SOURCE LINKS)
173 cmake_parse_arguments(
174   BuildApplication "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}
175   )
176
177 set(_lib __lib__${app})
178 if(BuildApplication_RECURRENT)
179   BuildLibrary(
180     ${_lib} STATIC
181     RECURRENT
182     SOURCE ${BuildApplication_SOURCE}
183     LINKS ${BuildApplication_LINKS}
184     )
185 else(BuildApplication_RECURRENT)
186   BuildLibrary(
187     ${_lib} STATIC
188     SOURCE ${BuildApplication_SOURCE}
189     LINKS ${BuildApplication_LINKS}
190     )
191 endif(BuildApplication_RECURRENT)
192
193 if(APPLE)
194 # use, i.e. don't skip the full RPATH for the build tree
195 SET(CMAKE_SKIP_BUILD_RPATH  FALSE)
196
197 # when building, don't use the install RPATH already
198 # (but later on when installing)
199 SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) 
200
201 SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
202
203 # add the automatically determined parts of the RPATH
204 # which point to directories outside the build tree to the install RPATH
205 SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
206
207
208 # the RPATH to be used when installing, but only if it's not a system directory
209 LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
210 IF("${isSystemDir}" STREQUAL "-1")
211    SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
212 ENDIF("${isSystemDir}" STREQUAL "-1")
213 endif(APPLE)
214
215 ## -- Create an empty application
216 set(_m ${CMAKE_CURRENT_BINARY_DIR}/__main__${app}.cxx)
217 file(WRITE ${_m} "// Automatically generated dummy file")
218 ## add_executable(${app} ${EXECUTABLE_TYPE} ${_m})
219 add_executable(${app} ${_m})
220
221 ## -- Link it against the static library
222 target_link_libraries(${app} PUBLIC ${_lib})
223
224 ## -- Installation rules
225 if(BuildApplication_INSTALL)
226   install(
227     TARGETS ${app}
228     EXPORT "${targets_export_name}"
229     LIBRARY DESTINATION "lib"
230     ARCHIVE DESTINATION "lib"
231     RUNTIME DESTINATION "bin"
232     INCLUDES DESTINATION "${include_install_dir}"
233     )
234 endif(BuildApplication_INSTALL)
235
236 endfunction()
237
238 ## eof - $RCSfile$