[CMake] Properly test if compiler supports compiler flags
[WebKit.git] / Source / cmake / OptionsCommon.cmake
1 add_definitions(-DBUILDING_WITH_CMAKE=1)
2 add_definitions(-DHAVE_CONFIG_H=1)
3
4 option(USE_THIN_ARCHIVES "Produce all static libraries as thin archives" ON)
5 if (USE_THIN_ARCHIVES)
6     execute_process(COMMAND ${CMAKE_AR} -V OUTPUT_VARIABLE AR_VERSION)
7     if ("${AR_VERSION}" MATCHES "^GNU ar")
8         set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> crT <TARGET> <LINK_FLAGS> <OBJECTS>")
9         set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> crT <TARGET> <LINK_FLAGS> <OBJECTS>")
10         set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> rT <TARGET> <LINK_FLAGS> <OBJECTS>")
11         set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> rT <TARGET> <LINK_FLAGS> <OBJECTS>")
12     endif ()
13 endif ()
14
15 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
16 define_property(TARGET PROPERTY FOLDER INHERITED BRIEF_DOCS "folder" FULL_DOCS "IDE folder name")
17
18 # Detect Cortex-A53 core if CPU is ARM64 and OS is Linux.
19 # Query /proc/cpuinfo for each available core and check reported CPU part number: 0xd03 signals Cortex-A53.
20 # (see Main ID Register in ARM Cortex-A53 MPCore Processor Technical Reference Manual)
21 set(WTF_CPU_ARM64_CORTEXA53_INITIALVALUE OFF)
22 if (WTF_CPU_ARM64 AND (${CMAKE_SYSTEM_NAME} STREQUAL "Linux"))
23     execute_process(COMMAND nproc OUTPUT_VARIABLE PROC_COUNT)
24     math(EXPR PROC_MAX ${PROC_COUNT}-1)
25     foreach (PROC_ID RANGE ${PROC_MAX})
26         execute_process(COMMAND taskset -c ${PROC_ID} grep "^CPU part" /proc/cpuinfo OUTPUT_VARIABLE PROC_PART)
27         if (PROC_PART MATCHES "0xd03")
28             set(WTF_CPU_ARM64_CORTEXA53_INITIALVALUE ON)
29             break ()
30         endif ()
31     endforeach ()
32 endif ()
33 option(WTF_CPU_ARM64_CORTEXA53 "Enable Cortex-A53-specific code paths" ${WTF_CPU_ARM64_CORTEXA53_INITIALVALUE})
34
35 if (WTF_CPU_ARM64_CORTEXA53)
36     if (NOT WTF_CPU_ARM64)
37         message(FATAL_ERROR "WTF_CPU_ARM64_CORTEXA53 set without WTF_CPU_ARM64")
38     endif ()
39     WEBKIT_PREPEND_GLOBAL_COMPILER_FLAG(-mfix-cortex-a53-835769)
40 endif ()
41
42 EXPOSE_VARIABLE_TO_BUILD(WTF_CPU_ARM64_CORTEXA53)
43
44 set(ARM_TRADITIONAL_DETECTED FALSE)
45 if (WTF_CPU_ARM)
46     set(ARM_THUMB2_TEST_SOURCE
47     "
48     #if !defined(thumb2) && !defined(__thumb2__)
49     #error \"Thumb2 instruction set isn't available\"
50     #endif
51     int main() {}
52    ")
53
54     CHECK_CXX_SOURCE_COMPILES("${ARM_THUMB2_TEST_SOURCE}" ARM_THUMB2_DETECTED)
55     if (NOT ARM_THUMB2_DETECTED)
56         set(ARM_TRADITIONAL_DETECTED TRUE)
57         # See https://bugs.webkit.org/show_bug.cgi?id=159880#c4 for details.
58         message(STATUS "Disabling GNU gold linker, because it doesn't support ARM instruction set properly.")
59     endif ()
60 endif ()
61
62 # Use ld.gold if it is available and isn't disabled explicitly
63 CMAKE_DEPENDENT_OPTION(USE_LD_GOLD "Use GNU gold linker" ON
64                        "NOT CXX_ACCEPTS_MFIX_CORTEX_A53_835769;NOT ARM_TRADITIONAL_DETECTED;NOT WIN32;NOT APPLE" OFF)
65 if (USE_LD_GOLD)
66     execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE LD_VERSION)
67     if ("${LD_VERSION}" MATCHES "GNU gold")
68         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags")
69         set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags")
70     else ()
71         message(WARNING "GNU gold linker isn't available, using the default system linker.")
72         set(USE_LD_GOLD OFF)
73     endif ()
74 endif ()
75
76 set(ENABLE_DEBUG_FISSION_DEFAULT OFF)
77 if (USE_LD_GOLD AND CMAKE_BUILD_TYPE STREQUAL "Debug")
78     check_cxx_compiler_flag(-gsplit-dwarf CXX_COMPILER_SUPPORTS_GSPLIT_DWARF)
79     if (CXX_COMPILER_SUPPORTS_GSPLIT_DWARF)
80         set(ENABLE_DEBUG_FISSION_DEFAULT ON)
81     endif ()
82 endif ()
83
84 option(DEBUG_FISSION "Use Debug Fission support" ${ENABLE_DEBUG_FISSION_DEFAULT})
85
86 if (DEBUG_FISSION)
87     if (NOT USE_LD_GOLD)
88         message(FATAL_ERROR "Need GNU gold linker for Debug Fission support")
89     endif ()
90     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -gsplit-dwarf")
91     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gsplit-dwarf")
92     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gdb-index")
93     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gdb-index")
94 endif ()
95
96 if (USE_ARM_LLVM_DISASSEMBLER)
97     find_package(LLVM REQUIRED)
98     SET_AND_EXPOSE_TO_BUILD(HAVE_LLVM TRUE)
99 endif ()
100
101 # Enable the usage of OpenMP.
102 #  - At this moment, OpenMP is only used as an alternative implementation
103 #    to native threads for the parallelization of the SVG filters.
104 if (USE_OPENMP)
105     find_package(OpenMP REQUIRED)
106     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
107     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
108 endif ()
109
110 # GTK uses the GNU installation directories as defaults.
111 if (NOT PORT STREQUAL "GTK")
112     set(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Absolute path to library installation directory")
113     set(EXEC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Absolute path to executable installation directory")
114     set(LIBEXEC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Absolute path to install executables executed by the library")
115 endif ()
116
117 # Macros for determining HAVE values.
118 macro(_HAVE_CHECK_INCLUDE _variable _header)
119     check_include_file(${_header} ${_variable}_value)
120     SET_AND_EXPOSE_TO_BUILD(${_variable} ${_variable}_value)
121 endmacro()
122
123 macro(_HAVE_CHECK_FUNCTION _variable _function)
124     check_function_exists(${_function} ${_variable}_value)
125     SET_AND_EXPOSE_TO_BUILD(${_variable} ${_variable}_value)
126 endmacro()
127
128 macro(_HAVE_CHECK_SYMBOL _variable _symbol _header)
129     check_symbol_exists(${_symbol} ${_header} ${_variable}_value)
130     SET_AND_EXPOSE_TO_BUILD(${_variable} ${_variable}_value)
131 endmacro()
132
133 macro(_HAVE_CHECK_STRUCT _variable _struct _member _header)
134     check_struct_has_member(${_struct} ${_member} ${_header} ${_variable}_value)
135     SET_AND_EXPOSE_TO_BUILD(${_variable} ${_variable}_value)
136 endmacro()
137
138 # Check whether features.h header exists.
139 # Including glibc's one defines __GLIBC__, that is used in Platform.h
140 _HAVE_CHECK_INCLUDE(HAVE_FEATURES_H features.h)
141
142 # Check for headers
143 _HAVE_CHECK_INCLUDE(HAVE_ERRNO_H errno.h)
144 _HAVE_CHECK_INCLUDE(HAVE_LANGINFO_H langinfo.h)
145 _HAVE_CHECK_INCLUDE(HAVE_MMAP sys/mman.h)
146 _HAVE_CHECK_INCLUDE(HAVE_PTHREAD_NP_H pthread_np.h)
147 _HAVE_CHECK_INCLUDE(HAVE_STRINGS_H strings.h)
148 _HAVE_CHECK_INCLUDE(HAVE_SYS_PARAM_H sys/param.h)
149 _HAVE_CHECK_INCLUDE(HAVE_SYS_TIME_H sys/time.h)
150 _HAVE_CHECK_INCLUDE(HAVE_SYS_TIMEB_H sys/timeb.h)
151
152 # Check for functions
153 _HAVE_CHECK_FUNCTION(HAVE_ALIGNED_MALLOC _aligned_malloc)
154 _HAVE_CHECK_FUNCTION(HAVE_ISDEBUGGERPRESENT IsDebuggerPresent)
155 _HAVE_CHECK_FUNCTION(HAVE_LOCALTIME_R localtime_r)
156 _HAVE_CHECK_FUNCTION(HAVE_STRNSTR strnstr)
157 _HAVE_CHECK_FUNCTION(HAVE_TIMEGM timegm)
158 _HAVE_CHECK_FUNCTION(HAVE_VASPRINTF vasprintf)
159
160 # Check for symbols
161 _HAVE_CHECK_SYMBOL(HAVE_REGEX_H regexec regex.h)
162 # Windows has signal.h but is missing symbols that are used in calls to signal.
163 _HAVE_CHECK_SYMBOL(HAVE_SIGNAL_H SIGTRAP signal.h)
164
165 # Check for struct members
166 _HAVE_CHECK_STRUCT(HAVE_STAT_BIRTHTIME "struct stat" st_birthtime sys/stat.h)
167 _HAVE_CHECK_STRUCT(HAVE_TM_GMTOFF "struct tm" tm_gmtoff time.h)
168 _HAVE_CHECK_STRUCT(HAVE_TM_ZONE "struct tm" tm_zone time.h)
169
170 # Check for int types
171 check_type_size("__int128_t" INT128_VALUE)
172
173 if (HAVE_INT128_VALUE)
174   SET_AND_EXPOSE_TO_BUILD(HAVE_INT128_T INT128_VALUE)
175 endif ()