Fix ReflectionTypeConstant::getTypeStructure for inherited type constants
[hiphop-php.git] / CMake / HHVMGenerateConfig.cmake
blob3a3cb2953d1e65a0b098325cf9e6274d1d79d3e2
1 # The core function for generating `hphp-config.h`. This is done here to keep
2 # the main CMake tree clean.
4 # If you add any defines here or in hphp-config.h.in, you must make sure
5 # to update the non-CMake side. If you are not sure if a particular define
6 # should be present, ask your FB reviewer.
8 include(CheckCXXSourceCompiles)
9 include(CheckFunctionExists)
10 include(CheckIncludeFile)
12 # Build a string to define the appropriate macros for the headers that
13 # we are able to find.
14 function(HHVM_GENERATE_CONFIG_HEADERS_FOUND_DEFINE_STRING destVarName)
15   # This is a list of headers to check for and add defines in the form of
16   # HAVE_*_H if they are present. Note that all slashes will be replaced
17   # with a single underscore. Please keep ordered such that all headers
18   # in a root directory are first, followed by headers in a subdirectory.
19   # Note that .h will automatically be appended to the names of these before
20   # searching for them.
21   set(HHVM_HEADERS_TO_CHECK)
22   list(APPEND HHVM_HEADERS_TO_CHECK
23     "inttypes"
24     "limits"
25     "utime"
26     "wchar"
27     "wctype"
28     "sys/mman"
29     "sys/utime"
30   )
32   set(builtString "")
33   list(LENGTH HHVM_HEADERS_TO_CHECK headersCount)
34   set(i 0)
35   while (i LESS ${headersCount})
36     list(GET HHVM_HEADERS_TO_CHECK ${i} curHeader)
37     string(REPLACE "/" "_" curHeaderClean "${curHeader}")
38     string(TOUPPER "${curHeaderClean}" curHeaderUpper)
39     CHECK_INCLUDE_FILE("${curHeader}.h" HAVE_${curHeaderUpper}_H)
40     if (${HAVE_${curHeaderUpper}_H})
41       set(builtString "${builtString}\n#define HAVE_${curHeaderUpper}_H 1")
42     else()
43       set(builtString "${builtString}\n/* #undef HAVE_${curHeaderUpper}_H */")
44     endif()
45     math(EXPR i "${i} + 1")
46   endwhile()
48   set(${destVarName} "${builtString}" PARENT_SCOPE)
49 endfunction()
51 # Build a string to define the appropriate macros for the functions that
52 # we are able to find.
53 function(HHVM_GENERATE_CONFIG_FUNCTIONS_FOUND_DEFINE_STRING destVarName)
54   # This is a list of functions that need to be checked for.
55   set(HHVM_FUNCTIONS_TO_CHECK)
56   list(APPEND HHVM_FUNCTIONS_TO_CHECK
57     "getline"
58     "mbrtowc"
59     "mkstemp"
60     "mmap"
61     "strerror"
62     "strlcpy"
63     "strtof"
64     "strtoul"
65     "utime"
66     "utimes"
67   )
69   # This is a list of functions that are known to be present under MSVC
70   # because they are implemented via Folly's portability headers. For an
71   # item in this list to have any effect, it must first fail to be found
72   # when checking the item in the main list.
73   set(HHVM_FUNCTIONS_KNOWN_TO_BE_PRESENT_MSVC)
74   list(APPEND HHVM_FUNCTIONS_KNOWN_TO_BE_PRESENT_MSVC
75     "mkstemp"
76     "mmap"
77   )
79   set(builtString "")
80   list(LENGTH HHVM_FUNCTIONS_TO_CHECK functionCount)
81   set(i 0)
82   while (i LESS ${functionCount})
83     list(GET HHVM_FUNCTIONS_TO_CHECK ${i} curFunc)
84     string(TOUPPER "${curFunc}" curFuncUpper)
85     CHECK_FUNCTION_EXISTS("${curFunc}" HAVE_${curFuncUpper})
86     if (${HAVE_${curFuncUpper}})
87       set(builtString "${builtString}\n#define HAVE_${curFuncUpper} 1")
88     else()
89       list(FIND HHVM_FUNCTIONS_KNOWN_TO_BE_PRESENT_MSVC "${curFunc}" curFuncIdx)
90       if (curFuncIdx EQUAL -1 OR NOT MSVC)
91         set(builtString "${builtString}\n/* #undef HAVE_${curFuncUpper} */")
92       else()
93         set(builtString "${builtString}\n#define HAVE_${curFuncUpper} 1 /* Implemented via Folly Portability header */")
94       endif()
95     endif()
96     math(EXPR i "${i} + 1")
97   endwhile()
99   set(${destVarName} "${builtString}" PARENT_SCOPE)
100 endfunction()
102 # Check if the source passed in compiles and add the appropriate define.
103 function(HHVM_GENERATE_CONFIG_CHECK_COMPILES destVarName defineName sourceToCheck)
104   CHECK_CXX_SOURCE_COMPILES("${sourceToCheck}" ${defineName})
105   if (${defineName})
106     set(${destVarName} "${${destVarName}}\n#define ${defineName} 1" PARENT_SCOPE)
107   else()
108     set(${destVarName} "${${destVarName}}\n/* #undef ${defineName} */" PARENT_SCOPE)
109   endif()
110 endfunction()
112 # A quick check for use by HHVM_GENERATE_CONFIG_COMPILES_DEFINE_STRING to
113 # check if a struct defined in a particular header has a member with
114 # the specified name.
115 function(HHVM_GENERATE_CONFIG_STRUCT_HAS_MEMBER destVarName defineName headers structName memberName)
116   HHVM_GENERATE_CONFIG_CHECK_COMPILES(${destVarName} ${defineName} "
117   ${headers}
118   int main(void) {
119     (void)((${structName}*)0)->${memberName};
120     return 0;
121   }")
123   set(${destVarName} ${${destVarName}} PARENT_SCOPE)
124 endfunction()
126 # A quick check for use by HHVM_GENERATE_CONFIG_COMPILES_DEFINE_STRING to
127 # check if a symbol is defined in a particular header.
128 function(HHVM_GENERATE_CONFIG_SYMBOL_EXISTS destVarName defineName headers symbolName)
129   HHVM_GENERATE_CONFIG_CHECK_COMPILES(${destVarName} ${defineName} "
130   ${headers}
131   int main(void) {
132   #ifdef ${symbolName}
133     return 0;
134   #else
135     return ((int*)(&${symbolName}))[0];
136   #endif
137   }")
139   set(${destVarName} ${${destVarName}} PARENT_SCOPE)
140 endfunction()
142 # Build a define string for checks that require checking if some source code
143 # compiles.
144 function(HHVM_GENERATE_CONFIG_COMPILES_DEFINE_STRING destVarName)
145   set(builtString "")
147   HHVM_GENERATE_CONFIG_STRUCT_HAS_MEMBER(builtString HAVE_TM_ISDST "#include <time.h>" "tm" "tm_isdst")
148   HHVM_GENERATE_CONFIG_STRUCT_HAS_MEMBER(builtString HAVE_STRUCT_TM_TM_GMTOFF "#include <time.h>" "tm" "tm_gmtoff")
149   HHVM_GENERATE_CONFIG_STRUCT_HAS_MEMBER(builtString HAVE_STRUCT_TM_TM_ZONE "#include <time.h>" "tm" "tm_zone")
150   HHVM_GENERATE_CONFIG_SYMBOL_EXISTS(builtString HAVE_DAYLIGHT "#include <time.h>" "daylight")
151   HHVM_GENERATE_CONFIG_SYMBOL_EXISTS(builtString MAJOR_IN_MKDEV "#include <mkdev.h>" "major")
152   HHVM_GENERATE_CONFIG_SYMBOL_EXISTS(builtString MAJOR_IN_SYSMACROS "#include <sysmacros.h>" "major")
153   HHVM_GENERATE_CONFIG_CHECK_COMPILES(builtString HAVE_VISIBILITY "
154   __attribute__ ((__visibility__(\"hidden\"))) void someMethod(void);
155   int main(void) {
156     return 0;
157   }")
159   set(${destVarName} "${builtString}" PARENT_SCOPE)
160 endfunction()
162 # Builds a string to define the macros for all enabled extensions.
163 function(HHVM_GENERATE_CONFIG_EXTENSIONS_ENABLED_DEFINE_STRING destVarName)
164   set(builtString "/* Extensions */")
165   set(i 0)
166   while (i LESS HHVM_EXTENSION_COUNT)
167     string(TOUPPER ${HHVM_EXTENSION_${i}_NAME} upperExtName)
168     if (${HHVM_EXTENSION_${i}_ENABLED_STATE} EQUAL 1)
169       set(builtString "${builtString}\n#define ENABLE_EXTENSION_${upperExtName} 1")
170     else()
171       set(builtString "${builtString}\n/* #undef ENABLE_EXTENSION_${upperExtName} */")
172     endif()
173     math(EXPR i "${i} + 1")
174   endwhile()
176   set(${destVarName} "${builtString}" PARENT_SCOPE)
177 endfunction()
179 # Generate the config file for HHVM.
180 function(HHVM_GENERATE_CONFIG dest)
181   HHVM_GENERATE_CONFIG_HEADERS_FOUND_DEFINE_STRING(HHVM_HEADERS_FOUND_DEFINE_STRING)
182   HHVM_GENERATE_CONFIG_FUNCTIONS_FOUND_DEFINE_STRING(HHVM_FUNCTIONS_FOUND_DEFINE_STRING)
183   HHVM_GENERATE_CONFIG_COMPILES_DEFINE_STRING(HHVM_COMPILES_DEFINE_STRING)
184   HHVM_GENERATE_CONFIG_EXTENSIONS_ENABLED_DEFINE_STRING(HHVM_EXTENSIONS_ENABLED_DEFINE_STRING)
185   configure_file("${HPHP_HOME}/hphp/util/hphp-config.h.in" "${dest}")
186 endfunction()