CMakeLists.txt 2.2 KB

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