Skip to content

Commit 83ddc58

Browse files
committed
Some improvements for clang
1 parent dcfa784 commit 83ddc58

File tree

1 file changed

+142
-124
lines changed

1 file changed

+142
-124
lines changed

CMakeLists.txt

Lines changed: 142 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1010

1111
option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored output (GNU/Clang only)." TRUE)
1212

13+
if(APPLE)
14+
set(UseLibCxxDefault TRUE)
15+
else()
16+
set(UseLibCxxDefault FALSE)
17+
endif()
18+
option(UseLibCxx "Compile using libc++" UseLibCxxDefault)
19+
1320
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
1421
if(DEFINED ENV{MSYSTEM_PREFIX})
1522
set(CMAKE_INSTALL_PREFIX "$ENV{MSYSTEM_PREFIX}" CACHE PATH "Default install prefix" FORCE)
@@ -29,12 +36,18 @@ function(add_compile_options_safe FLAG)
2936
endif(CXX_COMPILER_SUPPORTS_${FLAG_NO_SIGNS})
3037
endfunction()
3138

39+
if(UseLibCxx AND NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
40+
message(FATAL_ERROR "You can only use libc++ with clang" )
41+
endif()
42+
3243
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
3344
if (${FORCE_COLORED_OUTPUT})
34-
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
45+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
46+
add_compile_options(-fcolor-diagnostics -fansi-escape-codes)
47+
add_link_options(-fcolor-diagnostics -fansi-escape-codes)
48+
elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
3549
add_compile_options(-fdiagnostics-color=always)
36-
else()
37-
add_compile_options(-fcolor-diagnostics)
50+
add_link_options(-fdiagnostics-color=always)
3851
endif()
3952
endif()
4053
if (WARNINGS_ARE_ERRORS)
@@ -45,127 +58,132 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
4558
add_compile_options_safe(-Og)
4659
add_compile_options_safe(-g3)
4760
endif()
48-
add_compile_options("-Wall")
49-
add_compile_options("-Wextra")
50-
add_compile_options("-pedantic")
51-
add_compile_options("-Wc++14-compat")
52-
add_compile_options("-Wc++17-compat")
53-
add_compile_options_safe("-Wc++20-compat")
54-
add_compile_options_safe("-Wc++14-compat-pedantic")
55-
add_compile_options_safe("-Wc++17-compat-pedantic")
56-
add_compile_options_safe("-Wc++20-compat-pedantic")
57-
add_compile_options_safe("-Walloc-zero")
58-
add_compile_options("-Walloca")
59-
add_compile_options_safe("-Wanalyzer-too-complex")
60-
add_compile_options_safe("-Warith-conversion")
61-
add_compile_options_safe("-Warray-bounds-pointer-arithmetic")
62-
add_compile_options_safe("-Wassign-enum")
63-
add_compile_options_safe("-Wbad-function-cast")
64-
add_compile_options_safe("-Wbitwise-op-parentheses")
65-
add_compile_options_safe("-Wbraced-scalar-init")
66-
add_compile_options_safe("-Wbridge-cast")
67-
add_compile_options("-Wcast-align")
68-
add_compile_options_safe("-Wcast-align=strict")
69-
add_compile_options("-Wcast-qual")
70-
add_compile_options_safe("-Wchar-subscripts")
71-
add_compile_options_safe("-Wcomma")
72-
add_compile_options_safe("-Wcomma-subscript")
73-
add_compile_options_safe("-Wcomment")
74-
add_compile_options_safe("-Wconditionally-supported")
75-
add_compile_options_safe("-Wcovered-switch-default")
76-
add_compile_options("-Wctor-dtor-privacy")
77-
add_compile_options("-Wdate-time")
78-
add_compile_options_safe("-Wdeprecated-copy")
79-
add_compile_options_safe("-Wdeprecated-copy-dtor")
80-
add_compile_options_safe("-Wdeprecated-dynamic-exception-spec")
81-
add_compile_options("-Wdisabled-optimization")
82-
add_compile_options("-Wdouble-promotion")
83-
add_compile_options_safe("-Wduplicated-branches")
84-
add_compile_options_safe("-Wduplicated-cond")
85-
add_compile_options_safe("-Wempty-init-stmt")
86-
add_compile_options("-Wextra-semi")
87-
add_compile_options_safe("-Wfor-loop-analysis")
88-
add_compile_options("-Wformat-nonliteral")
89-
add_compile_options("-Wformat-security")
90-
add_compile_options_safe("-Wformat-signedness")
91-
add_compile_options("-Wformat-y2k")
92-
add_compile_options_safe("-Wfour-char-constants")
93-
add_compile_options_safe("-Wgcc-compat")
94-
add_compile_options_safe("-Wheader-hygiene")
95-
add_compile_options_safe("-Widiomatic-parentheses")
96-
add_compile_options_safe("-Wint-in-bool-context")
97-
add_compile_options("-Winvalid-pch")
98-
add_compile_options_safe("-Wlogical-op")
99-
add_compile_options_safe("-Wlogical-op-parentheses")
100-
add_compile_options_safe("-Wmany-braces-around-scalar-init")
101-
add_compile_options_safe("-Wmisleading-indentation")
102-
add_compile_options_safe("-Wmismatched-tags")
103-
# add_compile_options("-Wmissing-braces")
104-
add_compile_options("-Wmissing-include-dirs")
105-
add_compile_options_safe("-Wmove")
106-
add_compile_options("-Wmultichar")
107-
add_compile_options_safe("-Wnoexcept")
108-
add_compile_options("-Wnon-virtual-dtor")
109-
add_compile_options("-Wnull-dereference")
110-
add_compile_options("-Wold-style-cast")
111-
add_compile_options_safe("-Wover-aligned")
112-
add_compile_options("-Woverloaded-virtual")
113-
add_compile_options("-Wpacked")
114-
add_compile_options("-Wredundant-decls")
115-
add_compile_options_safe("-Wredundant-tags")
116-
add_compile_options("-Wregister")
117-
add_compile_options_safe("-Wshadow=compatible-local")
118-
add_compile_options_safe("-Wshadow=local")
119-
add_compile_options_safe("-Wshadow-field")
120-
add_compile_options_safe("-Wshadow-field-in-constructor")
121-
add_compile_options_safe("-Wshadow-field-in-constructor-modified")
122-
add_compile_options_safe("-Wshadow-ivar")
123-
add_compile_options_safe("-Wshadow-uncaptured-local")
124-
# add_compile_options("-Wsign-promo")
125-
add_compile_options("-Wstack-protector")
126-
add_compile_options_safe("-Wstrict-null-sentinel")
127-
add_compile_options_safe("-Wstring-conversion")
128-
add_compile_options_safe("-Wstring-plus-char")
129-
add_compile_options_safe("-Wsuggest-attribute=cold")
130-
add_compile_options_safe("-Wsuggest-attribute=const")
131-
add_compile_options_safe("-Wsuggest-attribute=format")
132-
add_compile_options_safe("-Wsuggest-attribute=malloc")
133-
add_compile_options_safe("-Wsuggest-attribute=noreturn")
134-
add_compile_options_safe("-Wsuggest-attribute=pure")
135-
add_compile_options_safe("-Wsuggest-final-methods")
136-
add_compile_options_safe("-Wsuggest-final-types")
137-
add_compile_options_safe("-Wsuggest-destructor-override")
138-
add_compile_options_safe("-Wsuggest-override")
139-
# add_compile_options("-Wswitch-default")
140-
add_compile_options("-Wswitch-enum")
141-
add_compile_options("-Wsynth")
142-
add_compile_options_safe("-Wtrampolines")
143-
add_compile_options("-Wundef")
144-
add_compile_options_safe("-Wunused-label")
145-
add_compile_options_safe("-Wunused-lambda-capture")
146-
add_compile_options_safe("-Wunused-local-typedef")
147-
add_compile_options("-Wunused-macros")
148-
add_compile_options_safe("-Wuseless-cast")
149-
add_compile_options_safe("-Wvector-conversion")
150-
add_compile_options_safe("-Wvector-operation-performance")
151-
add_compile_options_safe("-Wvirtual-inheritance")
152-
add_compile_options_safe("-Wvolatile")
153-
add_compile_options("-Wzero-as-null-pointer-constant")
154-
add_compile_options_safe("-Warray-bounds=2")
155-
add_compile_options_safe("-Wattribute-alias=2")
156-
add_compile_options_safe("-Wcatch-value=3")
157-
add_compile_options_safe("-Wformat-overflow=2")
158-
add_compile_options_safe("-Wformat-truncation=2")
159-
add_compile_options_safe("-Wformat=2")
160-
add_compile_options("-Wimplicit-fallthrough")
161-
add_compile_options_safe("-Wimplicit-fallthrough=3")
162-
add_compile_options_safe("-Wplacement-new=2")
163-
add_compile_options_safe("-Wshift-overflow=2")
164-
add_compile_options_safe("-Wstrict-aliasing=3")
165-
add_compile_options("-Wstrict-overflow=2")
166-
add_compile_options_safe("-Wstringop-overflow=4")
167-
add_compile_options("-Wunused-const-variable")
168-
add_compile_options_safe("-Wunused-const-variable=1")
61+
if(UseLibCxx)
62+
add_compile_options(-stdlib=libc++)
63+
add_link_options(-stdlib=libc++ -lc++)
64+
# add_link_options("-lc++abi")
65+
endif()
66+
add_compile_options(-Wall)
67+
add_compile_options(-Wextra)
68+
add_compile_options(-pedantic)
69+
add_compile_options(-Wc++14-compat)
70+
add_compile_options(-Wc++17-compat)
71+
add_compile_options_safe(-Wc++20-compat)
72+
add_compile_options_safe(-Wc++14-compat-pedantic)
73+
add_compile_options_safe(-Wc++17-compat-pedantic)
74+
add_compile_options_safe(-Wc++20-compat-pedantic)
75+
add_compile_options_safe(-Walloc-zero)
76+
add_compile_options(-Walloca)
77+
add_compile_options_safe(-Wanalyzer-too-complex)
78+
add_compile_options_safe(-Warith-conversion)
79+
add_compile_options_safe(-Warray-bounds-pointer-arithmetic)
80+
add_compile_options_safe(-Wassign-enum)
81+
add_compile_options_safe(-Wbad-function-cast)
82+
add_compile_options_safe(-Wbitwise-op-parentheses)
83+
add_compile_options_safe(-Wbraced-scalar-init)
84+
add_compile_options_safe(-Wbridge-cast)
85+
add_compile_options(-Wcast-align)
86+
add_compile_options_safe(-Wcast-align=strict)
87+
add_compile_options(-Wcast-qual)
88+
add_compile_options_safe(-Wchar-subscripts)
89+
add_compile_options_safe(-Wcomma)
90+
add_compile_options_safe(-Wcomma-subscript)
91+
add_compile_options_safe(-Wcomment)
92+
add_compile_options_safe(-Wconditionally-supported)
93+
add_compile_options_safe(-Wcovered-switch-default)
94+
add_compile_options(-Wctor-dtor-privacy)
95+
add_compile_options(-Wdate-time)
96+
add_compile_options_safe(-Wdeprecated-copy)
97+
add_compile_options_safe(-Wdeprecated-copy-dtor)
98+
add_compile_options_safe(-Wdeprecated-dynamic-exception-spec)
99+
add_compile_options(-Wdisabled-optimization)
100+
add_compile_options(-Wdouble-promotion)
101+
add_compile_options_safe(-Wduplicated-branches)
102+
add_compile_options_safe(-Wduplicated-cond)
103+
add_compile_options_safe(-Wempty-init-stmt)
104+
add_compile_options(-Wextra-semi)
105+
add_compile_options_safe(-Wfor-loop-analysis)
106+
add_compile_options(-Wformat-nonliteral)
107+
add_compile_options(-Wformat-security)
108+
add_compile_options_safe(-Wformat-signedness)
109+
add_compile_options(-Wformat-y2k)
110+
add_compile_options_safe(-Wfour-char-constants)
111+
add_compile_options_safe(-Wgcc-compat)
112+
add_compile_options_safe(-Wheader-hygiene)
113+
add_compile_options_safe(-Widiomatic-parentheses)
114+
add_compile_options_safe(-Wint-in-bool-context)
115+
add_compile_options(-Winvalid-pch)
116+
add_compile_options_safe(-Wlogical-op)
117+
add_compile_options_safe(-Wlogical-op-parentheses)
118+
add_compile_options_safe(-Wmany-braces-around-scalar-init)
119+
add_compile_options_safe(-Wmisleading-indentation)
120+
add_compile_options_safe(-Wmismatched-tags)
121+
# add_compile_options(-Wmissing-braces)
122+
add_compile_options(-Wmissing-include-dirs)
123+
add_compile_options_safe(-Wmove)
124+
add_compile_options(-Wmultichar)
125+
add_compile_options_safe(-Wnoexcept)
126+
add_compile_options(-Wnon-virtual-dtor)
127+
add_compile_options(-Wnull-dereference)
128+
add_compile_options(-Wold-style-cast)
129+
add_compile_options_safe(-Wover-aligned)
130+
add_compile_options(-Woverloaded-virtual)
131+
add_compile_options(-Wpacked)
132+
add_compile_options(-Wredundant-decls)
133+
add_compile_options_safe(-Wredundant-tags)
134+
add_compile_options(-Wregister)
135+
add_compile_options_safe(-Wshadow=compatible-local)
136+
add_compile_options_safe(-Wshadow=local)
137+
add_compile_options_safe(-Wshadow-field)
138+
add_compile_options_safe(-Wshadow-field-in-constructor)
139+
add_compile_options_safe(-Wshadow-field-in-constructor-modified)
140+
add_compile_options_safe(-Wshadow-ivar)
141+
add_compile_options_safe(-Wshadow-uncaptured-local)
142+
# add_compile_options(-Wsign-promo)
143+
add_compile_options(-Wstack-protector)
144+
add_compile_options_safe(-Wstrict-null-sentinel)
145+
add_compile_options_safe(-Wstring-conversion)
146+
add_compile_options_safe(-Wstring-plus-char)
147+
add_compile_options_safe(-Wsuggest-attribute=cold)
148+
add_compile_options_safe(-Wsuggest-attribute=const)
149+
add_compile_options_safe(-Wsuggest-attribute=format)
150+
add_compile_options_safe(-Wsuggest-attribute=malloc)
151+
add_compile_options_safe(-Wsuggest-attribute=noreturn)
152+
add_compile_options_safe(-Wsuggest-attribute=pure)
153+
add_compile_options_safe(-Wsuggest-final-methods)
154+
add_compile_options_safe(-Wsuggest-final-types)
155+
add_compile_options_safe(-Wsuggest-destructor-override)
156+
add_compile_options_safe(-Wsuggest-override)
157+
# add_compile_options(-Wswitch-default)
158+
add_compile_options(-Wswitch-enum)
159+
add_compile_options(-Wsynth)
160+
add_compile_options_safe(-Wtrampolines)
161+
add_compile_options(-Wundef)
162+
add_compile_options_safe(-Wunused-label)
163+
add_compile_options_safe(-Wunused-lambda-capture)
164+
add_compile_options_safe(-Wunused-local-typedef)
165+
add_compile_options(-Wunused-macros)
166+
add_compile_options_safe(-Wuseless-cast)
167+
add_compile_options_safe(-Wvector-conversion)
168+
add_compile_options_safe(-Wvector-operation-performance)
169+
add_compile_options_safe(-Wvirtual-inheritance)
170+
add_compile_options_safe(-Wvolatile)
171+
add_compile_options(-Wzero-as-null-pointer-constant)
172+
add_compile_options_safe(-Warray-bounds=2)
173+
add_compile_options_safe(-Wattribute-alias=2)
174+
add_compile_options_safe(-Wcatch-value=3)
175+
add_compile_options_safe(-Wformat-overflow=2)
176+
add_compile_options_safe(-Wformat-truncation=2)
177+
add_compile_options_safe(-Wformat=2)
178+
add_compile_options(-Wimplicit-fallthrough)
179+
add_compile_options_safe(-Wimplicit-fallthrough=3)
180+
add_compile_options_safe(-Wplacement-new=2)
181+
add_compile_options_safe(-Wshift-overflow=2)
182+
add_compile_options_safe(-Wstrict-aliasing=3)
183+
add_compile_options(-Wstrict-overflow=2)
184+
add_compile_options_safe(-Wstringop-overflow=4)
185+
add_compile_options(-Wunused-const-variable)
186+
add_compile_options_safe(-Wunused-const-variable=1)
169187
elseif (MSVC)
170188
if (WARNINGS_ARE_ERRORS)
171189
add_compile_options("/WX")

0 commit comments

Comments
 (0)