Skip to content

Commit c4e16aa

Browse files
committed
cmake: set warning and error options
The set is far from perfect, but guards against common errors with GCC.
1 parent 9841591 commit c4e16aa

9 files changed

Lines changed: 147 additions & 7 deletions

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ add_definitions(-DQBT_VERSION="v${PROJECT_VERSION}")
3030
add_definitions(-DQBT_VERSION_2="${PROJECT_VERSION}")
3131
# }
3232

33-
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Og")
3433
if (UNIX AND NOT APPLE)
3534
include(GNUInstallDirs)
3635
endif (UNIX AND NOT APPLE)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
###############################################################
2+
#
3+
# Copyright 2011 Red Hat, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you
6+
# may not use this file except in compliance with the License. You may
7+
# obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
###############################################################
18+
19+
MACRO (GLIBC_DETECT _VERSION)
20+
21+
# there are multiple ways to detect glibc, but given nmi's
22+
# cons'd up paths I will trust only gcc. I guess I could also use
23+
# ldd --version to detect.
24+
25+
set(_GLIB_SOURCE_DETECT "
26+
#include <limits.h>
27+
#include <stdio.h>
28+
int main()
29+
{
30+
printf(\"%d%d\",__GLIBC__, __GLIBC_MINOR__);
31+
return 0;
32+
}
33+
")
34+
35+
file (WRITE ${CMAKE_CURRENT_BINARY_DIR}/build/cmake/glibc.cpp "${_GLIB_SOURCE_DETECT}\n")
36+
37+
try_run(POST26_GLIBC_DETECTED
38+
POST26_GLIBC_COMPILE
39+
${CMAKE_CURRENT_BINARY_DIR}/build/cmake
40+
${CMAKE_CURRENT_BINARY_DIR}/build/cmake/glibc.cpp
41+
RUN_OUTPUT_VARIABLE GLIBC_VERSION )
42+
43+
if (GLIBC_VERSION AND POST26_GLIBC_COMPILE )
44+
set(${_VERSION} ${GLIBC_VERSION})
45+
else()
46+
message(STATUS "NOTE: Could not detect GLIBC_VERSION from compiler")
47+
endif()
48+
49+
ENDMACRO (GLIBC_DETECT)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Sets cache variable QBT_ADDITONAL_FLAGS and QBT_ADDITONAL_CXX_FLAGS to list of additional
2+
# compiler flags for C and C++ (QBT_ADDITONAL_FLAGS) and for C++ only (QBT_ADDITONAL_CXX_FLAGS)
3+
# and appends them to CMAKE_XXX_FLAGS variables.
4+
5+
# It could use add_compile_options(), but then it is needed to use generator expressions,
6+
# and most interesting of them are not compatible with Visual Studio :(
7+
8+
macro(qbt_set_compiler_options)
9+
# if (NOT QBT_ADDITONAL_FLAGS)
10+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
11+
#-Wshadow -Wconversion ?
12+
set(_GCC_COMMON_C_AND_CXX_FLAGS "-Wall -Wextra"
13+
"-Wfloat-equal -Wcast-qual -Wcast-align"
14+
"-Wsign-conversion -Winvalid-pch -Werror=return-type -Wno-long-long"
15+
# -fstack-protector-all
16+
"-Werror -Wno-error=deprecated-declarations"
17+
)
18+
set (_GCC_COMMON_CXX_FLAGS "-fexceptions -frtti"
19+
"-Woverloaded-virtual -Wold-style-cast -Wstrict-null-sentinel"
20+
"-Wnon-virtual-dtor -Wfloat-equal -Wcast-qual -Wcast-align"
21+
"-Werror=overloaded-virtual"
22+
# "-Weffc++"
23+
"-Werror -Wno-error=cpp"
24+
# we should modify code to make these ones obsolete
25+
"-Wno-error=old-style-cast -Wno-error=sign-conversion -Wno-error=float-equal"
26+
)
27+
28+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
29+
# GCC 4.8 has problems with std::array and its initialization
30+
list(APPEND _GCC_COMMON_CXX_FLAGS "-Wno-error=missing-field-initializers")
31+
endif()
32+
33+
include(CheckCXXCompilerFlag)
34+
# check for -pedantic
35+
check_cxx_compiler_flag(-pedantic _PEDANTIC_IS_SUPPORTED)
36+
if (_PEDANTIC_IS_SUPPORTED)
37+
list(APPEND _GCC_COMMON_CXX_FLAGS "-pedantic -pedantic-errors")
38+
else (_PEDANTIC_IS_SUPPORTED)
39+
list(APPEND _GCC_COMMON_CXX_FLAGS "-Wpedantic")
40+
endif (_PEDANTIC_IS_SUPPORTED)
41+
42+
if (CMAKE_SYSTEM_NAME MATCHES Linux)
43+
# if Glibc version is 2.20 or higher, set -D_DEFAULT_SOURCE
44+
include(MacroGlibcDetect)
45+
message(STATUS "Detecting Glibc version...")
46+
glibc_detect(GLIBC_VERSION)
47+
if(${GLIBC_VERSION})
48+
if(GLIBC_VERSION LESS "220")
49+
message(STATUS "Glibc version is ${GLIBC_VERSION}")
50+
else(GLIBC_VERSION LESS "220")
51+
message(STATUS "Glibc version is ${GLIBC_VERSION}, adding -D_DEFAULT_SOURCE")
52+
add_definitions(-D_DEFAULT_SOURCE)
53+
endif(GLIBC_VERSION LESS "220")
54+
endif(${GLIBC_VERSION})
55+
endif (CMAKE_SYSTEM_NAME MATCHES Linux)
56+
57+
string(REPLACE ";" " " _GCC_COMMON_C_AND_CXX_FLAGS_STRING "${_GCC_COMMON_C_AND_CXX_FLAGS}")
58+
string(REPLACE ";" " " _GCC_COMMON_CXX_FLAGS_STRING "${_GCC_COMMON_CXX_FLAGS}")
59+
60+
string(APPEND CMAKE_C_FLAGS " ${_GCC_COMMON_C_AND_CXX_FLAGS_STRING}")
61+
string(APPEND CMAKE_CXX_FLAGS " ${_GCC_COMMON_C_AND_CXX_FLAGS_STRING} ${_GCC_COMMON_CXX_FLAGS_STRING}")
62+
63+
set(QBT_ADDITONAL_FLAGS "${_GCC_COMMON_C_AND_CXX_FLAGS_STRING}" CACHE STRING
64+
"Additional qBittorent compile flags" FORCE)
65+
set(QBT_ADDITONAL_CXX_FLAGS "${_GCC_COMMON_CXX_FLAGS_STRING}" CACHE STRING
66+
"Additional qBittorent C++ compile flags" FORCE)
67+
68+
# check whether we can enable -Og optimization for debug build
69+
# also let's enable -march=native for debug builds
70+
check_cxx_compiler_flag(-Og _DEBUG_OPTIMIZATION_LEVEL_IS_SUPPORTED)
71+
72+
if (_DEBUG_OPTIMIZATION_LEVEL_IS_SUPPORTED)
73+
string(APPEND CMAKE_C_FLAGS_DEBUG " -Og -g3 -march=native -pipe" )
74+
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -Og -g3 -march=native -pipe" )
75+
else(_DEBUG_OPTIMIZATION_LEVEL_IS_SUPPORTED)
76+
string(APPEND CMAKE_C_FLAGS_DEBUG " -O0 -g3 -march=native -pipe" )
77+
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -O0 -g3 -march=native -pipe" )
78+
endif (_DEBUG_OPTIMIZATION_LEVEL_IS_SUPPORTED)
79+
endif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
80+
81+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
82+
set(QBT_ADDITONAL_FLAGS "-wd4290 -wd4275 -wd4251 /W4" CACHE STRING "Additional qBittorent compile flags")
83+
string(APPEND CMAKE_C_FLAGS " ${QBT_ADDITONAL_FLAGS}")
84+
string(APPEND CMAKE_CXX_FLAGS " ${QBT_ADDITONAL_FLAGS}")
85+
endif ()
86+
87+
# endif (NOT QBT_ADDITONAL_FLAGS)
88+
endmacro(qbt_set_compiler_options)
89+

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
22
set(CMAKE_CXX_STANDARD "11")
33
add_definitions(-DBOOST_NO_CXX11_RVALUE_REFERENCES)
44

5+
include(MacroQbtCompilerSettings)
6+
qbt_set_compiler_options()
7+
58
include(MacroLinkQtComponents)
69
include(QbtTargetSources)
710

src/base/net/private/geoipdatabase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class GeoIPDatabase
9696
// Search data
9797
mutable QHash<quint32, QString> m_countries;
9898
quint32 m_size;
99-
const uchar *m_data;
99+
uchar *m_data;
100100
};
101101

102102
#endif // GEOIPDATABASE_H

src/webui/abstractwebapplication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ Http::Response AbstractWebApplication::processRequest(const Http::Request &reque
121121
print(QObject::tr("Your IP address has been banned after too many failed authentication attempts."), Http::CONTENT_TYPE_TXT);
122122
}
123123
else {
124-
processRequest();
124+
doProcessRequest();
125125
}
126126

127127
return response();

src/webui/abstractwebapplication.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class AbstractWebApplication : public Http::ResponseBuilder, public Http::IReque
5454
explicit AbstractWebApplication(QObject *parent = 0);
5555
virtual ~AbstractWebApplication();
5656

57-
Http::Response processRequest(const Http::Request &request, const Http::Environment &env);
57+
Http::Response processRequest(const Http::Request &request, const Http::Environment &env) final;
5858

5959
protected:
60-
virtual void processRequest() = 0;
60+
virtual void doProcessRequest() = 0;
6161

6262
bool isBanned() const;
6363
int failedAttempts() const;

src/webui/webapplication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ bool WebApplication::isPublicScope()
814814
return (scope_ == DEFAULT_SCOPE || scope_ == VERSION_INFO);
815815
}
816816

817-
void WebApplication::processRequest()
817+
void WebApplication::doProcessRequest()
818818
{
819819
scope_ = DEFAULT_SCOPE;
820820
action_ = DEFAULT_ACTION;

src/webui/webapplication.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class WebApplication: public AbstractWebApplication
103103
QString action_;
104104
QStringList args_;
105105

106-
void processRequest();
106+
void doProcessRequest() override;
107107

108108
bool isPublicScope();
109109
void parsePath();

0 commit comments

Comments
 (0)