2008-03-25 18:27:18 +03:00
|
|
|
cmake_minimum_required (VERSION 2.6)
|
2012-08-13 21:47:32 +04:00
|
|
|
project(PrecompiledHeader C)
|
2007-02-09 00:18:21 +03:00
|
|
|
|
|
|
|
# Make sure the proper compiler is in use.
|
2012-08-13 21:47:32 +04:00
|
|
|
if(NOT MSVC AND NOT "${CMAKE_C_COMPILER_ID}" MATCHES "^(Intel)$")
|
|
|
|
message(FATAL_ERROR "The PrecompiledHeader test works only with MSVC or Intel")
|
|
|
|
endif()
|
2007-02-09 00:18:21 +03:00
|
|
|
|
|
|
|
# Compute a custom name for the precompiled header.
|
2012-08-13 21:47:32 +04:00
|
|
|
if(CMAKE_CONFIGURATION_TYPES)
|
|
|
|
set(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH/${CMAKE_CFG_INTDIR}")
|
|
|
|
foreach(cfg ${CMAKE_CONFIGURATION_TYPES})
|
|
|
|
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/PCH/${cfg})
|
|
|
|
endforeach()
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
set(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH")
|
|
|
|
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/PCH)
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2007-02-09 00:18:21 +03:00
|
|
|
|
2007-09-13 21:37:45 +04:00
|
|
|
# The VS6 IDE does not support renaming .pch files with /Fp.
|
2012-08-13 21:47:32 +04:00
|
|
|
if("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
|
|
|
|
set(PCH_USE_INCLUDE_DIR 1)
|
|
|
|
set(PCH_FILE)
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
set(PCH_USE_INCLUDE_DIR 0)
|
|
|
|
set(PCH_FILE "\"/Fp${PCH_DIR}/foo_precompiled.pch\"")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2007-03-12 20:28:57 +03:00
|
|
|
|
2007-02-09 00:18:21 +03:00
|
|
|
# Choose between an explicit include path and using /I during
|
2007-03-12 19:35:11 +03:00
|
|
|
# precompilation. The /I form is used to test that the PCH is
|
|
|
|
# actually used. In practice the include path form would be used.
|
2012-08-13 21:47:32 +04:00
|
|
|
if(PCH_USE_INCLUDE_DIR)
|
|
|
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
set(PCH_INCLUDE_DIR "\"/I${CMAKE_CURRENT_SOURCE_DIR}/include\"")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2007-02-09 00:18:21 +03:00
|
|
|
|
|
|
|
# Create a target that will use a precompiled header.
|
2012-08-13 21:47:32 +04:00
|
|
|
set(foo_SRCS foo1.c foo2.c)
|
|
|
|
add_executable(foo foo_precompile.c ${foo_SRCS})
|
2007-03-12 19:44:59 +03:00
|
|
|
|
2007-09-13 21:37:45 +04:00
|
|
|
# Setup flags on the target to create and use the precompiled header.
|
2012-08-13 21:47:32 +04:00
|
|
|
set_target_properties(foo PROPERTIES
|
2007-09-13 21:37:45 +04:00
|
|
|
COMPILE_FLAGS "/Yufoo_precompiled.h /FIfoo_precompiled.h ${PCH_FILE}")
|
2012-08-13 21:47:32 +04:00
|
|
|
set_source_files_properties(foo_precompile.c PROPERTIES
|
2007-09-13 21:37:45 +04:00
|
|
|
COMPILE_FLAGS "/Ycfoo_precompiled.h ${PCH_INCLUDE_DIR}")
|
2007-02-09 00:18:21 +03:00
|
|
|
|
2007-09-13 21:37:45 +04:00
|
|
|
# Setup dependencies for precompiled header creation and use. The VS
|
|
|
|
# IDE takes care of this automatically.
|
2012-08-13 21:47:32 +04:00
|
|
|
if("${CMAKE_GENERATOR}" MATCHES "Makefile" OR
|
2012-03-18 00:49:36 +04:00
|
|
|
"${CMAKE_GENERATOR}" MATCHES "Ninja")
|
2007-09-13 21:37:45 +04:00
|
|
|
# This source file creates the precompiled header as a side-effect.
|
2012-08-13 21:47:32 +04:00
|
|
|
set_source_files_properties(foo_precompile.c PROPERTIES
|
2007-09-13 21:37:45 +04:00
|
|
|
OBJECT_OUTPUTS "${PCH_DIR}/foo_precompiled.pch")
|
|
|
|
|
|
|
|
# These source files use the precompiled header.
|
2012-08-13 21:47:32 +04:00
|
|
|
set_source_files_properties(${foo_SRCS} PROPERTIES
|
2007-02-09 00:18:21 +03:00
|
|
|
OBJECT_DEPENDS "${PCH_DIR}/foo_precompiled.pch")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|