2007-02-09 00:18:21 +03:00
|
|
|
PROJECT(PrecompiledHeader C)
|
|
|
|
|
|
|
|
# Make sure the proper compiler is in use.
|
|
|
|
IF(NOT MSVC)
|
|
|
|
MESSAGE(FATAL_ERROR "The PrecompiledHeader test works only with MSVC")
|
|
|
|
ENDIF(NOT MSVC)
|
|
|
|
|
|
|
|
# Compute a custom name for the precompiled header.
|
|
|
|
IF(CMAKE_CONFIGURATION_TYPES)
|
|
|
|
SET(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH/${CMAKE_CFG_INTDIR}")
|
|
|
|
ELSE(CMAKE_CONFIGURATION_TYPES)
|
|
|
|
SET(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH")
|
|
|
|
ENDIF(CMAKE_CONFIGURATION_TYPES)
|
|
|
|
FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/PCH)
|
|
|
|
|
2007-09-13 21:37:45 +04:00
|
|
|
# The VS6 IDE does not support renaming .pch files with /Fp.
|
2007-03-12 20:28:57 +03:00
|
|
|
IF("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
|
|
|
|
SET(PCH_USE_INCLUDE_DIR 1)
|
2007-09-13 21:37:45 +04:00
|
|
|
SET(PCH_FILE)
|
2007-03-12 20:28:57 +03:00
|
|
|
ELSE("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
|
|
|
|
SET(PCH_USE_INCLUDE_DIR 0)
|
2007-09-13 21:37:45 +04:00
|
|
|
SET(PCH_FILE "\"/Fp${PCH_DIR}/foo_precompiled.pch\"")
|
2007-03-12 20:28:57 +03:00
|
|
|
ENDIF("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
|
|
|
|
|
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.
|
2007-02-09 00:18:21 +03:00
|
|
|
IF(PCH_USE_INCLUDE_DIR)
|
|
|
|
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
|
|
ELSE(PCH_USE_INCLUDE_DIR)
|
|
|
|
SET(PCH_INCLUDE_DIR "\"/I${CMAKE_CURRENT_SOURCE_DIR}/include\"")
|
|
|
|
ENDIF(PCH_USE_INCLUDE_DIR)
|
|
|
|
|
|
|
|
# Create a target that will use a precompiled header.
|
|
|
|
SET(foo_SRCS foo1.c foo2.c)
|
2007-09-13 21:37:45 +04:00
|
|
|
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.
|
|
|
|
SET_TARGET_PROPERTIES(foo PROPERTIES
|
|
|
|
COMPILE_FLAGS "/Yufoo_precompiled.h /FIfoo_precompiled.h ${PCH_FILE}")
|
|
|
|
SET_SOURCE_FILES_PROPERTIES(foo_precompile.c PROPERTIES
|
|
|
|
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.
|
2007-02-09 00:18:21 +03:00
|
|
|
IF("${CMAKE_GENERATOR}" MATCHES "Makefile")
|
2007-09-13 21:37:45 +04:00
|
|
|
# This source file creates the precompiled header as a side-effect.
|
|
|
|
SET_SOURCE_FILES_PROPERTIES(foo_precompile.c PROPERTIES
|
|
|
|
OBJECT_OUTPUTS "${PCH_DIR}/foo_precompiled.pch")
|
|
|
|
|
|
|
|
# These source files use the precompiled header.
|
2007-02-09 00:18:21 +03:00
|
|
|
SET_SOURCE_FILES_PROPERTIES(${foo_SRCS} PROPERTIES
|
|
|
|
OBJECT_DEPENDS "${PCH_DIR}/foo_precompiled.pch")
|
|
|
|
ENDIF("${CMAKE_GENERATOR}" MATCHES "Makefile")
|