CMP0058.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. CMP0058
  2. -------
  3. Ninja requires custom command byproducts to be explicit.
  4. When an intermediate file generated during the build is consumed
  5. by an expensive operation or a large tree of dependents, one may
  6. reduce the work needed for an incremental rebuild by updating the
  7. file timestamp only when its content changes. With this approach
  8. the generation rule must have a separate output file that is always
  9. updated with a new timestamp that is newer than any dependencies of
  10. the rule so that the build tool re-runs the rule only when the input
  11. changes. We refer to the separate output file as a rule's *witness*
  12. and the generated file as a rule's *byproduct*.
  13. Byproducts may not be listed as outputs because their timestamps are
  14. allowed to be older than the inputs. No build tools (like ``make``)
  15. that existed when CMake was designed have a way to express byproducts.
  16. Therefore CMake versions prior to 3.2 had no way to specify them.
  17. Projects typically left byproducts undeclared in the rules that
  18. generate them. For example:
  19. .. code-block:: cmake
  20. add_custom_command(
  21. OUTPUT witness.txt
  22. COMMAND ${CMAKE_COMMAND} -E copy_if_different
  23. ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
  24. byproduct.txt # timestamp may not change
  25. COMMAND ${CMAKE_COMMAND} -E touch witness.txt
  26. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
  27. )
  28. add_custom_target(Provider DEPENDS witness.txt)
  29. add_custom_command(
  30. OUTPUT generated.c
  31. COMMAND expensive-task -i byproduct.txt -o generated.c
  32. DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/byproduct.txt
  33. )
  34. add_library(Consumer generated.c)
  35. add_dependencies(Consumer Provider)
  36. This works well for all generators except :generator:`Ninja`.
  37. The Ninja build tool sees a rule listing ``byproduct.txt``
  38. as a dependency and no rule listing it as an output. Ninja then
  39. complains that there is no way to satisfy the dependency and
  40. stops building even though there are order-only dependencies
  41. that ensure ``byproduct.txt`` will exist before its consumers
  42. need it. See discussion of this problem in `Ninja Issue 760`_
  43. for further details on why Ninja works this way.
  44. .. _`Ninja Issue 760`: https://github.com/martine/ninja/issues/760
  45. Instead of leaving byproducts undeclared in the rules that generate
  46. them, Ninja expects byproducts to be listed along with other outputs.
  47. Such rules may be marked with a ``restat`` option that tells Ninja
  48. to check the timestamps of outputs after the rules run. This
  49. prevents byproducts whose timestamps do not change from causing
  50. their dependents to re-build unnecessarily.
  51. Since the above approach does not tell CMake what custom command
  52. generates ``byproduct.txt``, the Ninja generator does not have
  53. enough information to add the byproduct as an output of any rule.
  54. CMake 2.8.12 and above work around this problem and allow projects
  55. using the above approach to build by generating ``phony`` build
  56. rules to tell Ninja to tolerate such missing files. However, this
  57. workaround prevents Ninja from diagnosing a dependency that is
  58. really missing. It also works poorly in in-source builds where
  59. every custom command dependency, even on source files, needs to
  60. be treated this way because CMake does not have enough information
  61. to know which files are generated as byproducts of custom commands.
  62. CMake 3.2 introduced the ``BYPRODUCTS`` option to the
  63. :command:`add_custom_command` and :command:`add_custom_target`
  64. commands. This option allows byproducts to be specified explicitly:
  65. .. code-block:: cmake
  66. add_custom_command(
  67. OUTPUT witness.txt
  68. BYPRODUCTS byproduct.txt # explicit byproduct specification
  69. COMMAND ${CMAKE_COMMAND} -E copy_if_different
  70. ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
  71. byproduct.txt # timestamp may not change
  72. ...
  73. The ``BYPRODUCTS`` option is used by the :generator:`Ninja` generator
  74. to list byproducts among the outputs of the custom commands that
  75. generate them, and is ignored by other generators.
  76. CMake 3.3 and above prefer to require projects to specify custom
  77. command byproducts explicitly so that it can avoid using the
  78. ``phony`` rule workaround altogether. Policy ``CMP0058`` was
  79. introduced to provide compatibility with existing projects that
  80. still need the workaround.
  81. This policy has no effect on generators other than :generator:`Ninja`.
  82. The ``OLD`` behavior for this policy is to generate Ninja ``phony``
  83. rules for unknown dependencies in the build tree. The ``NEW``
  84. behavior for this policy is to not generate these and instead
  85. require projects to specify custom command ``BYPRODUCTS`` explicitly.
  86. This policy was introduced in CMake version 3.3.
  87. CMake version |release| warns when it sees unknown dependencies in
  88. out-of-source build trees if the policy is not set and then uses
  89. ``OLD`` behavior. Use the :command:`cmake_policy` command to set
  90. the policy to ``OLD`` or ``NEW`` explicitly. The policy setting
  91. must be in scope at the end of the top-level ``CMakeLists.txt``
  92. file of the project and has global effect.
  93. .. include:: DEPRECATED.txt