From 44768e92ad5757ce36b852d21336a70d3c0118a9 Mon Sep 17 00:00:00 2001 From: Mikhail Titov Date: Wed, 10 Jun 2015 01:07:39 -0500 Subject: [PATCH 1/3] CMake: fix static builds, add LTO for MinSizeRel --- build/CMakeLists.txt | 64 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index b31607f0..19509a74 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -82,15 +82,18 @@ source_group ("Header Files" FILES ${HEADERS}) source_group ("Source Files" FILES ${COMMON_SRC} ${DAEMON_SRC} ${LIBRARY_SRC}) # Default build is Debug -if (CMAKE_BUILD_TYPE STREQUAL "Release") - add_definitions( "-pedantic" ) -else () +if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Debug) endif () # compiler flags customization (by vendor) if (NOT MSVC) -add_definitions ( "-Wall -Wextra -fPIC" ) + set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra" ) + set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -pedantic" ) + # TODO: The following is incompatible with static build and enabled hardening for OpenWRT. + # Multiple definitions of __stack_chk_fail (libssp & libc) + set( CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -flto -s -ffunction-sections -fdata-sections" ) + set( CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "-Wl,--gc-sections" ) # -flto is added from above endif () # check for c++11 support @@ -136,10 +139,44 @@ if (WITH_AESNI) endif() # libraries +# TODO: once CMake 3.1+ becomes mainstream, see e.g. http://stackoverflow.com/a/29871891/673826 +# use imported Threads::Threads instead +set(THREADS_PREFER_PTHREAD_FLAG ON) find_package ( Threads REQUIRED ) +if(THREADS_HAVE_PTHREAD_ARG) # compile time flag + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") +endif() if (WITH_STATIC) set(Boost_USE_STATIC_LIBS ON) + set(Boost_USE_STATIC_RUNTIME ON) + if (WIN32) + # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace + # Note that you might need to rebuild Crypto++ + foreach(flag_var + CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE + CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) + if(${flag_var} MATCHES "/MD") + string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") + endif(${flag_var} MATCHES "/MD") + endforeach(flag_var) + else () + set(CMAKE_FIND_LIBRARY_SUFFIXES .a) + endif () + set(BUILD_SHARED_LIBS OFF) + if (${CMAKE_CXX_COMPILER} MATCHES ".*-openwrt-.*") + set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" ) + # set( CMAKE_THREAD_LIBS_INIT "gcc_eh -Wl,--whole-archive -lpthread -Wl,--no-whole-archive" ) + set( CMAKE_THREAD_LIBS_INIT "gcc_eh -Wl,-u,pthread_create,-u,pthread_once,-u,pthread_mutex_lock,-u,pthread_mutex_unlock,-u,pthread_join,-u,pthread_equal,-u,pthread_detach,-u,pthread_cond_wait,-u,pthread_cond_signal,-u,pthread_cond_destroy,-u,pthread_cond_broadcast,-u,pthread_cancel" ) + endif () +else() + if (NOT WIN32) + # TODO: Consider separate compilation for COMMON_SRC for library. + # No need in -fPIC overhead for binary if not interested in library + # HINT: revert c266cff CMakeLists.txt: compilation speed up + set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" ) + endif () + add_definitions(-DBOOST_ALL_DYN_LINK) endif () find_package ( Boost COMPONENTS system filesystem regex program_options date_time thread chrono REQUIRED ) @@ -183,19 +220,20 @@ if (WITH_BINARY) add_executable ( "${PROJECT_NAME}-bin" ${DAEMON_SRC} ) if(NOT MSVC) # FIXME: incremental linker file name (.ilk) collision for dll & exe set_target_properties("${PROJECT_NAME}-bin" PROPERTIES OUTPUT_NAME "${PROJECT_NAME}") + if (WITH_STATIC) + set_target_properties("${PROJECT_NAME}-bin" PROPERTIES LINK_FLAGS "-static" ) + endif () endif() if (WITH_HARDENING AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU") set_target_properties("${PROJECT_NAME}-bin" PROPERTIES LINK_FLAGS "-z relro -z now" ) endif () - if (WITH_STATIC) - set(BUILD_SHARED_LIBS OFF) - set_target_properties("${PROJECT_NAME}-bin" PROPERTIES LINK_FLAGS "-static" ) - else() - add_definitions(-DBOOST_ALL_DYN_LINK) - endif () - + # FindBoost pulls pthread for thread which is broken for static linking at least on Ubuntu 15.04 + list(GET Boost_LIBRARIES -1 LAST_Boost_LIBRARIES) + if(${LAST_Boost_LIBRARIES} MATCHES ".*pthread.*") + list(REMOVE_AT Boost_LIBRARIES -1) + endif() target_link_libraries( "${PROJECT_NAME}-bin" common ${Boost_LIBRARIES} ${CRYPTO++_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) install(TARGETS "${PROJECT_NAME}-bin" RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) @@ -207,9 +245,9 @@ endif () if (WITH_LIBRARY) if (MSVC) # FIXME: DLL would not have any symbols unless we use __declspec(dllexport) through out the code - add_library(${PROJECT_NAME} ${LIBRARY_SRC}) + add_library(${PROJECT_NAME} STATIC ${LIBRARY_SRC}) else () - add_library(${PROJECT_NAME} SHARED ${LIBRARY_SRC}) + add_library(${PROJECT_NAME} ${LIBRARY_SRC}) target_link_libraries( ${PROJECT_NAME} common ${Boost_LIBRARIES} ${CRYPTO++_LIBRARIES}) endif () install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) From ba2b792916013b0833c064f6b0fccf769f539018 Mon Sep 17 00:00:00 2001 From: Mikhail Titov Date: Wed, 10 Jun 2015 01:12:43 -0500 Subject: [PATCH 2/3] Cleanup cryptopp headers path search --- build/CMakeLists.txt | 2 +- build/cmake_modules/FindCryptoPP.cmake | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index 19509a74..4e125d58 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -195,7 +195,7 @@ if (NOT ${MINIUPNPC_FOUND}) endif() # load includes -include_directories( ${Boost_INCLUDE_DIRS} ${CRYPTO++_INCLUDE_DIR} "${CMAKE_SOURCE_DIR}/..") +include_directories( ${Boost_INCLUDE_DIRS} ${CRYPTO++_INCLUDE_DIR} ) # show summary message(STATUS "---------------------------------------") diff --git a/build/cmake_modules/FindCryptoPP.cmake b/build/cmake_modules/FindCryptoPP.cmake index 09b72184..396be144 100644 --- a/build/cmake_modules/FindCryptoPP.cmake +++ b/build/cmake_modules/FindCryptoPP.cmake @@ -4,17 +4,14 @@ if(CRYPTO++_INCLUDE_DIR AND CRYPTO++_LIBRARIES) set(CRYPTO++_FOUND TRUE) else(CRYPTO++_INCLUDE_DIR AND CRYPTO++_LIBRARIES) - find_path(CRYPTO++_INCLUDE_DIR cryptlib.h - /usr/include/crypto++ - /usr/include/cryptopp - /usr/local/include/crypto++ - /usr/local/include/cryptopp - /opt/local/include/crypto++ - /opt/local/include/cryptopp + find_path(CRYPTO++_INCLUDE_DIR cryptopp/cryptlib.h + /usr/include + /usr/local/include $ENV{SystemDrive}/Crypto++/include $ENV{CRYPTOPP} + $ENV{CRYPTOPP}/.. $ENV{CRYPTOPP}/include - ${PROJECT_SOURCE_DIR}/../../cryptopp + ${PROJECT_SOURCE_DIR}/../.. ) find_library(CRYPTO++_LIBRARIES NAMES cryptopp From 0354685e35c921ee457741b195b57ca57a788771 Mon Sep 17 00:00:00 2001 From: Mikhail Titov Date: Wed, 10 Jun 2015 01:04:33 -0500 Subject: [PATCH 3/3] Precompiled headers Sample times: MSVC 2013, debug x64: 5min 15sec -> 2min 15sec Ubuntu 15.04, with hardening, static, release: 5min 21sec -> 3min 24sec --- build/CMakeLists.txt | 46 ++++++++++++++++++++++++++++-- stdafx.cpp | 1 + stdafx.h | 67 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 stdafx.cpp create mode 100644 stdafx.h diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index 4e125d58..254574fe 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required ( VERSION 2.8.5 ) +cmake_minimum_required ( VERSION 2.8.12 ) project ( "i2pd" ) # configurale options @@ -8,6 +8,7 @@ option(WITH_LIBRARY "Build library" ON) option(WITH_BINARY "Build binary" ON) option(WITH_STATIC "Static build" OFF) option(WITH_UPNP "Include support for UPnP client" OFF) +option(WITH_PCH "Use precompiled header" OFF) # paths set ( CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules" ) @@ -88,7 +89,7 @@ endif () # compiler flags customization (by vendor) if (NOT MSVC) - set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra" ) + set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Winvalid-pch" ) set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -pedantic" ) # TODO: The following is incompatible with static build and enabled hardening for OpenWRT. # Multiple definitions of __stack_chk_fail (libssp & libc) @@ -179,6 +180,30 @@ else() add_definitions(-DBOOST_ALL_DYN_LINK) endif () +if (WITH_PCH) + include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..) + add_library(stdafx STATIC "${CMAKE_SOURCE_DIR}/stdafx.cpp") + if(MSVC) + target_compile_options(stdafx PRIVATE /Ycstdafx.h /Zm135) + add_custom_command(TARGET stdafx POST_BUILD + COMMAND xcopy /y stdafx.dir\\$\\*.pdb common.dir\\$\\ + COMMAND xcopy /y stdafx.dir\\$\\*.pdb i2pd-bin.dir\\$\\ + COMMAND xcopy /y stdafx.dir\\$\\*.pdb i2pd.dir\\$\\ + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + ) + target_compile_options(common PRIVATE /FIstdafx.h /Yustdafx.h /Zm135 "/Fp${CMAKE_BINARY_DIR}/stdafx.dir/$/stdafx.pch") + else() + string(TOUPPER ${CMAKE_BUILD_TYPE} BTU) + get_directory_property(DEFS DEFINITIONS) + string(REPLACE " " ";" FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${BTU}} ${DEFS}") + add_custom_command(TARGET stdafx PRE_BUILD + COMMAND ${CMAKE_CXX_COMPILER} ${FLAGS} -c ${CMAKE_CURRENT_SOURCE_DIR}/../stdafx.h + ) + target_compile_options(common PRIVATE -include stdafx.h) + endif() + target_link_libraries(common stdafx) +endif() + find_package ( Boost COMPONENTS system filesystem regex program_options date_time thread chrono REQUIRED ) if(NOT DEFINED Boost_INCLUDE_DIRS) message(SEND_ERROR "Boost is not found, or your boost version was bellow 1.46. Please download Boost!") @@ -211,6 +236,7 @@ message(STATUS " LIBRARY : ${WITH_LIBRARY}") message(STATUS " BINARY : ${WITH_BINARY}") message(STATUS " STATIC BUILD : ${WITH_STATIC}") message(STATUS " UPnP : ${WITH_UPNP}") +message(STATUS " PCH : ${WITH_PCH}") message(STATUS "---------------------------------------") #Handle paths nicely @@ -225,6 +251,14 @@ if (WITH_BINARY) endif () endif() + if (WITH_PCH) + if (MSVC) + target_compile_options("${PROJECT_NAME}-bin" PRIVATE /FIstdafx.h /Yustdafx.h /Zm135 "/Fp${CMAKE_BINARY_DIR}/stdafx.dir/$/stdafx.pch") + else() + target_compile_options("${PROJECT_NAME}-bin" PRIVATE -include stdafx.h) + endif() + endif() + if (WITH_HARDENING AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU") set_target_properties("${PROJECT_NAME}-bin" PROPERTIES LINK_FLAGS "-z relro -z now" ) endif () @@ -250,5 +284,13 @@ if (WITH_LIBRARY) add_library(${PROJECT_NAME} ${LIBRARY_SRC}) target_link_libraries( ${PROJECT_NAME} common ${Boost_LIBRARIES} ${CRYPTO++_LIBRARIES}) endif () + if (WITH_PCH) + if (MSVC) + add_dependencies(${PROJECT_NAME} stdafx) + target_compile_options(${PROJECT_NAME} PRIVATE /FIstdafx.h /Yustdafx.h /Zm135 "/Fp${CMAKE_BINARY_DIR}/stdafx.dir/$/stdafx.pch") + else() + target_compile_options(${PROJECT_NAME} PRIVATE -include stdafx.h) + endif() + endif() install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) endif () diff --git a/stdafx.cpp b/stdafx.cpp new file mode 100644 index 00000000..fd4f341c --- /dev/null +++ b/stdafx.cpp @@ -0,0 +1 @@ +#include "stdafx.h" diff --git a/stdafx.h b/stdafx.h new file mode 100644 index 00000000..edfac630 --- /dev/null +++ b/stdafx.h @@ -0,0 +1,67 @@ +#ifndef STDAFX_H__ +#define STDAFX_H__ + +#include +#include +#include +#include +#include + +#include // TODO: replace with cstring and std:: through out +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif