CMakeLists.txt 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 STREQUAL "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. set(PCH_USE_INCLUDE_DIR 0)
  18. set(PCH_FILE "\"/Fp${PCH_DIR}/foo_precompiled.pch\"")
  19. # Choose between an explicit include path and using /I during
  20. # precompilation. The /I form is used to test that the PCH is
  21. # actually used. In practice the include path form would be used.
  22. if(PCH_USE_INCLUDE_DIR)
  23. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
  24. else()
  25. set(PCH_INCLUDE_DIR "\"/I${CMAKE_CURRENT_SOURCE_DIR}/include\"")
  26. endif()
  27. # Create a target that will use a precompiled header.
  28. set(foo_SRCS foo1.c foo2.c)
  29. add_executable(foo foo_precompile.c ${foo_SRCS})
  30. # Setup flags on the target to create and use the precompiled header.
  31. set_target_properties(foo PROPERTIES
  32. COMPILE_FLAGS "/Yufoo_precompiled.h /FIfoo_precompiled.h ${PCH_FILE}")
  33. set_source_files_properties(foo_precompile.c PROPERTIES
  34. COMPILE_FLAGS "/Ycfoo_precompiled.h ${PCH_INCLUDE_DIR}")
  35. # Setup dependencies for precompiled header creation and use. The VS
  36. # IDE takes care of this automatically.
  37. if("${CMAKE_GENERATOR}" MATCHES "Makefile" OR
  38. "${CMAKE_GENERATOR}" MATCHES "Ninja")
  39. # This source file creates the precompiled header as a side-effect.
  40. set_source_files_properties(foo_precompile.c PROPERTIES
  41. OBJECT_OUTPUTS "${PCH_DIR}/foo_precompiled.pch")
  42. # These source files use the precompiled header.
  43. set_source_files_properties(${foo_SRCS} PROPERTIES
  44. OBJECT_DEPENDS "${PCH_DIR}/foo_precompiled.pch")
  45. endif()