Add config.h.in which produces the config.h on CMake configure.

master
Chunting Gu 7 years ago
parent 964d21748a
commit 8c53b24bb7

@ -8,12 +8,10 @@ endif()
project(webcc) project(webcc)
option(WEBCC_ENABLE_LOG "Enable logging?" ON)
option(WEBCC_ENABLE_SOAP "Enable SOAP support (need pugixml)?" ON) option(WEBCC_ENABLE_SOAP "Enable SOAP support (need pugixml)?" ON)
option(WEBCC_ENABLE_SSL "Enable SSL/HTTPS support (need OpenSSL)?" OFF) option(WEBCC_ENABLE_SSL "Enable SSL/HTTPS support (need OpenSSL)?" OFF)
option(WEBCC_ENABLE_INSTALL "Enable to install library and headers with CMake?" OFF) option(WEBCC_ENABLE_UNITTEST "Build unit test?" ON)
option(WEBCC_BUILD_UNITTEST "Build unit test?" ON) option(WEBCC_ENABLE_EXAMPLES "Build examples?" ON)
option(WEBCC_BUILD_EXAMPLE "Build examples?" ON)
if(WIN32) if(WIN32)
option(WEBCC_ENABLE_VLD "Enable VLD (Visual Leak Detector)?" OFF) option(WEBCC_ENABLE_VLD "Enable VLD (Visual Leak Detector)?" OFF)
@ -22,36 +20,10 @@ if(WIN32)
endif() endif()
endif() endif()
set(WEBCC_LOG_LEVEL "VERB" CACHE STRING "Log level (VERB, INFO, WARN, ERRO or FATA)") set(WEBCC_ENABLE_LOG 1 CACHE STRING "Enable logging? (0:OFF, 1:ON)")
set(WEBCC_LOG_LEVEL 2 CACHE STRING "Log level (0:VERB, 1:INFO, 2:WARN, 3:ERRO or 4:FATA)")
if(WEBCC_ENABLE_LOG) if(WEBCC_ENABLE_UNITTEST)
add_definitions(-DWEBCC_ENABLE_LOG)
message(STATUS "WEBCC_LOG_LEVEL: ${WEBCC_LOG_LEVEL}")
# Add definition of WEBCC_LOG_LEVEL according variable WEBCC_LOG_LEVEL.
if(WEBCC_LOG_LEVEL MATCHES "^VERB$")
add_definitions(-DWEBCC_LOG_LEVEL=0)
elseif(WEBCC_LOG_LEVEL MATCHES "^INFO$")
add_definitions(-DWEBCC_LOG_LEVEL=1)
elseif(WEBCC_LOG_LEVEL MATCHES "^WARN$")
add_definitions(-DWEBCC_LOG_LEVEL=2)
elseif(WEBCC_LOG_LEVEL MATCHES "^ERRO$")
add_definitions(-DWEBCC_LOG_LEVEL=3)
elseif(WEBCC_LOG_LEVEL MATCHES "^FATA$")
add_definitions(-DWEBCC_LOG_LEVEL=4)
endif()
endif()
if(WEBCC_ENABLE_SOAP)
add_definitions(-DWEBCC_ENABLE_SOAP)
endif()
if(WEBCC_ENABLE_SSL)
add_definitions(-DWEBCC_ENABLE_SSL)
endif()
if(WEBCC_BUILD_UNITTEST)
enable_testing() enable_testing()
endif() endif()
@ -121,8 +93,12 @@ if(WEBCC_ENABLE_SSL)
endif() endif()
endif() endif()
# For including its own headers as "webcc/http_client.h". include_directories(
include_directories(${PROJECT_SOURCE_DIR}) # For including its own headers as "webcc/http_client.h".
${PROJECT_SOURCE_DIR}
# For including config.h as "webcc/config.h".
${PROJECT_BINARY_DIR}
)
set(THIRD_PARTY_DIR ${PROJECT_SOURCE_DIR}/third_party) set(THIRD_PARTY_DIR ${PROJECT_SOURCE_DIR}/third_party)
@ -141,7 +117,7 @@ endif()
add_subdirectory(webcc) add_subdirectory(webcc)
if(WEBCC_BUILD_EXAMPLE) if(WEBCC_ENABLE_EXAMPLES)
add_subdirectory(example/http_hello_client) add_subdirectory(example/http_hello_client)
add_subdirectory(example/http_hello_async_client) add_subdirectory(example/http_hello_async_client)
@ -167,7 +143,7 @@ if(WEBCC_BUILD_EXAMPLE)
endif() endif()
endif() endif()
if(WEBCC_BUILD_UNITTEST) if(WEBCC_ENABLE_UNITTEST)
add_subdirectory(third_party/src/gtest) add_subdirectory(third_party/src/gtest)
add_subdirectory(unittest) add_subdirectory(unittest)
endif() endif()

@ -1,14 +1,12 @@
# Unit test # Unit test
if(WEBCC_BUILD_UNITTEST) set(UT_SRCS
set(UT_SRCS rest_service_manager_test.cc
rest_service_manager_test.cc )
)
set(UT_TARGET_NAME webcc_unittest) set(UT_TARGET_NAME webcc_unittest)
add_executable(${UT_TARGET_NAME} ${UT_SRCS}) add_executable(${UT_TARGET_NAME} ${UT_SRCS})
target_link_libraries(${UT_TARGET_NAME} webcc pugixml gtest ${Boost_LIBRARIES}) target_link_libraries(${UT_TARGET_NAME} webcc pugixml gtest ${Boost_LIBRARIES})
target_link_libraries(${UT_TARGET_NAME} "${CMAKE_THREAD_LIBS_INIT}") target_link_libraries(${UT_TARGET_NAME} "${CMAKE_THREAD_LIBS_INIT}")
add_test(${UT_TARGET_NAME} ${UT_TARGET_NAME}) add_test(${UT_TARGET_NAME} ${UT_TARGET_NAME})
endif()

@ -12,15 +12,12 @@ class TestRestService : public webcc::RestService {
TEST(RestServiceManager, URL_RegexBasic) { TEST(RestServiceManager, URL_RegexBasic) {
webcc::RestServiceManager service_manager; webcc::RestServiceManager service_manager;
{ service_manager.AddService(std::make_shared<TestRestService>(),
webcc::RestServicePtr service = std::make_shared<TestRestService>(); "/instance/(\\d+)", true);
service_manager.AddService(service, "/instances/(\\d+)", true);
}
std::vector<std::string> sub_matches; std::vector<std::string> sub_matches;
std::string url = "/instances/12345"; std::string url = "/instance/12345";
webcc::RestServicePtr service = service_manager.GetService(url, &sub_matches); webcc::RestServicePtr service = service_manager.GetService(url, &sub_matches);
EXPECT_TRUE(!!service); EXPECT_TRUE(!!service);
@ -28,7 +25,33 @@ TEST(RestServiceManager, URL_RegexBasic) {
EXPECT_EQ(1, sub_matches.size()); EXPECT_EQ(1, sub_matches.size());
EXPECT_EQ("12345", sub_matches[0]); EXPECT_EQ("12345", sub_matches[0]);
url = "/instances/abcde"; url = "/instance/abcde";
sub_matches.clear();
service = service_manager.GetService(url, &sub_matches);
EXPECT_FALSE(!!service);
}
TEST(RestServiceManager, URL_RegexMultiple) {
webcc::RestServiceManager service_manager;
service_manager.AddService(std::make_shared<TestRestService>(),
"/study/(\\d+)/series/(\\d+)/instance/(\\d+)",
true);
std::vector<std::string> sub_matches;
std::string url = "/study/1/series/2/instance/3";
webcc::RestServicePtr service = service_manager.GetService(url, &sub_matches);
EXPECT_TRUE(!!service);
EXPECT_EQ(3, sub_matches.size());
EXPECT_EQ("1", sub_matches[0]);
EXPECT_EQ("2", sub_matches[1]);
EXPECT_EQ("3", sub_matches[2]);
url = "/study/a/series/b/instance/c";
sub_matches.clear(); sub_matches.clear();
service = service_manager.GetService(url, &sub_matches); service = service_manager.GetService(url, &sub_matches);
@ -38,10 +61,8 @@ TEST(RestServiceManager, URL_RegexBasic) {
TEST(RestServiceManager, URL_NonRegex) { TEST(RestServiceManager, URL_NonRegex) {
webcc::RestServiceManager service_manager; webcc::RestServiceManager service_manager;
{ service_manager.AddService(std::make_shared<TestRestService>(), "/instances",
webcc::RestServicePtr service = std::make_shared<TestRestService>(); false);
service_manager.AddService(service, "/instances", false);
}
std::vector<std::string> sub_matches; std::vector<std::string> sub_matches;
std::string url = "/instances"; std::string url = "/instances";

@ -5,6 +5,11 @@ if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS) add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif() endif()
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/config.h"
)
# Adhere to GNU filesystem layout conventions. # Adhere to GNU filesystem layout conventions.
include(GNUInstallDirs) include(GNUInstallDirs)
@ -102,10 +107,8 @@ set(TARGET webcc)
add_library(${TARGET} STATIC ${HEADERS} ${SOURCES}) add_library(${TARGET} STATIC ${HEADERS} ${SOURCES})
if(WEBCC_ENABLE_INSTALL) # Install lib and header files.
# Install lib and header files. # On Linux, if CMAKE_INSTALL_PREFIX is ~, the lib (libwebcc.a) will be installed
# On Linux, if CMAKE_INSTALL_PREFIX is ~, the lib (libwebcc.a) will be installed # to ~/lib and header files will be installed to ~/include.
# to ~/lib and header files will be installed to ~/include. install(TARGETS ${TARGET} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(TARGETS ${TARGET} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/webcc)
install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/webcc)
endif()

@ -0,0 +1,12 @@
#ifndef WEBCC_CONFIG_H_
#define WEBCC_CONFIG_H_
// Compile configurations.
#define WEBCC_ENABLE_LOG @WEBCC_ENABLE_LOG@
#if WEBCC_ENABLE_LOG
#define WEBCC_LOG_LEVEL @WEBCC_LOG_LEVEL@
#endif
#endif // WEBCC_CONFIG_H_

@ -3,6 +3,9 @@
// Simple console logger. // Simple console logger.
// This file was generated from "config.h.in" by CMake.
#include "webcc/config.h"
#if WEBCC_ENABLE_LOG #if WEBCC_ENABLE_LOG
#include <cstring> // for strrchr() #include <cstring> // for strrchr()
@ -16,8 +19,6 @@
#define WEBCC_FATA 4 #define WEBCC_FATA 4
// Default log level. // Default log level.
// You have to define a proper log level in CMakeLists.txt, e.g.,
// add_definitions(-DWEBCC_LOG_LEVEL=2)
#ifndef WEBCC_LOG_LEVEL #ifndef WEBCC_LOG_LEVEL
#define WEBCC_LOG_LEVEL WEBCC_WARN #define WEBCC_LOG_LEVEL WEBCC_WARN
#endif #endif

Loading…
Cancel
Save