From 1b8b0b24acb3594f6a187842a52a2e1d41e6ed28 Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Mon, 1 Feb 2021 09:23:53 +0800 Subject: [PATCH] refine the usage of build options --- CMakeLists.txt | 22 +++++++++++----------- doc/Build-Instructions_zh_CN.md | 11 +++++++---- examples/CMakeLists.txt | 2 +- webcc/body.cc | 6 +++--- webcc/body.h | 4 ++-- webcc/client.cc | 2 +- webcc/client_session.cc | 4 ++-- webcc/client_session.h | 2 +- webcc/config.h.example | 2 +- webcc/config.h.in | 2 +- webcc/logger.cc | 2 +- webcc/logger.h | 2 +- webcc/parser.cc | 4 ++-- webcc/request_builder.cc | 6 +++--- webcc/request_builder.h | 6 +++--- webcc/response_builder.cc | 4 ++-- webcc/response_builder.h | 4 ++-- webcc/socket.cc | 4 ++-- webcc/socket.h | 4 ++-- 19 files changed, 48 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea6712c..6c988ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,10 +20,10 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIR}/bin/release) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIR}/bin/release) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIR}/bin/release) -option(WEBCC_ENABLE_AUTOTEST "Build automation test?" OFF) -option(WEBCC_ENABLE_UNITTEST "Build unit test?" OFF) -option(WEBCC_ENABLE_EXAMPLES "Build examples?" ON) -option(WEBCC_ENABLE_QT_EXAMPLES "Build Qt application examples?" OFF) +option(BUILD_AUTOTEST "Build automation test?" OFF) +option(BUILD_UNITTEST "Build unit test?" OFF) +option(BUILD_EXAMPLES "Build examples?" ON) +option(BUILD_QT_EXAMPLES "Build Qt application examples?" OFF) set(WEBCC_ENABLE_LOG 1 CACHE STRING "Enable logging? (1:Yes, 0:No)") set(WEBCC_ENABLE_SSL 0 CACHE STRING "Enable SSL/HTTPS (need OpenSSL)? (1:Yes, 0:No)") @@ -31,14 +31,14 @@ set(WEBCC_ENABLE_GZIP 0 CACHE STRING "Enable gzip compression (need Zlib)? (1:Y set(WEBCC_LOG_LEVEL 2 CACHE STRING "Log level (0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO)") -if(WEBCC_ENABLE_UNITTEST) +if(BUILD_UNITTEST) enable_testing() endif() if(WIN32) # Asio needs this! # 0x0601 means Win7. So our application targets Win7 and above. - add_definitions(-D_WIN32_WINNT=0x0601) + add_compile_definitions(_WIN32_WINNT=0x0601) endif() # C++ standard requirements. @@ -89,7 +89,7 @@ endif() add_subdirectory(webcc) -if(WEBCC_ENABLE_AUTOTEST OR WEBCC_ENABLE_EXAMPLES) +if(BUILD_AUTOTEST OR BUILD_EXAMPLES) # For including jsoncpp as "json/json.h". include_directories(${THIRD_PARTY_DIR}/src/jsoncpp) @@ -97,7 +97,7 @@ if(WEBCC_ENABLE_AUTOTEST OR WEBCC_ENABLE_EXAMPLES) endif() # GTest -if(WEBCC_ENABLE_AUTOTEST OR WEBCC_ENABLE_UNITTEST) +if(BUILD_AUTOTEST OR BUILD_UNITTEST) find_package(GTest REQUIRED) if(GTEST_FOUND) add_definitions(-DGTEST_LANG_CXX11=1) @@ -105,14 +105,14 @@ if(WEBCC_ENABLE_AUTOTEST OR WEBCC_ENABLE_UNITTEST) endif() endif() -if(WEBCC_ENABLE_AUTOTEST) +if(BUILD_AUTOTEST) add_subdirectory(autotest) endif() -if(WEBCC_ENABLE_UNITTEST) +if(BUILD_UNITTEST) add_subdirectory(unittest) endif() -if(WEBCC_ENABLE_EXAMPLES) +if(BUILD_EXAMPLES) add_subdirectory(examples) endif() diff --git a/doc/Build-Instructions_zh_CN.md b/doc/Build-Instructions_zh_CN.md index ebccbfc..e273ca3 100644 --- a/doc/Build-Instructions_zh_CN.md +++ b/doc/Build-Instructions_zh_CN.md @@ -141,7 +141,9 @@ $ ls -l /usr/local/lib/libboost* ### Googletest -TODO +``` +$ sudo apt install libgtest-dev +``` ### Webcc @@ -156,13 +158,14 @@ $ cd build ``` $ cmake -G"Unix Makefiles" \ + -DBUILD_AUTOTEST=OFF \ + -DBUILD_UNITTEST=OFF \ + -DBUILD_EXAMPLES=ON \ + -DBUILD_QT_EXAMPLES=OFF \ -DWEBCC_ENABLE_LOG=1 \ -DWEBCC_LOG_LEVEL=0 \ -DWEBCC_ENABLE_SSL=1 \ -DWEBCC_ENABLE_GZIP=1 \ - -DWEBCC_ENABLE_AUTOTEST=OFF \ - -DWEBCC_ENABLE_UNITTEST=OFF \ - -DWEBCC_ENABLE_EXAMPLES=ON \ .. ``` diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 342b81b..e97e879 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -48,6 +48,6 @@ endif() add_subdirectory(book_server) add_subdirectory(book_client) -if(WEBCC_ENABLE_QT_EXAMPLES) +if(BUILD_QT_EXAMPLES) add_subdirectory(qt_app_server) endif() diff --git a/webcc/body.cc b/webcc/body.cc index 0407cdf..83f606d 100644 --- a/webcc/body.cc +++ b/webcc/body.cc @@ -5,7 +5,7 @@ #include "webcc/logger.h" #include "webcc/utility.h" -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP #include "webcc/gzip.h" #endif @@ -15,7 +15,7 @@ namespace webcc { // ----------------------------------------------------------------------------- -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP bool StringBody::Compress() { if (compressed_) { @@ -110,7 +110,7 @@ void FormBody::InitPayload() { Payload FormBody::NextPayload(bool free_previous) { Payload payload; - + // Free previous payload. if (free_previous) { if (index_ > 0) { diff --git a/webcc/body.h b/webcc/body.h index c87528d..cf59f79 100644 --- a/webcc/body.h +++ b/webcc/body.h @@ -29,7 +29,7 @@ public: return GetSize() == 0; } -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP // Compress the data with Gzip. // If data size <= threshold (1400 bytes), no compression will be taken @@ -90,7 +90,7 @@ public: return compressed_; } -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP bool Compress() override; diff --git a/webcc/client.cc b/webcc/client.cc index beaca45..69ad674 100644 --- a/webcc/client.cc +++ b/webcc/client.cc @@ -78,7 +78,7 @@ void Client::Close() { void Client::Connect(RequestPtr request) { if (request->url().scheme() == "https") { -#if WEBCC_ENABLE_SSL +#ifdef WEBCC_ENABLE_SSL socket_.reset(new SslSocket{ io_context_, ssl_verify_ }); DoConnect(request, "443"); #else diff --git a/webcc/client_session.cc b/webcc/client_session.cc index 4f3552b..d61adb7 100644 --- a/webcc/client_session.cc +++ b/webcc/client_session.cc @@ -19,7 +19,7 @@ void ClientSession::Accept(const std::string& content_types) { } } -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP // Content-Encoding Tokens: // (https://en.wikipedia.org/wiki/HTTP_compression) @@ -117,7 +117,7 @@ ResponsePtr ClientSession::DoSend(RequestPtr request, bool stream) { client->set_ssl_verify(ssl_verify_); client->set_buffer_size(buffer_size_); client->set_timeout(timeout_); - + Error error = client->Request(request, !reuse, stream); if (error) { diff --git a/webcc/client_session.h b/webcc/client_session.h index b420c36..3de5e2f 100644 --- a/webcc/client_session.h +++ b/webcc/client_session.h @@ -51,7 +51,7 @@ public: // Set content types to accept. void Accept(const std::string& content_types); -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP // Accept Gzip compressed response data or not. void AcceptGzip(bool gzip = true); diff --git a/webcc/config.h.example b/webcc/config.h.example index 287847e..8f1ead0 100644 --- a/webcc/config.h.example +++ b/webcc/config.h.example @@ -6,7 +6,7 @@ // Set 1/0 to enable/disable logging. #define WEBCC_ENABLE_LOG 1 -#if WEBCC_ENABLE_LOG +#ifdef WEBCC_ENABLE_LOG // 0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO #define WEBCC_LOG_LEVEL 2 #endif diff --git a/webcc/config.h.in b/webcc/config.h.in index 3b84c81..d6be7d5 100644 --- a/webcc/config.h.in +++ b/webcc/config.h.in @@ -8,7 +8,7 @@ // Set 1/0 to enable/disable logging. #define WEBCC_ENABLE_LOG @WEBCC_ENABLE_LOG@ -#if WEBCC_ENABLE_LOG +#ifdef WEBCC_ENABLE_LOG // 0:VERB, 1:INFO, 2:USER, 3:WARN or 4:ERRO #define WEBCC_LOG_LEVEL @WEBCC_LOG_LEVEL@ #endif // WEBCC_ENABLE_LOG diff --git a/webcc/logger.cc b/webcc/logger.cc index a10439e..0aa123e 100644 --- a/webcc/logger.cc +++ b/webcc/logger.cc @@ -1,6 +1,6 @@ #include "webcc/logger.h" -#if WEBCC_ENABLE_LOG +#ifdef WEBCC_ENABLE_LOG #include #include diff --git a/webcc/logger.h b/webcc/logger.h index d106233..56f5417 100644 --- a/webcc/logger.h +++ b/webcc/logger.h @@ -4,7 +4,7 @@ // This file was generated from "config.h.in" by CMake. #include "webcc/config.h" -#if WEBCC_ENABLE_LOG +#ifdef WEBCC_ENABLE_LOG #include // for strrchr() #include diff --git a/webcc/parser.cc b/webcc/parser.cc index c6807bf..5671cc3 100644 --- a/webcc/parser.cc +++ b/webcc/parser.cc @@ -7,7 +7,7 @@ #include "webcc/string.h" #include "webcc/utility.h" -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP #include "webcc/gzip.h" #endif @@ -48,7 +48,7 @@ bool StringBodyHandler::Finish() { auto body = std::make_shared(std::move(content_), IsCompressed()); -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP LOG_INFO("Decompress the HTTP content..."); if (!body->Decompress()) { LOG_ERRO("Cannot decompress the HTTP content!"); diff --git a/webcc/request_builder.cc b/webcc/request_builder.cc index 2d4096f..2344b1a 100644 --- a/webcc/request_builder.cc +++ b/webcc/request_builder.cc @@ -5,7 +5,7 @@ #include "webcc/string.h" #include "webcc/utility.h" -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP #include "webcc/gzip.h" #endif @@ -32,7 +32,7 @@ RequestPtr RequestBuilder::operator()() { if (body_) { request->SetContentType(media_type_, charset_); -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP if (gzip_ && body_->Compress()) { request->SetHeader(headers::kContentEncoding, "gzip"); } @@ -54,7 +54,7 @@ RequestPtr RequestBuilder::operator()() { return request; } -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP // Accept Gzip compressed response data or not. RequestBuilder& RequestBuilder::AcceptGzip(bool gzip) { diff --git a/webcc/request_builder.h b/webcc/request_builder.h index 609c518..ff49dc2 100644 --- a/webcc/request_builder.h +++ b/webcc/request_builder.h @@ -124,7 +124,7 @@ public: return Header(headers::kAccept, content_types); } -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP // Accept Gzip compressed response data or not. RequestBuilder& AcceptGzip(bool gzip = true); @@ -179,7 +179,7 @@ public: // Add the `Date` header to the request. RequestBuilder& Date(); -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP // Compress the body data (only for string body). // NOTE: @@ -220,7 +220,7 @@ private: // Persistent connection. bool keep_alive_ = true; -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP bool gzip_ = false; #endif // WEBCC_ENABLE_GZIP }; diff --git a/webcc/response_builder.cc b/webcc/response_builder.cc index bbedf1b..bfa18c5 100644 --- a/webcc/response_builder.cc +++ b/webcc/response_builder.cc @@ -4,7 +4,7 @@ #include "webcc/logger.h" #include "webcc/utility.h" -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP #include "webcc/gzip.h" #endif @@ -24,7 +24,7 @@ ResponsePtr ResponseBuilder::operator()() { if (body_) { response->SetContentType(media_type_, charset_); -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP if (gzip_) { // Don't try to compress the response if the request doesn't accept gzip. if (request_ && request_->AcceptEncodingGzip()) { diff --git a/webcc/response_builder.h b/webcc/response_builder.h index 955b8d6..e46d5c1 100644 --- a/webcc/response_builder.h +++ b/webcc/response_builder.h @@ -109,7 +109,7 @@ public: // Add the `Date` header to the response. ResponseBuilder& Date(); -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP ResponseBuilder& Gzip(bool gzip = true) { gzip_ = gzip; return *this; @@ -131,7 +131,7 @@ private: // Character set of the body (e.g., "utf-8"). std::string charset_; -#if WEBCC_ENABLE_GZIP +#ifdef WEBCC_ENABLE_GZIP // Compress the body data (only for string body). bool gzip_ = false; #endif // WEBCC_ENABLE_GZIP diff --git a/webcc/socket.cc b/webcc/socket.cc index 86577f8..125a244 100644 --- a/webcc/socket.cc +++ b/webcc/socket.cc @@ -1,6 +1,6 @@ #include "webcc/socket.h" -#if WEBCC_ENABLE_SSL +#ifdef WEBCC_ENABLE_SSL #if (defined(_WIN32) || defined(_WIN64)) #include @@ -75,7 +75,7 @@ bool Socket::Close() { // ----------------------------------------------------------------------------- -#if WEBCC_ENABLE_SSL +#ifdef WEBCC_ENABLE_SSL #if (defined(_WIN32) || defined(_WIN64)) diff --git a/webcc/socket.h b/webcc/socket.h index 0c628d2..43e0b44 100644 --- a/webcc/socket.h +++ b/webcc/socket.h @@ -8,7 +8,7 @@ #include "webcc/config.h" #include "webcc/request.h" -#if WEBCC_ENABLE_SSL +#ifdef WEBCC_ENABLE_SSL #include "boost/asio/ssl.hpp" #endif // WEBCC_ENABLE_SSL @@ -62,7 +62,7 @@ private: // ----------------------------------------------------------------------------- -#if WEBCC_ENABLE_SSL +#ifdef WEBCC_ENABLE_SSL class SslSocket : public SocketBase { public: