CMakeLists.txt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # The PCH that dolphin uses for MSVC is non-standard;
  2. # Instead of having one PCH per module, dolphin has one PCH shared between all modules.
  3. # So we need to implement PCH manually, rather than using the PCH support built into cmake
  4. # linking against this interface libary will cause targets to enable PCH
  5. add_library(use_pch INTERFACE)
  6. # Uncomment this return to disable PCH
  7. #return()
  8. add_library(build_pch pch.h pch.cpp)
  9. # fmt/format.h is included in the PCH
  10. target_link_libraries(build_pch PUBLIC fmt::fmt)
  11. # pch.cpp should be compiled with the /Yc command, which creates the precompiled header
  12. target_compile_options(build_pch PRIVATE /Ycpch.h)
  13. # /Fp sets the location of the PCH. By forcing it to a fixed location, all modules
  14. # will share this one PCH. We give it a fixed name so we can depend on it later
  15. target_compile_options(build_pch PUBLIC /Fp$<TARGET_FILE_DIR:build_pch>/dolphin.pch )
  16. # Sharing a PCH breaks pdb files. So we use the /Z7 option to inline the pdb into
  17. # the binary. However MSVC gets noisy if you set both /Zi and /Z7
  18. if (POLICY CMP0141)
  19. # CMake 3.25 has a policy that makes us control this somewhat sanely
  20. set_property(TARGET build_pch PROPERTY MSVC_DEBUG_INFORMATION_FORMAT "$<$<CONFIG:Debug,RelWithDebInfo>:Embedded>")
  21. # Unfortnually, properties don't propagate. So we also set it globally via parent scope
  22. set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<$<CONFIG:Debug,RelWithDebInfo>:Embedded>" PARENT_SCOPE)
  23. else()
  24. if (CMAKE_CXX_FLAGS_DEBUG MATCHES "/Zi")
  25. # Otherwise we do an ugly string replace to remove it from FLAGS_DEBUG
  26. string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
  27. string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
  28. # and also overwrite the version in the parent scope
  29. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}" PARENT_SCOPE)
  30. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}" PARENT_SCOPE)
  31. target_compile_options(build_pch PUBLIC "$<$<CONFIG:Debug,RelWithDebInfo>:/Z7>")
  32. endif()
  33. endif()
  34. # Setting /Z7 also requires us to disable minimal rebuilds.
  35. target_compile_options(build_pch PUBLIC "$<$<CONFIG:Debug,RelWithDebInfo>:/Gm->")
  36. # To get this working with ninja, we need to tell it that compiling pch.cpp generates an extra output
  37. set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/pch.cpp PROPERTIES
  38. OBJECT_OUTPUTS $<TARGET_FILE_DIR:build_pch>/dolphin.pch
  39. )
  40. # and then create a custom target that depends on the pch output
  41. # so that ninja won't start building anything that depends on this
  42. # target before the pch is built
  43. add_custom_target(force_build_pch
  44. DEPENDS $<TARGET_FILE_DIR:build_pch>/dolphin.pch
  45. )
  46. # link the pch into anything that depends on use_pch
  47. target_link_libraries(use_pch INTERFACE build_pch)
  48. # targets which use the pch need these compile options
  49. # /Yu - Use precompiled header named "pch.h"
  50. # /FI - Force include "pch.h" at top of every source file
  51. target_compile_options(use_pch INTERFACE /Yupch.h /FIpch.h)
  52. # For ninja, we need to depend on force_build_pch
  53. add_dependencies(use_pch force_build_pch)