1 PROJECT(PrecompiledHeader C)
3 # Make sure the proper compiler is in use.
5 MESSAGE(FATAL_ERROR "The PrecompiledHeader test works only with MSVC")
8 # Compute a custom name for the precompiled header.
9 IF(CMAKE_CONFIGURATION_TYPES)
10 SET(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH/${CMAKE_CFG_INTDIR}")
11 ELSE(CMAKE_CONFIGURATION_TYPES)
12 SET(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH")
13 ENDIF(CMAKE_CONFIGURATION_TYPES)
14 FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/PCH)
16 # The VS6 IDE does not support renaming .pch files with /Fp.
17 IF("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
18 SET(PCH_USE_INCLUDE_DIR 1)
20 ELSE("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
21 SET(PCH_USE_INCLUDE_DIR 0)
22 SET(PCH_FILE "\"/Fp${PCH_DIR}/foo_precompiled.pch\"")
23 ENDIF("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
25 # Choose between an explicit include path and using /I during
26 # precompilation. The /I form is used to test that the PCH is
27 # actually used. In practice the include path form would be used.
28 IF(PCH_USE_INCLUDE_DIR)
29 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
30 ELSE(PCH_USE_INCLUDE_DIR)
31 SET(PCH_INCLUDE_DIR "\"/I${CMAKE_CURRENT_SOURCE_DIR}/include\"")
32 ENDIF(PCH_USE_INCLUDE_DIR)
34 # Create a target that will use a precompiled header.
35 SET(foo_SRCS foo1.c foo2.c)
36 ADD_EXECUTABLE(foo foo_precompile.c ${foo_SRCS})
38 # Setup flags on the target to create and use the precompiled header.
39 SET_TARGET_PROPERTIES(foo PROPERTIES
40 COMPILE_FLAGS "/Yufoo_precompiled.h /FIfoo_precompiled.h ${PCH_FILE}")
41 SET_SOURCE_FILES_PROPERTIES(foo_precompile.c PROPERTIES
42 COMPILE_FLAGS "/Ycfoo_precompiled.h ${PCH_INCLUDE_DIR}")
44 # Setup dependencies for precompiled header creation and use. The VS
45 # IDE takes care of this automatically.
46 IF("${CMAKE_GENERATOR}" MATCHES "Makefile")
47 # This source file creates the precompiled header as a side-effect.
48 SET_SOURCE_FILES_PROPERTIES(foo_precompile.c PROPERTIES
49 OBJECT_OUTPUTS "${PCH_DIR}/foo_precompiled.pch")
51 # These source files use the precompiled header.
52 SET_SOURCE_FILES_PROPERTIES(${foo_SRCS} PROPERTIES
53 OBJECT_DEPENDS "${PCH_DIR}/foo_precompiled.pch")
54 ENDIF("${CMAKE_GENERATOR}" MATCHES "Makefile")