Skip to content

Commit

Permalink
[cmake] Add precompiled headers support
Browse files Browse the repository at this point in the history
  • Loading branch information
greshilov authored and mpimenov committed Jun 15, 2018
1 parent 0c5b3b7 commit 10ca98f
Show file tree
Hide file tree
Showing 44 changed files with 245 additions and 46 deletions.
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ option(USE_TSAN "Enable Thread Sanitizer" OFF)
option(PYBINDINGS "Create makefiles for building python bindings" OFF)
option(SKIP_DESKTOP "Skip building of desktop application" OFF)
option(BUILD_MAPSHOT "Build mapshot tool" OFF)
option(USE_PCH "Use precompiled headers" OFF)

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(PCH_EXTENSION "pch")
endif()

if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(PCH_EXTENSION "gch")
endif()

if (PLATFORM_LINUX)
option(USE_PPROF "Enable Google Profiler" OFF)
Expand Down Expand Up @@ -213,6 +222,15 @@ if (USE_TSAN)
)
endif()

if (USE_PCH)
message("Precompiled headers are ON")
set(OMIM_PCH_TARGET_NAME "omim_pch")
add_precompiled_headers(
${OMIM_ROOT}/precompiled_headers.hpp
${OMIM_PCH_TARGET_NAME}
)
endif()

# Include subdirectories
add_subdirectory(3party/agg)
add_subdirectory(3party/bsdiff-courgette)
Expand Down
2 changes: 1 addition & 1 deletion android/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ set(
com/mapswithme/util/statistics/PushwooshHelper.cpp
)

add_library(mapswithme SHARED ${SRC})
omim_add_library(mapswithme SHARED ${SRC})

target_link_libraries(
mapswithme
Expand Down
2 changes: 1 addition & 1 deletion base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ set(
worker_thread.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})

omim_add_test_subdirectory(base_tests)
168 changes: 163 additions & 5 deletions cmake/OmimHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,50 @@ endfunction()
macro(find_qt5_desktop_package package)
find_package(${package})
if (NOT ${package}_FOUND)
message(FATAL_ERROR "Can't find ${package}, consider to set SKIP_DESKTOP if you don't need desktop app")
message(FATAL_ERROR "Can't find ${package}, consider to set SKIP_DESKTOP"
" if you don't need desktop app")
endif()
endmacro()

# Functions for using in subdirectories
function(omim_add_executable executable)
add_executable(${executable} ${ARGN})
if (USE_ASAN)
target_link_libraries(${executable} "-fsanitize=address" "-fno-omit-frame-pointer")
target_link_libraries(
${executable}
"-fsanitize=address"
"-fno-omit-frame-pointer"
)
endif()
if (USE_TSAN)
target_link_libraries(${executable} "-fsanitize=thread" "-fno-omit-frame-pointer")
target_link_libraries(
${executable}
"-fsanitize=thread"
"-fno-omit-frame-pointer"
)
endif()
if (USE_PPROF)
target_link_libraries(${executable} "-lprofiler")
endif()
if (USE_PCH)
add_precompiled_headers_to_target(${executable} ${OMIM_PCH_TARGET_NAME})
endif()
endfunction()

function(omim_add_library library)
add_library(${library} ${ARGN})
if (USE_PCH)
add_precompiled_headers_to_target(${library} ${OMIM_PCH_TARGET_NAME})
endif()
endfunction()

function(omim_add_test executable)
if (NOT SKIP_TESTS)
omim_add_executable(${executable} ${ARGN} ${OMIM_ROOT}/testing/testingmain.cpp)
omim_add_executable(
${executable}
${ARGN}
${OMIM_ROOT}/testing/testingmain.cpp
)
endif()
endfunction()

Expand Down Expand Up @@ -80,7 +103,8 @@ function(omim_link_libraries target)
target_link_libraries(${target} ${ARGN} ${CMAKE_THREAD_LIBS_INIT})
omim_link_platform_deps(${target} ${ARGN})
else()
message("~> Skipping linking the libraries to the target ${target} as it does not exist")
message("~> Skipping linking the libraries to the target ${target} as it"
" does not exist")
endif()
endfunction()

Expand Down Expand Up @@ -137,3 +161,137 @@ function(add_clang_compile_options)
add_compile_options(${ARGV})
endif()
endfunction()

function(export_directory_flags filename)
get_directory_property(include_directories INCLUDE_DIRECTORIES)
get_directory_property(definitions COMPILE_DEFINITIONS)
get_directory_property(flags COMPILE_FLAGS)
get_directory_property(options COMPILE_OPTIONS)

if (PLATFORM_ANDROID)
set(
include_directories
${include_directories}
${CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES}
)
set(
platform_flags
"${ANDROID_COMPILER_FLAGS} ${ANDROID_COMPILER_FLAGS_CXX}"
)
set(
flags
"--target=${CMAKE_C_COMPILER_TARGET}"
"--sysroot=${CMAKE_SYSROOT}" ${flags}
)
endif()

# Append Release/Debug flags:
string(TOUPPER "${CMAKE_BUILD_TYPE}" upper_build_type)
set(flags ${flags} ${CMAKE_CXX_FLAGS_${upper_build_type}})

set(
include_directories
"$<$<BOOL:${include_directories}>\:-I$<JOIN:${include_directories},\n-I>\n>"
)
set(definitions "$<$<BOOL:${definitions}>:-D$<JOIN:${definitions},\n-D>\n>")
set(flags "$<$<BOOL:${flags}>:$<JOIN:${flags},\n>\n>")
set(options "$<$<BOOL:${options}>:$<JOIN:${options},\n>\n>")
file(
GENERATE OUTPUT
${filename}
CONTENT
"${definitions}${include_directories}${platform_flags}\n${flags}${options}\n"
)
endfunction()

function(add_pic_pch_target header pch_target_name
pch_file_name suffix pic_flag)
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/pch_${suffix}")
file(COPY "${header}" DESTINATION "${CMAKE_BINARY_DIR}/pch_${suffix}")
set(_header "${CMAKE_BINARY_DIR}/pch_${suffix}/${pch_file_name}")
set(
_compiled_header
"${CMAKE_BINARY_DIR}/pch_${suffix}/${pch_file_name}.${PCH_EXTENSION}"
)
add_custom_target(
"${pch_target_name}_${suffix}"
COMMAND
"${CMAKE_CXX_COMPILER}" ${compiler_flags} ${c_standard_flags} ${pic_flag}
-x c++-header
-c "${_header}" -o "${_compiled_header}"
COMMENT "Building precompiled omim CXX ${suffix} header"
)
endfunction()

function(add_precompiled_headers header pch_target_name)
set(pch_flags_file "${CMAKE_BINARY_DIR}/${pch_target_name}_flags_file")
export_directory_flags("${pch_flags_file}")
set(compiler_flags "@${pch_flags_file}")

# CMAKE_CXX_STANDARD 14 flags:
set(c_standard_flags "-std=c++14" "-std=gnu++14")
get_filename_component(pch_file_name ${header} NAME)

add_pic_pch_target(${header} ${pch_target_name} ${pch_file_name} lib "-fPIC")
add_pic_pch_target(${header} ${pch_target_name} ${pch_file_name} exe "-fPIE")

add_custom_target(
"${pch_target_name}"
COMMENT "Waiting for both lib and exe precompiled headers to build"
DEPENDS "${pch_target_name}_lib" "${pch_target_name}_exe"
)
set_target_properties(
${pch_target_name}
PROPERTIES
PCH_NAME
"${pch_file_name}"
)
endfunction()

function(add_precompiled_headers_to_target target pch_target)
add_dependencies(${target} "${pch_target}")
get_property(sources TARGET ${target} PROPERTY SOURCES)
get_target_property(target_type ${target} TYPE)
get_target_property(pch_file_name ${pch_target} PCH_NAME)

if (target_type STREQUAL "EXECUTABLE")
set(include_compiled_header_dir "${CMAKE_BINARY_DIR}/pch_exe")
# CMake automatically adds additional compile options after linking.
# For example '-fPIC' flag on skin_generator_tool, because it is linked to Qt libs.
# We force correct flag for executables.
set(additional_clang_flags "-fPIE")
endif()

if (target_type MATCHES "LIBRARY")
set(include_compiled_header_dir "${CMAKE_BINARY_DIR}/pch_lib")
endif()

# Force gcc first search gch header in pch_exe/pch_lib:
target_include_directories(
${target}
BEFORE
PUBLIC
${include_compiled_header_dir}
)

foreach(source ${sources})
if(source MATCHES \\.\(cc|cpp|h|hpp\)$)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set_source_files_properties(
${source}
PROPERTIES
COMPILE_FLAGS
"${additional_clang_flags} -include-pch \
${include_compiled_header_dir}/${pch_file_name}.${PCH_EXTENSION}"
)
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set_source_files_properties(
${source}
PROPERTIES
COMPILE_FLAGS "-include ${pch_file_name}"
)
endif()
endif()
endforeach()
endfunction()
2 changes: 1 addition & 1 deletion coding/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ set(
zlib.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})

omim_add_test_subdirectory(coding_tests)
2 changes: 1 addition & 1 deletion drape/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ if (PLATFORM_IPHONE)
)
endif()

add_library(${PROJECT_NAME} ${DRAPE_COMMON_SRC} ${SRC})
omim_add_library(${PROJECT_NAME} ${DRAPE_COMMON_SRC} ${SRC})

omim_add_test_subdirectory(drape_tests)
2 changes: 1 addition & 1 deletion drape_frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ set(
visual_params.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})

set(
DRAPE_SHADERS_SRC
Expand Down
2 changes: 1 addition & 1 deletion editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ set(
yes_no_unknown.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})

omim_add_test_subdirectory(editor_tests)
omim_add_test_subdirectory(editor_tests_support)
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_tests_support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ set(
helpers.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})
2 changes: 1 addition & 1 deletion generator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ set(SRC
world_map_generator.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})

omim_add_test_subdirectory(generator_tests_support)
omim_add_test_subdirectory(generator_tests)
Expand Down
2 changes: 1 addition & 1 deletion generator/generator_tests_support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ set(
test_mwm_builder.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})
2 changes: 1 addition & 1 deletion generator/mwm_diff/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(
diff.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})

omim_add_pybindings_subdirectory(pymwm_diff)
omim_add_test_subdirectory(mwm_diff_tests)
2 changes: 1 addition & 1 deletion generator/mwm_diff/pymwm_diff/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(
bindings.cpp
)

add_library(${PROJECT_NAME} MODULE ${SRC})
omim_add_library(${PROJECT_NAME} MODULE ${SRC})

omim_link_libraries(
${PROJECT_NAME}
Expand Down
2 changes: 1 addition & 1 deletion geometry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ set(
region2d/boost_concept.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})

omim_add_test_subdirectory(geometry_tests)
2 changes: 1 addition & 1 deletion indexer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ set(

file(COPY ${OTHER_FILES} DESTINATION ${CMAKE_BINARY_DIR})

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})

omim_add_test_subdirectory(indexer_tests_support)
omim_add_test_subdirectory(indexer_tests)
2 changes: 1 addition & 1 deletion indexer/indexer_tests_support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ set(
test_with_custom_mwms.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})
2 changes: 1 addition & 1 deletion kml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set(
visitors.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})

omim_add_pybindings_subdirectory(pykmlib)
omim_add_test_subdirectory(kml_tests)
2 changes: 1 addition & 1 deletion kml/pykmlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(
bindings.cpp
)

add_library(${PROJECT_NAME} MODULE ${SRC})
omim_add_library(${PROJECT_NAME} MODULE ${SRC})

omim_link_libraries(
${PROJECT_NAME}
Expand Down
2 changes: 1 addition & 1 deletion local_ads/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ set(
statistics.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})

omim_add_pybindings_subdirectory(pylocal_ads)
omim_add_test_subdirectory(local_ads_tests)
2 changes: 1 addition & 1 deletion local_ads/pylocal_ads/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(
bindings.cpp
)

add_library(${PROJECT_NAME} MODULE ${SRC})
omim_add_library(${PROJECT_NAME} MODULE ${SRC})

omim_link_libraries(
${PROJECT_NAME}
Expand Down
2 changes: 1 addition & 1 deletion map/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ set(
viewport_search_callback.hpp
)

add_library(${PROJECT_NAME} ${SRC})
omim_add_library(${PROJECT_NAME} ${SRC})

omim_add_test_subdirectory(map_integration_tests)
omim_add_test_subdirectory(map_tests)
Expand Down
Loading

0 comments on commit 10ca98f

Please sign in to comment.