1# Common flags for libcouchbase modules. This defines the specific flags
2# required for various compilation modes
3# Exports:
4#   LCB_CORE_CFLAGS:
5#       C flags to be used by our "Core" modules. This contains
6#       many warnings.
7#   LCB_CORE_CXXFLAGS:
8#       Like LCB_CORE_CFLAGS, but for C++
9#
10#   LCB_BASIC_CFLAGS
11#       Basic C flags without extra warnings
12#   LCB_BASIC_CXXFLAGS
13#       Basic C++ flags without extra warnings.
14#
15# Note that global flags will still be modified for debug settings and the
16# like.
17
18MACRO(list2args VAR)
19    STRING(REPLACE ";" " " _tmp "${${VAR}}")
20    SET("${VAR}" "${_tmp}")
21ENDMACRO(list2args)
22
23LIST(APPEND LCB_GNUC_CPP_WARNINGS
24    -Wall -pedantic -Wshadow -fdiagnostics-show-option -Wformat
25    -Wno-strict-aliasing -Wextra -Winit-self -Wno-missing-field-initializers
26    -Wno-variadic-macros)
27
28IF(CMAKE_C_COMPILER_ID STREQUAL "Clang")
29    LIST(APPEND LCB_GNUC_CPP_WARNINGS -Wno-cast-align -Wno-dollar-in-identifier-extension)
30ENDIF()
31
32IF(LCB_USE_ASAN)
33    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
34    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
35    SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup -fsanitize=address")
36    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
37ENDIF()
38
39IF(LCB_USE_COVERAGE)
40    SET(_covflags "-fprofile-arcs -ftest-coverage")
41    IF(CMAKE_COMPILER_IS_GNUCC)
42        SET(_covflags "--coverage ${_covflags}")
43    ENDIF()
44
45    LIST(APPEND LCB_GNUC_CPP_WARNINGS ${_covflags})
46    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${_covflags}")
47    SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${_covflags}")
48ENDIF()
49
50list2args(LCB_GNUC_CPP_WARNINGS)
51
52LIST(APPEND LCB_GNUC_C_WARNINGS
53    ${LCB_GNUC_CPP_WARNINGS}
54    -std=gnu99
55    -Wundef -Wstrict-prototypes -Wmissing-prototypes -Wredundant-decls
56    -Wmissing-declarations)
57list2args(LCB_GNUC_C_WARNINGS)
58
59LIST(APPEND LCB_GNUC_CXX_WARNINGS
60    ${LCB_GNUC_CPP_WARNINGS}
61    -Woverloaded-virtual -Wnon-virtual-dtor -Wctor-dtor-privacy
62    -Wno-long-long -Wredundant-decls)
63
64INCLUDE(CheckCXXCompilerFlag)
65CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
66IF(COMPILER_SUPPORTS_CXX11)
67    LIST(APPEND LCB_GNUC_CXX_WARNINGS -std=c++11)
68ENDIF()
69
70list2args(LCB_GNUC_CXX_WARNINGS)
71
72#MSVC-specific flags for C/C++
73LIST(APPEND LCB_CL_CPPFLAGS /nologo /W3 /MP /EHsc)
74LIST(APPEND LCB_CL_CPPFLAGS /wd4800 /wd4244 /wd4267)
75list2args(LCB_CL_CPPFLAGS)
76
77# Common flags for DEBUG
78LIST(APPEND LCB_CL_CPPFLAGS_DEBUG /RTC1)
79list2args( LCB_CL_CPPFLAGS_DEBUG)
80
81# Common flags for RELEASE
82LIST(APPEND LCB_CL_CPPFLAGS_REL /O2)
83list2args(LCB_CL_CPPFLAGS_REL)
84
85MACRO(SET_ALL_FLAGS extra_flags)
86    FOREACH(variant C CXX)
87        FOREACH(config RELEASE DEBUG RELWITHDEBINFO)
88            SET(varname "CMAKE_${variant}_FLAGS_${config}")
89            SET(existing ${${varname}})
90            SET(${varname} "${existing} ${extra_flags}")
91        ENDFOREACH()
92        SET(CMAKE_${variant}_FLAGS "${CMAKE_${variant}_FLAGS} ${extra_flags}")
93    ENDFOREACH()
94ENDMACRO()
95
96IF(MSVC)
97    ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
98    # Don't warn about "deprecated POSIX names"
99    ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
100
101    # Need this for VS 2012 for googletest and C++
102    IF(MSVC_VERSION EQUAL 1700 OR MSVC_VERSION GREATER 1700)
103        ADD_DEFINITIONS(-D_VARIADIC_MAX=10)
104    ENDIF()
105    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /TC ${LCB_CL_CPPFLAGS}")
106    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LCB_CL_CPPFLAGS}")
107    SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${LCB_CL_CPPFLAGS_DEBUG}")
108    SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${LCB_CL_CPPFLAGS_DEBUG}")
109    SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${LCB_CL_CPPFLAGS_REL}")
110    SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${LCB_CL_CPPFLAGS_REL}")
111
112    # put debug info into release build and revert /OPT defaults after
113    # /DEBUG so that it won't degrade performance and size
114    # http://msdn.microsoft.com/en-us/library/xe4t6fc1(v=vs.80).aspx
115    # Since CMake for some odd reason strips 'incremental' and 'INCREMENTAL', we'll
116    # use weird casing here
117    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /OPT:REF /OPT:ICF /IncReMenTal:no")
118    SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /OPT:REF /OPT:ICF /InCreMenTal:no")
119    SET(LCB_CORE_CXXFLAGS "")
120    SET(LCB_CORE_CFLAGS "")
121    SET(LCB_BASIC_CFLAGS "")
122    SET(LCB_BASIC_CXXFLAGS "")
123
124ELSE()
125    # GCC
126    SET_ALL_FLAGS("-fno-strict-aliasing -ggdb3")
127    IF(WIN32)
128        SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -gstabs")
129        SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -gstabs")
130        SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
131        SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc -static-libstdc++")
132    ELSE()
133        SET_ALL_FLAGS("-pthread")
134    ENDIF()
135    SET(LCB_CORE_CFLAGS "${LCB_GNUC_C_WARNINGS} -DHAVE_VISIBILITY -fvisibility=hidden")
136    SET(LCB_CORE_CXXFLAGS "${LCB_GNUC_CXX_WARNINGS} -DHAVE_VISIBILITY -fvisibility=hidden")
137ENDIF()
138
139IF(LCB_UNIVERSAL_BINARY AND (${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
140    SET(CMAKE_C_FLAGS
141        "${CMAKE_C_FLAGS} -force_cpusubtype_ALL -arch i386 -arch x86_64")
142ENDIF()
143