320b94f5107b3cbda24e43d9e8b9a06e69c215a1
[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         get_target_property(TARGET_TYPE ${_target} TYPE)
14         if (${TARGET_TYPE} STREQUAL "STATIC_LIBRARY") # -fPIC is automatically added to shared libraries
15             set(OLD_COMPILE_FLAGS "-fPIC ${OLD_COMPILE_FLAGS}")
16         endif ()
17
18         # Enable warnings by default
19         if (NOT ${OPTION_IGNORECXX_WARNINGS})
20             set(OLD_COMPILE_FLAGS "-Wall -Wextra -Wcast-align -Wformat-security -Wmissing-format-attribute -Wpointer-arith -Wundef -Wwrite-strings ${OLD_COMPILE_FLAGS}")
21         endif ()
22
23         # Enable errors on warning
24         if (OPTION_ENABLE_WERROR)
25             set(OLD_COMPILE_FLAGS "-Werror ${OLD_COMPILE_FLAGS}")
26         endif ()
27
28         set_target_properties(${_target} PROPERTIES
29             COMPILE_FLAGS "${OLD_COMPILE_FLAGS}")
30
31         unset(OLD_COMPILE_FLAGS)
32     endif ()
33 endmacro()
34
35
36 # Append the given flag to the target property.
37 # Builds on top of get_target_property() and set_target_properties()
38 macro(ADD_TARGET_PROPERTIES _target _property _flags)
39     get_target_property(_tmp ${_target} ${_property})
40     if (NOT _tmp)
41         set(_tmp "")
42     endif (NOT _tmp)
43
44     foreach (f ${_flags})
45         set(_tmp "${_tmp} ${f}")
46     endforeach (f ${_flags})
47
48     set_target_properties(${_target} PROPERTIES ${_property} ${_tmp})
49     unset(_tmp)
50 endmacro(ADD_TARGET_PROPERTIES _target _property _flags)
51
52
53 # Append the given dependencies to the source file
54 macro(ADD_SOURCE_DEPENDENCIES _source _deps)
55     get_source_file_property(_tmp ${_source} OBJECT_DEPENDS)
56     if (NOT _tmp)
57         set(_tmp "")
58     endif ()
59
60     foreach (f ${_deps})
61         list(APPEND _tmp "${f}")
62     endforeach ()
63
64     set_source_files_properties(${_source} PROPERTIES OBJECT_DEPENDS "${_tmp}")
65     unset(_tmp)
66 endmacro()
67
68
69 # Append the given dependencies to the source file
70 # This one consider the given dependencies are in ${DERIVED_SOURCES_WEBCORE_DIR}
71 # and prepends this to every member of dependencies list
72 macro(ADD_SOURCE_WEBCORE_DERIVED_DEPENDENCIES _source _deps)
73     set(_tmp "")
74     foreach (f ${_deps})
75         list(APPEND _tmp "${DERIVED_SOURCES_WEBCORE_DIR}/${f}")
76     endforeach ()
77
78     ADD_SOURCE_DEPENDENCIES(${_source} ${_tmp})
79     unset(_tmp)
80 endmacro()
81
82 macro(CALCULATE_LIBRARY_VERSIONS_FROM_LIBTOOL_TRIPLE library_name current revision age)
83     math(EXPR ${library_name}_VERSION_MAJOR "${current} - ${age}")
84     set(${library_name}_VERSION_MINOR ${age})
85     set(${library_name}_VERSION_MICRO ${revision})
86     set(${library_name}_VERSION ${${library_name}_VERSION_MAJOR}.${age}.${revision})
87 endmacro()
88
89 macro(POPULATE_LIBRARY_VERSION library_name)
90 if (NOT DEFINED ${library_name}_VERSION_MAJOR)
91     set(${library_name}_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
92 endif ()
93 if (NOT DEFINED ${library_name}_VERSION_MINOR)
94     set(${library_name}_VERSION_MINOR ${PROJECT_VERSION_MINOR})
95 endif ()
96 if (NOT DEFINED ${library_name}_VERSION_MICRO)
97     set(${library_name}_VERSION_MICRO ${PROJECT_VERSION_MICRO})
98 endif ()
99 if (NOT DEFINED ${library_name}_VERSION)
100     set(${library_name}_VERSION ${PROJECT_VERSION})
101 endif ()
102 endmacro()