]> Creatis software - cpPlugins.git/blob - cmake/Functions.cmake
436494f9e96a2750e728265fff1ada039c17e360
[cpPlugins.git] / cmake / Functions.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   ## -- Process .in files
52   string(COMPARE EQUAL "${_ext}" ".in" _in_cmp)
53   if(_in_cmp)
54     string(REPLACE ${PROJECT_SOURCE_DIR} ${PROJECT_BINARY_DIR} _out ${_name})
55     configure_file(${_f} ${_out} @ONLY)
56     string(REGEX REPLACE "\\.[^.]*$" "" _out_name ${_out})
57     string(REPLACE ${_out_name} "" _out_ext ${_out})
58   endif(_in_cmp)
59
60   ## -- Now, get real extension
61   string(SUBSTRING ${_out_ext} 0 2 _ext_cmp)
62
63   ## -- Process .c?? files
64   string(COMPARE EQUAL "${_ext_cmp}" ".c" _c_cmp)
65   if(_c_cmp)
66     list(APPEND _cpp ${_out_name}${_out_ext})
67   endif(_c_cmp)
68
69   ## -- Process .h?? files
70   string(COMPARE EQUAL "${_ext_cmp}" ".h" _h_cmp)
71   if(_h_cmp)
72     list(APPEND _hpp ${_out_name}${_out_ext})
73   endif(_h_cmp)
74
75   ## -- Process .ui files
76   string(COMPARE EQUAL "${_out_ext}" ".ui" _u_cmp)
77   if(_u_cmp)
78     list(APPEND _qui ${_out_name}${_out_ext})
79   endif(_u_cmp)
80
81 endforeach(_f)
82
83 ## -- Process Qt ui files
84 list(LENGTH _qui _qui_len)
85 if(${_qui_len} GREATER 0)
86   qt5_wrap_ui(_qui_hpp ${_qui})
87 endif(${_qui_len} GREATER 0)
88
89 if(_cpp)
90
91   ## -- Real build
92   add_library(${lib} ${typ} ${_cpp} ${_hpp} ${_qui_hpp})
93
94   ## -- Header creation
95   generate_export_header(${lib})
96
97   ## -- Put version strings
98   if(BuildLibrary_VERSION)
99     list(GET BuildLibrary_VERSION 0 _a)
100     list(GET BuildLibrary_VERSION 1 _b)
101     list(GET BuildLibrary_VERSION 2 _c)
102     set_property(TARGET ${lib} PROPERTY VERSION "${_a}.${_b}.${_c}")
103     set_property(TARGET ${lib} PROPERTY SOVERSION ${_a})
104     set_property(TARGET ${lib} PROPERTY INTERFACE_${lib}_MAJOR_VERSION ${_a})
105     set_property(
106       TARGET ${lib} APPEND PROPERTY COMPATIBLE_INTERFACE_STRING ${_a}
107       )
108   endif(BuildLibrary_VERSION)
109
110   ## -- Link library
111   if(BuildLibrary_LINKS)
112     target_link_libraries(${lib} PUBLIC ${BuildLibrary_LINKS})
113   endif(BuildLibrary_LINKS)
114
115 endif(_cpp)
116
117 ## -- Installation rules
118 if(BuildLibrary_INSTALL_ALL)
119   set(BuildLibrary_INSTALL_BIN ${BuildLibrary_INSTALL_ALL})
120   set(BuildLibrary_INSTALL_DEV ${BuildLibrary_INSTALL_ALL})
121 endif(BuildLibrary_INSTALL_ALL)
122
123 ## -- Installation rules
124 if(_cpp)
125   if(BuildLibrary_INSTALL_BIN)
126     install(
127       TARGETS ${lib}
128       EXPORT "${targets_export_name}"
129       LIBRARY DESTINATION "lib"
130       ARCHIVE DESTINATION "lib"
131       RUNTIME DESTINATION "bin"
132       INCLUDES DESTINATION "${include_install_dir}"
133       )
134   endif(BuildLibrary_INSTALL_BIN)
135 endif(_cpp)
136 if(BuildLibrary_INSTALL_DEV)
137   string(TOLOWER ${lib} _lower_lib)
138   set(_install_hdr ${_hpp})
139   if(_cpp)
140     list(
141       APPEND _install_hdr
142       ${CMAKE_CURRENT_BINARY_DIR}/${_lower_lib}_export.h
143       )
144   endif(_cpp)
145   set(_install_dirs)
146   foreach(_h ${_install_hdr})
147     string(REPLACE ${CMAKE_CURRENT_SOURCE_DIR} "" _h_name ${_h})
148     string(COMPARE EQUAL "${_h_name}" "${_h}" _h_cmp)
149     if(_h_cmp)
150       string(REPLACE ${CMAKE_CURRENT_BINARY_DIR} "" _h_name ${_h})
151     endif(_h_cmp)
152     set(_h_out ${include_install_dir}/${lib}${_h_name})
153     get_filename_component(_h_dir ${_h_out} DIRECTORY)
154     install(
155       FILES "${_h}"
156       DESTINATION "${_h_dir}"
157       )
158   endforeach(_h)
159 endif(BuildLibrary_INSTALL_DEV)
160
161 endfunction()
162
163 ## -------------------------------------------------------------------------
164 function(BuildApplication app)
165
166 set(options INSTALL RECURRENT)
167 set(oneValueArgs)
168 set(multiValueArgs SOURCE LINKS)
169 cmake_parse_arguments(
170   BuildApplication "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}
171   )
172
173 set(_lib __lib__${app})
174 if(BuildApplication_RECURRENT)
175   BuildLibrary(
176     ${_lib} STATIC
177     RECURRENT
178     SOURCE ${BuildApplication_SOURCE}
179     LINKS ${BuildApplication_LINKS}
180     )
181 else(BuildApplication_RECURRENT)
182   BuildLibrary(
183     ${_lib} STATIC
184     SOURCE ${BuildApplication_SOURCE}
185     LINKS ${BuildApplication_LINKS}
186     )
187 endif(BuildApplication_RECURRENT)
188
189 ## -- Create an empty application
190 set(_m ${CMAKE_CURRENT_BINARY_DIR}/__main__${app}.cxx)
191 file(WRITE ${_m} "// Automatically generated dummy file")
192 add_executable(${app} ${EXECUTABLE_TYPE} ${_m})
193
194 ## -- Link it against the static library
195 target_link_libraries(${app} PUBLIC ${_lib})
196
197 ## -- Installation rules
198 if(BuildApplication_INSTALL)
199   install(
200     TARGETS ${app}
201     EXPORT "${targets_export_name}"
202     LIBRARY DESTINATION "lib"
203     ARCHIVE DESTINATION "lib"
204     RUNTIME DESTINATION "bin"
205     INCLUDES DESTINATION "${include_install_dir}"
206     )
207 endif(BuildApplication_INSTALL)
208
209 endfunction()
210
211 ## eof - $RCSfile$