c17d5ea9da9d3801b499b7ce578c7482e413cbdf
[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     set(CODE_GENERATOR_PREPROCESSOR "\"${CODE_GENERATOR_PREPROCESSOR_EXECUTABLE}\" /nologo /EP")
12     set(CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS "${CODE_GENERATOR_PREPROCESSOR}")
13 else ()
14     set(CODE_GENERATOR_PREPROCESSOR "${CMAKE_CXX_COMPILER} -E -P -x c++")
15     set(CODE_GENERATOR_PREPROCESSOR_WITH_LINEMARKERS "${CMAKE_CXX_COMPILER} -E -x c++")
16 endif ()
17
18 option(USE_THIN_ARCHIVES "Produce all static libraries as thin archives" ON)
19 if (USE_THIN_ARCHIVES)
20     execute_process(COMMAND ${CMAKE_AR} -V OUTPUT_VARIABLE AR_VERSION)
21     if ("${AR_VERSION}" MATCHES "^GNU ar")
22         set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> crT <TARGET> <LINK_FLAGS> <OBJECTS>")
23         set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> crT <TARGET> <LINK_FLAGS> <OBJECTS>")
24         set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> rT <TARGET> <LINK_FLAGS> <OBJECTS>")
25         set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> rT <TARGET> <LINK_FLAGS> <OBJECTS>")
26     endif ()
27 endif ()
28
29 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
30 define_property(TARGET PROPERTY FOLDER INHERITED BRIEF_DOCS "folder" FULL_DOCS "IDE folder name")
31
32 if (COMPILER_IS_GCC_OR_CLANG)
33     set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fno-exceptions -fno-strict-aliasing")
34     set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-exceptions -fno-strict-aliasing -fno-rtti")
35     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y")
36 endif ()
37
38 if (COMPILER_IS_CLANG AND CMAKE_GENERATOR STREQUAL "Ninja")
39     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fcolor-diagnostics")
40     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcolor-diagnostics")
41 endif ()
42
43 # Ensure that the default include system directories are added to the list of CMake implicit includes.
44 # This workarounds an issue that happens when using GCC 6 and using system includes (-isystem).
45 # For more details check: https://bugs.webkit.org/show_bug.cgi?id=161697
46 macro(DETERMINE_GCC_SYSTEM_INCLUDE_DIRS _lang _compiler _flags _result)
47     file(WRITE "${CMAKE_BINARY_DIR}/CMakeFiles/dummy" "\n")
48     separate_arguments(_buildFlags UNIX_COMMAND "${_flags}")
49     execute_process(COMMAND ${_compiler} ${_buildFlags} -v -E -x ${_lang} -dD dummy
50                     WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/CMakeFiles OUTPUT_QUIET
51                     ERROR_VARIABLE _gccOutput)
52     file(REMOVE "${CMAKE_BINARY_DIR}/CMakeFiles/dummy")
53     if ("${_gccOutput}" MATCHES "> search starts here[^\n]+\n *(.+) *\n *End of (search) list")
54         set(${_result} ${CMAKE_MATCH_1})
55         string(REPLACE "\n" " " ${_result} "${${_result}}")
56         separate_arguments(${_result})
57     endif ()
58 endmacro()
59
60 if (CMAKE_COMPILER_IS_GNUCC)
61    DETERMINE_GCC_SYSTEM_INCLUDE_DIRS("c" "${CMAKE_C_COMPILER}" "${CMAKE_C_FLAGS}" SYSTEM_INCLUDE_DIRS)
62    set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES} ${SYSTEM_INCLUDE_DIRS})
63 endif ()
64
65 if (CMAKE_COMPILER_IS_GNUCXX)
66    DETERMINE_GCC_SYSTEM_INCLUDE_DIRS("c++" "${CMAKE_CXX_COMPILER}" "${CMAKE_CXX_FLAGS}" SYSTEM_INCLUDE_DIRS)
67    set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES} ${SYSTEM_INCLUDE_DIRS})
68 endif ()
69
70 # Detect Cortex-A53 core if CPU is ARM64 and OS is Linux.
71 # Query /proc/cpuinfo for each available core and check reported CPU part number: 0xd03 signals Cortex-A53.
72 # (see Main ID Register in ARM Cortex-A53 MPCore Processor Technical Reference Manual)
73 set(WTF_CPU_ARM64_CORTEXA53_INITIALVALUE OFF)
74 if (WTF_CPU_ARM64 AND (${CMAKE_SYSTEM_NAME} STREQUAL "Linux"))
75     execute_process(COMMAND nproc OUTPUT_VARIABLE PROC_COUNT)
76     math(EXPR PROC_MAX ${PROC_COUNT}-1)
77     foreach (PROC_ID RANGE ${PROC_MAX})
78         execute_process(COMMAND taskset -c ${PROC_ID} grep "^CPU part" /proc/cpuinfo OUTPUT_VARIABLE PROC_PART)
79         if (PROC_PART MATCHES "0xd03")
80             set(WTF_CPU_ARM64_CORTEXA53_INITIALVALUE ON)
81             break ()
82         endif ()
83     endforeach ()
84 endif ()
85 option(WTF_CPU_ARM64_CORTEXA53 "Enable Cortex-A53-specific code paths" ${WTF_CPU_ARM64_CORTEXA53_INITIALVALUE})
86
87 if (WTF_CPU_ARM64_CORTEXA53)
88     if (NOT WTF_CPU_ARM64)
89         message(FATAL_ERROR "WTF_CPU_ARM64_CORTEXA53 set without WTF_CPU_ARM64")
90     endif ()
91     include(TestCXXAcceptsFlag)
92     CHECK_CXX_ACCEPTS_FLAG(-mfix-cortex-a53-835769 CXX_ACCEPTS_MFIX_CORTEX_A53_835769)
93     if (CXX_ACCEPTS_MFIX_CORTEX_A53_835769)
94         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfix-cortex-a53-835769")
95         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfix-cortex-a53-835769")
96         message(STATUS "Enabling Cortex-A53 workaround for compiler and disabling GNU gold linker, because it doesn't support this workaround.")
97     endif ()
98 endif ()
99
100 EXPOSE_VARIABLE_TO_BUILD(WTF_CPU_ARM64_CORTEXA53)
101
102 set(ARM_TRADITIONAL_DETECTED FALSE)
103 if (WTF_CPU_ARM)
104     set(ARM_THUMB2_TEST_SOURCE
105     "
106     #if !defined(thumb2) && !defined(__thumb2__)
107     #error \"Thumb2 instruction set isn't available\"
108     #endif
109     int main() {}
110    ")
111
112     include(CheckCXXSourceCompiles)
113     CHECK_CXX_SOURCE_COMPILES("${ARM_THUMB2_TEST_SOURCE}" ARM_THUMB2_DETECTED)
114     if (NOT ARM_THUMB2_DETECTED)
115         set(ARM_TRADITIONAL_DETECTED TRUE)
116         # See https://bugs.webkit.org/show_bug.cgi?id=159880#c4 for details.
117         message(STATUS "Disabling GNU gold linker, because it doesn't support ARM instruction set properly.")
118     endif ()
119 endif ()
120
121 # Use ld.gold if it is available and isn't disabled explicitly
122 include(CMakeDependentOption)
123 CMAKE_DEPENDENT_OPTION(USE_LD_GOLD "Use GNU gold linker" ON
124                        "NOT CXX_ACCEPTS_MFIX_CORTEX_A53_835769;NOT ARM_TRADITIONAL_DETECTED" OFF)
125 if (USE_LD_GOLD)
126     execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE LD_VERSION)
127     if ("${LD_VERSION}" MATCHES "GNU gold")
128         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags")
129         set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags")
130     else ()
131         message(WARNING "GNU gold linker isn't available, using the default system linker.")
132         set(USE_LD_GOLD OFF)
133     endif ()
134 endif ()
135
136 set(ENABLE_DEBUG_FISSION_DEFAULT OFF)
137 if (USE_LD_GOLD AND CMAKE_BUILD_TYPE STREQUAL "Debug")
138     include(TestCXXAcceptsFlag)
139     CHECK_CXX_ACCEPTS_FLAG(-gsplit-dwarf CXX_ACCEPTS_GSPLIT_DWARF)
140     if (CXX_ACCEPTS_GSPLIT_DWARF)
141         set(ENABLE_DEBUG_FISSION_DEFAULT ON)
142     endif ()
143 endif ()
144
145 option(DEBUG_FISSION "Use Debug Fission support" ${ENABLE_DEBUG_FISSION_DEFAULT})
146
147 if (DEBUG_FISSION)
148     if (NOT USE_LD_GOLD)
149         message(FATAL_ERROR "Need GNU gold linker for Debug Fission support")
150     endif ()
151     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -gsplit-dwarf")
152     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gsplit-dwarf")
153     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gdb-index")
154     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gdb-index")
155 endif ()
156
157 if (COMPILER_IS_CLANG)
158     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Qunused-arguments")
159     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments")
160 endif ()
161
162 string(TOLOWER ${CMAKE_HOST_SYSTEM_PROCESSOR} LOWERCASE_CMAKE_HOST_SYSTEM_PROCESSOR)
163 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND "${LOWERCASE_CMAKE_HOST_SYSTEM_PROCESSOR}" MATCHES "(i[3-6]86|x86|armhf)")
164     # To avoid out of memory when building with debug option in 32bit system.
165     # See https://bugs.webkit.org/show_bug.cgi?id=77327
166     set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "-Wl,--no-keep-memory ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
167 endif ()
168
169 if (NOT MSVC)
170     string(REGEX MATCHALL "-fsanitize=[^ ]*" ENABLED_COMPILER_SANITIZERS ${CMAKE_CXX_FLAGS})
171 endif ()
172
173 if (UNIX AND NOT APPLE AND NOT ENABLED_COMPILER_SANITIZERS)
174     set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined ${CMAKE_SHARED_LINKER_FLAGS}")
175 endif ()
176
177 if (USE_ARM_LLVM_DISASSEMBLER)
178     find_package(LLVM REQUIRED)
179     SET_AND_EXPOSE_TO_BUILD(HAVE_LLVM TRUE)
180 endif ()
181
182 # Enable the usage of OpenMP.
183 #  - At this moment, OpenMP is only used as an alternative implementation
184 #    to native threads for the parallelization of the SVG filters.
185 if (USE_OPENMP)
186     find_package(OpenMP REQUIRED)
187     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
188     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
189 endif ()
190
191 # GTK uses the GNU installation directories as defaults.
192 if (NOT PORT STREQUAL "GTK")
193     set(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Absolute path to library installation directory")
194     set(EXEC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Absolute path to executable installation directory")
195     set(LIBEXEC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Absolute path to install executables executed by the library")
196 endif ()
197
198 # The Ninja generator does not yet know how to build archives in pieces, and so response
199 # files must be used to deal with very long linker command lines.
200 # See https://bugs.webkit.org/show_bug.cgi?id=129771
201 # The Apple Toolchain doesn't support response files.
202 if (NOT APPLE)
203     set(CMAKE_NINJA_FORCE_RESPONSE_FILE 1)
204 endif ()
205
206 # Check whether features.h header exists.
207 # Including glibc's one defines __GLIBC__, that is used in Platform.h
208 include(CheckIncludeFiles)
209 check_include_files(features.h HAVE_FEATURES_H)
210 SET_AND_EXPOSE_TO_BUILD(HAVE_FEATURES_H ${HAVE_FEATURES_H})