CMakeLists.txt 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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()
  14. set(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH")
  15. file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/PCH)
  16. endif()
  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()
  22. set(PCH_USE_INCLUDE_DIR 0)
  23. set(PCH_FILE "\"/Fp${PCH_DIR}/foo_precompiled.pch\"")
  24. endif()
  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()
  31. set(PCH_INCLUDE_DIR "\"/I${CMAKE_CURRENT_SOURCE_DIR}/include\"")
  32. endif()
  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" OR
  44. "${CMAKE_GENERATOR}" MATCHES "Ninja")
  45. # This source file creates the precompiled header as a side-effect.
  46. set_source_files_properties(foo_precompile.c PROPERTIES
  47. OBJECT_OUTPUTS "${PCH_DIR}/foo_precompiled.pch")
  48. # These source files use the precompiled header.
  49. set_source_files_properties(${foo_SRCS} PROPERTIES
  50. OBJECT_DEPENDS "${PCH_DIR}/foo_precompiled.pch")
  51. endif()