CMakeLists.txt 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. PROJECT(PrecompiledHeader C)
  2. # Make sure the proper compiler is in use.
  3. IF(NOT MSVC)
  4. MESSAGE(FATAL_ERROR "The PrecompiledHeader test works only with MSVC")
  5. ENDIF(NOT MSVC)
  6. # Compute a custom name for the precompiled header.
  7. IF(CMAKE_CONFIGURATION_TYPES)
  8. SET(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH/${CMAKE_CFG_INTDIR}")
  9. ELSE(CMAKE_CONFIGURATION_TYPES)
  10. SET(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH")
  11. ENDIF(CMAKE_CONFIGURATION_TYPES)
  12. FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/PCH)
  13. # The VS6 IDE does not support renaming .pch files so we cannot use a
  14. # separate target.
  15. IF("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
  16. SET(PCH_USE_TARGET 0)
  17. SET(PCH_USE_INCLUDE_DIR 1)
  18. ELSE("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
  19. SET(PCH_USE_TARGET 1)
  20. SET(PCH_USE_INCLUDE_DIR 0)
  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. IF(PCH_USE_TARGET)
  33. ADD_EXECUTABLE(foo ${foo_SRCS})
  34. # Create a target to precompile the header for the executable.
  35. ADD_LIBRARY(foo_precompile foo_precompile.c include/foo_precompiled.h)
  36. SET_TARGET_PROPERTIES(foo_precompile PROPERTIES OUTPUT_NAME foo)
  37. ADD_DEPENDENCIES(foo foo_precompile)
  38. SET(PCH_TARGETS foo foo_precompile)
  39. SET(PCH_FILE "\"/Fp${PCH_DIR}/foo_precompiled.pch\"")
  40. ELSE(PCH_USE_TARGET)
  41. # Put the precompiled header source directly in the target.
  42. ADD_EXECUTABLE(foo foo_precompile.c ${foo_SRCS})
  43. SET(PCH_TARGETS foo)
  44. ENDIF(PCH_USE_TARGET)
  45. # Add the PCH to the list of files to clean. It is created as a
  46. # side-effect so CMake does not know about it.
  47. SET_DIRECTORY_PROPERTIES(PROPERTIES
  48. ADDITIONAL_MAKE_CLEAN_FILES ${PCH_DIR}/foo_precompiled.pch
  49. )
  50. # Setup flags on the two targets to create and use the precompiled header.
  51. SET_TARGET_PROPERTIES(${PCH_TARGETS} PROPERTIES COMPILE_FLAGS
  52. "/Yufoo_precompiled.h /FIfoo_precompiled.h ${PCH_FILE}")
  53. SET_SOURCE_FILES_PROPERTIES(foo_precompile.c PROPERTIES COMPILE_FLAGS
  54. "/Ycfoo_precompiled.h ${PCH_INCLUDE_DIR}")
  55. # Make sure the object files rebuild when their precompiled header has changed.
  56. # The VS IDE takes care of this automatically.
  57. IF("${CMAKE_GENERATOR}" MATCHES "Makefile")
  58. SET_SOURCE_FILES_PROPERTIES(${foo_SRCS} PROPERTIES
  59. OBJECT_DEPENDS "${PCH_DIR}/foo_precompiled.pch")
  60. ENDIF("${CMAKE_GENERATOR}" MATCHES "Makefile")