62e185b783818ed85c96d694b82116df12281348
[WebKit-https.git] / Source / cmake / OptionsCommon.cmake
1 add_definitions(-DBUILDING_WITH_CMAKE=1)
2 add_definitions(-DHAVE_CONFIG_H=1)
3
4 # CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS only matters with GCC >= 4.7.0.  Since this
5 # version, -P does not output empty lines, which currently breaks make_names.pl in
6 # WebCore. Investigating whether make_names.pl should be changed instead is left as an exercise to
7 # the reader.
8 if (MSVC)
9     set(CODE_GENERATOR_PREPROCESSOR_ARGUMENTS "/nologo /EP /TP")
10     set(CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS_ARGUMENTS ${CODE_GENERATOR_PREPROCESSOR_ARGUMENTS})
11 else ()
12     set(CODE_GENERATOR_PREPROCESSOR_ARGUMENTS "-E -P -x c++")
13     set(CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS_ARGUMENTS "-E -x c++")
14 endif ()
15
16 set(CODE_GENERATOR_PREPROCESSOR "\"${CMAKE_CXX_COMPILER}\" ${CODE_GENERATOR_PREPROCESSOR_ARGUMENTS}")
17 set(CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS "\"${CMAKE_CXX_COMPILER}\" ${CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS_ARGUMENTS}")
18
19 option(USE_THIN_ARCHIVES "Produce all static libraries as thin archives" ON)
20 if (USE_THIN_ARCHIVES)
21     execute_process(COMMAND ${CMAKE_AR} -V OUTPUT_VARIABLE AR_VERSION)
22     if ("${AR_VERSION}" MATCHES "^GNU ar")
23         set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> crT <TARGET> <LINK_FLAGS> <OBJECTS>")
24         set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> crT <TARGET> <LINK_FLAGS> <OBJECTS>")
25         set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> rT <TARGET> <LINK_FLAGS> <OBJECTS>")
26         set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> rT <TARGET> <LINK_FLAGS> <OBJECTS>")
27     endif ()
28 endif ()
29
30 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
31 define_property(TARGET PROPERTY FOLDER INHERITED BRIEF_DOCS "folder" FULL_DOCS "IDE folder name")
32
33 if (CMAKE_GENERATOR STREQUAL "Ninja")
34     if (COMPILER_IS_CLANG)
35         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fcolor-diagnostics")
36         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcolor-diagnostics")
37     else ()
38         if (CMAKE_COMPILER_IS_GNUCC)
39             set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fdiagnostics-color=always")
40         endif ()
41         if (CMAKE_COMPILER_IS_GNUCXX)
42             set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")
43         endif ()
44     endif ()
45 endif ()
46
47 # FIXME: Some warning flags should probably be set in WEBKIT_SET_EXTRA_COMPILER_FLAGS instead.
48 # But language-specific warnings probably cannot be moved there.
49 if (COMPILER_IS_GCC_OR_CLANG)
50     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing")
51     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
52
53     if (COMPILER_IS_CLANG_CL)
54         # clang-cl.exe impersonates cl.exe so some clang arguments like -fno-rtti are
55         # represented using cl.exe's options and should not be passed as flags, so 
56         # we do not add -fno-rtti or -fno-exceptions for clang-cl
57
58         # FIXME: These warnings should be addressed
59         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-undef -Wno-macro-redefined -Wno-unknown-pragmas -Wno-nonportable-include-path -Wno-unknown-argument")
60         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-undef -Wno-macro-redefined -Wno-unknown-pragmas -Wno-nonportable-include-path -Wno-unknown-argument")
61     else ()
62         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-exceptions")
63         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -fno-exceptions -fno-rtti")
64
65         if (WIN32)
66             set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mno-ms-bitfields -Wno-unknown-pragmas")
67             add_definitions(-D{CMAKE_CXX_FLAGS} -mno-ms-bitfields -Wno-unknown-pragmas)
68             add_definitions(-D__USE_MINGW_ANSI_STDIO=1)
69         endif ()
70     endif ()
71
72     if (NOT (COMPILER_IS_CLANG AND "${CLANG_VERSION}" VERSION_LESS 4.0.0))
73         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-expansion-to-defined")
74         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-expansion-to-defined")
75     endif ()
76
77     if (CMAKE_COMPILER_IS_GNUCXX AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER "7.0")
78         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-noexcept-type")
79     endif ()
80 endif ()
81
82 # Ensure that the default include system directories are added to the list of CMake implicit includes.
83 # This workarounds an issue that happens when using GCC 6 and using system includes (-isystem).
84 # For more details check: https://bugs.webkit.org/show_bug.cgi?id=161697
85 macro(DETERMINE_GCC_SYSTEM_INCLUDE_DIRS _lang _compiler _flags _result)
86     file(WRITE "${CMAKE_BINARY_DIR}/CMakeFiles/dummy" "\n")
87     separate_arguments(_buildFlags UNIX_COMMAND "${_flags}")
88     execute_process(COMMAND ${_compiler} ${_buildFlags} -v -E -x ${_lang} -dD dummy
89                     WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/CMakeFiles OUTPUT_QUIET
90                     ERROR_VARIABLE _gccOutput)
91     file(REMOVE "${CMAKE_BINARY_DIR}/CMakeFiles/dummy")
92     if ("${_gccOutput}" MATCHES "> search starts here[^\n]+\n *(.+) *\n *End of (search) list")
93         set(${_result} ${CMAKE_MATCH_1})
94         string(REPLACE "\n" " " ${_result} "${${_result}}")
95         separate_arguments(${_result})
96     endif ()
97 endmacro()
98
99 if (CMAKE_COMPILER_IS_GNUCC)
100    DETERMINE_GCC_SYSTEM_INCLUDE_DIRS("c" "${CMAKE_C_COMPILER}" "${CMAKE_C_FLAGS}" SYSTEM_INCLUDE_DIRS)
101    set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES} ${SYSTEM_INCLUDE_DIRS})
102 endif ()
103
104 if (CMAKE_COMPILER_IS_GNUCXX)
105    DETERMINE_GCC_SYSTEM_INCLUDE_DIRS("c++" "${CMAKE_CXX_COMPILER}" "${CMAKE_CXX_FLAGS}" SYSTEM_INCLUDE_DIRS)
106    set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES} ${SYSTEM_INCLUDE_DIRS})
107 endif ()
108
109 # Detect Cortex-A53 core if CPU is ARM64 and OS is Linux.
110 # Query /proc/cpuinfo for each available core and check reported CPU part number: 0xd03 signals Cortex-A53.
111 # (see Main ID Register in ARM Cortex-A53 MPCore Processor Technical Reference Manual)
112 set(WTF_CPU_ARM64_CORTEXA53_INITIALVALUE OFF)
113 if (WTF_CPU_ARM64 AND (${CMAKE_SYSTEM_NAME} STREQUAL "Linux"))
114     execute_process(COMMAND nproc OUTPUT_VARIABLE PROC_COUNT)
115     math(EXPR PROC_MAX ${PROC_COUNT}-1)
116     foreach (PROC_ID RANGE ${PROC_MAX})
117         execute_process(COMMAND taskset -c ${PROC_ID} grep "^CPU part" /proc/cpuinfo OUTPUT_VARIABLE PROC_PART)
118         if (PROC_PART MATCHES "0xd03")
119             set(WTF_CPU_ARM64_CORTEXA53_INITIALVALUE ON)
120             break ()
121         endif ()
122     endforeach ()
123 endif ()
124 option(WTF_CPU_ARM64_CORTEXA53 "Enable Cortex-A53-specific code paths" ${WTF_CPU_ARM64_CORTEXA53_INITIALVALUE})
125
126 if (WTF_CPU_ARM64_CORTEXA53)
127     if (NOT WTF_CPU_ARM64)
128         message(FATAL_ERROR "WTF_CPU_ARM64_CORTEXA53 set without WTF_CPU_ARM64")
129     endif ()
130     include(TestCXXAcceptsFlag)
131     CHECK_CXX_ACCEPTS_FLAG(-mfix-cortex-a53-835769 CXX_ACCEPTS_MFIX_CORTEX_A53_835769)
132     if (CXX_ACCEPTS_MFIX_CORTEX_A53_835769)
133         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfix-cortex-a53-835769")
134         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfix-cortex-a53-835769")
135         message(STATUS "Enabling Cortex-A53 workaround for compiler and disabling GNU gold linker, because it doesn't support this workaround.")
136     endif ()
137 endif ()
138
139 EXPOSE_VARIABLE_TO_BUILD(WTF_CPU_ARM64_CORTEXA53)
140
141 set(ARM_TRADITIONAL_DETECTED FALSE)
142 if (WTF_CPU_ARM)
143     set(ARM_THUMB2_TEST_SOURCE
144     "
145     #if !defined(thumb2) && !defined(__thumb2__)
146     #error \"Thumb2 instruction set isn't available\"
147     #endif
148     int main() {}
149    ")
150
151     include(CheckCXXSourceCompiles)
152     CHECK_CXX_SOURCE_COMPILES("${ARM_THUMB2_TEST_SOURCE}" ARM_THUMB2_DETECTED)
153     if (NOT ARM_THUMB2_DETECTED)
154         set(ARM_TRADITIONAL_DETECTED TRUE)
155         # See https://bugs.webkit.org/show_bug.cgi?id=159880#c4 for details.
156         message(STATUS "Disabling GNU gold linker, because it doesn't support ARM instruction set properly.")
157     endif ()
158 endif ()
159
160 # Use ld.gold if it is available and isn't disabled explicitly
161 include(CMakeDependentOption)
162 CMAKE_DEPENDENT_OPTION(USE_LD_GOLD "Use GNU gold linker" ON
163                        "NOT CXX_ACCEPTS_MFIX_CORTEX_A53_835769;NOT ARM_TRADITIONAL_DETECTED;NOT WIN32;NOT APPLE" OFF)
164 if (USE_LD_GOLD)
165     execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE LD_VERSION)
166     if ("${LD_VERSION}" MATCHES "GNU gold")
167         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags")
168         set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags")
169     else ()
170         message(WARNING "GNU gold linker isn't available, using the default system linker.")
171         set(USE_LD_GOLD OFF)
172     endif ()
173 endif ()
174
175 set(ENABLE_DEBUG_FISSION_DEFAULT OFF)
176 if (USE_LD_GOLD AND CMAKE_BUILD_TYPE STREQUAL "Debug")
177     include(TestCXXAcceptsFlag)
178     CHECK_CXX_ACCEPTS_FLAG(-gsplit-dwarf CXX_ACCEPTS_GSPLIT_DWARF)
179     if (CXX_ACCEPTS_GSPLIT_DWARF)
180         set(ENABLE_DEBUG_FISSION_DEFAULT ON)
181     endif ()
182 endif ()
183
184 option(DEBUG_FISSION "Use Debug Fission support" ${ENABLE_DEBUG_FISSION_DEFAULT})
185
186 if (DEBUG_FISSION)
187     if (NOT USE_LD_GOLD)
188         message(FATAL_ERROR "Need GNU gold linker for Debug Fission support")
189     endif ()
190     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -gsplit-dwarf")
191     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gsplit-dwarf")
192     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gdb-index")
193     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gdb-index")
194 endif ()
195
196 if (COMPILER_IS_CLANG)
197     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Qunused-arguments")
198     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments")
199 endif ()
200
201 string(TOLOWER ${CMAKE_HOST_SYSTEM_PROCESSOR} LOWERCASE_CMAKE_HOST_SYSTEM_PROCESSOR)
202 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND NOT "${LOWERCASE_CMAKE_HOST_SYSTEM_PROCESSOR}" MATCHES "x86_64")
203     # To avoid out of memory when building with debug option in 32bit system.
204     # See https://bugs.webkit.org/show_bug.cgi?id=77327
205     set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "-Wl,--no-keep-memory ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
206 endif ()
207
208 if (NOT MSVC)
209     string(REGEX MATCHALL "-fsanitize=[^ ]*" ENABLED_COMPILER_SANITIZERS ${CMAKE_CXX_FLAGS})
210 endif ()
211
212 if (UNIX AND NOT APPLE AND NOT ENABLED_COMPILER_SANITIZERS)
213     set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined ${CMAKE_SHARED_LINKER_FLAGS}")
214 endif ()
215
216 if (USE_ARM_LLVM_DISASSEMBLER)
217     find_package(LLVM REQUIRED)
218     SET_AND_EXPOSE_TO_BUILD(HAVE_LLVM TRUE)
219 endif ()
220
221 # Enable the usage of OpenMP.
222 #  - At this moment, OpenMP is only used as an alternative implementation
223 #    to native threads for the parallelization of the SVG filters.
224 if (USE_OPENMP)
225     find_package(OpenMP REQUIRED)
226     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
227     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
228 endif ()
229
230 # GTK uses the GNU installation directories as defaults.
231 if (NOT PORT STREQUAL "GTK")
232     set(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Absolute path to library installation directory")
233     set(EXEC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Absolute path to executable installation directory")
234     set(LIBEXEC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Absolute path to install executables executed by the library")
235 endif ()
236
237 # The Ninja generator does not yet know how to build archives in pieces, and so response
238 # files must be used to deal with very long linker command lines.
239 # CMake does this automatically, but the condition was wrong on Linux until CMake 3.2.
240 # See https://cmake.org/Bug/view.php?id=14892
241 if ((CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux") AND (CMAKE_VERSION VERSION_LESS 3.2))
242     set(CMAKE_NINJA_FORCE_RESPONSE_FILE 1)
243 endif ()
244
245 # Macros for determining HAVE values.
246 include(CheckIncludeFile)
247 include(CheckFunctionExists)
248 include(CheckSymbolExists)
249 include(CheckStructHasMember)
250 include(CheckTypeSize)
251
252 macro(_HAVE_CHECK_INCLUDE _variable _header)
253     check_include_file(${_header} ${_variable}_value)
254     SET_AND_EXPOSE_TO_BUILD(${_variable} ${_variable}_value)
255 endmacro()
256
257 macro(_HAVE_CHECK_FUNCTION _variable _function)
258     check_function_exists(${_function} ${_variable}_value)
259     SET_AND_EXPOSE_TO_BUILD(${_variable} ${_variable}_value)
260 endmacro()
261
262 macro(_HAVE_CHECK_SYMBOL _variable _symbol _header)
263     check_symbol_exists(${_symbol} ${_header} ${_variable}_value)
264     SET_AND_EXPOSE_TO_BUILD(${_variable} ${_variable}_value)
265 endmacro()
266
267 macro(_HAVE_CHECK_STRUCT _variable _struct _member _header)
268     check_struct_has_member(${_struct} ${_member} ${_header} ${_variable}_value)
269     SET_AND_EXPOSE_TO_BUILD(${_variable} ${_variable}_value)
270 endmacro()
271
272 # Check whether features.h header exists.
273 # Including glibc's one defines __GLIBC__, that is used in Platform.h
274 _HAVE_CHECK_INCLUDE(HAVE_FEATURES_H features.h)
275
276 # Check for headers
277 _HAVE_CHECK_INCLUDE(HAVE_ERRNO_H errno.h)
278 _HAVE_CHECK_INCLUDE(HAVE_LANGINFO_H langinfo.h)
279 _HAVE_CHECK_INCLUDE(HAVE_MMAP sys/mman.h)
280 _HAVE_CHECK_INCLUDE(HAVE_PTHREAD_NP_H pthread_np.h)
281 _HAVE_CHECK_INCLUDE(HAVE_STRINGS_H strings.h)
282 _HAVE_CHECK_INCLUDE(HAVE_SYS_PARAM_H sys/param.h)
283 _HAVE_CHECK_INCLUDE(HAVE_SYS_TIME_H sys/time.h)
284 _HAVE_CHECK_INCLUDE(HAVE_SYS_TIMEB_H sys/timeb.h)
285
286 # Check for functions
287 _HAVE_CHECK_FUNCTION(HAVE_ALIGNED_MALLOC _aligned_malloc)
288 _HAVE_CHECK_FUNCTION(HAVE_ISDEBUGGERPRESENT IsDebuggerPresent)
289 _HAVE_CHECK_FUNCTION(HAVE_LOCALTIME_R localtime_r)
290 _HAVE_CHECK_FUNCTION(HAVE_STRNSTR strnstr)
291 _HAVE_CHECK_FUNCTION(HAVE_TIMEGM timegm)
292 _HAVE_CHECK_FUNCTION(HAVE_VASPRINTF vasprintf)
293
294 # Check for symbols
295 _HAVE_CHECK_SYMBOL(HAVE_REGEX_H regexec regex.h)
296 # Windows has signal.h but is missing symbols that are used in calls to signal.
297 _HAVE_CHECK_SYMBOL(HAVE_SIGNAL_H SIGTRAP signal.h)
298
299 # Check for struct members
300 _HAVE_CHECK_STRUCT(HAVE_STAT_BIRTHTIME "struct stat" st_birthtime sys/stat.h)
301 _HAVE_CHECK_STRUCT(HAVE_TM_GMTOFF "struct tm" tm_gmtoff time.h)
302 _HAVE_CHECK_STRUCT(HAVE_TM_ZONE "struct tm" tm_zone time.h)
303
304 # Check for int types
305 check_type_size("__int128_t" INT128_VALUE)
306
307 if (HAVE_INT128_VALUE)
308   SET_AND_EXPOSE_TO_BUILD(HAVE_INT128_T INT128_VALUE)
309 endif ()