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