aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/common.cmake
blob: f26c61a90c610fdab94e0026079c0d958658d19e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Copyright (c) 2017 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "cmake-common" project.
# For details, see https://github.com/egor-tensin/cmake-common.
# Distributed under the MIT License.

# It's a CMake code snippet I use in all of my CMake projects.
# It makes targets link the runtime statically by default, strips debug symbols
# in release builds and sets a couple of useful compilation options.

# Add this to the top-level CMakeLists.txt (unless a higher version has already
# been specified):
#
#     cmake_minimum_required(VERSION 3.1)

# Without this policy set, this line:
#
#     if(toolset STREQUAL "MSVC")
#
# evaluates to false even when using Visual Studio (since MSVC is a predefined
# variable; it's completely bonkers).
if(NOT POLICY CMP0054)
    message(FATAL_ERROR "common.cmake uses CMP0054, which is unsupported by this CMake version")
endif()
cmake_policy(SET CMP0054 NEW)

# Toolset identification:

if(CMAKE_C_COMPILER_ID)
    set(toolset "${CMAKE_C_COMPILER_ID}")
elseif(CMAKE_CXX_COMPILER_ID)
    set(toolset "${CMAKE_CXX_COMPILER_ID}")
else()
    set(toolset "unknown")
endif()

if(toolset STREQUAL "GNU")
    set(is_gcc ON)
elseif(toolset STREQUAL "MSVC")
    set(is_msvc ON)
else()
    message(WARNING "Unrecognized toolset: ${toolset}")
endif()

# User-defined switches:

set(default_value ON)
get_directory_property(parent_dir PARENT_DIRECTORY)
if(parent_dir)
    set(default_value OFF)
endif()

if(NOT DEFINED CC_CXX_STANDARD)
    set(CC_CXX_STANDARD "14" CACHE STRING "C++ standard version")
endif()
if(NOT DEFINED CC_BEST_PRACTICES)
    option(CC_BEST_PRACTICES "Set common compiler options" "${default_value}")
endif()
if(NOT DEFINED CC_WINDOWS_DEF)
    option(CC_WINDOWS_DEF "Define useful Windows macros" "${default_value}")
endif()
if(NOT DEFINED CC_STATIC_RUNTIME)
    set(static_runtime_default_value "${default_value}")
    if(DEFINED Boost_USE_STATIC_LIBS AND NOT Boost_USE_STATIC_LIBS)
        # Linking to dynamic Boost libs and the static runtime is a no-no:
        set(static_runtime_default_value OFF)
    elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows" OR MINGW)
    else()
        # At this point, Linux-like environment & the GNU C Library are assumed.
        set(static_runtime_default_value OFF)
    endif()
    option(CC_STATIC_RUNTIME "Link the runtime statically" "${static_runtime_default_value}")
endif()
if(NOT DEFINED CC_STRIP_SYMBOLS)
    option(CC_STRIP_SYMBOLS  "Strip debug symbols" "${default_value}")
endif()

option(Boost_USE_STATIC_LIBS "Use the static Boost libraries" "${default_value}")
option(Boost_USE_STATIC_RUNTIME "Use Boost libraries linked to the runtime statically" "${CC_STATIC_RUNTIME}")

if(NOT parent_dir)
    message(STATUS "Toolset:                        ${toolset}")
    message(STATUS "C++ standard:                   ${CC_CXX_STANDARD}")
    message(STATUS "Set common compiler options:    ${CC_BEST_PRACTICES}")
    message(STATUS "Define useful Windows macros:   ${CC_WINDOWS_DEF}")
    message(STATUS "Use the static Boost libraries: ${Boost_USE_STATIC_LIBS}")
    message(STATUS "Link the runtime statically:    ${CC_STATIC_RUNTIME}")
    message(STATUS "Strip symbols:                  ${CC_STRIP_SYMBOLS}")
endif()

# C++ standard version:

set(CMAKE_CXX_STANDARD "${CC_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Common compiler options routines:

function(_cc_best_practices_msvc target)
    set(compile_options /MP /W4)
    get_target_property(target_type "${target}" TYPE)
    get_target_property(aliased "${target}" ALIASED_TARGET)
    if(NOT target_type STREQUAL "INTERFACE_LIBRARY" AND NOT aliased)
        target_compile_options("${target}" PRIVATE ${compile_options})
    endif()
endfunction()

function(_cc_best_practices_gcc target)
    set(compile_options -Wall -Wextra)
    get_target_property(target_type "${target}" TYPE)
    get_target_property(aliased "${target}" ALIASED_TARGET)
    if(NOT target_type STREQUAL "INTERFACE_LIBRARY" AND NOT aliased)
        target_compile_options("${target}" PRIVATE ${compile_options})
    endif()
endfunction()

function(_cc_best_practices target)
    if(is_msvc)
        _cc_best_practices_msvc("${target}")
    elseif(is_gcc)
        _cc_best_practices_gcc("${target}")
    endif()
endfunction()

# Useful Windows macros routines:

function(_cc_common_windows_definitions target)
    set(compile_definitions WIN32_LEAN_AND_MEAN NOMINMAX)
    get_target_property(target_type "${target}" TYPE)
    if(target_type STREQUAL "INTERFACE_LIBRARY")
        target_compile_definitions("${target}" INTERFACE ${compile_definitions})
    else()
        get_target_property(aliased "${target}" ALIASED_TARGET)
        if(NOT aliased)
            target_compile_definitions("${target}" PRIVATE ${compile_definitions})
        endif()
    endif()
endfunction()

# Static runtime routines:

function(_cc_static_runtime_msvc target)
    get_target_property(target_type "${target}" TYPE)
    get_target_property(aliased "${target}" ALIASED_TARGET)
    if(NOT target_type STREQUAL "INTERFACE_LIBRARY" AND NOT aliased)
        target_compile_options("${target}" PRIVATE
            $<$<CONFIG:Debug>:/MTd>
            $<$<NOT:$<CONFIG:Debug>>:/MT>)
    endif()
endfunction()

function(_cc_static_runtime_gcc target)
    get_target_property(target_type "${target}" TYPE)
    get_target_property(aliased "${target}" ALIASED_TARGET)
    if(NOT target_type STREQUAL "INTERFACE_LIBRARY" AND NOT aliased)
        # This causes issues with mixing keyword- and plain- versions of
        # target_link_libraries:
        #target_link_libraries("${target}" PRIVATE -static)

        set_property(TARGET "${target}" APPEND_STRING PROPERTY LINK_FLAGS " -static")

        # Or (haven't tested this), if CMake 3.13+ is used:
        #target_link_options("${target}" PRIVATE -static)
    endif()
endfunction()

function(_cc_static_runtime target)
    if(is_msvc)
        _cc_static_runtime_msvc("${target}")
    elseif(is_gcc)
        _cc_static_runtime_gcc("${target}")
    endif()
endfunction()

# Symbol stripping routines:

function(_cc_strip_symbols_gcc target)
    get_target_property(target_type "${target}" TYPE)
    get_target_property(aliased "${target}" ALIASED_TARGET)
    if(NOT target_type STREQUAL "INTERFACE_LIBRARY" AND NOT aliased)
        set(release_build $<OR:$<CONFIG:Release>,$<CONFIG:MinSizeRel>>)
        if(release_build)
            # This causes issues with mixing keyword- and plain- versions of
            # target_link_libraries:
            #target_link_libraries("${target}" PRIVATE -s)

            set_property(TARGET "${target}" APPEND_STRING PROPERTY LINK_FLAGS " -s")
        endif()
    endif()
endfunction()

function(_cc_strip_symbols target)
    if(is_gcc)
        _cc_strip_symbols_gcc("${target}")
    endif()
endfunction()

# Main macros:

function(_cc_apply_settings target)
    if(TARGET "${target}")
        get_target_property(target_imported "${target}" IMPORTED)
        if(NOT target_imported)
            if(CC_BEST_PRACTICES)
                _cc_best_practices("${target}")
            endif()
            if(CC_WINDOWS_DEF)
                _cc_common_windows_definitions("${target}")
            endif()
            if(CC_STRIP_SYMBOLS)
                _cc_strip_symbols("${target}")
            endif()
            if(CC_STATIC_RUNTIME)
                _cc_static_runtime("${target}")
            endif()
        endif()
    endif()
endfunction()

if(NOT parent_dir)
    macro(add_executable target)
        _add_executable(${ARGV})
        _cc_apply_settings("${target}")
    endmacro()

    macro(add_library target)
        _add_library(${ARGV})
        _cc_apply_settings("${target}")
    endmacro()
endif()