1
0

CMakeLists.txt 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. # Choose between an explicit include path and using /I during
  14. # precompilation. The /I form is provided as an example. In practice
  15. # the include path form would be used.
  16. SET(PCH_USE_INCLUDE_DIR 1)
  17. IF(PCH_USE_INCLUDE_DIR)
  18. INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
  19. ELSE(PCH_USE_INCLUDE_DIR)
  20. SET(PCH_INCLUDE_DIR "\"/I${CMAKE_CURRENT_SOURCE_DIR}/include\"")
  21. ENDIF(PCH_USE_INCLUDE_DIR)
  22. # The VS6 IDE does not support renaming .pch files so we cannot use a
  23. # separate target.
  24. IF(NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
  25. SET(PCH_USE_TARGET 1)
  26. ENDIF(NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
  27. # Create a target that will use a precompiled header.
  28. SET(foo_SRCS foo1.c foo2.c)
  29. IF(PCH_USE_TARGET)
  30. ADD_EXECUTABLE(foo ${foo_SRCS})
  31. # Create a target to precompile the header for the executable.
  32. ADD_LIBRARY(foo_precompile foo_precompile.c include/foo_precompiled.h)
  33. SET_TARGET_PROPERTIES(foo_precompile PROPERTIES OUTPUT_NAME foo)
  34. ADD_DEPENDENCIES(foo foo_precompile)
  35. SET(PCH_TARGETS foo foo_precompile)
  36. SET(PCH_FILE "\"/Fp${PCH_DIR}/foo_precompiled.pch\"")
  37. ELSE(PCH_USE_TARGET)
  38. # Put the precompiled header source directly in the target.
  39. ADD_EXECUTABLE(foo foo_precompile.c ${foo_SRCS})
  40. SET(PCH_TARGETS foo)
  41. ENDIF(PCH_USE_TARGET)
  42. # Setup flags on the two targets to create and use the precompiled header.
  43. SET_TARGET_PROPERTIES(${PCH_TARGETS} PROPERTIES COMPILE_FLAGS
  44. "/Yufoo_precompiled.h /FIfoo_precompiled.h ${PCH_FILE}")
  45. SET_SOURCE_FILES_PROPERTIES(foo_precompile.c PROPERTIES COMPILE_FLAGS
  46. "/Ycfoo_precompiled.h ${PCH_INCLUDE_DIR}")
  47. # Make sure the object files rebuild when their precompiled header has changed.
  48. # The VS IDE takes care of this automatically.
  49. IF("${CMAKE_GENERATOR}" MATCHES "Makefile")
  50. SET_SOURCE_FILES_PROPERTIES(${foo_SRCS} PROPERTIES
  51. OBJECT_DEPENDS "${PCH_DIR}/foo_precompiled.pch")
  52. ENDIF("${CMAKE_GENERATOR}" MATCHES "Makefile")