Optional Two-phase heap tracing
[hiphop-php.git] / CMake / HPHPFunctions.cmake
blob58fe03c5eab847cba02557d76621e09e6906c156
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     COMMAND "${HPHP_HOME}/hphp/hhvm/generate-buildinfo.sh"
138     WORKING_DIRECTORY "${HPHP_HOME}/hphp/util"
139     COMMENT "Generating Repo Schema ID and Compiler ID"
140     VERBATIM)
142   if (APPLE)
143     set(COMPILER_ID -Wl,-sectcreate,__text,"compiler_id","${HPHP_HOME}/hphp/util/generated-compiler-id.txt")
144     set(REPO_SCHEMA -Wl,-sectcreate,__text,"repo_schema_id","${HPHP_HOME}/hphp/util/generated-repo-schema-id.txt")
145     target_link_libraries(${TARGET} ${${TARGET}_SLIBS} ${COMPILER_ID} ${REPO_SCHEMA})
146   elseif(MSVC)
147     set(RESOURCE_FILE "#pragma code_page(1252)\n")
148     set(RESOURCE_FILE "${RESOURCE_FILE}LANGUAGE 0, 0\n")
149     set(RESOURCE_FILE "${RESOURCE_FILE}\n")
150     set(RESOURCE_FILE "${RESOURCE_FILE}#include \"${HPHP_HOME}/hphp/runtime/version.h\"\n")
151     file(READ "${HPHP_HOME}/hphp/hhvm/hhvm.rc" VERSION_INFO)
152     set(RESOURCE_FILE "${RESOURCE_FILE}compiler_id RCDATA \"${HPHP_HOME}/hphp/util/generated-compiler-id.txt\"\n")
153     set(RESOURCE_FILE "${RESOURCE_FILE}repo_schema_id RCDATA \"${HPHP_HOME}/hphp/util/generated-repo-schema-id.txt\"\n")
154     set(RESOURCE_FILE "${RESOURCE_FILE}${VERSION_INFO}\n")
155     set(i 0)
156     foreach (nm ${${TARGET}_SLIBS_NAMES})
157       list(GET ${TARGET}_SLIBS_SOURCES ${i} source)
158       set(RESOURCE_FILE "${RESOURCE_FILE}${nm} RCDATA \"${source}\"\n")
159       math(EXPR i "${i} + 1")
160     endforeach()
161     file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/embed.rc "${RESOURCE_FILE}")
162   else()
163     add_custom_command(TARGET ${TARGET} POST_BUILD
164       COMMAND "objcopy"
165       ARGS "--add-section" "compiler_id=${HPHP_HOME}/hphp/util/generated-compiler-id.txt"
166            "--add-section" "repo_schema_id=${HPHP_HOME}/hphp/util/generated-repo-schema-id.txt"
167            ${${TARGET}_SLIBS}
168            ${DEST}
169       DEPENDS "${HPHP_HOME}/hphp/util/generated-compiler-id.txt"
170               "${HPHP_HOME}/hphp/util/generated-repo-schema-id.txt"
171       COMMENT "Embedding php in ${TARGET}")
172   endif()
173 endfunction(embed_sections)
175 macro(embed_systemlib_byname TARGET SLIB)
176   get_filename_component(SLIB_BN ${SLIB} "NAME_WE")
177   string(LENGTH ${SLIB_BN} SLIB_BN_LEN)
178   math(EXPR SLIB_BN_REL_LEN "${SLIB_BN_LEN} - 4")
179   string(SUBSTRING ${SLIB_BN} 4 ${SLIB_BN_REL_LEN} SLIB_EXTNAME)
180   string(MD5 SLIB_HASH_NAME ${SLIB_EXTNAME})
181   # Some platforms limit section names to 16 characters :(
182   string(SUBSTRING ${SLIB_HASH_NAME} 0 12 SLIB_HASH_NAME_SHORT)
183   if (MSVC)
184     # The dot would be causing the RC lexer to begin a number in the
185     # middle of our resource name, so use an underscore instead.
186     append_systemlib(${TARGET} ${SLIB} "ext_${SLIB_HASH_NAME_SHORT}")
187   else()
188     append_systemlib(${TARGET} ${SLIB} "ext.${SLIB_HASH_NAME_SHORT}")
189   endif()
190 endmacro()
192 function(embed_all_systemlibs TARGET ROOT DEST)
193   append_systemlib(${TARGET} ${ROOT}/system/systemlib.php systemlib)
194   foreach(SLIB ${EXTENSION_SYSTEMLIB_SOURCES} ${EZC_SYSTEMLIB_SOURCES})
195     embed_systemlib_byname(${TARGET} ${SLIB})
196   endforeach()
197   embed_sections(${TARGET} ${DEST})
198 endfunction(embed_all_systemlibs)
200 # Custom install function that doesn't relink, instead it uses chrpath to change it, if
201 # it's available, otherwise, it leaves the chrpath alone
202 function(HHVM_INSTALL TARGET DEST)
203   get_target_property(LOC ${TARGET} LOCATION)
204   get_target_property(TY ${TARGET} TYPE)
205   if (FOUND_CHRPATH)
206     get_target_property(RPATH ${TARGET} INSTALL_RPATH)
207     if (NOT RPATH STREQUAL "RPATH-NOTFOUND")
208       if (RPATH STREQUAL "")
209         install(CODE "execute_process(COMMAND \"${CHRPATH}\" \"-d\" \"${LOC}\" ERROR_QUIET)")
210       else()
211         install(CODE "execute_process(COMMAND \"${CHRPATH}\" \"-r\" \"${RPATH}\" \"${LOC}\" ERROR_QUIET)")
212       endif()
213     endif()
214   endif()
215   string(TOUPPER ${DEST} DEST_UPPER)
216   install(CODE "FILE(INSTALL DESTINATION \"\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_${DEST_UPPER}DIR}\" TYPE ${TY} FILES \"${LOC}\")")
217 endfunction(HHVM_INSTALL)
219 function(HHVM_PUBLIC_HEADERS TARGET)
220   install(
221     CODE "INCLUDE(\"${HPHP_HOME}/CMake/HPHPFunctions.cmake\")
222       HHVM_INSTALL_HEADERS(${TARGET} ${HPHP_HOME}
223       \"\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}\" ${ARGN})"
224     COMPONENT dev)
225 endfunction()
227 function(HHVM_INSTALL_HEADERS TARGET SRCROOT DEST)
228   message(STATUS "Installing header files for ${TARGET}")
229   foreach(src_rel ${ARGN})
230     # Determine the relative directory name so that we can mirror the
231     # directory structure in the output
232     file(RELATIVE_PATH dest_rel ${SRCROOT} ${src_rel})
233     if (IS_ABSOLUTE ${dest_rel})
234       message(WARNING "${TARGET}: Header file ${dest_rel} is not inside ${SRCROOT}")
235     else()
236       string(FIND ${dest_rel} / slash_pos REVERSE)
237       if(slash_pos EQUAL -1)
238         set(dest_rel)
239       else()
240         string(SUBSTRING ${dest_rel} 0 ${slash_pos} dest_rel)
241       endif()
242       file(COPY ${src_rel}
243         DESTINATION "$ENV{DESTDIR}${DEST}/${dest_rel}"
244         NO_SOURCE_PERMISSIONS)
245     endif()
246   endforeach()
247 endfunction()
249 macro(HHVM_EXT_OPTION EXTNAME PACKAGENAME)
250   if (NOT DEFINED EXT_${EXTNAME})
251     # Implicit check
252     find_package(${PACKAGENAME})
253   elseif (EXT_${EXTNAME} STREQUAL "ON")
254     # Explicit check
255     find_package(${PACKAGENAME} REQUIRED)
256   endif()
257 endmacro()
259 # Remove all files matching a set of patterns, and,
260 # optionally, not matching a second set of patterns,
261 # from a set of lists.
263 # Example:
264 # This will remove all files in the CPP_SOURCES list
265 # matching "/test/" or "Test.cpp$", but not matching
266 # "BobTest.cpp$".
267 # HHVM_REMOVE_MATCHES_FROM_LISTS(CPP_SOURCES MATCHES "/test/" "Test.cpp$" IGNORE_MATCHES "BobTest.cpp$")
269 # Parameters:
271 # [...]:
272 # The names of the lists to remove matches from.
274 # [MATCHES ...]:
275 # The matches to remove from the lists.
277 # [IGNORE_MATCHES ...]:
278 # The matches not to remove, even if they match
279 # the main set of matches to remove.
280 function(HHVM_REMOVE_MATCHES_FROM_LISTS)
281   set(LISTS_TO_SEARCH)
282   set(MATCHES_TO_REMOVE)
283   set(MATCHES_TO_IGNORE)
284   set(argumentState 0)
285   foreach (arg ${ARGN})
286     if ("x${arg}" STREQUAL "xMATCHES")
287       set(argumentState 1)
288     elseif ("x${arg}" STREQUAL "xIGNORE_MATCHES")
289       set(argumentState 2)
290     elseif (argumentState EQUAL 0)
291       list(APPEND LISTS_TO_SEARCH ${arg})
292     elseif (argumentState EQUAL 1)
293       list(APPEND MATCHES_TO_REMOVE ${arg})
294     elseif (argumentState EQUAL 2)
295       list(APPEND MATCHES_TO_IGNORE ${arg})
296     else()
297       message(FATAL_ERROR "Unknown argument state!")
298     endif()
299   endforeach()
301   foreach (theList ${LISTS_TO_SEARCH})
302     foreach (entry ${${theList}})
303       foreach (match ${MATCHES_TO_REMOVE})
304         if (${entry} MATCHES ${match})
305           set(SHOULD_IGNORE OFF)
306           foreach (ign ${MATCHES_TO_IGNORE})
307             if (${entry} MATCHES ${ign})
308               set(SHOULD_IGNORE ON)
309               break()
310             endif()
311           endforeach()
313           if (NOT SHOULD_IGNORE)
314             list(REMOVE_ITEM ${theList} ${entry})
315           endif()
316         endif()
317       endforeach()
318     endforeach()
319     set(${theList} ${${theList}} PARENT_SCOPE)
320   endforeach()
321 endfunction()
323 # Automatically create source_group directives for the sources passed in.
324 function(auto_source_group rootName rootDir)
325   file(TO_CMAKE_PATH "${rootDir}" rootDir)
326   string(LENGTH "${rootDir}" rootDirLength)
327   set(sourceGroups)
328   foreach (fil ${ARGN})
329     file(TO_CMAKE_PATH "${fil}" filePath)
330     string(FIND "${filePath}" "/" rIdx REVERSE)
331     if (rIdx EQUAL -1)
332       message(FATAL_ERROR "Unable to locate the final forward slash in '${filePath}'!")
333     endif()
334     string(SUBSTRING "${filePath}" 0 ${rIdx} filePath)
336     string(LENGTH "${filePath}" filePathLength)
337     string(FIND "${filePath}" "${rootDir}" rIdx)
338     if (rIdx EQUAL 0)
339       math(EXPR filePathLength "${filePathLength} - ${rootDirLength}")
340       string(SUBSTRING "${filePath}" ${rootDirLength} ${filePathLength} fileGroup)
342       string(REPLACE "/" "\\" fileGroup "${fileGroup}")
343       set(fileGroup "\\${rootName}${fileGroup}")
345       list(FIND sourceGroups "${fileGroup}" rIdx)
346       if (rIdx EQUAL -1)
347         list(APPEND sourceGroups "${fileGroup}")
348         source_group("${fileGroup}" REGULAR_EXPRESSION "${filePath}/[^/.]+(.(tab|yy))?.(c|cc|cpp|h|hpp|ll|php|tcc|y)$")
349       endif()
350     endif()
351   endforeach()
352 endfunction()
354 macro(add_precompiled_header PrecompiledHead PrecompiledSrc SourcesVar)
355   if (MSVC AND MSVC_ENABLE_PCH)
356     get_filename_component(PrecompiledHeader "${PrecompiledHead}" ABSOLUTE)
357     get_filename_component(PrecompiledSource "${PrecompiledSrc}" ABSOLUTE)
358     get_filename_component(PrecompiledBasename "${PrecompiledHeader}" NAME_WE)
359     get_filename_component(PrecompiledHeaderFilename "${PrecompiledHeader}" NAME)
360     set(PrecompiledBinary "${CMAKE_CURRENT_BINARY_DIR}/${PrecompiledBasename}.pch")
361     set(Sources ${${SourcesVar}})
363     set_source_files_properties(${PrecompiledSource} PROPERTIES
364       COMPILE_FLAGS "/Yc\"${PrecompiledHeaderFilename}\" /Fp\"${PrecompiledBinary}\""
365       OBJECT_OUTPUTS "${PrecompiledBinary}")
366     set_source_files_properties(${Sources} PROPERTIES
367       COMPILE_FLAGS "/Yu\"${PrecompiledHeader}\" /FI\"${PrecompiledHeader}\" /Fp\"${PrecompiledBinary}\""
368       OBJECT_DEPENDS "${PrecompiledBinary}")
370     list(APPEND ${SourcesVar} ${PrecompiledSource} ${PrecompiledHeader})
371   endif()
372 endmacro()
374 function(parse_version PREFIX VERSION)
375   if (NOT ${VERSION} MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+)?(-.+)?$")
376     message(FATAL_ERROR "VERSION must conform to X.Y(.Z)?(-.+)?")
377   endif()
379   string(FIND ${VERSION} "-" SUFFIX_POS)
380   set(SUFFIX "")
381   if (SUFFIX_POS)
382     string(SUBSTRING ${VERSION} ${SUFFIX_POS} -1 SUFFIX)
383     string(SUBSTRING ${VERSION} 0 ${SUFFIX_POS} NUMERIC_VERSION)
384   else()
385     set(NUMERIC_VERSION ${VERSION})
386   endif()
388   string(REPLACE "." ";" VERSION_LIST "${NUMERIC_VERSION}")
389   list(GET VERSION_LIST 0 MAJOR)
390   list(GET VERSION_LIST 1 MINOR)
391   list(LENGTH VERSION_LIST VERSION_LIST_LENGTH)
392   if (VERSION_LIST_LENGTH GREATER 2)
393     list(GET VERSION_LIST 2 PATCH)
394   else()
395     set(PATCH 0)
396   endif()
398   set(${PREFIX}MAJOR ${MAJOR} PARENT_SCOPE)
399   set(${PREFIX}MINOR ${MINOR} PARENT_SCOPE)
400   set(${PREFIX}PATCH ${PATCH} PARENT_SCOPE)
401   set(${PREFIX}SUFFIX ${SUFFIX} PARENT_SCOPE)
402 endfunction()
404 # MSVC doesn't support a --whole-archive flag, but newer versions
405 # of CMake do support object libraries, which give the same result.
406 # As we can't easily upgrade the normal builds to CMake 3.0, we
407 # will just require CMake 3.0+ for MSVC builds only.
408 function(add_object_library libraryName)
409   if (MSVC)
410     add_library(${libraryName} OBJECT ${ARGN})
411   else()
412     add_library(${libraryName} STATIC ${ARGN})
413   endif()
414 endfunction()
416 # Get what might be the objects of the object libraries, if needed.
417 function(get_object_libraries_objects targetVariable)
418   set(OBJECTS)
419   if (MSVC)
420     foreach (fil ${ARGN})
421       list(APPEND OBJECTS $<TARGET_OBJECTS:${fil}>)
422     endforeach()
423   endif()
425   set(${targetVariable} ${OBJECTS} PARENT_SCOPE)
426 endfunction()
428 # Add the additional link targets for a set of object libraries,
429 # if needed.
430 function(link_object_libraries target)
431   if (MSVC)
432     return()
433   endif()
435   set(WHOLE_ARCHIVE_LIBS)
436   foreach (fil ${ARGN})
437     list(APPEND WHOLE_ARCHIVE_LIBS ${fil})
438   endforeach()
440   set(ANCHOR_SYMS)
441   if (APPLE)
442     set(ANCHOR_SYMS
443       -Wl,-pagezero_size,0x00001000
444       # Set the .text.keep section to be executable.
445       -Wl,-segprot,.text,rx,rx)
446     foreach(lib ${WHOLE_ARCHIVE_LIBS})
447       # It's important to use -Xlinker and not -Wl here: ${lib} needs to be its
448       # own option on the command line, since target_link_libraries will expand it
449       # from its logical name here into the full .a path. (Eww.)
450       list(APPEND ANCHOR_SYMS -Xlinker -force_load -Xlinker ${lib})
451     endforeach()
452   else()
453     set(ANCHOR_SYMS -Wl,--whole-archive ${WHOLE_ARCHIVE_LIBS} -Wl,--no-whole-archive)
454   endif()
456   target_link_libraries(${target} ${ANCHOR_SYMS})
457 endfunction()
459 # This should be called for object libraries, rather than calling
460 # hphp_link directly.
461 function(object_library_hphp_link target)
462   # Gold doesn't need this, and MSVC can't have it. (see below)
463   if (NOT ENABLE_LD_GOLD AND NOT MSVC)
464     hphp_link(${target})
465   endif()
466 endfunction()
468 # If a library needs to be linked in to make GNU ld happy,
469 # it should be done by calling this.
470 function(object_library_ld_link_libraries target)
471   if (${ARGC})
472     # CMake doesn't allow calls to target_link_libraries if the target
473     # is an OBJECT library, so MSVC can't have this.
474     if (NOT MSVC)
475       target_link_libraries(${target} ${ARGN})
476     endif()
477   endif()
478 endfunction()