Skip to content

Commit 10ca98f

Browse files
greshilovmpimenov
authored andcommitted
[cmake] Add precompiled headers support
1 parent 0c5b3b7 commit 10ca98f

File tree

44 files changed

+245
-46
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+245
-46
lines changed

CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ option(USE_TSAN "Enable Thread Sanitizer" OFF)
6464
option(PYBINDINGS "Create makefiles for building python bindings" OFF)
6565
option(SKIP_DESKTOP "Skip building of desktop application" OFF)
6666
option(BUILD_MAPSHOT "Build mapshot tool" OFF)
67+
option(USE_PCH "Use precompiled headers" OFF)
68+
69+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
70+
set(PCH_EXTENSION "pch")
71+
endif()
72+
73+
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
74+
set(PCH_EXTENSION "gch")
75+
endif()
6776

6877
if (PLATFORM_LINUX)
6978
option(USE_PPROF "Enable Google Profiler" OFF)
@@ -213,6 +222,15 @@ if (USE_TSAN)
213222
)
214223
endif()
215224

225+
if (USE_PCH)
226+
message("Precompiled headers are ON")
227+
set(OMIM_PCH_TARGET_NAME "omim_pch")
228+
add_precompiled_headers(
229+
${OMIM_ROOT}/precompiled_headers.hpp
230+
${OMIM_PCH_TARGET_NAME}
231+
)
232+
endif()
233+
216234
# Include subdirectories
217235
add_subdirectory(3party/agg)
218236
add_subdirectory(3party/bsdiff-courgette)

android/jni/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ set(
8888
com/mapswithme/util/statistics/PushwooshHelper.cpp
8989
)
9090

91-
add_library(mapswithme SHARED ${SRC})
91+
omim_add_library(mapswithme SHARED ${SRC})
9292

9393
target_link_libraries(
9494
mapswithme

base/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ set(
9898
worker_thread.hpp
9999
)
100100

101-
add_library(${PROJECT_NAME} ${SRC})
101+
omim_add_library(${PROJECT_NAME} ${SRC})
102102

103103
omim_add_test_subdirectory(base_tests)

cmake/OmimHelpers.cmake

Lines changed: 163 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,50 @@ endfunction()
2121
macro(find_qt5_desktop_package package)
2222
find_package(${package})
2323
if (NOT ${package}_FOUND)
24-
message(FATAL_ERROR "Can't find ${package}, consider to set SKIP_DESKTOP if you don't need desktop app")
24+
message(FATAL_ERROR "Can't find ${package}, consider to set SKIP_DESKTOP"
25+
" if you don't need desktop app")
2526
endif()
2627
endmacro()
2728

2829
# Functions for using in subdirectories
2930
function(omim_add_executable executable)
3031
add_executable(${executable} ${ARGN})
3132
if (USE_ASAN)
32-
target_link_libraries(${executable} "-fsanitize=address" "-fno-omit-frame-pointer")
33+
target_link_libraries(
34+
${executable}
35+
"-fsanitize=address"
36+
"-fno-omit-frame-pointer"
37+
)
3338
endif()
3439
if (USE_TSAN)
35-
target_link_libraries(${executable} "-fsanitize=thread" "-fno-omit-frame-pointer")
40+
target_link_libraries(
41+
${executable}
42+
"-fsanitize=thread"
43+
"-fno-omit-frame-pointer"
44+
)
3645
endif()
3746
if (USE_PPROF)
3847
target_link_libraries(${executable} "-lprofiler")
3948
endif()
49+
if (USE_PCH)
50+
add_precompiled_headers_to_target(${executable} ${OMIM_PCH_TARGET_NAME})
51+
endif()
52+
endfunction()
53+
54+
function(omim_add_library library)
55+
add_library(${library} ${ARGN})
56+
if (USE_PCH)
57+
add_precompiled_headers_to_target(${library} ${OMIM_PCH_TARGET_NAME})
58+
endif()
4059
endfunction()
4160

4261
function(omim_add_test executable)
4362
if (NOT SKIP_TESTS)
44-
omim_add_executable(${executable} ${ARGN} ${OMIM_ROOT}/testing/testingmain.cpp)
63+
omim_add_executable(
64+
${executable}
65+
${ARGN}
66+
${OMIM_ROOT}/testing/testingmain.cpp
67+
)
4568
endif()
4669
endfunction()
4770

@@ -80,7 +103,8 @@ function(omim_link_libraries target)
80103
target_link_libraries(${target} ${ARGN} ${CMAKE_THREAD_LIBS_INIT})
81104
omim_link_platform_deps(${target} ${ARGN})
82105
else()
83-
message("~> Skipping linking the libraries to the target ${target} as it does not exist")
106+
message("~> Skipping linking the libraries to the target ${target} as it"
107+
" does not exist")
84108
endif()
85109
endfunction()
86110

@@ -137,3 +161,137 @@ function(add_clang_compile_options)
137161
add_compile_options(${ARGV})
138162
endif()
139163
endfunction()
164+
165+
function(export_directory_flags filename)
166+
get_directory_property(include_directories INCLUDE_DIRECTORIES)
167+
get_directory_property(definitions COMPILE_DEFINITIONS)
168+
get_directory_property(flags COMPILE_FLAGS)
169+
get_directory_property(options COMPILE_OPTIONS)
170+
171+
if (PLATFORM_ANDROID)
172+
set(
173+
include_directories
174+
${include_directories}
175+
${CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES}
176+
)
177+
set(
178+
platform_flags
179+
"${ANDROID_COMPILER_FLAGS} ${ANDROID_COMPILER_FLAGS_CXX}"
180+
)
181+
set(
182+
flags
183+
"--target=${CMAKE_C_COMPILER_TARGET}"
184+
"--sysroot=${CMAKE_SYSROOT}" ${flags}
185+
)
186+
endif()
187+
188+
# Append Release/Debug flags:
189+
string(TOUPPER "${CMAKE_BUILD_TYPE}" upper_build_type)
190+
set(flags ${flags} ${CMAKE_CXX_FLAGS_${upper_build_type}})
191+
192+
set(
193+
include_directories
194+
"$<$<BOOL:${include_directories}>\:-I$<JOIN:${include_directories},\n-I>\n>"
195+
)
196+
set(definitions "$<$<BOOL:${definitions}>:-D$<JOIN:${definitions},\n-D>\n>")
197+
set(flags "$<$<BOOL:${flags}>:$<JOIN:${flags},\n>\n>")
198+
set(options "$<$<BOOL:${options}>:$<JOIN:${options},\n>\n>")
199+
file(
200+
GENERATE OUTPUT
201+
${filename}
202+
CONTENT
203+
"${definitions}${include_directories}${platform_flags}\n${flags}${options}\n"
204+
)
205+
endfunction()
206+
207+
function(add_pic_pch_target header pch_target_name
208+
pch_file_name suffix pic_flag)
209+
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/pch_${suffix}")
210+
file(COPY "${header}" DESTINATION "${CMAKE_BINARY_DIR}/pch_${suffix}")
211+
set(_header "${CMAKE_BINARY_DIR}/pch_${suffix}/${pch_file_name}")
212+
set(
213+
_compiled_header
214+
"${CMAKE_BINARY_DIR}/pch_${suffix}/${pch_file_name}.${PCH_EXTENSION}"
215+
)
216+
add_custom_target(
217+
"${pch_target_name}_${suffix}"
218+
COMMAND
219+
"${CMAKE_CXX_COMPILER}" ${compiler_flags} ${c_standard_flags} ${pic_flag}
220+
-x c++-header
221+
-c "${_header}" -o "${_compiled_header}"
222+
COMMENT "Building precompiled omim CXX ${suffix} header"
223+
)
224+
endfunction()
225+
226+
function(add_precompiled_headers header pch_target_name)
227+
set(pch_flags_file "${CMAKE_BINARY_DIR}/${pch_target_name}_flags_file")
228+
export_directory_flags("${pch_flags_file}")
229+
set(compiler_flags "@${pch_flags_file}")
230+
231+
# CMAKE_CXX_STANDARD 14 flags:
232+
set(c_standard_flags "-std=c++14" "-std=gnu++14")
233+
get_filename_component(pch_file_name ${header} NAME)
234+
235+
add_pic_pch_target(${header} ${pch_target_name} ${pch_file_name} lib "-fPIC")
236+
add_pic_pch_target(${header} ${pch_target_name} ${pch_file_name} exe "-fPIE")
237+
238+
add_custom_target(
239+
"${pch_target_name}"
240+
COMMENT "Waiting for both lib and exe precompiled headers to build"
241+
DEPENDS "${pch_target_name}_lib" "${pch_target_name}_exe"
242+
)
243+
set_target_properties(
244+
${pch_target_name}
245+
PROPERTIES
246+
PCH_NAME
247+
"${pch_file_name}"
248+
)
249+
endfunction()
250+
251+
function(add_precompiled_headers_to_target target pch_target)
252+
add_dependencies(${target} "${pch_target}")
253+
get_property(sources TARGET ${target} PROPERTY SOURCES)
254+
get_target_property(target_type ${target} TYPE)
255+
get_target_property(pch_file_name ${pch_target} PCH_NAME)
256+
257+
if (target_type STREQUAL "EXECUTABLE")
258+
set(include_compiled_header_dir "${CMAKE_BINARY_DIR}/pch_exe")
259+
# CMake automatically adds additional compile options after linking.
260+
# For example '-fPIC' flag on skin_generator_tool, because it is linked to Qt libs.
261+
# We force correct flag for executables.
262+
set(additional_clang_flags "-fPIE")
263+
endif()
264+
265+
if (target_type MATCHES "LIBRARY")
266+
set(include_compiled_header_dir "${CMAKE_BINARY_DIR}/pch_lib")
267+
endif()
268+
269+
# Force gcc first search gch header in pch_exe/pch_lib:
270+
target_include_directories(
271+
${target}
272+
BEFORE
273+
PUBLIC
274+
${include_compiled_header_dir}
275+
)
276+
277+
foreach(source ${sources})
278+
if(source MATCHES \\.\(cc|cpp|h|hpp\)$)
279+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
280+
set_source_files_properties(
281+
${source}
282+
PROPERTIES
283+
COMPILE_FLAGS
284+
"${additional_clang_flags} -include-pch \
285+
${include_compiled_header_dir}/${pch_file_name}.${PCH_EXTENSION}"
286+
)
287+
endif()
288+
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
289+
set_source_files_properties(
290+
${source}
291+
PROPERTIES
292+
COMPILE_FLAGS "-include ${pch_file_name}"
293+
)
294+
endif()
295+
endif()
296+
endforeach()
297+
endfunction()

coding/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,6 @@ set(
107107
zlib.hpp
108108
)
109109

110-
add_library(${PROJECT_NAME} ${SRC})
110+
omim_add_library(${PROJECT_NAME} ${SRC})
111111

112112
omim_add_test_subdirectory(coding_tests)

drape/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,6 @@ if (PLATFORM_IPHONE)
134134
)
135135
endif()
136136

137-
add_library(${PROJECT_NAME} ${DRAPE_COMMON_SRC} ${SRC})
137+
omim_add_library(${PROJECT_NAME} ${DRAPE_COMMON_SRC} ${SRC})
138138

139139
omim_add_test_subdirectory(drape_tests)

drape_frontend/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ set(
220220
visual_params.hpp
221221
)
222222

223-
add_library(${PROJECT_NAME} ${SRC})
223+
omim_add_library(${PROJECT_NAME} ${SRC})
224224

225225
set(
226226
DRAPE_SHADERS_SRC

editor/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ set(
3535
yes_no_unknown.hpp
3636
)
3737

38-
add_library(${PROJECT_NAME} ${SRC})
38+
omim_add_library(${PROJECT_NAME} ${SRC})
3939

4040
omim_add_test_subdirectory(editor_tests)
4141
omim_add_test_subdirectory(editor_tests_support)

editor/editor_tests_support/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ set(
66
helpers.hpp
77
)
88

9-
add_library(${PROJECT_NAME} ${SRC})
9+
omim_add_library(${PROJECT_NAME} ${SRC})

generator/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ set(SRC
111111
world_map_generator.hpp
112112
)
113113

114-
add_library(${PROJECT_NAME} ${SRC})
114+
omim_add_library(${PROJECT_NAME} ${SRC})
115115

116116
omim_add_test_subdirectory(generator_tests_support)
117117
omim_add_test_subdirectory(generator_tests)

0 commit comments

Comments
 (0)