update version.h
[hiphop-php.git] / CMake / HPHPFunctions.cmake
blob2ab4c1d59b52863aaa96e4cc8b033f1cb3b09d3e
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-repo-schema-id.txt"
137     #        "${CMAKE_CURRENT_SOURCE_DIR}/generated-build-id.txt"
138     COMMAND
139       "INSTALL_DIR=${CMAKE_BINARY_DIR}/hphp/util"
140       "${HPHP_HOME}/hphp/hhvm/generate-buildinfo.sh"
141     WORKING_DIRECTORY "${HPHP_HOME}/hphp/util"
142     COMMENT "Generating Repo Schema ID and Compiler ID"
143     VERBATIM)
145   if (APPLE)
146     set(COMPILER_ID -Wl,-sectcreate,__text,"compiler_id","${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-id.txt")
147     set(REPO_SCHEMA -Wl,-sectcreate,__text,"repo_schema_id","${CMAKE_BINARY_DIR}/hphp/util/generated-repo-schema-id.txt")
148     set(BUILD_ID -Wl,-sectcreate,__text,"build_id","${CMAKE_BINARY_DIR}/hphp/util/generated-build-id.txt")
149     target_link_libraries(${TARGET} ${${TARGET}_SLIBS} ${COMPILER_ID} ${REPO_SCHEMA} ${BUILD_ID})
150   elseif(MSVC)
151     set(RESOURCE_FILE "#pragma code_page(1252)\n")
152     set(RESOURCE_FILE "${RESOURCE_FILE}LANGUAGE 0, 0\n")
153     set(RESOURCE_FILE "${RESOURCE_FILE}\n")
154     set(RESOURCE_FILE "${RESOURCE_FILE}#include \"${CMAKE_BINARY_DIR}/hphp/runtime/version.h\"\n")
155     file(READ "${CMAKE_BINARY_DIR}/hphp/hhvm/hhvm.rc" VERSION_INFO)
156     set(RESOURCE_FILE "${RESOURCE_FILE}compiler_id RCDATA \"${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-id.txt\"\n")
157     set(RESOURCE_FILE "${RESOURCE_FILE}repo_schema_id RCDATA \"${CMAKE_BINARY_DIR}/hphp/util/generated-repo-schema-id.txt\"\n")
158     set(RESOURCE_FILE "${RESOURCE_FILE}build_id RCDATA \"${CMAKE_BINARY_DIR}/hphp/util/generated-build-id.txt\"\n")
159     set(RESOURCE_FILE "${RESOURCE_FILE}${VERSION_INFO}\n")
160     set(i 0)
161     foreach (nm ${${TARGET}_SLIBS_NAMES})
162       list(GET ${TARGET}_SLIBS_SOURCES ${i} source)
163       set(RESOURCE_FILE "${RESOURCE_FILE}${nm} RCDATA \"${source}\"\n")
164       math(EXPR i "${i} + 1")
165     endforeach()
166     file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/embed.rc "${RESOURCE_FILE}")
167   else()
168     add_custom_command(TARGET ${TARGET} POST_BUILD
169       COMMAND "objcopy"
170       ARGS "--add-section" "compiler_id=${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-id.txt"
171            "--add-section" "repo_schema_id=${CMAKE_BINARY_DIR}/hphp/util/generated-repo-schema-id.txt"
172            "--add-section" "build_id=${CMAKE_BINARY_DIR}/hphp/util/generated-build-id.txt"
173            ${${TARGET}_SLIBS}
174            ${DEST}
175       DEPENDS "${CMAKE_BINARY_DIR}/hphp/util/generated-compiler-id.txt"
176               "${CMAKE_BINARY_DIR}/hphp/util/generated-repo-schema-id.txt"
177               "${CMAKE_BINARY_DIR}/hphp/util/generated-build-id.txt"
178       COMMENT "Embedding php in ${TARGET}")
179   endif()
180 endfunction(embed_sections)
182 macro(embed_systemlib_byname TARGET SLIB)
183   get_filename_component(SLIB_BN ${SLIB} "NAME_WE")
184   string(LENGTH ${SLIB_BN} SLIB_BN_LEN)
185   math(EXPR SLIB_BN_REL_LEN "${SLIB_BN_LEN} - 4")
186   string(SUBSTRING ${SLIB_BN} 4 ${SLIB_BN_REL_LEN} SLIB_EXTNAME)
187   string(MD5 SLIB_HASH_NAME ${SLIB_EXTNAME})
188   # Some platforms limit section names to 16 characters :(
189   string(SUBSTRING ${SLIB_HASH_NAME} 0 12 SLIB_HASH_NAME_SHORT)
190   if (MSVC)
191     # The dot would be causing the RC lexer to begin a number in the
192     # middle of our resource name, so use an underscore instead.
193     append_systemlib(${TARGET} ${SLIB} "ext_${SLIB_HASH_NAME_SHORT}")
194   else()
195     append_systemlib(${TARGET} ${SLIB} "ext.${SLIB_HASH_NAME_SHORT}")
196   endif()
197 endmacro()
199 function(embed_all_systemlibs TARGET ROOT DEST)
200   add_dependencies(${TARGET} systemlib)
201   append_systemlib(${TARGET} ${ROOT}/system/systemlib.php systemlib)
202   foreach(SLIB ${EXTENSION_SYSTEMLIB_SOURCES} ${EZC_SYSTEMLIB_SOURCES})
203     embed_systemlib_byname(${TARGET} ${SLIB})
204   endforeach()
205   embed_sections(${TARGET} ${DEST})
206 endfunction(embed_all_systemlibs)
208 # Custom install function that doesn't relink, instead it uses chrpath to change it, if
209 # it's available, otherwise, it leaves the chrpath alone
210 function(HHVM_INSTALL TARGET DEST)
211   get_target_property(LOC ${TARGET} LOCATION)
212   get_target_property(TY ${TARGET} TYPE)
213   if (FOUND_CHRPATH)
214     get_target_property(RPATH ${TARGET} INSTALL_RPATH)
215     if (NOT RPATH STREQUAL "RPATH-NOTFOUND")
216       if (RPATH STREQUAL "")
217         install(CODE "execute_process(COMMAND \"${CHRPATH}\" \"-d\" \"${LOC}\" ERROR_QUIET)")
218       else()
219         install(CODE "execute_process(COMMAND \"${CHRPATH}\" \"-r\" \"${RPATH}\" \"${LOC}\" ERROR_QUIET)")
220       endif()
221     endif()
222   endif()
223   string(TOUPPER ${DEST} DEST_UPPER)
224   install(CODE "FILE(INSTALL DESTINATION \"\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_${DEST_UPPER}DIR}\" TYPE ${TY} FILES \"${LOC}\")")
225 endfunction(HHVM_INSTALL)
227 function(HHVM_PUBLIC_HEADERS TARGET)
228   install(
229     CODE "INCLUDE(\"${HPHP_HOME}/CMake/HPHPFunctions.cmake\")
230       HHVM_INSTALL_HEADERS(${TARGET} ${HPHP_HOME}
231       \"\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}\" ${ARGN})"
232     COMPONENT dev)
233 endfunction()
235 function(HHVM_INSTALL_HEADERS TARGET SRCROOT DEST)
236   message(STATUS "Installing header files for ${TARGET}")
237   foreach(src_rel ${ARGN})
238     # Determine the relative directory name so that we can mirror the
239     # directory structure in the output
240     file(RELATIVE_PATH dest_rel ${SRCROOT} ${src_rel})
241     if (IS_ABSOLUTE ${dest_rel})
242       message(WARNING "${TARGET}: Header file ${dest_rel} is not inside ${SRCROOT}")
243     else()
244       string(FIND ${dest_rel} / slash_pos REVERSE)
245       if(slash_pos EQUAL -1)
246         set(dest_rel)
247       else()
248         string(SUBSTRING ${dest_rel} 0 ${slash_pos} dest_rel)
249       endif()
250       file(COPY ${src_rel}
251         DESTINATION "$ENV{DESTDIR}${DEST}/${dest_rel}"
252         NO_SOURCE_PERMISSIONS)
253     endif()
254   endforeach()
255 endfunction()
257 macro(HHVM_EXT_OPTION EXTNAME PACKAGENAME)
258   if (NOT DEFINED EXT_${EXTNAME})
259     # Implicit check
260     find_package(${PACKAGENAME})
261   elseif (EXT_${EXTNAME} STREQUAL "ON")
262     # Explicit check
263     find_package(${PACKAGENAME} REQUIRED)
264   endif()
265 endmacro()
267 # Remove all files matching a set of patterns, and,
268 # optionally, not matching a second set of patterns,
269 # from a set of lists.
271 # Example:
272 # This will remove all files in the CPP_SOURCES list
273 # matching "/test/" or "Test.cpp$", but not matching
274 # "BobTest.cpp$".
275 # HHVM_REMOVE_MATCHES_FROM_LISTS(CPP_SOURCES MATCHES "/test/" "Test.cpp$" IGNORE_MATCHES "BobTest.cpp$")
277 # Parameters:
279 # [...]:
280 # The names of the lists to remove matches from.
282 # [MATCHES ...]:
283 # The matches to remove from the lists.
285 # [IGNORE_MATCHES ...]:
286 # The matches not to remove, even if they match
287 # the main set of matches to remove.
288 function(HHVM_REMOVE_MATCHES_FROM_LISTS)
289   set(LISTS_TO_SEARCH)
290   set(MATCHES_TO_REMOVE)
291   set(MATCHES_TO_IGNORE)
292   set(argumentState 0)
293   foreach (arg ${ARGN})
294     if ("x${arg}" STREQUAL "xMATCHES")
295       set(argumentState 1)
296     elseif ("x${arg}" STREQUAL "xIGNORE_MATCHES")
297       set(argumentState 2)
298     elseif (argumentState EQUAL 0)
299       list(APPEND LISTS_TO_SEARCH ${arg})
300     elseif (argumentState EQUAL 1)
301       list(APPEND MATCHES_TO_REMOVE ${arg})
302     elseif (argumentState EQUAL 2)
303       list(APPEND MATCHES_TO_IGNORE ${arg})
304     else()
305       message(FATAL_ERROR "Unknown argument state!")
306     endif()
307   endforeach()
309   foreach (theList ${LISTS_TO_SEARCH})
310     foreach (entry ${${theList}})
311       foreach (match ${MATCHES_TO_REMOVE})
312         if (${entry} MATCHES ${match})
313           set(SHOULD_IGNORE OFF)
314           foreach (ign ${MATCHES_TO_IGNORE})
315             if (${entry} MATCHES ${ign})
316               set(SHOULD_IGNORE ON)
317               break()
318             endif()
319           endforeach()
321           if (NOT SHOULD_IGNORE)
322             list(REMOVE_ITEM ${theList} ${entry})
323           endif()
324         endif()
325       endforeach()
326     endforeach()
327     set(${theList} ${${theList}} PARENT_SCOPE)
328   endforeach()
329 endfunction()
331 # Automatically create source_group directives for the sources passed in.
332 function(auto_source_group rootName rootDir)
333   file(TO_CMAKE_PATH "${rootDir}" rootDir)
334   string(LENGTH "${rootDir}" rootDirLength)
335   set(sourceGroups)
336   foreach (fil ${ARGN})
337     file(TO_CMAKE_PATH "${fil}" filePath)
338     string(FIND "${filePath}" "/" rIdx REVERSE)
339     if (rIdx EQUAL -1)
340       message(FATAL_ERROR "Unable to locate the final forward slash in '${filePath}'!")
341     endif()
342     string(SUBSTRING "${filePath}" 0 ${rIdx} filePath)
344     string(LENGTH "${filePath}" filePathLength)
345     string(FIND "${filePath}" "${rootDir}" rIdx)
346     if (rIdx EQUAL 0)
347       math(EXPR filePathLength "${filePathLength} - ${rootDirLength}")
348       string(SUBSTRING "${filePath}" ${rootDirLength} ${filePathLength} fileGroup)
350       string(REPLACE "/" "\\" fileGroup "${fileGroup}")
351       set(fileGroup "\\${rootName}${fileGroup}")
353       list(FIND sourceGroups "${fileGroup}" rIdx)
354       if (rIdx EQUAL -1)
355         list(APPEND sourceGroups "${fileGroup}")
356         source_group("${fileGroup}" REGULAR_EXPRESSION "${filePath}/[^/.]+(.(tab|yy))?.(c|cc|cpp|h|hpp|ll|php|tcc|y)$")
357       endif()
358     endif()
359   endforeach()
360 endfunction()
362 function(parse_version PREFIX VERSION)
363   if (NOT ${VERSION} MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+)?(-.+)?$")
364     message(FATAL_ERROR "VERSION must conform to X.Y(.Z)?(-.+)?")
365   endif()
367   string(FIND ${VERSION} "-" SUFFIX_POS)
368   set(SUFFIX "")
369   if (SUFFIX_POS)
370     string(SUBSTRING ${VERSION} ${SUFFIX_POS} -1 SUFFIX)
371     string(SUBSTRING ${VERSION} 0 ${SUFFIX_POS} NUMERIC_VERSION)
372   else()
373     set(NUMERIC_VERSION ${VERSION})
374   endif()
376   string(REPLACE "." ";" VERSION_LIST "${NUMERIC_VERSION}")
377   list(GET VERSION_LIST 0 MAJOR)
378   list(GET VERSION_LIST 1 MINOR)
379   list(LENGTH VERSION_LIST VERSION_LIST_LENGTH)
380   if (VERSION_LIST_LENGTH GREATER 2)
381     list(GET VERSION_LIST 2 PATCH)
382   else()
383     set(PATCH 0)
384   endif()
386   set(${PREFIX}MAJOR ${MAJOR} PARENT_SCOPE)
387   set(${PREFIX}MINOR ${MINOR} PARENT_SCOPE)
388   set(${PREFIX}PATCH ${PATCH} PARENT_SCOPE)
389   set(${PREFIX}SUFFIX ${SUFFIX} PARENT_SCOPE)
390 endfunction()
392 # MSVC doesn't support a --whole-archive flag, but newer versions
393 # of CMake do support object libraries, which give the same result.
394 # As we can't easily upgrade the normal builds to CMake 3.0, we
395 # will just require CMake 3.0+ for MSVC builds only.
396 function(add_object_library libraryName)
397   if (MSVC)
398     add_library(${libraryName} OBJECT ${ARGN})
399   else()
400     add_library(${libraryName} STATIC ${ARGN})
401   endif()
402 endfunction()
404 # Get what might be the objects of the object libraries, if needed.
405 function(get_object_libraries_objects targetVariable)
406   set(OBJECTS)
407   if (MSVC)
408     foreach (fil ${ARGN})
409       list(APPEND OBJECTS $<TARGET_OBJECTS:${fil}>)
410     endforeach()
411   endif()
413   set(${targetVariable} ${OBJECTS} PARENT_SCOPE)
414 endfunction()
416 # Add the additional link targets for a set of object libraries,
417 # if needed.
418 function(link_object_libraries target)
419   if (MSVC)
420     return()
421   endif()
423   set(WHOLE_ARCHIVE_LIBS)
424   foreach (fil ${ARGN})
425     list(APPEND WHOLE_ARCHIVE_LIBS ${fil})
426   endforeach()
428   set(ANCHOR_SYMS)
429   if (APPLE)
430     set(ANCHOR_SYMS
431       -Wl,-pagezero_size,0x00001000
432       # Set the .text.keep section to be executable.
433       -Wl,-segprot,.text,rx,rx)
434     foreach(lib ${WHOLE_ARCHIVE_LIBS})
435       # It's important to use -Xlinker and not -Wl here: ${lib} needs to be its
436       # own option on the command line, since target_link_libraries will expand it
437       # from its logical name here into the full .a path. (Eww.)
438       list(APPEND ANCHOR_SYMS -Xlinker -force_load -Xlinker ${lib})
439     endforeach()
440   else()
441     set(ANCHOR_SYMS -Wl,--whole-archive ${WHOLE_ARCHIVE_LIBS} -Wl,--no-whole-archive)
442   endif()
444   target_link_libraries(${target} ${ANCHOR_SYMS})
445 endfunction()
447 # This should be called for object libraries, rather than calling
448 # hphp_link directly.
449 function(object_library_hphp_link target)
450   # MSVC can't have it. (see below)
451   if (NOT MSVC)
452     hphp_link(${target})
453   endif()
454 endfunction()
456 # If a library needs to be linked in to make GNU ld happy,
457 # it should be done by calling this.
458 function(object_library_ld_link_libraries target)
459   if (${ARGC})
460     # CMake doesn't allow calls to target_link_libraries if the target
461     # is an OBJECT library, so MSVC can't have this.
462     if (NOT MSVC)
463       target_link_libraries(${target} ${ARGN})
464     endif()
465   endif()
466 endfunction()
468 set(
469   HHVM_THIRD_PARTY_SOURCE_CACHE_PREFIX
470   ""
471   CACHE
472   STRING
473   "URL prefix containing cache of third-party dependencies"
475 set(
476   HHVM_THIRD_PARTY_SOURCE_CACHE_SUFFIX
477   ""
478   CACHE
479   STRING
480   "URL suffix for third-party dependency cache"
482 set(
483   HHVM_THIRD_PARTY_SOURCE_ONLY_USE_CACHE
484   OFF
485   CACHE
486   BOOL
487   "Do not download sources that are not in cache; may cause build to fail."
489 set(
490   HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT
491   ""
492   CACHE
493   STRING
494   "Path to a text file to put a list of sources that should be in the cache"
497 # Usage:
498 #  SET_HHVM_THIRD_PARTY_SOURCE_ARGS(
499 #    MY_VAR_NAME
500 #    SOURCE_URL https://example.com/
501 #    SOURCE_HASH SHA256=deadbeef
502 #  )
503 #  ... or ...
504 #  SET_HHVM_THIRD_PARTY_SOURCE_ARGS(
505 #    MY_VAR_NAME
506 #    Linux_URL https://example.com/linux.tar.bz2
507 #    Linux_HASH SHA256=deadbeef
508 #    Darwin_URL https://example.com/macos.tar.bz2
509 #    Darwin_HASH SHA256=deadbeef
510 #  )
512 macro(SET_HHVM_THIRD_PARTY_SOURCE_ARGS VAR_NAME)
513   cmake_parse_arguments(
514     # Prefix (FOO becomes _arg_FOO - trailing _ implied)
515     _arg
516     # No-arg parameter (none):
517     ""
518     # Single-argument parameters
519     "SOURCE_URL;SOURCE_HASH;Linux_URL;Linux_HASH;Darwin_URL;Darwin_HASH;FILENAME_PREFIX"
520     # Multi-argument parameters (none)
521     ""
522     ${ARGN}
523   )
525   # Try source, but fall back to platform-specific
526   if (NOT "${_arg_SOURCE_URL}" STREQUAL "")
527     set(_URL "${_arg_SOURCE_URL}")
528     set("${VAR_NAME}" URL_HASH "${_arg_SOURCE_HASH}")
529     if (NOT "${HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT}" STREQUAL "")
530       FILE(
531         APPEND
532         "${HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT}"
533         "${_arg_SOURCE_URL}\n"
534       )
535     endif()
536   else()
537     set(_URL "${_arg_${CMAKE_HOST_SYSTEM_NAME}_URL}")
538     set("${VAR_NAME}" URL_HASH "${_arg_${CMAKE_HOST_SYSTEM_NAME}_HASH}")
539     if (NOT "${HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT}" STREQUAL "")
540       FILE(
541         APPEND
542         "${HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT}"
543         "${_arg_Linux_URL}\n"
544         "${_arg_Darwin_URL}\n"
545       )
546     endif()
547   endif()
550   get_filename_component("_URL_NAME" "${_URL}" NAME)
551   if ("${HHVM_THIRD_PARTY_SOURCE_CACHE_PREFIX}" STREQUAL "")
552     list(APPEND ${VAR_NAME} URL "${_URL}")
553     if (${HHVM_THIRD_PARTY_SOURCE_ONLY_USE_CACHE})
554       message(
555         FATAL_ERROR
556         "HHVM_THIRD_PARTY_ONLY_USE_CACHE is set, but cache is not configured"
557       )
558     endif()
559   else()
560     list(
561       APPEND "${VAR_NAME}"
562       URL
563       "${HHVM_THIRD_PARTY_SOURCE_CACHE_PREFIX}${_arg_FILENAME_PREFIX}${_URL_NAME}${HHVM_THIRD_PARTY_SOURCE_CACHE_SUFFIX}"
564     )
565     if (NOT ${HHVM_THIRD_PARTY_SOURCE_ONLY_USE_CACHE})
566       list(APPEND "${VAR_NAME}" "${_URL}")
567     endif()
568   endif()
569   list(APPEND "${VAR_NAME}" DOWNLOAD_NAME "${_arg_FILENAME_PREFIX}${_URL_NAME}")
570   message(STATUS "Download name: ${_arg_FILENAME_PREFIX}${_URL_NAME}")
571 endmacro()