From d7a7f9d9b05c3e3a9728bb1644a556527a721afd Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 28 Apr 2017 14:24:36 +0300 Subject: CMakeLists.txt updates Strip symbols, link the runtime statically, fix MinGW-w64 builds, etc. --- um/CMakeLists.txt | 72 ++++++++++++++++++++++ um/service/include/libservice/windows_error.hpp | 23 +++++++ um/service/test/CMakeLists.txt | 5 +- um/service/utils/CMakeLists.txt | 8 +-- um/wrappers/simple/utils/CMakeLists.txt | 2 +- .../special/nt_path_converter/utils/CMakeLists.txt | 6 +- .../nt_path_converter/utils/convert_nt_path.cpp | 2 +- 7 files changed, 108 insertions(+), 10 deletions(-) diff --git a/um/CMakeLists.txt b/um/CMakeLists.txt index f7d8b24..0329c45 100644 --- a/um/CMakeLists.txt +++ b/um/CMakeLists.txt @@ -1,4 +1,76 @@ project(windows7_drivers_utils) +get_directory_property(parent_directory PARENT_DIRECTORY) +set(is_root_project $) + +set(WIN7DRV_USE_STATIC_RUNTIME "${is_root_project}" CACHE BOOL "Link the runtime statically") +set(WIN7DRV_STRIP_SYMBOL_TABLE "${is_root_project}" CACHE BOOL "Strip symbol tables") + +if(is_root_project) + if(MSVC) + add_compile_options(/MP /W4) + elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) + add_compile_options(-Wall -Wextra -std=c++11) + endif() +endif() + +function(use_static_runtime_msvc target) + target_compile_options("${target}" PRIVATE + $<$:/MTd> + $<$>:/MT>) +endfunction() + +function(use_static_runtime_gcc target) + get_target_property(target_type "${target}" TYPE) + if(target_type STREQUAL EXECUTABLE) + target_link_libraries("${target}" PRIVATE -static) + endif() +endfunction() + +function(use_static_runtime target) + if(MSVC) + use_static_runtime_msvc("${target}") + elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) + use_static_runtime_gcc("${target}") + else() + message(WARNING "Unrecognized toolset") + endif() +endfunction() + +function(strip_symbol_table_gcc target) + target_link_libraries("${target}" PRIVATE + $<$,$>:-s>) +endfunction() + +function(strip_symbol_table target) + if(MSVC) + elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) + strip_symbol_table_gcc("${target}") + else() + message(WARNING "Unrecognized toolset") + endif() +endfunction() + +macro(add_executable target) + _add_executable(${ARGV}) + if(TARGET "${target}") + if(WIN7DRV_USE_STATIC_RUNTIME) + use_static_runtime("${target}") + endif() + if(WIN7DRV_STRIP_SYMBOL_TABLE) + strip_symbol_table("${target}") + endif() + endif() +endmacro() + +macro(add_library target) + _add_library(${ARGV}) + if(TARGET "${target}") + if(WIN7DRV_USE_STATIC_RUNTIME) + use_static_runtime("${target}") + endif() + endif() +endmacro() + add_subdirectory(service) add_subdirectory(wrappers) diff --git a/um/service/include/libservice/windows_error.hpp b/um/service/include/libservice/windows_error.hpp index f7ac90e..13d7f47 100644 --- a/um/service/include/libservice/windows_error.hpp +++ b/um/service/include/libservice/windows_error.hpp @@ -8,6 +8,7 @@ #include "common.hpp" #include "singleton.hpp" +#include #include #include @@ -25,7 +26,29 @@ namespace libservice private: friend class Singleton; }; + + namespace error + { + inline std::string build_what( + const char* function, + const char* file, + int line) + { + std::ostringstream oss; + oss << "Error in function '" << function + << "' at file '" << file + << "', line " << line; + return oss.str(); + } + } } +#if defined(_MSC_VER) #define LIBSERVICE_ERROR_PREFIX \ "Error in function '" LIBSERVICE_FUNCTION_NAME "' at file '" LIBSERVICE_FILE_PATH "', line " LIBSERVICE_LINE_NUMBER_STRING +#elif defined(__GNUC__) +#define LIBSERVICE_ERROR_PREFIX \ + libservice::error::build_what(LIBSERVICE_FUNCTION_NAME, LIBSERVICE_FILE_PATH, LIBSERVICE_LINE_NUMBER) +#else +#define LIBSERVICE_ERROR_PREFIX "Error" +#endif diff --git a/um/service/test/CMakeLists.txt b/um/service/test/CMakeLists.txt index 4780deb..6825337 100644 --- a/um/service/test/CMakeLists.txt +++ b/um/service/test/CMakeLists.txt @@ -1,4 +1,3 @@ add_executable(libservice_test_windows_error windows_error.cpp) -target_link_libraries(libservice_test_windows_error libservice) -set_target_properties(libservice_test_windows_error PROPERTIES - OUTPUT_NAME windows_error) +target_link_libraries(libservice_test_windows_error PRIVATE libservice) +set_target_properties(libservice_test_windows_error PROPERTIES OUTPUT_NAME windows_error) diff --git a/um/service/utils/CMakeLists.txt b/um/service/utils/CMakeLists.txt index 061ab25..fc5cc1b 100644 --- a/um/service/utils/CMakeLists.txt +++ b/um/service/utils/CMakeLists.txt @@ -1,11 +1,11 @@ add_executable(install_service install_service.cpp) -target_link_libraries(install_service libservice) +target_link_libraries(install_service PRIVATE libservice) add_executable(start_service start_service.cpp) -target_link_libraries(start_service libservice) +target_link_libraries(start_service PRIVATE libservice) add_executable(stop_service stop_service.cpp) -target_link_libraries(stop_service libservice) +target_link_libraries(stop_service PRIVATE libservice) add_executable(uninstall_service uninstall_service.cpp) -target_link_libraries(uninstall_service libservice) +target_link_libraries(uninstall_service PRIVATE libservice) diff --git a/um/wrappers/simple/utils/CMakeLists.txt b/um/wrappers/simple/utils/CMakeLists.txt index 8824e3e..2a653fe 100644 --- a/um/wrappers/simple/utils/CMakeLists.txt +++ b/um/wrappers/simple/utils/CMakeLists.txt @@ -1,2 +1,2 @@ add_executable(exchange_ints exchange_ints.cpp) -target_link_libraries(exchange_ints libsimple) +target_link_libraries(exchange_ints PRIVATE libsimple) diff --git a/um/wrappers/special/nt_path_converter/utils/CMakeLists.txt b/um/wrappers/special/nt_path_converter/utils/CMakeLists.txt index 9915995..9c5254b 100644 --- a/um/wrappers/special/nt_path_converter/utils/CMakeLists.txt +++ b/um/wrappers/special/nt_path_converter/utils/CMakeLists.txt @@ -1,2 +1,6 @@ add_executable(convert_nt_path convert_nt_path.cpp) -target_link_libraries(convert_nt_path libnt_path_converter) +target_link_libraries(convert_nt_path PRIVATE libnt_path_converter) +if(MINGW) + target_compile_options(convert_nt_path PRIVATE -municode) + target_link_libraries(convert_nt_path PRIVATE -municode) +endif() diff --git a/um/wrappers/special/nt_path_converter/utils/convert_nt_path.cpp b/um/wrappers/special/nt_path_converter/utils/convert_nt_path.cpp index 0055db2..9d25242 100644 --- a/um/wrappers/special/nt_path_converter/utils/convert_nt_path.cpp +++ b/um/wrappers/special/nt_path_converter/utils/convert_nt_path.cpp @@ -18,7 +18,7 @@ int wmain(int argc, wchar_t* argv[]) } catch (const std::exception& e) { - std::wcerr << e.what() << "\n"; + std::cerr << e.what() << "\n"; return 1; } -- cgit v1.2.3