Merge branch 'upstream/OpenFOAM' into master
[freefoam.git] / CMake / FFThirdPartyUtils.cmake
blob209d46cbfddc139033241239a612ea7e4039263b
1 #-------------------------------------------------------------------------------
2 #                ______             ______ ____          __  __
3 #               |  ____|           |  ____/ __ \   /\   |  \/  |
4 #               | |__ _ __ ___  ___| |__ | |  | | /  \  | \  / |
5 #               |  __| '__/ _ \/ _ \  __|| |  | |/ /\ \ | |\/| |
6 #               | |  | | |  __/  __/ |   | |__| / ____ \| |  | |
7 #               |_|  |_|  \___|\___|_|    \____/_/    \_\_|  |_|
9 #                   FreeFOAM: The Cross-Platform CFD Toolkit
11 # Copyright (C) 2008-2009 Michael Wild <themiwi@users.sf.net>
12 #                         Gerber van der Graaf <gerber_graaf@users.sf.net>
13 #-------------------------------------------------------------------------------
14 # License
15 #   This file is part of FreeFOAM.
17 #   FreeFOAM is free software; you can redistribute it and/or modify it
18 #   under the terms of the GNU General Public License as published by the
19 #   Free Software Foundation; either version 2 of the License, or (at your
20 #   option) any later version.
22 #   FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
23 #   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 #   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25 #   for more details.
27 #   You should have received a copy of the GNU General Public License
28 #   along with FreeFOAM; if not, write to the Free Software Foundation,
29 #   Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #-------------------------------------------------------------------------------
32 # macro to download a dist file if necessary
33 function( ff_download_distfile URL FILE MD5 )
34   # find out whether we need to download the dist file
35   set( MUST_DOWNLOAD FALSE )
36   if( NOT EXISTS ${FILE} )
37     message( STATUS "Could not find the ${FILE} tarball. Will download it now." )
38     set( MUST_DOWNLOAD TRUE )
39   else( NOT EXISTS ${FILE} )
40     execute_process(
41       COMMAND ${CMAKE_COMMAND} -E md5sum ${FILE}
42       OUTPUT_VARIABLE MD5_computed
43       OUTPUT_STRIP_TRAILING_WHITESPACE
44       )
45     separate_arguments( MD5_computed )
46     list( GET MD5_computed 0 MD5_computed )
47     if( NOT MD5_computed STREQUAL ${MD5} )
48       message( STATUS "MD5SUM mismatch of ${FILE}. Expected ${MD5}, actual ${MD5_computed}. Will redownload it." )
49       set( MUST_DOWNLOAD TRUE )
50     endif( NOT MD5_computed STREQUAL ${MD5} )
51   endif( NOT EXISTS ${FILE} )
53   # if necessary download distfile
54   if( MUST_DOWNLOAD )
55     message( STATUS "Downloading ${FILE}" )
56     # if it is https, file( DOWNLOAD ... ) won't do
57     set( DL_STAT 1 )
58     set( DL_ERR "No suitable program to download ${URL} to ${FILE} found." )
59     if( URL MATCHES "^https://" )
60       find_package( Wget )
61       if( WGET_FOUND )
62         execute_process(
63           COMMAND ${WGET_EXECUTABLE} --no-check-certificate ${URL} -O ${FILE}
64           RESULT_VARIABLE DL_STAT
65           ERROR_VARIABLE DL_ERR
66           )
67       else( WGET_FOUND )
68         find_program( CURL_EXECUTABLE curl )
69         mark_as_advanced( CURL_EXECUTABLE )
70         if( CURL_EXECUTABLE )
71           execute_process(
72             COMMAND ${CURL_EXECUTABLE} --insecure ${URL} -o ${FILE}
73             RESULT_VARIABLE DL_STAT
74             ERROR_VARIABLE DL_ERR
75             )
76         endif( CURL_EXECUTABLE )
77       endif( WGET_FOUND )
78     else( URL MATCHES "^https://" )
79       file( DOWNLOAD ${URL} ${FILE} STATUS DL_STAT )
80       list( GET DL_STAT 1 DL_ERR )
81       list( GET DL_STAT 0 DL_STAT )
82     endif( URL MATCHES "^https://" )
83     if( DL_STAT )
84       message( SEND_ERROR "
85 Failed to download ${FILE} from ${URL}.
86 The error message was: ${DL_ERR}
87 One obvious reason might be that you are not connected to the
88 internet, so you might want to first check that. Another possible
89 reason for the failure might be that in its default configuration
90 CMake does not support all common communication protocols (in
91 particular not https).  Also, if it has been enabled to use a
92 download library which actually supports SSL encryption, the
93 download might still fail if the security certificate is
94 self-signed.  Please try to download
95   ${URL}
96 manually and put the file into this place:
97   ${FILE}
98 You can either achieve this with your web browser or on a Unix like
99 system (such as Linux or Mac OS X) you can try to use curl or wget.
100 If your system has wget installed, type
101   wget --no-check-certificate ${URL} -O ${FILE}
102 on a single line. If, instead you have curl installed, you can use
103   curl --insecure ${URL} -o ${FILE}
104 Then restart the CMake configuration process. It should pick up
105 where it left off.
106 " )
107     endif( DL_STAT )
108   endif( MUST_DOWNLOAD )
109 endfunction( ff_download_distfile )
111 function( ff_unpack_distfile FILE DIR STAMPVAR )
112   # construct stamp file name
113   get_filename_component( stamp ${FILE} NAME )
114   set( stamp ${CMAKE_CURRENT_BINARY_DIR}/unpack-${stamp}.stamp )
115   # add the custom command
116   add_custom_command(
117     OUTPUT ${stamp}
118     COMMAND ${CMAKE_COMMAND} -E tar xzf ${FILE}
119     COMMAND ${CMAKE_COMMAND} -E touch ${stamp}
120     DEPENDS ${FILE}
121     WORKING_DIRECTORY ${DIR}
122     COMMENT "Unpacking ${FILE}"
123     VERBATIM
124     )
125   set( ${STAMPVAR} ${stamp} PARENT_SCOPE )
126 endfunction( ff_unpack_distfile )
128 function( ff_apply_patches PATCHES DIR DEPENDFILE STAMPVAR )
129   # we need patch
130   find_program( PATCH_EXECUTABLE patch )
131   if( NOT PATCH_EXECUTABLE )
132     message( SEND_ERROR "Required program `patch' not found" )
133   endif( NOT PATCH_EXECUTABLE )
134   file( TO_NATIVE_PATH ${PATCH_EXECUTABLE} PATCH_EXECUTABLE )
136   set( stamps )
138   foreach( patch ${PATCHES} )
139     # construct the stamp file name
140     get_filename_component( stamp ${patch} NAME )
141     set( stamp ${CMAKE_CURRENT_BINARY_DIR}/applied-${stamp}.stamp )
142     list( APPEND stamps ${stamp} )
144     # construct native, absolute path to patch file
145     get_filename_component( patch ${patch} ABSOLUTE )
146     file( TO_NATIVE_PATH ${patch} patch )
148     # construct patch command line
149     if(WIN32)
150       set(PATCH_COMMAND_LINE "${PATCH_EXECUTABLE}" -p1 --binary -N -i "${patch}")
151     else()
152       set(PATCH_COMMAND_LINE "${PATCH_EXECUTABLE}" -p1 -N -i "${patch}")
153     endif()
155     # add the command to apply the patch
156     add_custom_command( OUTPUT ${stamp}
157       COMMAND ${PATCH_COMMAND_LINE}
158       COMMAND ${CMAKE_COMMAND} -E touch ${stamp}
159       DEPENDS ${DEPENDFILE}
160       WORKING_DIRECTORY ${DIR}
161       COMMENT "Applying patch ${patch}"
162       VERBATIM
163       )
164   endforeach( patch )
165   set( ${STAMPVAR} ${stamps} PARENT_SCOPE )
166 endfunction( ff_apply_patches )
168 function( ff_download_unpack_patch_package NAME STAMPVAR )
169   set( url ${${NAME}_url} )
170   set( tarball ${CMAKE_CURRENT_BINARY_DIR}/${${NAME}_tarball} )
171   set( md5 ${${NAME}_md5} )
172   set( parentdir ${CMAKE_CURRENT_BINARY_DIR} )
173   set( directory ${${NAME}_directory} )
174   set( patches ${${NAME}_patches} )
175   ff_download_distfile( ${url} ${tarball} ${md5} )
176   ff_unpack_distfile( ${tarball} ${parentdir} ${NAME}_unpack_stamp )
177   ff_apply_patches( "${patches}" ${directory} ${${NAME}_unpack_stamp}
178     ${NAME}_patch_stamps )
179   set( ${STAMPVAR}
180     ${${NAME}_unpack_stamp} ${${NAME}_patch_stamps} PARENT_SCOPE )
181 endfunction( ff_download_unpack_patch_package )
183 # ------------------------- vim: set sw=2 sts=2 et: --------------- end-of-file