[CMake] Executing custom command behavior.

Albrecht Fritzsche afritzsche at nvidia.com
Fri Nov 22 12:45:38 EST 2019


Hi,

what exactly is the difference between the following both variants of adding an executable and sources to a project? I probably should add that for both versions the build happens via a custom command added via
      add_custom_command( TARGET ${cmake_target} POST_BUILD ... )
Hence I set all source files to header files only such that you cannot rebuild source files accidentally.

While the following variant gives me for Visual Studio 2017 a properly working project
      # working properly
      add_executable(${cmake_target} ${sources})
      set_source_files_properties(${sources} PROPERTIES HEADER_FILE_ONLY TRUE)

this version fails to rebuild the project when something has changed
      # fails to build again when something changed!
      add_executable(${cmake_target})
      if(sources)
          target_sources(${cmake_target} PRIVATE ${sources})
          set_source_files_properties(${sources} PROPERTIES HEADER_FILE_ONLY TRUE)
      endif()

When comparing the generated files for both variants I somehow fail to see any difference which could cause this difference in behavior.

In the documentation for add_custom_command(TARGET .) I see the mentioning 
    "If the target is already built, the command will not execute."
Which might explain the behavior of the second variant. But why is the rebuilding working as expected in the first variant?

Thanks for any insights,
Ali
-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain
confidential information.  Any unauthorized review, use, disclosure or distribution
is prohibited.  If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------


More information about the CMake mailing list