1 if (${CMAKE_MAJOR_VERSION} GREATER 2)
2 cmake_policy(SET CMP0026 OLD)
5 include(GNUInstallDirs)
7 function(auto_sources RETURN_VALUE PATTERN SOURCE_SUBDIRS)
9 if ("${SOURCE_SUBDIRS}" STREQUAL "RECURSE")
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})
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})
41 list(REMOVE_ITEM ${RETURN_VALUE} ${FILTER_OUT})
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}")
50 if (NOT (${f} MATCHES "(ext_hhvm|/(old-)?tests?/)"))
51 list(APPEND CXX_SOURCES ${f})
54 auto_sources(files "*.c" "RECURSE" "${DIR}")
56 if (NOT (${f} MATCHES "(ext_hhvm|/(old-)?tests?/)"))
57 list(APPEND C_SOURCES ${f})
61 auto_sources(files "*.asm" "RECURSE" "${DIR}")
63 if (NOT (${f} MATCHES "(ext_hhvm|/(old-)?tests?/)"))
64 list(APPEND ASM_SOURCES ${f})
68 auto_sources(files "*.S" "RECURSE" "${DIR}")
70 if (NOT (${f} MATCHES "(ext_hhvm|/(old-)?tests?/)"))
71 list(APPEND ASM_SOURCES ${f})
75 auto_sources(files "*.h" "RECURSE" "${DIR}")
77 if (NOT (${f} MATCHES "(/(old-)?tests?/)"))
78 list(APPEND HEADER_SOURCES ${f})
81 endmacro(HHVM_SELECT_SOURCES)
83 function(CONTAINS_STRING FILE SEARCH RETURN_VALUE)
84 file(STRINGS ${FILE} FILE_CONTENTS REGEX ".*${SEARCH}.*")
86 set(${RETURN_VALUE} True PARENT_SCOPE)
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)
94 /var/run/mysqld/mysqld.sock
96 /var/run/mysql/mysql.sock
97 /var/lib/mysql/mysql.sock
99 /usr/local/mysql/var/mysql.sock
100 /Private/tmp/mysql.sock
101 /private/tmp/mysql.sock
111 set(MYSQL_UNIX_SOCK_ADDR ${MYSQL_SOCK} CACHE STRING "Path to MySQL Socket")
115 function(append_systemlib TARGET SOURCE SECTNAME)
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)
123 set(${TARGET}_SLIBS ${${TARGET}_SLIBS} -Wl,-sectcreate,__text,${SECTNAME},${SOURCE} PARENT_SCOPE)
125 set(${TARGET}_SLIBS ${${TARGET}_SLIBS} "--add-section" "${SECTNAME}=${SOURCE}" PARENT_SCOPE)
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})
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"
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"
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})
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")
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")
170 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/embed.rc "${RESOURCE_FILE}")
172 add_custom_command(TARGET ${TARGET} POST_BUILD
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"
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}")
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)
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}")
201 append_systemlib(${TARGET} ${SLIB} "ext.${SLIB_HASH_NAME_SHORT}")
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})
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)
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)")
225 install(CODE "execute_process(COMMAND \"${CHRPATH}\" \"-r\" \"${RPATH}\" \"${LOC}\" ERROR_QUIET)")
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)
235 CODE "INCLUDE(\"${HPHP_HOME}/CMake/HPHPFunctions.cmake\")
236 HHVM_INSTALL_HEADERS(${TARGET} ${HPHP_HOME}
237 \"${CMAKE_INSTALL_FULL_INCLUDEDIR}\" ${ARGN})"
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}")
250 string(FIND ${dest_rel} / slash_pos REVERSE)
251 if(slash_pos EQUAL -1)
254 string(SUBSTRING ${dest_rel} 0 ${slash_pos} dest_rel)
257 DESTINATION "$ENV{DESTDIR}${DEST}/${dest_rel}"
258 NO_SOURCE_PERMISSIONS)
263 macro(HHVM_EXT_OPTION EXTNAME PACKAGENAME)
264 if (NOT DEFINED EXT_${EXTNAME})
266 find_package(${PACKAGENAME})
267 elseif (EXT_${EXTNAME} STREQUAL "ON")
269 find_package(${PACKAGENAME} REQUIRED)
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.
278 # This will remove all files in the CPP_SOURCES list
279 # matching "/test/" or "Test.cpp$", but not matching
281 # HHVM_REMOVE_MATCHES_FROM_LISTS(CPP_SOURCES MATCHES "/test/" "Test.cpp$" IGNORE_MATCHES "BobTest.cpp$")
286 # The names of the lists to remove matches from.
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)
296 set(MATCHES_TO_REMOVE)
297 set(MATCHES_TO_IGNORE)
299 foreach (arg ${ARGN})
300 if ("x${arg}" STREQUAL "xMATCHES")
302 elseif ("x${arg}" STREQUAL "xIGNORE_MATCHES")
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})
311 message(FATAL_ERROR "Unknown argument state!")
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)
327 if (NOT SHOULD_IGNORE)
328 list(REMOVE_ITEM ${theList} ${entry})
333 set(${theList} ${${theList}} PARENT_SCOPE)
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)
342 foreach (fil ${ARGN})
343 file(TO_CMAKE_PATH "${fil}" filePath)
344 string(FIND "${filePath}" "/" rIdx REVERSE)
346 message(FATAL_ERROR "Unable to locate the final forward slash in '${filePath}'!")
348 string(SUBSTRING "${filePath}" 0 ${rIdx} filePath)
350 string(LENGTH "${filePath}" filePathLength)
351 string(FIND "${filePath}" "${rootDir}" rIdx)
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)
361 list(APPEND sourceGroups "${fileGroup}")
362 source_group("${fileGroup}" REGULAR_EXPRESSION "${filePath}/[^/.]+(.(tab|yy))?.(c|cc|cpp|h|hpp|ll|php|tcc|y)$")
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)?(-.+)?")
373 string(FIND ${VERSION} "-" SUFFIX_POS)
376 string(SUBSTRING ${VERSION} ${SUFFIX_POS} -1 SUFFIX)
377 string(SUBSTRING ${VERSION} 0 ${SUFFIX_POS} NUMERIC_VERSION)
379 set(NUMERIC_VERSION ${VERSION})
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)
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)
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)
404 add_library(${libraryName} OBJECT ${ARGN})
406 add_library(${libraryName} STATIC ${ARGN})
410 # Get what might be the objects of the object libraries, if needed.
411 function(get_object_libraries_objects targetVariable)
414 foreach (fil ${ARGN})
415 list(APPEND OBJECTS $<TARGET_OBJECTS:${fil}>)
419 set(${targetVariable} ${OBJECTS} PARENT_SCOPE)
422 # Add the additional link targets for a set of object libraries,
424 function(link_object_libraries target)
429 set(WHOLE_ARCHIVE_LIBS)
430 foreach (fil ${ARGN})
431 list(APPEND WHOLE_ARCHIVE_LIBS ${fil})
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})
447 set(ANCHOR_SYMS -Wl,--whole-archive ${WHOLE_ARCHIVE_LIBS} -Wl,--no-whole-archive)
450 target_link_libraries(${target} ${ANCHOR_SYMS})
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)
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)
466 # CMake doesn't allow calls to target_link_libraries if the target
467 # is an OBJECT library, so MSVC can't have this.
469 target_link_libraries(${target} ${ARGN})
475 HHVM_THIRD_PARTY_SOURCE_CACHE_PREFIX
479 "URL prefix containing cache of third-party dependencies"
482 HHVM_THIRD_PARTY_SOURCE_CACHE_SUFFIX
486 "URL suffix for third-party dependency cache"
489 HHVM_THIRD_PARTY_SOURCE_ONLY_USE_CACHE
493 "Do not download sources that are not in cache; may cause build to fail."
496 HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT
500 "Path to a text file to put a list of sources that should be in the cache"
504 # SET_HHVM_THIRD_PARTY_SOURCE_ARGS(
506 # SOURCE_URL https://example.com/
507 # SOURCE_HASH SHA256=deadbeef
510 # SET_HHVM_THIRD_PARTY_SOURCE_ARGS(
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
518 macro(SET_HHVM_THIRD_PARTY_SOURCE_ARGS VAR_NAME)
519 cmake_parse_arguments(
520 # Prefix (FOO becomes _arg_FOO - trailing _ implied)
522 # No-arg parameter (none):
524 # Single-argument parameters
525 "SOURCE_URL;SOURCE_HASH;Linux_URL;Linux_HASH;Darwin_URL;Darwin_HASH;FILENAME_PREFIX"
526 # Multi-argument parameters (none)
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 "")
538 "${HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT}"
539 "${_arg_SOURCE_URL}\n"
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 "")
548 "${HHVM_THIRD_PARTY_SOURCE_URL_LIST_OUTPUT}"
549 "${_arg_Linux_URL}\n"
550 "${_arg_Darwin_URL}\n"
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})
562 "HHVM_THIRD_PARTY_ONLY_USE_CACHE is set, but cache is not configured"
569 "${HHVM_THIRD_PARTY_SOURCE_CACHE_PREFIX}${_arg_FILENAME_PREFIX}${_URL_NAME}${HHVM_THIRD_PARTY_SOURCE_CACHE_SUFFIX}"
571 if (NOT ${HHVM_THIRD_PARTY_SOURCE_ONLY_USE_CACHE})
572 list(APPEND "${VAR_NAME}" "${_URL}")
575 list(APPEND "${VAR_NAME}" DOWNLOAD_NAME "${_arg_FILENAME_PREFIX}${_URL_NAME}")
576 message(STATUS "Download name: ${_arg_FILENAME_PREFIX}${_URL_NAME}")