DOC: Fixed FOAM_INSTALL_*_PATH listing in INSTALL
[freefoam.git] / CMake / FOAMInternal.cmake
blobe06e6915a5cf2cecfdd3885d5aea50bb040e694f
1 # - Internal function for FreeFOAM
3 #  FOAM_OPTION(<var> <short> <description> <value>)
5 # Like OPTION(<var> <description> <value>), but also adds the variable <var>
6 # with the short description <short> to the feature-summary table.
8 #  FOAM_INSTALLATION_PATH(<var> <path> <short> <description>)
10 # Like SET(<var> <path> CACHE PATH <description>), but also adds the variable
11 # <var> with the short description <short> to the feature-summary table.
12 # Further if the value of <var> is not an absolute path, it computes the
13 # absolute path and assigns it to the <var> in the parent scope.
15 #  FOAM_CONDITIONAL_COMPONENT(<name> <enabled> <subdir> <reason>)
17 # Like ADD_SUBDIRECTORY(<subdir>) but if <enabled> evaulates to FALSE, <name>
18 # is entered as being disabled because of <reason> into the feature-summary
19 # table.
21 #  FOAM_PRINT_FEATURE_SUMMARY()
23 # Prints summaries of enabled/disabled features, installation paths and
24 # disabled components.
26 #  FOAM_TARGET_LINK_LIBRARIES(<target> item1 [item2 [...]]
27 #                             [[debug|optimized|general] <item>] ...)
29 # Like target_link_libraries() but explicitly sets the LINK_INTERFACE_LIBRARIES
30 # property to exclude the third-party libraries.
32 #  FOAM_ADD_TUTORIAL_TESTS()
34 # Adds the individual cases of the tutorial in the current source directory as
35 # tests. It runs the configured Allrun script in the directory with the --cases
36 # option to obtain a list of cases, and adds each of them as a test. Each case
37 # depends on its predecessor.
39 #  FOAM_DETERMINE_PYTHON_VERSION(<VERSION_VAR> <PYTHON_EXECUTABLE>)
41 # Determines the version of the Python interpreter in <PYTHON_EXECUTABLE> and
42 # returns the result in the variable <VERSION_VAR>.
45 #-------------------------------------------------------------------------------
46 #               ______                _     ____          __  __
47 #              |  ____|             _| |_  / __ \   /\   |  \/  |
48 #              | |__ _ __ ___  ___ /     \| |  | | /  \  | \  / |
49 #              |  __| '__/ _ \/ _ ( (| |) ) |  | |/ /\ \ | |\/| |
50 #              | |  | | |  __/  __/\_   _/| |__| / ____ \| |  | |
51 #              |_|  |_|  \___|\___|  |_|   \____/_/    \_\_|  |_|
53 #                   FreeFOAM: The Cross-Platform CFD Toolkit
55 # Copyright (C) 2008-2012 Michael Wild <themiwi@users.sf.net>
56 #                         Gerber van der Graaf <gerber_graaf@users.sf.net>
57 #-------------------------------------------------------------------------------
58 # License
59 #   This file is part of FreeFOAM.
61 #   FreeFOAM is free software: you can redistribute it and/or modify it
62 #   under the terms of the GNU General Public License as published by the
63 #   Free Software Foundation, either version 3 of the License, or (at your
64 #   option) any later version.
66 #   FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
67 #   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
68 #   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
69 #   for more details.
71 #   You should have received a copy of the GNU General Public License
72 #   along with FreeFOAM.  If not, see <http://www.gnu.org/licenses/>.
74 # Description
75 #   Internal CMake functions for FreeFOAM
77 #-------------------------------------------------------------------------------
79 function(foam_option var short description value)
80   _foam_set_feature_property("OPTION" ${var} "${short}")
81   option(${var} "${description}" ${value})
82 endfunction()
84 function(foam_installation_path var value short description)
85   _foam_set_feature_property("PATH" ${var} "${short}")
86   set(${var} ${value} CACHE PATH "${description}")
87   mark_as_advanced(${var})
88   set(${var}_ORIG ${${var}} PARENT_SCOPE)
89   # make absolute path
90   if(NOT IS_ABSOLUTE "${${var}}")
91     set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}" PARENT_SCOPE)
92   endif()
93 endfunction()
95 macro(foam_conditional_component name enabled subdir description)
96   if(${enabled})
97     add_subdirectory(${subdir})
98   else()
99     _foam_set_feature_property("COMPONENT" ${name} "${description}")
100   endif()
101 endmacro()
103 function(_foam_set_feature_property type var short)
104   set_property(GLOBAL APPEND PROPERTY FOAM_FEATURES ${var})
105   set_property(GLOBAL PROPERTY FOAM_FEATURES_${var}_COMMENT "${short}")
106   set_property(GLOBAL PROPERTY FOAM_FEATURES_${var}_TYPE "${type}")
107 endfunction()
109 function(foam_print_feature_summary)
110   set(OPTION_msg "Feature Summary:")
111   set(PATH_msg "Installation Directories:")
112   set(COMPONENT_msg "Disabled Components:")
113   get_property(features GLOBAL PROPERTY FOAM_FEATURES)
114   set(OPTION_len 0)
115   set(OPTION_space)
116   set(PATH_len 0)
117   set(PATH_space)
118   set(COMPONENT_len 0)
119   set(COMPONENT_space)
120   foreach(f ${features})
121     get_property(desc GLOBAL PROPERTY FOAM_FEATURES_${f}_COMMENT)
122     get_property(type GLOBAL PROPERTY FOAM_FEATURES_${f}_TYPE)
123     if(NOT type STREQUAL "COMPONENT")
124       string(LENGTH "${desc}" l)
125     else()
126       string(LENGTH "${f}" l)
127     endif()
128     if(l GREATER ${${type}_len})
129       set(${type}_len ${l})
130       set(${type}_space "${desc}")
131     endif()
132   endforeach()
133   string(REGEX REPLACE "." " " OPTION_space "${OPTION_space}")
134   string(REGEX REPLACE "." " " PATH_space "${PATH_space}")
135   string(REGEX REPLACE "." " " COMPONENT_space "${COMPONENT_space}")
136   foreach(f ${features})
137     get_property(desc GLOBAL PROPERTY FOAM_FEATURES_${f}_COMMENT)
138     get_property(type GLOBAL PROPERTY FOAM_FEATURES_${f}_TYPE)
139     if(NOT type STREQUAL "COMPONENT")
140       string(LENGTH "${desc}" l)
141     else()
142       string(LENGTH "${f}" l)
143     endif()
144     math(EXPR l "${${type}_len} - ${l}")
145     string(SUBSTRING "${${type}_space}" 0 ${l} s)
146     if(type STREQUAL "OPTION")
147       if(${${f}})
148         set(value ON)
149       else()
150         set(value OFF)
151       endif()
152     else()
153       set(value "${${f}}")
154     endif()
155     if(NOT type STREQUAL "COMPONENT")
156       set(${type}_msg "${${type}_msg}\n     ${desc}${s}  ${value}")
157     else()
158       set(${type}_msg "${${type}_msg}\n     ${f}${s}  ${desc}")
159     endif()
160   endforeach()
161   message(STATUS "${OPTION_msg}\n")
162   message(STATUS "${PATH_msg}\n")
163   message(STATUS "${COMPONENT_msg}\n")
164 endfunction()
166 function(foam_target_link_libraries target)
167   set(libs "${ARGN}")
168   target_link_libraries(${target} ${libs})
169   get_property(tp_libs GLOBAL PROPERTY FOAM_THIRDPARTY_LIBRARIES)
170   if(tp_libs)
171     list(REMOVE_ITEM libs ${tp_libs})
172     set_target_properties(${target} PROPERTIES
173       LINK_INTERFACE_LIBRARIES "${libs}")
174   endif()
175 endfunction()
177 function(foam_add_tutorial_tests)
178   if(NOT BUILD_TESTING)
179     return()
180   endif()
181   set(allrun_script
182     "${CMAKE_CURRENT_BINARY_DIR}/Allrun${FOAM_PY_SCRIPT_SUFFIX}")
183   if(NOT EXISTS ${allrun_script})
184     message(FATAL_ERROR
185       "No Allrun script configured in ${CMAKE_CURRENT_SOURCE_DIR}")
186     return()
187   endif()
188   execute_process(
189     COMMAND "${PYTHON_EXECUTABLE}" "${allrun_script}" --cases
190     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
191     RESULT_VARIABLE result
192     OUTPUT_VARIABLE cases
193     ERROR_VARIABLE errmsg
194     OUTPUT_STRIP_TRAILING_WHITESPACE
195     )
196   if(result)
197     message(SEND_ERROR "Error running Allrun --cases:\n${errmsg}")
198     return()
199   endif()
200   string(REPLACE "\n" ";" cases "${cases}")
201   set(last_c)
202   if(NOT FOAM_ENABLE_FULL_TUTORIAL_TESTS)
203     set(testarg --test)
204   else()
205     set(testarg)
206   endif()
207   foreach(c ${cases})
208     add_test(NAME ${c} COMMAND ${PYTHON_EXECUTABLE} ${allrun_script}
209       ${testarg} --verbose ${c})
210     if(last_c)
211       set_tests_properties(${c} PROPERTIES DEPENDS ${last_c})
212     endif()
213     set(last_c ${c})
214   endforeach()
215   foreach(c ${cases})
216     add_test(NAME ${c}_clean COMMAND ${PYTHON_EXECUTABLE} ${allrun_script}
217       --clean)
218     set_tests_properties(${c}_clean PROPERTIES DEPENDS ${last_c})
219   endforeach()
220 endfunction()
222 function(foam_determine_python_version VERSION_VAR EXECUTABLE)
223   execute_process(
224     COMMAND ${EXECUTABLE} -c
225     "import sys; sys.stdout.write('%s.%s.%s\\n'%sys.version_info[0:3])"
226     RESULT_VARIABLE version_result
227     OUTPUT_VARIABLE version_output
228     ERROR_VARIABLE version_output
229     OUTPUT_STRIP_TRAILING_WHITESPACE
230     )
231   if(NOT version_result)
232     string(REGEX MATCH "([0-9]+\\.)+[0-9]+" version
233       "${version_output}")
234     message(STATUS "Python version ${version} found")
235   else()
236     set(version ${VERSION_VAR}-NOTFOUND)
237     message(SEND_ERROR
238       "Failed to determine the python version: ${version_output}")
239   endif()
240   set(${VERSION_VAR} ${version} PARENT_SCOPE)
241 endfunction()
243 # ------------------------- vim: set sw=2 sts=2 et: --------------- end-of-file