ENH: Don't patch metis 5.0.1
[freefoam.git] / CMake / FOAMThirdPartyUtilities.cmake
blob221204eee0c0b4ec394a039ac08eae632484f261
1 # - Utilities to build Third-Party libraries.
3 #  FOAM_THIRDPARTY_OPTION(<name> <description> <pkgname> <optional> <enabled>)
5 # Offers the user with options related to a third-party library. If <optional>
6 # is TRUE, the option FOAM_<UPPER_name>_ENABLED is created with the initial
7 # value <enabled> and the description given in <description>. If <optional>
8 # is FALSE, the option is not generated, and <description> can be the empty
9 # string and the value of <enabled> is meaningless. The function then first
10 # tries to find the package <pkgname> using FIND_PACKAGE(). Depending on the
11 # result, the function creates the option FOAM_BUILD_PRIVATE_<UPPER_name>
12 # with the default value set to FALSE if FIND_PACKAGE() found <pkgname>,
13 # or TRUE otherwise. Lastly an entry in the feature-summary table is made.
14 # All remaining arguments are considered to be variables that have to be
15 # purged using FOAM_DEPENDENT_VARIABLES() when FOAM_BUILD_PRIVATE_<UPPER_name>
16 # changes its value.
18 #  FOAM_EXTERNAL_PROJECT_ADD(<name> [options ...])
20 # Like ExternalProject_Add() with simplified patching, MD5 checking and cache
21 # initialization. You can use the PATCH_FILE <patch> option to specify one
22 # patch file which is to be applied to the sources. Note that patch must apply
23 # with the patch-option -p1.  If you want to compare a download against an MD5
24 # sum, use the MD5SUM <file> <sum> option to specify a file to check and the
25 # check-sum. The former should be a base-name, the download directory will be
26 # added automatically. CACHE_INIT can be used to specify a cache-initialization
27 # script.
29 #  FOAM_WRITE_CACHE_INIT(<fname> [<varname1> ...])
31 # Write a cache initializer file <fname> that can be used for external
32 # projects. The file will contain SET() commands that pre-populate the cache of
33 # the external project with the variables <varname1>...
36 #-------------------------------------------------------------------------------
37 #               ______                _     ____          __  __
38 #              |  ____|             _| |_  / __ \   /\   |  \/  |
39 #              | |__ _ __ ___  ___ /     \| |  | | /  \  | \  / |
40 #              |  __| '__/ _ \/ _ ( (| |) ) |  | |/ /\ \ | |\/| |
41 #              | |  | | |  __/  __/\_   _/| |__| / ____ \| |  | |
42 #              |_|  |_|  \___|\___|  |_|   \____/_/    \_\_|  |_|
44 #                   FreeFOAM: The Cross-Platform CFD Toolkit
46 # Copyright (C) 2008-2011 Michael Wild <themiwi@users.sf.net>
47 #                         Gerber van der Graaf <gerber_graaf@users.sf.net>
48 #-------------------------------------------------------------------------------
49 # License
50 #   This file is part of FreeFOAM.
52 #   FreeFOAM is free software; you can redistribute it and/or modify it
53 #   under the terms of the GNU General Public License as published by the
54 #   Free Software Foundation; either version 2 of the License, or (at your
55 #   option) any later version.
57 #   FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
58 #   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
59 #   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
60 #   for more details.
62 #   You should have received a copy of the GNU General Public License
63 #   along with FreeFOAM; if not, write to the Free Software Foundation,
64 #   Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
66 # Description
67 #   Functions to build thirdparty libraries
69 #-------------------------------------------------------------------------------
71 # helper variables
72 set(_foam_sf_master "http://master.dl.sourceforge.net/project")
73 set(_foam_sf_url "${_foam_sf_master}/freefoam")
75 macro(foam_thirdparty_option name description
76     pkgname optional enable)
77   set(_tpo_name "${name}")
78   set(_tpo_description "${description}")
79   set(_tpo_pkgname "${pkgname}")
80   set(_tpo_optional "${optional}")
81   set(_tpo_enable "${enable}")
82   string(TOUPPER ${_tpo_name} _tpo_upper_name)
83   string(TOUPPER ${_tpo_pkgname} _tpo_upper_pkgname)
84   if(_tpo_optional)
85     foam_option(FOAM_ENABLE_${_tpo_upper_name} "Enable ${_tpo_name}"
86       "${_tpo_description}" ${_tpo_enable})
87   endif()
88   if(FOAM_ENABLE_${_tpo_upper_name} OR NOT _tpo_optional)
89     find_package(${_tpo_pkgname} QUIET)
90     if(${_tpo_upper_pkgname}_FOUND)
91       set(_tpo_private OFF)
92     else()
93       set(_tpo_private ON)
94     endif()
95     foam_option(FOAM_BUILD_PRIVATE_${_tpo_upper_name}
96       "Build private ${_tpo_name}"
97       "Download and compile ${_tpo_name} instead of searching in on the system"
98       ${_tpo_private})
99     foam_dependent_variables(FOAM_BUILD_PRIVATE_${_tpo_upper_name}
100       ${_tpo_upper_name}_INCLUDE_DIR ${_tpo_upper_name}_LIBRARY ${ARGN})
101     if(FOAM_BUILD_PRIVATE_${_tpo_upper_name})
102       foam_build_thirdparty(${_tpo_upper_name})
103     elseif(NOT ${_tpo_upper_pkgname}_FOUND)
104       # just to get the not-found-message on the screen
105       find_package(${_tpo_pkgname})
106       set(_tpo_msg
107         " If you have ${_tpo_name} installed, edit the variables beginning"
108         " with ${_tpo_upper_name}_ to refer to the installation or enable the"
109         " setting FOAM_BUILD_PRIVATE_${_tpo_upper_name} in the cache.\n"
110         )
111       if(_tpo_optional)
112         message(SEND_ERROR
113           "FOAM_ENABLE_${_tpo_upper_name} is TRUE, but ${_tpo_name} cannot be"
114           " found." ${_tpo_msg}
115           )
116       else()
117         message(SEND_ERROR
118           "${_tpo_name} is required but cannot be found.\n"
119           ${_tpo_msg}
120           )
121       endif()
122     else()
123       # just to get the found-message on the screen
124       find_package(${_tpo_pkgname})
125     endif()
126     mark_as_advanced(FOAM_BUILD_PRIVATE_${_tpo_upper_name})
127   endif()
128 endmacro()
130 include(ExternalProject)
132 find_program(PATCH_EXECUTABLE patch)
133 mark_as_advanced(PATCH_EXECUTABLE)
134 configure_file("${CMAKE_SOURCE_DIR}/CMake/foam_apply_patch.cmake.in"
135   "${CMAKE_BINARY_DIR}/CMake/foam_apply_patch.cmake" @ONLY)
137 function(foam_external_project_add name)
138   # argument parsing
139   foam_parse_arguments(_fepa
140     "CMAKE_ARGS;MD5SUM:2;CACHE_INIT:1;PATCH_FILE:1" "" "${ARGN}")
141   set(args "${_fepa_DEFAULT_ARGS}")
142   if(_fepa_PATCH_FILE)
143     if(NOT PATCH_EXECUTABLE)
144       message(SEND_ERROR "Required program `patch' not found")
145     endif()
146     list(APPEND args PATCH_COMMAND "${CMAKE_COMMAND}"
147       -DPATCH_FILE=${_fepa_PATCH_FILE}
148       -P "${CMAKE_BINARY_DIR}/CMake/foam_apply_patch.cmake")
149   endif()
150   if(_fepa_CMAKE_ARGS OR _fepa_CACHE_INIT)
151     list(APPEND args CMAKE_ARGS)
152     if(_fepa_CACHE_INIT)
153       list(APPEND args -C ${_fepa_CACHE_INIT})
154     endif()
155     list(APPEND args ${_fepa_CMAKE_ARGS})
156   endif()
157   ExternalProject_Add(${name} "${args}")
158   if(_fepa_MD5SUM)
159     list(GET _fepa_MD5SUM 0 md5file)
160     list(GET _fepa_MD5SUM 1 md5sum)
161     if(NOT md5file OR NOT md5sum)
162       message(SEND_ERROR "MD5SUM requires file and md5sum arguments")
163     endif()
164     ExternalProject_Get_Property(${name} download_dir stamp_dir)
165     file(WRITE "${stamp_dir}/md5check-${name}.cmake"
166 "execute_process(
167   COMMAND \"${CMAKE_COMMAND}\" -E md5sum \"${download_dir}/${md5file}\"
168   OUTPUT_VARIABLE md5sum_computed
169   OUTPUT_STRIP_TRAILING_WHITESPACE
170   )
171 string(REGEX REPLACE \" +.*$\" \"\" md5sum_computed \${md5sum_computed})
172 if(NOT md5sum_computed STREQUAL \"${md5sum}\")
173   message(FATAL_ERROR \"error: Mismatching MD5 sum for
174 ${download_dir}/${md5file}
175 Expected ${md5sum}
176 Got      \${md5sum_computed}\")
177 endif()
179     ExternalProject_Add_Step(${name} md5check
180       COMMENT "Checking MD5 sum for '${name}'"
181       COMMAND "${CMAKE_COMMAND}" -P "${stamp_dir}/md5check-${name}.cmake"
182       WORKING_DIRECTORY ${download_dir}
183       DEPENDEES download
184       DEPENDERS patch
185       )
186   endif()
187 endfunction()
189 function(_foam_append_init_var strvar)
190   foreach(varname ${ARGN})
191     get_property(type CACHE ${varname} PROPERTY TYPE)
192     get_property(help CACHE ${varname} PROPERTY HELPSTRING)
193     if(NOT type)
194       set(type STRING)
195     endif()
196     if(NOT help)
197       set(help "Set by CMake")
198     endif()
199     set(${strvar}
200       "${${strvar}}\nset(${varname} \"${${varname}}\" CACHE ${type} \"${help}\")")
201   endforeach()
202   set(${strvar} "${${strvar}}" PARENT_SCOPE)
203 endfunction()
205 function(foam_write_cache_init fname)
206   set(FOAM_CONFIGURABLE_CONTENT)
207   if(CMAKE_CONFIGURATION_TYPES)
208     string(TOUPPER "${CMAKE_CONFIGURATION_TYPES}" config_types)
209   else()
210     set(config_types DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
211   endif()
212   foreach(lang C CXX)
213     # make sure that the static libraries are suitable to be used in shared
214     # libraries
215     set(CMAKE_${lang}_FLAGS "${CMAKE_SHARED_LIBRARY_${lang}_FLAGS}")
216     _foam_append_init_var(FOAM_CONFIGURABLE_CONTENT
217       CMAKE_${lang}_COMPILER CMAKE_${lang}_FLAGS)
218     foreach(config ${config_types})
219       _foam_append_init_var(FOAM_CONFIGURABLE_CONTENT
220         CMAKE_${lang}_FLAGS_${config})
221     endforeach()
222   endforeach()
223   foreach(type EXE MODULE SHARED)
224     _foam_append_init_var(FOAM_CONFIGURABLE_CONTENT CMAKE_${type}_LINKER_FLAGS)
225     foreach(config ${config_types})
226       _foam_append_init_var(FOAM_CONFIGURABLE_CONTENT
227         CMAKE_${type}_LINKER_FLAGS_${config})
228     endforeach()
229   endforeach()
230   _foam_append_init_var(FOAM_CONFIGURABLE_CONTENT
231     A2X_EXECUTABLE ASCIIDOC_EXECUTABLE BISON_EXECUTABLE CMAKE_AR
232     CMAKE_BUILD_TYPE CMAKE_COLOR_MAKEFILE CMAKE_HAVE_HP_CMA CMAKE_HAVE_SPROC_H
233     CMAKE_HAVE_THR_CREATE CMAKE_INSTALL_NAME_TOOL CMAKE_LINKER
234     CMAKE_MAKE_PROGRAM CMAKE_NM CMAKE_OBJCOPY CMAKE_OBJDUMP
235     CMAKE_OSX_ARCHITECTURES CMAKE_OSX_DEPLOYMENT_TARGET CMAKE_OSX_SYSROOT
236     CMAKE_RANLIB CMAKE_SKIP_RPATH CMAKE_STRIP CMAKE_USE_RELATIVE_PATHS
237     CMAKE_VERBOSE_MAKEFILE FLEX_CXX_FLAGS FLEX_EXECUTABLE FL_LIBRARY MPIEXEC
238     MPIEXEC_MAX_NUMPROCS MPIEXEC_NUMPROC_FLAG MPIEXEC_POSTFLAGS
239     MPIEXEC_PREFLAGS MPI_COMPILER MPI_COMPILE_FLAGS MPI_EXTRA_LIBRARY
240     MPI_INCLUDE_PATH MPI_LIBRARY MPI_LINK_FLAGS ZLIB_INCLUDE_DIR ZLIB_LIBRARY)
241   configure_file(
242     "${CMAKE_SOURCE_DIR}/CMake/FOAMConfigurableFile.in" "${fname}" @ONLY)
243 endfunction()
245 # build metis
246 function(foam_build_metis)
247   find_package(Threads REQUIRED)
248   _foam_ep_download_if_required(download_opts
249     "http://freefoam.sf.net/nonfree/metis-5.0.1.tar.gz"
250     "6daba5e64ed86d46e038437eda720532"
251     )
252   foam_write_cache_init("${CMAKE_BINARY_DIR}/ThirdParty/METIS/metis_init.cmake")
253   foam_external_project_add(METIS
254     PREFIX ${CMAKE_BINARY_DIR}/ThirdParty/METIS
255     ${download_opts}
256     CACHE_INIT "${CMAKE_BINARY_DIR}/ThirdParty/METIS/metis_init.cmake"
257     CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/ThirdParty/METIS"
258        "-DGKLIB_PATH:PATH=<SOURCE_DIR>/GKlib"
259     )
260   # create imported library
261   ExternalProject_Get_Property(METIS prefix)
262   add_library(foam_metis STATIC IMPORTED)
263   set_target_properties(foam_metis PROPERTIES
264     IMPORTED_LOCATION
265     "${prefix}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}metis${CMAKE_STATIC_LIBRARY_SUFFIX}"
266     )
267   # FindMETIS compatibility
268   set(METIS_INCLUDE_DIRS
269     ${CMAKE_BINARY_DIR}/ThirdParty/METIS/include PARENT_SCOPE)
270   set(METIS_LIBRARIES foam_metis PARENT_SCOPE)
271   set(METIS_LINK_FLAGS PARENT_SCOPE)
272   set(METIS_NEW_API TRUE PARENT_SCOPE)
273   # mark it for removal from the interface libraries by foam_install_targets
274   set_property(GLOBAL APPEND PROPERTY FOAM_THIRDPARTY_LIBRARIES foam_metis)
275 endfunction()
277 # build parmetis
278 function(foam_build_parmetis)
279   find_package(Threads REQUIRED)
280   _foam_ep_download_if_required(download_opts
281     "http://freefoam.sf.net/nonfree/ParMetis-3.1.tar.gz"
282     "15f252eabc397a8c2f56fa1f5ed91354"
283     )
284   foam_write_cache_init("${CMAKE_BINARY_DIR}/ThirdParty/ParMetis/parmetis_init.cmake")
285   foam_external_project_add(ParMetis
286     PREFIX ${CMAKE_BINARY_DIR}/ThirdParty/ParMetis
287     ${download_opts}
288     PATCH_FILE "${CMAKE_SOURCE_DIR}/ThirdParty/parmetis.patch"
289     CACHE_INIT "${CMAKE_BINARY_DIR}/ThirdParty/ParMetis/parmetis_init.cmake"
290     CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/ThirdParty/ParMetis"
291     )
292   # create imported library
293   ExternalProject_Get_Property(ParMetis prefix)
294   add_library(foam_parmetis STATIC IMPORTED)
295   set_target_properties(foam_parmetis PROPERTIES
296     IMPORTED_LOCATION
297     "${prefix}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}foam_parmetis${CMAKE_STATIC_LIBRARY_SUFFIX}"
298     )
299   # FindPARMETIS compatibility
300   set(PARMETIS_INCLUDE_DIRS
301     ${CMAKE_BINARY_DIR}/ThirdParty/ParMetis/include PARENT_SCOPE)
302   set(PARMETIS_LIBRARIES foam_parmetis ${MPI_LIBRARIES} PARENT_SCOPE)
303   set(PARMETIS_COMPILE_FLAGS "${MPIL_COMPILE_FLAGS}" PARENT_SCOPE)
304   set(PARMETIS_LINK_FLAGS "${MPIL_LINK_FLAGS}" PARENT_SCOPE)
305   # mark it for removal from the interface libraries by foam_install_targets
306   set_property(GLOBAL APPEND PROPERTY FOAM_THIRDPARTY_LIBRARIES foam_parmetis)
307 endfunction()
309 # build ccmio
310 function(foam_build_ccmio)
311   set(filename libccmio-2.6.1.tar.gz)
312   set(URL "http://freefoam.sf.net/nonfree/${filename}")
313   _foam_ep_download_if_required(download_opts
314     "${URL}"
315     "f81fbdfb960b1a4f3bcc7feee491efe4"
316     )
317   list(FIND download_opts DOWNLOAD_COMMAND download_cmd)
318   if(download_cmd EQUAL -1)
319     # the file is not local, remove the URL
320     set(is_local FALSE)
321     list(REMOVE_AT download_opts 0 1)
322     find_package(Wget)
323     find_program(CURL_EXECUTABLE curl)
324     mark_as_advanced(CURL_EXECUTABLE)
325     set(filepath "${CMAKE_BINARY_DIR}/ThirdParty/ccmio/src/${filename}")
326     if(WGET_FOUND)
327       list(INSERT download_opts 0 DOWNLOAD_COMMAND
328         "${WGET_EXECUTABLE}" --no-check-certificate -O "${filename}" ${URL})
329     elseif(CURL_EXECUTABLE)
330       list(INSERT download_opts 0 DOWNLOAD_COMMAND
331         "${CURL_EXECUTABLE}" --insecure -o "${filename}" ${URL})
332     else()
333       message(SEND_ERROR "Cannot download ${URL}\n"
334         "Please do so manually and place the file in\n"
335         "${CMAKE_SOURCE_DIR}/ThirdParty/${filename}")
336     endif()
337   else()
338     set(is_local TRUE)
339     set(filepath "${CMAKE_SOURCE_DIR}/ThirdParty/${filename}")
340   endif()
341   foam_write_cache_init("${CMAKE_BINARY_DIR}/ThirdParty/ccmio/ccmio_init.cmake")
342   foam_external_project_add(ccmio
343     PREFIX ${CMAKE_BINARY_DIR}/ThirdParty/ccmio
344     ${download_opts}
345     PATCH_FILE "${CMAKE_SOURCE_DIR}/ThirdParty/ccmio.patch"
346     CACHE_INIT "${CMAKE_BINARY_DIR}/ThirdParty/ccmio/ccmio_init.cmake"
347     CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/ThirdParty/ccmio"
348     )
349   # have to manually unpack..
350   ExternalProject_Get_Property(ccmio source_dir download_dir stamp_dir tmp_dir)
351   file(WRITE "${stamp_dir}/extract-ccmio.cmake"
352 "# Make file names absolute:
354 get_filename_component(tmp_dir \"${tmp_dir}\" ABSOLUTE)
355 get_filename_component(source_dir \"${source_dir}\" ABSOLUTE)
357 message(STATUS \"extracting...
358      src='${filepath}'
359      dst='\${source_dir}'\")
361 # Prepare a space for extracting:
363 set(i 1)
364 while(EXISTS \"\${tmp_dir}/extract\${i}\")
365   math(EXPR i \"\${i} + 1\")
366 endwhile()
367 set(ut_dir \"\${tmp_dir}/extract\${i}\")
368 file(MAKE_DIRECTORY \"\${ut_dir}\")
370 # Extract it:
372 message(STATUS \"extracting... [tar xzf]\")
373 execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E tar xzf ${filepath}
374   WORKING_DIRECTORY \${ut_dir}
375   RESULT_VARIABLE rv)
377 if(NOT rv EQUAL 0)
378   message(STATUS \"extracting... [error clean up]\")
379   file(REMOVE_RECURSE \"\${ut_dir}\")
380   message(FATAL_ERROR \"error: extract of '${filepath}' failed\")
381 endif()
383 # Analyze what came out of the tar file:
385 message(STATUS \"extracting... [analysis]\")
386 file(GLOB contents \"\${ut_dir}/*\")
387 list(LENGTH contents n)
388 if(NOT n EQUAL 1 OR NOT IS_DIRECTORY \"\${contents}\")
389   set(contents \"\${ut_dir}\")
390 endif()
392 # Copy \"the one\" directory to the final directory:
394 message(STATUS \"extracting... [copy]\")
395 file(COPY \"\${contents}/\" DESTINATION \${source_dir})
397 # Clean up:
399 message(STATUS \"extracting... [clean up]\")
400 file(REMOVE_RECURSE \"\${ut_dir}\")
402 message(STATUS \"extracting... done\")
405   if(is_local)
406     set(md5file-dep)
407     set(md5check)
408   else()
409     set(md5file-dep DEPENDS "${download_dir}/${md5file}")
410     set(md5check ${md5check})
411   endif()
412   ExternalProject_Add_Step(ccmio unpack
413     COMMENT "Performing extraction step for 'ccmio'"
414     COMMAND "${CMAKE_COMMAND}" -P "${stamp_dir}/extract-ccmio.cmake"
415     WORKING_DIRECTORY ${download_dir}
416     DEPENDEES download ${md5check}
417     DEPENDERS patch
418     ${md5file-dep}
419     )
420   # create imported library
421   ExternalProject_Get_Property(ccmio prefix)
422   add_library(foam_ccmio STATIC IMPORTED)
423   set_target_properties(foam_ccmio PROPERTIES
424     IMPORTED_LOCATION
425     "${prefix}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}foam_ccmio${CMAKE_STATIC_LIBRARY_SUFFIX}"
426     )
427   # FindCcmio compatibility
428   set(CCMIO_INCLUDE_DIRS
429     ${CMAKE_BINARY_DIR}/ThirdParty/ccmio/include PARENT_SCOPE)
430   set(CCMIO_LIBRARIES foam_ccmio PARENT_SCOPE)
431   # mark it for removal from the interface libraries by foam_install_targets
432   set_property(GLOBAL APPEND PROPERTY FOAM_THIRDPARTY_LIBRARIES foam_ccmio)
433 endfunction()
435 # build mgridgen
436 function(foam_build_mgridgen)
437   _foam_ep_download_if_required(download_opts
438     "http://freefoam.sf.net/nonfree/ParMGridGen-1.0.tar.gz"
439     "2872fa95b7fb91d6bd525490eed62038"
440     )
441   foam_write_cache_init("${CMAKE_BINARY_DIR}/ThirdParty/MGridGen/mgridgen_init.cmake")
442   foam_external_project_add(MGridGen
443     PREFIX ${CMAKE_BINARY_DIR}/ThirdParty/MGridGen
444     ${download_opts}
445     PATCH_FILE "${CMAKE_SOURCE_DIR}/ThirdParty/mgridgen.patch"
446     CACHE_INIT "${CMAKE_BINARY_DIR}/ThirdParty/MGridGen/mgridgen_init.cmake"
447     CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/ThirdParty/MGridGen"
448     )
449   # create imported library
450   ExternalProject_Get_Property(MGridGen prefix)
451   add_library(foam_mgridgen STATIC IMPORTED)
452   set_target_properties(foam_mgridgen PROPERTIES
453     IMPORTED_LOCATION
454     "${prefix}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}foam_mgridgen${CMAKE_STATIC_LIBRARY_SUFFIX}"
455     )
456   # FindMGRIDGEN compatibility
457   set(MGRIDGEN_INCLUDE_DIRS
458     ${CMAKE_BINARY_DIR}/ThirdParty/MGridGen/include PARENT_SCOPE)
459   set(MGRIDGEN_LIBRARIES foam_mgridgen PARENT_SCOPE)
460   set(PARMETIS_LINK_FLAGS PARENT_SCOPE)
461   # mark it for removal from the interface libraries by foam_install_targets
462   set_property(GLOBAL APPEND PROPERTY FOAM_THIRDPARTY_LIBRARIES foam_mgridgen)
463 endfunction()
465 # build scotch
466 function(foam_build_scotch)
467   find_package(BISON REQUIRED)
468   find_package(FLEX REQUIRED)
469   find_package(Threads)
470   _foam_ep_download_if_required(download_opts
471     "${_foam_sf_url}/ThirdParty/scotch/scotch_5.1.7.dfsg.orig.tar.gz"
472     "53d9715e11ae8e4cec1a48e8859e817f"
473     )
474   foam_write_cache_init("${CMAKE_BINARY_DIR}/ThirdParty/scotch/scotch_init.cmake")
475   foam_external_project_add(scotch
476     PREFIX ${CMAKE_BINARY_DIR}/ThirdParty/scotch
477     ${download_opts}
478     PATCH_FILE "${CMAKE_SOURCE_DIR}/ThirdParty/scotch.patch"
479     CACHE_INIT "${CMAKE_BINARY_DIR}/ThirdParty/scotch/scotch_init.cmake"
480     CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/ThirdParty/scotch"
481     )
482   # create imported library
483   ExternalProject_Get_Property(scotch prefix)
484   add_library(foam_scotch STATIC IMPORTED)
485   set_target_properties(foam_scotch PROPERTIES
486     IMPORTED_LOCATION
487     "${prefix}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}foam_scotch${CMAKE_STATIC_LIBRARY_SUFFIX}"
488     )
489   # FindSCOTCH compatibility
490   set(SCOTCH_INCLUDE_DIRS
491     ${CMAKE_BINARY_DIR}/ThirdParty/scotch/include PARENT_SCOPE)
492   set(SCOTCH_LIBRARIES foam_scotch PARENT_SCOPE)
493   # mark it for removal from the interface libraries by foam_install_targets
494   set_property(GLOBAL APPEND PROPERTY FOAM_THIRDPARTY_LIBRARIES foam_scotch)
495 endfunction()
497 # build zlib
498 function(foam_build_zlib)
499   _foam_ep_download_if_required(download_opts
500     "${_foam_sf_url}/ThirdParty/zlib/zlib-1.2.5.tar.gz"
501     "c735eab2d659a96e5a594c9e8541ad63"
502     )
503   foam_write_cache_init("${CMAKE_BINARY_DIR}/ThirdParty/zlib/zlib_init.cmake")
504   foam_external_project_add(ZLIB
505     PREFIX ${CMAKE_BINARY_DIR}/ThirdParty/zlib
506     ${download_opts}
507     PATCH_COMMAND "${CMAKE_COMMAND}" -E remove -f zconf.h
508     CACHE_INIT "${CMAKE_BINARY_DIR}/ThirdParty/zlib/zlib_init.cmake"
509     CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/ThirdParty/zlib"
510                "-DBUILD_SHARED_LIBS=OFF"
511     )
512   # create imported library
513   ExternalProject_Get_Property(ZLIB prefix)
514   add_library(foam_zlib STATIC IMPORTED)
515   set_target_properties(foam_zlib PROPERTIES
516     IMPORTED_LOCATION
517     "${prefix}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}z${CMAKE_STATIC_LIBRARY_SUFFIX}"
518     )
519   # FindZLIB compatibility
520   set(ZLIB_INCLUDE_DIRS
521     ${CMAKE_BINARY_DIR}/ThirdParty/zlib/include PARENT_SCOPE)
522   set(ZLIB_LIBRARIES foam_zlib PARENT_SCOPE)
523   # mark it for removal from the interface libraries by foam_install_targets
524   set_property(GLOBAL APPEND PROPERTY FOAM_THIRDPARTY_LIBRARIES foam_zlib)
525 endfunction()
527 # build MathJax
528 function(foam_build_mathjax)
529   set(install_dir ${CMAKE_BINARY_DIR}/doc/MathJax)
530   _foam_ep_download_if_required(download_opts
531     "${_foam_sf_master}/mathjax/MathJax/v1.0.1/MathJax-v1.0.1a.zip"
532     "b93c1f1e26a898faee072ab7aec57251"
533     )
534   foam_external_project_add(MathJax-PreFontUpdate
535     PREFIX "${CMAKE_BINARY_DIR}/ThirdParty/MathJax"
536     ${download_opts}
537     CONFIGURE_COMMAND ""
538     BUILD_COMMAND ""
539     INSTALL_DIR "${install_dir}"
540     INSTALL_COMMAND "${CMAKE_COMMAND}" -E copy_directory
541       <SOURCE_DIR> <INSTALL_DIR>
542     )
543   ExternalProject_Add_Step(MathJax-PreFontUpdate fontCleanup
544     COMMENT "Removing broken web fonts in MathJax"
545     COMMAND "${CMAKE_COMMAND}" -E remove_directory
546       "${install_dir}/MathJax/fonts/HTML-CSS/TeX/otf"
547     DEPENDEES install
548     )
549   _foam_ep_download_if_required(download_opts
550     "http://www.mathjax.org/dl/MathJax-Font-Update.zip"
551     "03ce6b5a62cfb9a8a8d205aea2e6997e"
552     )
553   # download & install font update
554   foam_external_project_add(MathJax-FontUpdate
555     DEPENDS MathJax-PreFontUpdate
556     PREFIX "${CMAKE_BINARY_DIR}/ThirdParty/MathJax-FontUpdate"
557     ${download_opts}
558     CONFIGURE_COMMAND ""
559     BUILD_COMMAND ""
560     INSTALL_DIR "${install_dir}/fonts/HTML-CSS/TeX/otf"
561     INSTALL_COMMAND "${CMAKE_COMMAND}" -E copy_directory
562       <SOURCE_DIR>/MathJax-Font-Update/otf <INSTALL_DIR>
563     )
564   add_custom_target(MathJax)
565   add_dependencies(MathJax MathJax-FontUpdate)
566   # FindMathJax compatibility
567   set(MATHJAX_DIR "${install_dir}" PARENT_SCOPE)
568 endfunction()
570 # build third-party library
571 macro(foam_build_thirdparty _btp_what)
572   if(${_btp_what} STREQUAL METIS)
573     foam_build_metis()
574   elseif(${_btp_what} STREQUAL PARMETIS)
575     foam_build_parmetis()
576   elseif(${_btp_what} STREQUAL CCMIO)
577     foam_build_ccmio()
578   elseif(${_btp_what} STREQUAL MGRIDGEN)
579     foam_build_mgridgen()
580   elseif(${_btp_what} STREQUAL SCOTCH)
581     foam_build_scotch()
582   elseif(${_btp_what} STREQUAL ZLIB)
583     foam_build_zlib()
584   elseif(${_btp_what} STREQUAL MATHJAX)
585     foam_build_mathjax()
586   else()
587     message(FATAL_ERROR "Unknown third-party package ${_btp_what}")
588   endif()
589 endmacro()
591 # Assemble download options for foam_external_project_add.
592 # If the file already exists in ${CMAKE_SOURCE_DIR}/ThirdParty and the MD5SUM
593 # matches, don't download it.
594 function(_foam_ep_download_if_required var url md5sum)
595   string(REGEX REPLACE "^.*/" "" filename "${url}")
596   set(filepath "${CMAKE_SOURCE_DIR}/ThirdParty/${filename}")
597   if(EXISTS "${filepath}")
598     execute_process(
599       COMMAND "${CMAKE_COMMAND}" -E md5sum "${filepath}"
600       OUTPUT_VARIABLE md5sum_computed
601       OUTPUT_STRIP_TRAILING_WHITESPACE
602       )
603     string(REGEX REPLACE " +.*$" "" md5sum_computed ${md5sum_computed})
604     if(md5sum_computed STREQUAL "${md5sum}")
605       # it's already there and the MD5 sum matches
606       set(${var} URL "${filepath}" DOWNLOAD_COMMAND "" PARENT_SCOPE)
607     else()
608       message(SEND_ERROR "error: Mismatching MD5 sum for\n"
609         "${filepath}\nExpected ${md5sum}\nGot      ${md5sum_computed}\n"
610         "Either re-download the file from\n${url}\n"
611         "or delete it and CMake will download it for you")
612     endif()
613   else()
614     # use ExternalCommand to download it
615     set(${var} URL "${url}" MD5SUM "${filename}" "${md5sum}" PARENT_SCOPE)
616   endif()
617 endfunction()
619 # ------------------------- vim: set sw=2 sts=2 et: --------------- end-of-file