CMakeLists.txt 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. cmake_minimum_required (VERSION 2.6)
  2. PROJECT(PrecompiledHeader C)
  3. # Make sure the proper compiler is in use.
  4. IF(NOT MSVC AND NOT "${CMAKE_C_COMPILER_ID}" MATCHES "^(Intel)$")
  5. MESSAGE(FATAL_ERROR "The PrecompiledHeader test works only with MSVC or Intel")
  6. ENDIF()
  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. FOREACH(cfg ${CMAKE_CONFIGURATION_TYPES})
  11. FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/PCH/${cfg})
  12. ENDFOREACH()
  13. ELSE(CMAKE_CONFIGURATION_TYPES)
  14. SET(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH")
  15. FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/PCH)
  16. ENDIF(CMAKE_CONFIGURATION_TYPES)
  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)
  20. SET(PCH_FILE)
  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")
  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)
  33. # Create a target that will use a precompiled header.
  34. SET(foo_SRCS foo1.c foo2.c)
  35. ADD_EXECUTABLE(foo foo_precompile.c ${foo_SRCS})
  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. # Setup dependencies for precompiled header creation and use. The VS
  42. # IDE takes care of this automatically.
  43. IF("${CMAKE_GENERATOR}" MATCHES "Makefile")
  44. # This source file creates the precompiled header as a side-effect.
  45. SET_SOURCE_FILES_PROPERTIES(foo_precompile.c PROPERTIES
  46. OBJECT_OUTPUTS "${PCH_DIR}/foo_precompiled.pch")
  47. # These source files use the precompiled header.
  48. SET_SOURCE_FILES_PROPERTIES(${foo_SRCS} PROPERTIES
  49. OBJECT_DEPENDS "${PCH_DIR}/foo_precompiled.pch")
  50. ENDIF("${CMAKE_GENERATOR}" MATCHES "Makefile")