409931c8132bea62259d8bad0da99a61a48db568
[WebKit-https.git] / Source / cmake / WebKitHelpers.cmake
1 include(CMakeParseArguments)
2 # Sets extra compile flags for a target, depending on the compiler being used.
3 # Currently, only GCC is supported.
4 macro(WEBKIT_SET_EXTRA_COMPILER_FLAGS _target)
5     set(options ENABLE_WERROR IGNORECXX_WARNINGS)
6     CMAKE_PARSE_ARGUMENTS("OPTION" "${options}" "" "" ${ARGN})
7     if (CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
8         get_target_property(OLD_COMPILE_FLAGS ${_target} COMPILE_FLAGS)
9         if (${OLD_COMPILE_FLAGS} STREQUAL "OLD_COMPILE_FLAGS-NOTFOUND")
10             set(OLD_COMPILE_FLAGS "")
11         endif ()
12
13         include(TestCXXAcceptsFlag)
14         CHECK_CXX_ACCEPTS_FLAG("-dumpversion" CMAKE_CXX_ACCEPTS_DUMPVERSION)
15         if (CMAKE_CXX_ACCEPTS_DUMPVERSION)
16             EXEC_PROGRAM(${CMAKE_CXX_COMPILER} ARGS -dumpversion OUTPUT_VARIABLE COMPILER_VERSION)
17         else ()
18             EXEC_PROGRAM("${CMAKE_CXX_COMPILER} -E -Wp,-dM - < /dev/null | grep '#define __VERSION__' | grep -E -o '[0-9]+\\.[0-9]+\\.?[0-9]+?'" OUTPUT_VARIABLE COMPILER_VERSION)
19         endif ()
20
21         # Disable some optimizations on buggy compiler versions
22         # GCC 4.5.1 does not implement -ftree-sra correctly
23         if (${COMPILER_VERSION} STREQUAL "4.5.1")
24             set(OLD_COMPILE_FLAGS "-fno-tree-sra ${OLD_COMPILE_FLAGS}")
25         endif ()
26
27         # For GTK+ we will rely on a linker script to deal with symbol visibility on
28         # production builds, we want all symbols visible for development builds.
29         if (NOT SHARED_CORE AND NOT ${PORT} STREQUAL "GTK")
30             set(OLD_COMPILE_FLAGS "-fvisibility=hidden ${OLD_COMPILE_FLAGS}")
31         endif ()
32
33         get_target_property(TARGET_TYPE ${_target} TYPE)
34         if (${TARGET_TYPE} STREQUAL "STATIC_LIBRARY") # -fPIC is automatically added to shared libraries
35             set(OLD_COMPILE_FLAGS "-fPIC ${OLD_COMPILE_FLAGS}")
36         endif ()
37
38         set(OLD_COMPILE_FLAGS "-fno-exceptions -fno-strict-aliasing ${OLD_COMPILE_FLAGS}")
39
40         # Enable warnings by default
41         if (NOT ${OPTION_IGNORECXX_WARNINGS})
42             set(OLD_COMPILE_FLAGS "-Wall -Wextra -Wcast-align -Wformat-security -Wmissing-format-attribute -Wpointer-arith -Wundef -Wwrite-strings ${OLD_COMPILE_FLAGS}")
43         endif ()
44
45         # Enable errors on warning
46         if (OPTION_ENABLE_WERROR)
47             set(OLD_COMPILE_FLAGS "-Werror -Wno-error=unused-parameter ${OLD_COMPILE_FLAGS}")
48         endif ()
49
50         set_target_properties(${_target} PROPERTIES
51             COMPILE_FLAGS "${OLD_COMPILE_FLAGS}")
52
53         unset(OLD_COMPILE_FLAGS)
54     endif ()
55 endmacro()
56
57
58 # Append the given flag to the target property.
59 # Builds on top of get_target_property() and set_target_properties()
60 macro(ADD_TARGET_PROPERTIES _target _property _flags)
61     get_target_property(_tmp ${_target} ${_property})
62     if (NOT _tmp)
63         set(_tmp "")
64     endif (NOT _tmp)
65
66     foreach (f ${_flags})
67         set(_tmp "${_tmp} ${f}")
68     endforeach (f ${_flags})
69
70     set_target_properties(${_target} PROPERTIES ${_property} ${_tmp})
71     unset(_tmp)
72 endmacro(ADD_TARGET_PROPERTIES _target _property _flags)
73
74
75 # Append the given dependencies to the source file
76 macro(ADD_SOURCE_DEPENDENCIES _source _deps)
77     get_source_file_property(_tmp ${_source} OBJECT_DEPENDS)
78     if (NOT _tmp)
79         set(_tmp "")
80     endif ()
81
82     foreach (f ${_deps})
83         list(APPEND _tmp "${f}")
84     endforeach ()
85
86     set_source_files_properties(${_source} PROPERTIES OBJECT_DEPENDS "${_tmp}")
87     unset(_tmp)
88 endmacro()
89
90
91 # Append the given dependencies to the source file
92 # This one consider the given dependencies are in ${DERIVED_SOURCES_WEBCORE_DIR}
93 # and prepends this to every member of dependencies list
94 macro(ADD_SOURCE_WEBCORE_DERIVED_DEPENDENCIES _source _deps)
95     set(_tmp "")
96     foreach (f ${_deps})
97         list(APPEND _tmp "${DERIVED_SOURCES_WEBCORE_DIR}/${f}")
98     endforeach ()
99
100     ADD_SOURCE_DEPENDENCIES(${_source} ${_tmp})
101     unset(_tmp)
102 endmacro()
103
104 macro(CALCULATE_LIBRARY_VERSIONS_FROM_LIBTOOL_TRIPLE library_name current revision age)
105     math(EXPR ${library_name}_VERSION_MAJOR "${current} - ${age}")
106     set(${library_name}_VERSION_MINOR ${age})
107     set(${library_name}_VERSION_PATCH ${revision})
108     set(${library_name}_VERSION ${${library_name}_VERSION_MAJOR}.${age}.${revision})
109 endmacro()
110
111 macro(POPULATE_LIBRARY_VERSION library_name)
112 if (NOT DEFINED ${library_name}_VERSION_MAJOR)
113     set(${library_name}_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
114 endif ()
115 if (NOT DEFINED ${library_name}_VERSION_MINOR)
116     set(${library_name}_VERSION_MINOR ${PROJECT_VERSION_MINOR})
117 endif ()
118 if (NOT DEFINED ${library_name}_VERSION_PATCH)
119     set(${library_name}_VERSION_PATCH ${PROJECT_VERSION_PATCH})
120 endif ()
121 if (NOT DEFINED ${library_name}_VERSION)
122     set(${library_name}_VERSION ${PROJECT_VERSION})
123 endif ()
124 endmacro()