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