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