CMakeLists.txt 2.02 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1
cmake_minimum_required (VERSION 2.6)
2
project(PrecompiledHeader C)
3 4

# Make sure the proper compiler is in use.
5
if(NOT MSVC AND NOT CMAKE_C_COMPILER_ID STREQUAL "Intel")
6 7
  message(FATAL_ERROR "The PrecompiledHeader test works only with MSVC or Intel")
endif()
8 9

# Compute a custom name for the precompiled header.
10 11 12 13 14
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()
15
else()
16 17
  set(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH")
  file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/PCH)
18
endif()
19

Brad King's avatar
Brad King committed
20 21
set(PCH_USE_INCLUDE_DIR 0)
set(PCH_FILE "\"/Fp${PCH_DIR}/foo_precompiled.pch\"")
Brad King's avatar
Brad King committed
22

23
# Choose between an explicit include path and using /I during
24 25
# precompilation.  The /I form is used to test that the PCH is
# actually used.  In practice the include path form would be used.
26 27
if(PCH_USE_INCLUDE_DIR)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
28
else()
29
  set(PCH_INCLUDE_DIR "\"/I${CMAKE_CURRENT_SOURCE_DIR}/include\"")
30
endif()
31 32

# Create a target that will use a precompiled header.
33 34
set(foo_SRCS foo1.c foo2.c)
add_executable(foo foo_precompile.c ${foo_SRCS})
35

36
# Setup flags on the target to create and use the precompiled header.
37
set_target_properties(foo PROPERTIES
38
  COMPILE_FLAGS "/Yufoo_precompiled.h /FIfoo_precompiled.h ${PCH_FILE}")
39
set_source_files_properties(foo_precompile.c PROPERTIES
40
  COMPILE_FLAGS "/Ycfoo_precompiled.h ${PCH_INCLUDE_DIR}")
41

42 43
# Setup dependencies for precompiled header creation and use.  The VS
# IDE takes care of this automatically.
44
if("${CMAKE_GENERATOR}" MATCHES "Makefile" OR
45
   "${CMAKE_GENERATOR}" MATCHES "Ninja")
46
  # This source file creates the precompiled header as a side-effect.
47
  set_source_files_properties(foo_precompile.c PROPERTIES
48 49 50
    OBJECT_OUTPUTS "${PCH_DIR}/foo_precompiled.pch")

  # These source files use the precompiled header.
51
  set_source_files_properties(${foo_SRCS} PROPERTIES
52
    OBJECT_DEPENDS "${PCH_DIR}/foo_precompiled.pch")
53
endif()