]> Creatis software - FrontAlgorithms.git/blob - cmake/Functions.cmake
821717353e34b5f33b6215b28ca5e9c6fa9ae78b
[FrontAlgorithms.git] / cmake / Functions.cmake
1 ## =========================================================================
2 ## @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 ## =========================================================================
4
5 ## -------------------------------------------------------------------------
6 function(BuildLibrary lib typ src maj min rel)
7
8 ## -- Get sources
9 set(_files)
10 foreach(_s ${src})
11
12   ## -- Canonicalize path
13   get_filename_component(_p "${_s}" ABSOLUTE)
14
15   ## -- Check type of input
16   if(IS_DIRECTORY ${_p})
17     file(GLOB _f "${_p}/*")
18     foreach(_x ${_f})
19       if(NOT IS_DIRECTORY ${_x})
20         list(APPEND _files ${_x})
21       endif(NOT IS_DIRECTORY ${_x})
22     endforeach(_x)
23   else(IS_DIRECTORY ${_p})
24     list(APPEND _files ${_p})
25   endif(IS_DIRECTORY ${_p})
26
27 endforeach(_s)
28
29 ## -- Process sources
30 set(_cpp)
31 set(_hpp)
32 set(_qui)
33 foreach(_f ${_files})
34
35   ## -- Separate filename from extension
36   string(REGEX REPLACE "\\.[^.]*$" "" _name ${_f})
37   string(REPLACE ${_name} "" _ext ${_f})
38   set(_out_name ${_name})
39   set(_out_ext ${_ext})
40
41   ## -- Process .in files
42   string(COMPARE EQUAL "${_ext}" ".in" _in_cmp)
43   if(_in_cmp)
44     string(REPLACE ${PROJECT_SOURCE_DIR} ${PROJECT_BINARY_DIR} _out ${_name})
45     configure_file(${_f} ${_out} @ONLY)
46     string(REGEX REPLACE "\\.[^.]*$" "" _out_name ${_out})
47     string(REPLACE ${_out_name} "" _out_ext ${_out})
48   endif(_in_cmp)
49
50   ## -- Now, get real extension
51   string(SUBSTRING ${_out_ext} 0 2 _ext_cmp)
52
53   ## -- Process .c?? files
54   string(COMPARE EQUAL "${_ext_cmp}" ".c" _c_cmp)
55   if(_c_cmp)
56     list(APPEND _cpp ${_out_name}${_out_ext})
57   endif(_c_cmp)
58
59   ## -- Process .h?? files
60   string(COMPARE EQUAL "${_ext_cmp}" ".h" _h_cmp)
61   if(_h_cmp)
62     list(APPEND _hpp ${_out_name}${_out_ext})
63   endif(_h_cmp)
64
65   ## -- Process .ui files
66   string(COMPARE EQUAL "${_out_ext}" ".ui" _u_cmp)
67   if(_u_cmp)
68     list(APPEND _qui ${_out_name}${_out_ext})
69   endif(_u_cmp)
70
71 endforeach(_f)
72
73 ## -- Process Qt ui files
74 list(LENGTH _qui _qui_len)
75 if(${_qui_len} GREATER 0)
76   qt5_wrap_ui(_qui_hpp ${_qui})
77 endif(${_qui_len} GREATER 0)
78
79 ## -- Real build
80 add_library(${lib} ${typ} ${_cpp} ${_hpp} ${_qui_hpp})
81
82 ## -- Header creation
83 generate_export_header(${lib})
84 set_property(TARGET ${lib} PROPERTY VERSION "${maj}.${min}.${rel}")
85 set_property(TARGET ${lib} PROPERTY SOVERSION ${maj})
86 set_property(TARGET ${lib} PROPERTY INTERFACE_${lib}_MAJOR_VERSION ${maj})
87 set_property(TARGET ${lib} APPEND PROPERTY COMPATIBLE_INTERFACE_STRING ${maj})
88
89 ## -- Link library
90 target_link_libraries(${lib} PUBLIC ${ARGN})
91
92 ## -- Installation rules
93 option(${lib}_INSTALL_DEVEL "Install development files for ${lib}" OFF)
94 string(COMPARE EQUAL "${type}" "SHARED" _cmp)
95 if(_cmp OR ${lib}_INSTALL_DEVEL)
96   install(
97     TARGETS ${lib}
98     EXPORT "${targets_export_name}"
99     LIBRARY DESTINATION "lib"
100     ARCHIVE DESTINATION "lib"
101     RUNTIME DESTINATION "bin"
102     INCLUDES DESTINATION "${include_install_dir}"
103     )
104 endif(_cmp OR ${lib}_INSTALL_DEVEL)
105 if(${lib}_INSTALL_DEVEL)
106   string(TOLOWER ${lib} _lower_lib)
107   set(
108     _install_hdr
109     ${_hpp}
110     ${CMAKE_CURRENT_BINARY_DIR}/${_lower_lib}_export.h
111     )
112   set(_install_dirs)
113   foreach(_h ${_install_hdr})
114     string(REPLACE ${CMAKE_CURRENT_SOURCE_DIR} "" _h_name ${_h})
115     string(COMPARE EQUAL "${_h_name}" "${_h}" _h_cmp)
116     if(_h_cmp)
117       string(REPLACE ${CMAKE_CURRENT_BINARY_DIR} "" _h_name ${_h})
118     endif(_h_cmp)
119     set(_h_out ${include_install_dir}/${lib}${_h_name})
120     get_filename_component(_h_dir ${_h_out} DIRECTORY)
121     install(
122       FILES "${_h}"
123       DESTINATION "${_h_dir}"
124       )
125   endforeach(_h)
126 endif(${lib}_INSTALL_DEVEL)
127
128 endfunction()
129
130 ## -------------------------------------------------------------------------
131 function(BuildLibraryRecursive lib typ dir maj min rel)
132
133 ## -- Globbing directory
134 file(GLOB_RECURSE _files "${dir}/*")
135
136 ## -- Build library
137 BuildLibrary(${lib} ${typ} "${_files}" ${maj} ${min} ${rel} ${ARGN})
138
139 endfunction()
140
141 ## -------------------------------------------------------------------------
142 function(BuildApplication app)
143 BuildLibraryRecursive(
144   _${app}_ STATIC ${CMAKE_CURRENT_SOURCE_DIR}
145   0 0 0
146   )
147 ## ${EXECUTABLE_TYPE}
148 ## target_link_libraries(${app} _${app}_)
149 endfunction()
150
151 ## eof - $RCSfile$