From 7f345b7a4ed910bdba1d91c442cf6a890c4a0bb6 Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Fri, 5 Feb 2021 14:12:26 +0800 Subject: [PATCH] fix build option issues; refine cmake --- CMakeLists.txt | 2 + autotest/CMakeLists.txt | 3 +- examples/CMakeLists.txt | 45 ++++++++----------- examples/book_client/CMakeLists.txt | 9 ++-- examples/book_server/CMakeLists.txt | 1 + examples/qt_app_server/CMakeLists.txt | 5 +-- examples/qt_app_server/{main.cpp => main.cc} | 0 .../{main_window.cpp => main_window.cc} | 0 unittest/CMakeLists.txt | 14 ++++-- webcc/CMakeLists.txt | 4 +- webcc/body.cc | 4 +- webcc/body.h | 4 +- webcc/client.cc | 2 +- webcc/client_session.cc | 2 +- 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 +- 26 files changed, 71 insertions(+), 66 deletions(-) rename examples/qt_app_server/{main.cpp => main.cc} (100%) rename examples/qt_app_server/{main_window.cpp => main_window.cc} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c988ae..620d5c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,6 +87,8 @@ if(WEBCC_ENABLE_GZIP) endif() endif() +set_property(GLOBAL PROPERTY USE_FOLDERS ON) + add_subdirectory(webcc) if(BUILD_AUTOTEST OR BUILD_EXAMPLES) diff --git a/autotest/CMakeLists.txt b/autotest/CMakeLists.txt index d9d1bfc..cb3f7ec 100644 --- a/autotest/CMakeLists.txt +++ b/autotest/CMakeLists.txt @@ -17,7 +17,8 @@ if(UNIX) set(AT_LIBS ${AT_LIBS} ${CMAKE_DL_LIBS}) endif() -set(AT_TARGET_NAME webcc_autotest) +set(AT_TARGET_NAME autotest) add_executable(${AT_TARGET_NAME} ${AT_SRCS}) target_link_libraries(${AT_TARGET_NAME} ${AT_LIBS}) +set_target_properties(${AT_TARGET_NAME} PROPERTIES FOLDER "Tests") diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e97e879..611a2e4 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -8,41 +8,34 @@ if(UNIX) set(EXAMPLE_LIBS ${EXAMPLE_LIBS} ${CMAKE_DL_LIBS}) endif() -add_executable(concurrency_test concurrency_test.cc) -target_link_libraries(concurrency_test ${EXAMPLE_LIBS}) - -add_executable(client_basics client_basics.cc) -target_link_libraries(client_basics ${EXAMPLE_LIBS}) +set(SIMPLE_EXAMPLES + concurrency_test + client_basics + hello_world_server + static_file_server + file_downloader + server_states + form_client + form_server + form_urlencoded_client + ) + +foreach(example ${SIMPLE_EXAMPLES}) + add_executable(${example} ${example}.cc) + target_link_libraries(${example} ${EXAMPLE_LIBS}) + set_target_properties(${example} PROPERTIES FOLDER "Examples") +endforeach() if(WEBCC_ENABLE_SSL) add_executable(github_client github_client.cc) target_link_libraries(github_client ${EXAMPLE_LIBS} jsoncpp) + set_target_properties(github_client PROPERTIES FOLDER "Examples") endif() -add_executable(hello_world_server hello_world_server.cc) -target_link_libraries(hello_world_server ${EXAMPLE_LIBS}) - -add_executable(static_file_server static_file_server.cc) -target_link_libraries(static_file_server ${EXAMPLE_LIBS}) - -add_executable(file_downloader file_downloader.cc) -target_link_libraries(file_downloader ${EXAMPLE_LIBS}) - -add_executable(server_states server_states.cc) -target_link_libraries(server_states ${EXAMPLE_LIBS}) - -add_executable(form_client form_client.cc) -target_link_libraries(form_client ${EXAMPLE_LIBS}) - -add_executable(form_urlencoded_client form_urlencoded_client.cc) -target_link_libraries(form_urlencoded_client ${EXAMPLE_LIBS}) - -add_executable(form_server form_server.cc) -target_link_libraries(form_server ${EXAMPLE_LIBS}) - if(WIN32) add_executable(url_unicode url_unicode.cc encoding.cc encoding.h) target_link_libraries(url_unicode ${EXAMPLE_LIBS}) + set_target_properties(url_unicode PROPERTIES FOLDER "Examples") endif() add_subdirectory(book_server) diff --git a/examples/book_client/CMakeLists.txt b/examples/book_client/CMakeLists.txt index efd14b5..122cd18 100644 --- a/examples/book_client/CMakeLists.txt +++ b/examples/book_client/CMakeLists.txt @@ -1,8 +1,8 @@ set(SRCS - book.cc - book.h - book_json.cc - book_json.h + book.cc + book.h + book_json.cc + book_json.h book_client.cc book_client.h main.cc @@ -10,3 +10,4 @@ set(SRCS add_executable(book_client ${SRCS}) target_link_libraries(book_client ${EXAMPLE_LIBS} jsoncpp) +set_target_properties(book_client PROPERTIES FOLDER "Examples") diff --git a/examples/book_server/CMakeLists.txt b/examples/book_server/CMakeLists.txt index bf4aa4f..b999e6a 100644 --- a/examples/book_server/CMakeLists.txt +++ b/examples/book_server/CMakeLists.txt @@ -12,3 +12,4 @@ set(SRCS add_executable(book_server ${SRCS}) target_link_libraries(book_server ${EXAMPLE_LIBS} jsoncpp) +set_target_properties(book_server PROPERTIES FOLDER "Examples") diff --git a/examples/qt_app_server/CMakeLists.txt b/examples/qt_app_server/CMakeLists.txt index f56cf9d..37d7e1a 100644 --- a/examples/qt_app_server/CMakeLists.txt +++ b/examples/qt_app_server/CMakeLists.txt @@ -1,11 +1,10 @@ -project(qt_app_server) - find_package(Qt5 CONFIG REQUIRED Core Gui Widgets) set(CMAKE_AUTOMOC ON) -file(GLOB SRCS *.cpp *.h) +set(SRCS main.cc main_window.cc main_window.h) add_executable(qt_app_server ${SRCS}) target_link_libraries(qt_app_server Qt5::Core Qt5::Gui Qt5::Widgets ${EXAMPLE_LIBS}) +set_target_properties(qt_app_server PROPERTIES FOLDER "Examples") diff --git a/examples/qt_app_server/main.cpp b/examples/qt_app_server/main.cc similarity index 100% rename from examples/qt_app_server/main.cpp rename to examples/qt_app_server/main.cc diff --git a/examples/qt_app_server/main_window.cpp b/examples/qt_app_server/main_window.cc similarity index 100% rename from examples/qt_app_server/main_window.cpp rename to examples/qt_app_server/main_window.cc diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 2a8b93a..f7c6da5 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -1,9 +1,16 @@ # Unit test -file(GLOB UT_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) +set(UT_TARGET_NAME unittest) -set(UT_TARGET_NAME webcc_unittest) +set(UT_SRCS + base64_unittest.cc + body_unittest.cc + request_parser_unittest.cc + response_builder_unittest.cc + router_unittest.cc + string_unittest.cc + url_unittest.cc + ) # Common libraries to link. set(UT_LIBS @@ -18,5 +25,6 @@ endif() add_executable(${UT_TARGET_NAME} ${UT_SRCS}) target_link_libraries(${UT_TARGET_NAME} ${UT_LIBS}) +set_target_properties(${UT_TARGET_NAME} PROPERTIES FOLDER "Tests") add_test(${UT_TARGET_NAME} ${UT_TARGET_NAME}) diff --git a/webcc/CMakeLists.txt b/webcc/CMakeLists.txt index d5647f6..f6619c9 100644 --- a/webcc/CMakeLists.txt +++ b/webcc/CMakeLists.txt @@ -1,10 +1,10 @@ # webcc # Don't use any deprecated definitions (e.g., io_service). -add_definitions(-DBOOST_ASIO_NO_DEPRECATED) +add_compile_definitions(BOOST_ASIO_NO_DEPRECATED) if(MSVC) - add_definitions(-D_CRT_SECURE_NO_WARNINGS) + add_compile_definitions(_CRT_SECURE_NO_WARNINGS) endif() configure_file( diff --git a/webcc/body.cc b/webcc/body.cc index 83f606d..8f13c6a 100644 --- a/webcc/body.cc +++ b/webcc/body.cc @@ -5,7 +5,7 @@ #include "webcc/logger.h" #include "webcc/utility.h" -#ifdef WEBCC_ENABLE_GZIP +#if WEBCC_ENABLE_GZIP #include "webcc/gzip.h" #endif @@ -15,7 +15,7 @@ namespace webcc { // ----------------------------------------------------------------------------- -#ifdef WEBCC_ENABLE_GZIP +#if WEBCC_ENABLE_GZIP bool StringBody::Compress() { if (compressed_) { diff --git a/webcc/body.h b/webcc/body.h index cf59f79..c87528d 100644 --- a/webcc/body.h +++ b/webcc/body.h @@ -29,7 +29,7 @@ public: return GetSize() == 0; } -#ifdef WEBCC_ENABLE_GZIP +#if 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_; } -#ifdef WEBCC_ENABLE_GZIP +#if WEBCC_ENABLE_GZIP bool Compress() override; diff --git a/webcc/client.cc b/webcc/client.cc index e0b3b63..981a630 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") { -#ifdef WEBCC_ENABLE_SSL +#if 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 d61adb7..4472347 100644 --- a/webcc/client_session.cc +++ b/webcc/client_session.cc @@ -19,7 +19,7 @@ void ClientSession::Accept(const std::string& content_types) { } } -#ifdef WEBCC_ENABLE_GZIP +#if WEBCC_ENABLE_GZIP // Content-Encoding Tokens: // (https://en.wikipedia.org/wiki/HTTP_compression) diff --git a/webcc/client_session.h b/webcc/client_session.h index 3de5e2f..b420c36 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); -#ifdef WEBCC_ENABLE_GZIP +#if 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 8f1ead0..287847e 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 -#ifdef WEBCC_ENABLE_LOG +#if 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 d6be7d5..3b84c81 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@ -#ifdef WEBCC_ENABLE_LOG +#if 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 0aa123e..a10439e 100644 --- a/webcc/logger.cc +++ b/webcc/logger.cc @@ -1,6 +1,6 @@ #include "webcc/logger.h" -#ifdef WEBCC_ENABLE_LOG +#if WEBCC_ENABLE_LOG #include #include diff --git a/webcc/logger.h b/webcc/logger.h index 56f5417..d106233 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" -#ifdef WEBCC_ENABLE_LOG +#if WEBCC_ENABLE_LOG #include // for strrchr() #include diff --git a/webcc/parser.cc b/webcc/parser.cc index 5671cc3..c6807bf 100644 --- a/webcc/parser.cc +++ b/webcc/parser.cc @@ -7,7 +7,7 @@ #include "webcc/string.h" #include "webcc/utility.h" -#ifdef WEBCC_ENABLE_GZIP +#if WEBCC_ENABLE_GZIP #include "webcc/gzip.h" #endif @@ -48,7 +48,7 @@ bool StringBodyHandler::Finish() { auto body = std::make_shared(std::move(content_), IsCompressed()); -#ifdef WEBCC_ENABLE_GZIP +#if 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 2344b1a..2d4096f 100644 --- a/webcc/request_builder.cc +++ b/webcc/request_builder.cc @@ -5,7 +5,7 @@ #include "webcc/string.h" #include "webcc/utility.h" -#ifdef WEBCC_ENABLE_GZIP +#if WEBCC_ENABLE_GZIP #include "webcc/gzip.h" #endif @@ -32,7 +32,7 @@ RequestPtr RequestBuilder::operator()() { if (body_) { request->SetContentType(media_type_, charset_); -#ifdef WEBCC_ENABLE_GZIP +#if WEBCC_ENABLE_GZIP if (gzip_ && body_->Compress()) { request->SetHeader(headers::kContentEncoding, "gzip"); } @@ -54,7 +54,7 @@ RequestPtr RequestBuilder::operator()() { return request; } -#ifdef WEBCC_ENABLE_GZIP +#if 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 ff49dc2..609c518 100644 --- a/webcc/request_builder.h +++ b/webcc/request_builder.h @@ -124,7 +124,7 @@ public: return Header(headers::kAccept, content_types); } -#ifdef WEBCC_ENABLE_GZIP +#if 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(); -#ifdef WEBCC_ENABLE_GZIP +#if WEBCC_ENABLE_GZIP // Compress the body data (only for string body). // NOTE: @@ -220,7 +220,7 @@ private: // Persistent connection. bool keep_alive_ = true; -#ifdef WEBCC_ENABLE_GZIP +#if WEBCC_ENABLE_GZIP bool gzip_ = false; #endif // WEBCC_ENABLE_GZIP }; diff --git a/webcc/response_builder.cc b/webcc/response_builder.cc index bfa18c5..bbedf1b 100644 --- a/webcc/response_builder.cc +++ b/webcc/response_builder.cc @@ -4,7 +4,7 @@ #include "webcc/logger.h" #include "webcc/utility.h" -#ifdef WEBCC_ENABLE_GZIP +#if WEBCC_ENABLE_GZIP #include "webcc/gzip.h" #endif @@ -24,7 +24,7 @@ ResponsePtr ResponseBuilder::operator()() { if (body_) { response->SetContentType(media_type_, charset_); -#ifdef WEBCC_ENABLE_GZIP +#if 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 e46d5c1..955b8d6 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(); -#ifdef WEBCC_ENABLE_GZIP +#if 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_; -#ifdef WEBCC_ENABLE_GZIP +#if 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 125a244..86577f8 100644 --- a/webcc/socket.cc +++ b/webcc/socket.cc @@ -1,6 +1,6 @@ #include "webcc/socket.h" -#ifdef WEBCC_ENABLE_SSL +#if WEBCC_ENABLE_SSL #if (defined(_WIN32) || defined(_WIN64)) #include @@ -75,7 +75,7 @@ bool Socket::Close() { // ----------------------------------------------------------------------------- -#ifdef WEBCC_ENABLE_SSL +#if WEBCC_ENABLE_SSL #if (defined(_WIN32) || defined(_WIN64)) diff --git a/webcc/socket.h b/webcc/socket.h index 43e0b44..0c628d2 100644 --- a/webcc/socket.h +++ b/webcc/socket.h @@ -8,7 +8,7 @@ #include "webcc/config.h" #include "webcc/request.h" -#ifdef WEBCC_ENABLE_SSL +#if WEBCC_ENABLE_SSL #include "boost/asio/ssl.hpp" #endif // WEBCC_ENABLE_SSL @@ -62,7 +62,7 @@ private: // ----------------------------------------------------------------------------- -#ifdef WEBCC_ENABLE_SSL +#if WEBCC_ENABLE_SSL class SslSocket : public SocketBase { public: