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} -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    -std=c++11
62    -Woverloaded-virtual -Wnon-virtual-dtor -Wctor-dtor-privacy
63    -Wno-long-long -Wredundant-decls)
64
65list2args(LCB_GNUC_CXX_WARNINGS)
66
67#MSVC-specific flags for C/C++
68LIST(APPEND LCB_CL_CPPFLAGS /nologo /W3 /MP /EHsc)
69LIST(APPEND LCB_CL_CPPFLAGS /wd4800 /wd4244 /wd4267)
70list2args(LCB_CL_CPPFLAGS)
71
72# Common flags for DEBUG
73LIST(APPEND LCB_CL_CPPFLAGS_DEBUG /RTC1)
74list2args( LCB_CL_CPPFLAGS_DEBUG)
75
76# Common flags for RELEASE
77LIST(APPEND LCB_CL_CPPFLAGS_REL /O2)
78list2args(LCB_CL_CPPFLAGS_REL)
79
80MACRO(SET_ALL_FLAGS extra_flags)
81    FOREACH(variant C CXX)
82        FOREACH(config RELEASE DEBUG RELWITHDEBINFO)
83            SET(varname "CMAKE_${variant}_FLAGS_${config}")
84            SET(existing ${${varname}})
85            SET(${varname} "${existing} ${extra_flags}")
86        ENDFOREACH()
87        SET(CMAKE_${variant}_FLAGS "${CMAKE_${variant}_FLAGS} ${extra_flags}")
88    ENDFOREACH()
89ENDMACRO()
90
91IF(MSVC)
92    ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
93    # Don't warn about "deprecated POSIX names"
94    ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
95
96    # Need this for VS 2012 for googletest and C++
97    ADD_DEFINITIONS(-D_VARIADIC_MAX=10)
98    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /TC ${LCB_CL_CPPFLAGS}")
99    # We do not support anything below VS 2015
100    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${LCB_CL_CPPFLAGS}")
101    SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${LCB_CL_CPPFLAGS_DEBUG}")
102    SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${LCB_CL_CPPFLAGS_DEBUG}")
103    SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${LCB_CL_CPPFLAGS_REL}")
104    SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${LCB_CL_CPPFLAGS_REL}")
105
106    # put debug info into release build and revert /OPT defaults after
107    # /DEBUG so that it won't degrade performance and size
108    # http://msdn.microsoft.com/en-us/library/xe4t6fc1(v=vs.80).aspx
109    # Since CMake for some odd reason strips 'incremental' and 'INCREMENTAL', we'll
110    # use weird casing here
111    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /OPT:REF /OPT:ICF /IncReMenTal:no")
112    SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /OPT:REF /OPT:ICF /InCreMenTal:no")
113    SET(LCB_CORE_CXXFLAGS "")
114    SET(LCB_CORE_CFLAGS "")
115    SET(LCB_BASIC_CFLAGS "")
116    SET(LCB_BASIC_CXXFLAGS "")
117
118ELSE()
119    # GCC
120    SET_ALL_FLAGS("-fno-strict-aliasing -ggdb3")
121    IF(WIN32)
122        SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -gstabs")
123        SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -gstabs")
124        SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
125        SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc -static-libstdc++")
126    ELSE()
127        SET_ALL_FLAGS("-pthread")
128    ENDIF()
129    SET(LCB_CORE_CFLAGS "${LCB_GNUC_C_WARNINGS} -DHAVE_VISIBILITY -fvisibility=hidden")
130    SET(LCB_CORE_CXXFLAGS "${LCB_GNUC_CXX_WARNINGS} -DHAVE_VISIBILITY -fvisibility=hidden")
131ENDIF()
132
133IF(LCB_UNIVERSAL_BINARY AND (${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
134    SET(CMAKE_C_FLAGS
135        "${CMAKE_C_FLAGS} -force_cpusubtype_ALL -arch i386 -arch x86_64")
136ENDIF()
137