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