Add StringInterner to FuncBuilder
[hiphop-php.git] / CMake / HPHPFunctions.cmake
blob9a3f47ddf36106300f0f4245d931799f4a10fcb9
1 if (${CMAKE_MAJOR_VERSION} GREATER 2)
2   cmake_policy(SET CMP0026 OLD)
3 endif()
5 include(GNUInstallDirs)
7 function(auto_sources RETURN_VALUE PATTERN SOURCE_SUBDIRS)
9   if ("${SOURCE_SUBDIRS}" STREQUAL "RECURSE")
10     SET(PATH ".")
11     if (${ARGC} EQUAL 4)
12       list(GET ARGV 3 PATH)
13     endif ()
14   endif()
16   if ("${SOURCE_SUBDIRS}" STREQUAL "RECURSE")
17     unset(${RETURN_VALUE})
18     file(GLOB SUBDIR_FILES "${PATH}/${PATTERN}")
19     list(APPEND ${RETURN_VALUE} ${SUBDIR_FILES})
21     file(GLOB subdirs RELATIVE ${PATH} ${PATH}/*)
23     foreach(DIR ${subdirs})
24       if (IS_DIRECTORY ${PATH}/${DIR})
25         if (NOT "${DIR}" STREQUAL "CMakeFiles")
26           file(GLOB_RECURSE SUBDIR_FILES "${PATH}/${DIR}/${PATTERN}")
27           list(APPEND ${RETURN_VALUE} ${SUBDIR_FILES})
28         endif()
29       endif()
30     endforeach()
31   else ()
32     file(GLOB ${RETURN_VALUE} "${PATTERN}")
34     foreach (PATH ${SOURCE_SUBDIRS})
35       file(GLOB SUBDIR_FILES "${PATH}/${PATTERN}")
36       list(APPEND ${RETURN_VALUE} ${SUBDIR_FILES})
37     endforeach(PATH ${SOURCE_SUBDIRS})
38   endif ()
40   if (${FILTER_OUT})
41     list(REMOVE_ITEM ${RETURN_VALUE} ${FILTER_OUT})
42   endif()
44   set(${RETURN_VALUE} ${${RETURN_VALUE}} PARENT_SCOPE)
45 endfunction(auto_sources)
47 macro(HHVM_SELECT_SOURCES DIR)
48   auto_sources(files "*.cpp" "RECURSE" "${DIR}")
49   foreach(f ${files})
50     if (NOT (${f} MATCHES "(ext_hhvm|/(old-)?tests?/)"))
51       list(APPEND CXX_SOURCES ${f})
52     endif()
53   endforeach()
54   auto_sources(files "*.c" "RECURSE" "${DIR}")
55   foreach(f ${files})
56     if (NOT (${f} MATCHES "(ext_hhvm|/(old-)?tests?/)"))
57       list(APPEND C_SOURCES ${f})
58     endif()
59   endforeach()
60   if (MSVC)
61     auto_sources(files "*.asm" "RECURSE" "${DIR}")
62     foreach(f ${files})
63       if (NOT (${f} MATCHES "(ext_hhvm|/(old-)?tests?/)"))
64         list(APPEND ASM_SOURCES ${f})
65       endif()
66     endforeach()
67   else()
68     auto_sources(files "*.S" "RECURSE" "${DIR}")
69     foreach(f ${files})
70       if (NOT (${f} MATCHES "(ext_hhvm|/(old-)?tests?/)"))
71         list(APPEND ASM_SOURCES ${f})
72       endif()
73     endforeach()
74   endif()
75   auto_sources(files "*.h" "RECURSE" "${DIR}")
76   foreach(f ${files})
77     if (NOT (${f} MATCHES "(/(old-)?tests?/)"))
78       list(APPEND HEADER_SOURCES ${f})
79     endif()
80   endforeach()
81 endmacro(HHVM_SELECT_SOURCES)
83 function(CONTAINS_STRING FILE SEARCH RETURN_VALUE)
84   file(STRINGS ${FILE} FILE_CONTENTS REGEX ".*${SEARCH}.*")
85   if (FILE_CONTENTS)
86     set(${RETURN_VALUE} True PARENT_SCOPE)
87   endif()
88 endfunction(CONTAINS_STRING)
90 macro(MYSQL_SOCKET_SEARCH)
91   execute_process(COMMAND mysql_config --socket OUTPUT_VARIABLE MYSQL_SOCK OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
92   if (NOT MYSQL_SOCK)
93     foreach (i
94       /var/run/mysqld/mysqld.sock
95       /var/tmp/mysql.sock
96       /var/run/mysql/mysql.sock
97       /var/lib/mysql/mysql.sock
98       /var/mysql/mysql.sock
99       /usr/local/mysql/var/mysql.sock
100       /Private/tmp/mysql.sock
101       /private/tmp/mysql.sock
102       /tmp/mysql.sock
103       )
104       if (EXISTS ${i})
105         set(MYSQL_SOCK ${i})
106         break()
107       endif()
108     endforeach()
109   endif()
110   if (MYSQL_SOCK)
111     set(MYSQL_UNIX_SOCK_ADDR ${MYSQL_SOCK} CACHE STRING "Path to MySQL Socket")
112   endif()
113 endmacro()
115 function(append_systemlib TARGET SOURCE SECTNAME)
116   if(MSVC)
117     list(APPEND ${TARGET}_SLIBS_NAMES "${SECTNAME}")
118     set(${TARGET}_SLIBS_NAMES ${${TARGET}_SLIBS_NAMES} PARENT_SCOPE)
119     list(APPEND ${TARGET}_SLIBS_SOURCES "${SOURCE}")
120     set(${TARGET}_SLIBS_SOURCES ${${TARGET}_SLIBS_SOURCES} PARENT_SCOPE)
121   else()
122     if (APPLE)
123       set(${TARGET}_SLIBS ${${TARGET}_SLIBS} -Wl,-sectcreate,__text,${SECTNAME},${SOURCE} PARENT_SCOPE)
124     else()
125       set(${TARGET}_SLIBS ${${TARGET}_SLIBS} "--add-section" "${SECTNAME}=${SOURCE}" PARENT_SCOPE)
126     endif()
127     # Add the systemlib file to the "LINK_DEPENDS" for the systemlib, this will cause it
128     # to be relinked and the systemlib re-embedded
129     set_property(TARGET ${TARGET} APPEND PROPERTY LINK_DEPENDS ${SOURCE})
130   endif()
131 endfunction(append_systemlib)
133 function(embed_sections TARGET DEST)
134   add_custom_command(TARGET ${TARGET} PRE_BUILD
135     # OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/generated-compiler-id.txt"
136     #        "${CMAKE_CURRENT_SOURCE_DIR}/generated-compiler-timestamp.txt"
137     #        "${CMAKE_CURRENT_SOURCE_DIR}/generated-repo-schema-id.txt"
138     #        "${CMAKE_CURRENT_SOURCE_DIR}/generated-build-id.txt"
139     COMMAND
140       "INSTALL_DIR=${CMAKE_BINARY_DIR}/hphp/util"
141       "SOURCE_ROOT=${CMAKE_SOURCE_DIR}"
142       "${HPHP_HOME}/hphp/hhvm/generate-buildinfo.sh"
143     WORKING_DIRECTORY "${HPHP_HOME}/hphp/util"
144     COMMENT "Generating Repo Schema ID and Compiler ID"
145     VERBATIM)
147   if (APPLE)
148     set(COMPILER_ID -Wl,-sectcreate,__text,"compiler_id","${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-id.txt")
149     set(COMPILER_TIMESTAMP -Wl,-sectcreate,__text,"compiler_ts","${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-timestamp.txt")
150     set(REPO_SCHEMA -Wl,-sectcreate,__text,"repo_schema_id","${CMAKE_BINARY_DIR}/hphp/util/generated-repo-schema-id.txt")
151     set(BUILD_ID -Wl,-sectcreate,__text,"build_id","${CMAKE_BINARY_DIR}/hphp/util/generated-build-id.txt")
152     target_link_libraries(${TARGET} ${${TARGET}_SLIBS} ${COMPILER_ID} ${COMPILER_TIMESTAMP} ${REPO_SCHEMA} ${BUILD_ID})
153   elseif(MSVC)
154     set(RESOURCE_FILE "#pragma code_page(1252)\n")
155     set(RESOURCE_FILE "${RESOURCE_FILE}LANGUAGE 0, 0\n")
156     set(RESOURCE_FILE "${RESOURCE_FILE}\n")
157     set(RESOURCE_FILE "${RESOURCE_FILE}#include \"${CMAKE_BINARY_DIR}/hphp/runtime/version.h\"\n")
158     file(READ "${CMAKE_BINARY_DIR}/hphp/hhvm/hhvm.rc" VERSION_INFO)
159     set(RESOURCE_FILE "${RESOURCE_FILE}compiler_id RCDATA \"${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-id.txt\"\n")
160     set(RESOURCE_FILE "${RESOURCE_FILE}compiler_ts RCDATA \"${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-timestamp.txt\"\n")
161     set(RESOURCE_FILE "${RESOURCE_FILE}repo_schema_id RCDATA \"${CMAKE_BINARY_DIR}/hphp/util/generated-repo-schema-id.txt\"\n")
162     set(RESOURCE_FILE "${RESOURCE_FILE}build_id RCDATA \"${CMAKE_BINARY_DIR}/hphp/util/generated-build-id.txt\"\n")
163     set(RESOURCE_FILE "${RESOURCE_FILE}${VERSION_INFO}\n")
164     set(i 0)
165     foreach (nm ${${TARGET}_SLIBS_NAMES})
166       list(GET ${TARGET}_SLIBS_SOURCES ${i} source)
167       set(RESOURCE_FILE "${RESOURCE_FILE}${nm} RCDATA \"${source}\"\n")
168       math(EXPR i "${i} + 1")
169     endforeach()
170     file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/embed.rc "${RESOURCE_FILE}")
171   else()
172     add_custom_command(TARGET ${TARGET} POST_BUILD
173       COMMAND "objcopy"
174       ARGS "--add-section" "compiler_id=${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-id.txt"
175            "--add-section" "compiler_ts=${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-timestamp.txt"
176            "--add-section" "repo_schema_id=${CMAKE_BINARY_DIR}/hphp/util/generated-repo-schema-id.txt"
177            "--add-section" "build_id=${CMAKE_BINARY_DIR}/hphp/util/generated-build-id.txt"
178            ${${TARGET}_SLIBS}
179            ${DEST}
180       DEPENDS "${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-id.txt"
181               "${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-timestamp.txt"
182               "${CMAKE_BINARY_DIR}/hphp/util/generated-repo-schema-id.txt"
183               "${CMAKE_BINARY_DIR}/hphp/util/generated-build-id.txt"
184       COMMENT "Embedding php in ${TARGET}")
185   endif()
186 endfunction(embed_sections)
188 macro(embed_systemlib_byname TARGET SLIB)
189   get_filename_component(SLIB_BN ${SLIB} "NAME_WE")
190   string(LENGTH ${SLIB_BN} SLIB_BN_LEN)
191   math(EXPR SLIB_BN_REL_LEN "${SLIB_BN_LEN} - 4")
192   string(SUBSTRING ${SLIB_BN} 4 ${SLIB_BN_REL_LEN} SLIB_EXTNAME)
193   string(MD5 SLIB_HASH_NAME ${SLIB_EXTNAME})
194   # Some platforms limit section names to 16 characters :(
195   string(SUBSTRING ${SLIB_HASH_NAME} 0 12 SLIB_HASH_NAME_SHORT)
196   if (MSVC)
197     # The dot would be causing the RC lexer to begin a number in the
198     # middle of our resource name, so use an underscore instead.
199     append_systemlib(${TARGET} ${SLIB} "ext_${SLIB_HASH_NAME_SHORT}")
200   else()
201     append_systemlib(${TARGET} ${SLIB} "ext.${SLIB_HASH_NAME_SHORT}")
202   endif()
203 endmacro()
205 function(embed_all_systemlibs TARGET ROOT DEST)
206   add_dependencies(${TARGET} systemlib)
207   append_systemlib(${TARGET} ${ROOT}/system/systemlib.php systemlib)
208   foreach(SLIB ${EXTENSION_SYSTEMLIB_SOURCES} ${EZC_SYSTEMLIB_SOURCES})
209     embed_systemlib_byname(${TARGET} ${SLIB})
210   endforeach()
211   embed_sections(${TARGET} ${DEST})
212 endfunction(embed_all_systemlibs)
214 # Custom install function that doesn't relink, instead it uses chrpath to change it, if
215 # it's available, otherwise, it leaves the chrpath alone
216 function(HHVM_INSTALL TARGET DEST)
217   get_target_property(LOC ${TARGET} LOCATION)
218   get_target_property(TY ${TARGET} TYPE)
219   if (FOUND_CHRPATH)
220     get_target_property(RPATH ${TARGET} INSTALL_RPATH)
221     if (NOT RPATH STREQUAL "RPATH-NOTFOUND")
222       if (RPATH STREQUAL "")
223         install(CODE "execute_process(COMMAND \"${CHRPATH}\" \"-d\" \"${LOC}\" ERROR_QUIET)")
224       else()
225         install(CODE "execute_process(COMMAND \"${CHRPATH}\" \"-r\" \"${RPATH}\" \"${LOC}\" ERROR_QUIET)")
226       endif()
227     endif()
228   endif()
229   string(TOUPPER ${DEST} DEST_UPPER)
230   install(CODE "FILE(INSTALL DESTINATION \"${CMAKE_INSTALL_FULL_${DEST_UPPER}DIR}\" TYPE ${TY} FILES \"${LOC}\")")
231 endfunction(HHVM_INSTALL)
233 function(HHVM_PUBLIC_HEADERS TARGET)
234   install(
235     CODE "INCLUDE(\"${HPHP_HOME}/CMake/HPHPFunctions.cmake\")
236       HHVM_INSTALL_HEADERS(${TARGET} ${HPHP_HOME}
237       \"${CMAKE_INSTALL_FULL_INCLUDEDIR}\" ${ARGN})"
238     COMPONENT dev)
239 endfunction()
241 function(HHVM_INSTALL_HEADERS TARGET SRCROOT DEST)
242   message(STATUS "Installing header files for ${TARGET}")
243   foreach(src_rel ${ARGN})
244     # Determine the relative directory name so that we can mirror the
245     # directory structure in the output
246     file(RELATIVE_PATH dest_rel ${SRCROOT} ${src_rel})
247     if (IS_ABSOLUTE ${dest_rel})
248       message(WARNING "${TARGET}: Header file ${dest_rel} is not inside ${SRCROOT}")
249     else()
250       string(FIND ${dest_rel} / slash_pos REVERSE)
251       if(slash_pos EQUAL -1)
252         set(dest_rel)
253       else()
254         string(SUBSTRING ${dest_rel} 0 ${slash_pos} dest_rel)
255       endif()
256       file(COPY ${src_rel}
257         DESTINATION "$ENV{DESTDIR}${DEST}/${dest_rel}"
258         NO_SOURCE_PERMISSIONS)
259     endif()
260   endforeach()
261 endfunction()
263 macro(HHVM_EXT_OPTION EXTNAME PACKAGENAME)
264   if (NOT DEFINED EXT_${EXTNAME})
265     # Implicit check
266     find_package(${PACKAGENAME})
267   elseif (EXT_${EXTNAME} STREQUAL "ON")
268     # Explicit check
269     find_package(${PACKAGENAME} REQUIRED)
270   endif()
271 endmacro()
273 # Remove all files matching a set of patterns, and,
274 # optionally, not matching a second set of patterns,
275 # from a set of lists.
277 # Example:
278 # This will remove all files in the CPP_SOURCES list
279 # matching "/test/" or "Test.cpp$", but not matching
280 # "BobTest.cpp$".
281 # HHVM_REMOVE_MATCHES_FROM_LISTS(CPP_SOURCES MATCHES "/test/" "Test.cpp$" IGNORE_MATCHES "BobTest.cpp$")
283 # Parameters:
285 # [...]:
286 # The names of the lists to remove matches from.
288 # [MATCHES ...]:
289 # The matches to remove from the lists.
291 # [IGNORE_MATCHES ...]:
292 # The matches not to remove, even if they match
293 # the main set of matches to remove.
294 function(HHVM_REMOVE_MATCHES_FROM_LISTS)
295   set(LISTS_TO_SEARCH)
296   set(MATCHES_TO_REMOVE)
297   set(MATCHES_TO_IGNORE)
298   set(argumentState 0)
299   foreach (arg ${ARGN})
300     if ("x${arg}" STREQUAL "xMATCHES")
301       set(argumentState 1)
302     elseif ("x${arg}" STREQUAL "xIGNORE_MATCHES")
303       set(argumentState 2)
304     elseif (argumentState EQUAL 0)
305       list(APPEND LISTS_TO_SEARCH ${arg})
306     elseif (argumentState EQUAL 1)
307       list(APPEND MATCHES_TO_REMOVE ${arg})
308     elseif (argumentState EQUAL 2)
309       list(APPEND MATCHES_TO_IGNORE ${arg})
310     else()
311       message(FATAL_ERROR "Unknown argument state!")
312     endif()
313   endforeach()
315   foreach (theList ${LISTS_TO_SEARCH})
316     foreach (entry ${${theList}})
317       foreach (match ${MATCHES_TO_REMOVE})
318         if (${entry} MATCHES ${match})
319           set(SHOULD_IGNORE OFF)
320           foreach (ign ${MATCHES_TO_IGNORE})
321             if (${entry} MATCHES ${ign})
322               set(SHOULD_IGNORE ON)
323               break()
324             endif()
325           endforeach()
327           if (NOT SHOULD_IGNORE)
328             list(REMOVE_ITEM ${theList} ${entry})
329           endif()
330         endif()
331       endforeach()
332     endforeach()
333     set(${theList} ${${theList}} PARENT_SCOPE)
334   endforeach()
335 endfunction()
337 # Automatically create source_group directives for the sources passed in.
338 function(auto_source_group rootName rootDir)
339   file(TO_CMAKE_PATH "${rootDir}" rootDir)
340   string(LENGTH "${rootDir}" rootDirLength)
341   set(sourceGroups)
342   foreach (fil ${ARGN})
343     file(TO_CMAKE_PATH "${fil}" filePath)
344     string(FIND "${filePath}" "/" rIdx REVERSE)
345     if (rIdx EQUAL -1)
346       message(FATAL_ERROR "Unable to locate the final forward slash in '${filePath}'!")
347     endif()
348     string(SUBSTRING "${filePath}" 0 ${rIdx} filePath)
350     string(LENGTH "${filePath}" filePathLength)
351     string(FIND "${filePath}" "${rootDir}" rIdx)
352     if (rIdx EQUAL 0)
353       math(EXPR filePathLength "${filePathLength} - ${rootDirLength}")
354       string(SUBSTRING "${filePath}" ${rootDirLength} ${filePathLength} fileGroup)
356       string(REPLACE "/" "\\" fileGroup "${fileGroup}")
357       set(fileGroup "\\${rootName}${fileGroup}")
359       list(FIND sourceGroups "${fileGroup}" rIdx)
360       if (rIdx EQUAL -1)
361         list(APPEND sourceGroups "${fileGroup}")
362         source_group("${fileGroup}" REGULAR_EXPRESSION "${filePath}/[^/.]+(.(tab|yy))?.(c|cc|cpp|h|hpp|ll|php|tcc|y)$")
363       endif()
364     endif()
365   endforeach()
366 endfunction()
368 function(parse_version PREFIX VERSION)
369   if (NOT ${VERSION} MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+)?(-.+)?$")
370     message(FATAL_ERROR "VERSION must conform to X.Y(.Z)?(-.+)?")
371   endif()
373   string(FIND ${VERSION} "-" SUFFIX_POS)
374   set(SUFFIX "")
375   if (SUFFIX_POS)
376     string(SUBSTRING ${VERSION} ${SUFFIX_POS} -1 SUFFIX)
377     string(SUBSTRING ${VERSION} 0 ${SUFFIX_POS} NUMERIC_VERSION)
378   else()
379     set(NUMERIC_VERSION ${VERSION})
380   endif()
382   string(REPLACE "." ";" VERSION_LIST "${NUMERIC_VERSION}")
383   list(GET VERSION_LIST 0 MAJOR)
384   list(GET VERSION_LIST 1 MINOR)
385   list(LENGTH VERSION_LIST VERSION_LIST_LENGTH)
386   if (VERSION_LIST_LENGTH GREATER 2)
387     list(GET VERSION_LIST 2 PATCH)
388   else()
389     set(PATCH 0)
390   endif()
392   set(${PREFIX}MAJOR ${MAJOR} PARENT_SCOPE)
393   set(${PREFIX}MINOR ${MINOR} PARENT_SCOPE)
394   set(${PREFIX}PATCH ${PATCH} PARENT_SCOPE)
395   set(${PREFIX}SUFFIX ${SUFFIX} PARENT_SCOPE)
396 endfunction()
398 # MSVC doesn't support a --whole-archive flag, but newer versions
399 # of CMake do support object libraries, which give the same result.
400 # As we can't easily upgrade the normal builds to CMake 3.0, we
401 # will just require CMake 3.0+ for MSVC builds only.
402 function(add_object_library libraryName)
403   if (MSVC)
404     add_library(${libraryName} OBJECT ${ARGN})
405   else()
406     add_library(${libraryName} STATIC ${ARGN})
407   endif()
408 endfunction()
410 # Get what might be the objects of the object libraries, if needed.
411 function(get_object_libraries_objects targetVariable)
412   set(OBJECTS)
413   if (MSVC)
414     foreach (fil ${ARGN})
415       list(APPEND OBJECTS $<TARGET_OBJECTS:${fil}>)
416     endforeach()
417   endif()
419   set(${targetVariable} ${OBJECTS} PARENT_SCOPE)
420 endfunction()
422 # Add the additional link targets for a set of object libraries,
423 # if needed.
424 function(link_object_libraries target)
425   if (MSVC)
426     return()
427   endif()
429   set(WHOLE_ARCHIVE_LIBS)
430   foreach (fil ${ARGN})
431     list(APPEND WHOLE_ARCHIVE_LIBS ${fil})
432   endforeach()
434   set(ANCHOR_SYMS)
435   if (APPLE)
436     set(ANCHOR_SYMS
437       -Wl,-pagezero_size,0x00001000
438       # Set the .text.keep section to be executable.
439       -Wl,-segprot,.text,rx,rx)
440     foreach(lib ${WHOLE_ARCHIVE_LIBS})
441       # It's important to use -Xlinker and not -Wl here: ${lib} needs to be its
442       # own option on the command line, since target_link_libraries will expand it
443       # from its logical name here into the full .a path. (Eww.)
444       list(APPEND ANCHOR_SYMS -Xlinker -force_load -Xlinker ${lib})
445     endforeach()
446   else()
447     set(ANCHOR_SYMS -Wl,--whole-archive ${WHOLE_ARCHIVE_LIBS} -Wl,--no-whole-archive)
448   endif()
450   target_link_libraries(${target} ${ANCHOR_SYMS})
451 endfunction()
453 # This should be called for object libraries, rather than calling
454 # hphp_link directly.
455 function(object_library_hphp_link target)
456   # MSVC can't have it. (see below)
457   if (NOT MSVC)
458     hphp_link(${target})
459   endif()
460 endfunction()
462 # If a library needs to be linked in to make GNU ld happy,
463 # it should be done by calling this.
464 function(object_library_ld_link_libraries target)
465   if (${ARGC})
466     # CMake doesn't allow calls to target_link_libraries if the target
467     # is an OBJECT library, so MSVC can't have this.
468     if (NOT MSVC)
469       target_link_libraries(${target} ${ARGN})
470     endif()
471   endif()
472 endfunction()
474 set(
475   HHVM_THIRD_PARTY_SOURCE_CACHE_PREFIX
476   ""
477   CACHE
478   STRING
479   "URL prefix containing cache of third-party dependencies"
481 set(
482   HHVM_THIRD_PARTY_SOURCE_CACHE_SUFFIX
483   ""
484   CACHE
485   STRING
486   "URL suffix for third-party dependency cache"
488 set(
489   HHVM_THIRD_PARTY_SOURCE_ONLY_USE_CACHE
490   OFF
491   CACHE
492   BOOL
493   "Do not download sources that are not in cache; may cause build to fail."
495 set(
496   HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT
497   ""
498   CACHE
499   STRING
500   "Path to a text file to put a list of sources that should be in the cache"
503 # Usage:
504 #  SET_HHVM_THIRD_PARTY_SOURCE_ARGS(
505 #    MY_VAR_NAME
506 #    SOURCE_URL https://example.com/
507 #    SOURCE_HASH SHA256=deadbeef
508 #  )
509 #  ... or ...
510 #  SET_HHVM_THIRD_PARTY_SOURCE_ARGS(
511 #    MY_VAR_NAME
512 #    Linux_URL https://example.com/linux.tar.bz2
513 #    Linux_HASH SHA256=deadbeef
514 #    Darwin_URL https://example.com/macos.tar.bz2
515 #    Darwin_HASH SHA256=deadbeef
516 #  )
518 macro(SET_HHVM_THIRD_PARTY_SOURCE_ARGS VAR_NAME)
519   cmake_parse_arguments(
520     # Prefix (FOO becomes _arg_FOO - trailing _ implied)
521     _arg
522     # No-arg parameter (none):
523     ""
524     # Single-argument parameters
525     "SOURCE_URL;SOURCE_HASH;Linux_URL;Linux_HASH;Darwin_URL;Darwin_HASH;FILENAME_PREFIX"
526     # Multi-argument parameters (none)
527     ""
528     ${ARGN}
529   )
531   # Try source, but fall back to platform-specific
532   if (NOT "${_arg_SOURCE_URL}" STREQUAL "")
533     set(_URL "${_arg_SOURCE_URL}")
534     set("${VAR_NAME}" URL_HASH "${_arg_SOURCE_HASH}")
535     if (NOT "${HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT}" STREQUAL "")
536       FILE(
537         APPEND
538         "${HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT}"
539         "${_arg_SOURCE_URL}\n"
540       )
541     endif()
542   else()
543     set(_URL "${_arg_${CMAKE_HOST_SYSTEM_NAME}_URL}")
544     set("${VAR_NAME}" URL_HASH "${_arg_${CMAKE_HOST_SYSTEM_NAME}_HASH}")
545     if (NOT "${HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT}" STREQUAL "")
546       FILE(
547         APPEND
548         "${HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT}"
549         "${_arg_Linux_URL}\n"
550         "${_arg_Darwin_URL}\n"
551       )
552     endif()
553   endif()
556   get_filename_component("_URL_NAME" "${_URL}" NAME)
557   if ("${HHVM_THIRD_PARTY_SOURCE_CACHE_PREFIX}" STREQUAL "")
558     list(APPEND ${VAR_NAME} URL "${_URL}")
559     if (${HHVM_THIRD_PARTY_SOURCE_ONLY_USE_CACHE})
560       message(
561         FATAL_ERROR
562         "HHVM_THIRD_PARTY_ONLY_USE_CACHE is set, but cache is not configured"
563       )
564     endif()
565   else()
566     list(
567       APPEND "${VAR_NAME}"
568       URL
569       "${HHVM_THIRD_PARTY_SOURCE_CACHE_PREFIX}${_arg_FILENAME_PREFIX}${_URL_NAME}${HHVM_THIRD_PARTY_SOURCE_CACHE_SUFFIX}"
570     )
571     if (NOT ${HHVM_THIRD_PARTY_SOURCE_ONLY_USE_CACHE})
572       list(APPEND "${VAR_NAME}" "${_URL}")
573     endif()
574   endif()
575   list(APPEND "${VAR_NAME}" DOWNLOAD_NAME "${_arg_FILENAME_PREFIX}${_URL_NAME}")
576   message(STATUS "Download name: ${_arg_FILENAME_PREFIX}${_URL_NAME}")
577 endmacro()