BlueBrain / hpc-coding-conventions

Apache License 2.0
8 stars 5 forks source link

Add NO_TARGET option to build time copy helper. #96

Closed olupton closed 3 years ago

olupton commented 3 years ago

By default cpp_cc_build_time_copy (recently added in https://github.com/BlueBrain/hpc-coding-conventions/pull/95) creates a target that is always built that depends on the output file, ensuring that it is already up to date. This potentially generates a large number of top-level always-built targets, which can be rather slow when using legacy make/Makefiles. The Ninja build system does not seem to have the same problem.

This commit adds an optional NO_TARGET flag, which sets up the rule to copy the file but does not create a top-level always-built target. This means that the caller can create a single such target depending on many files, which is much more efficient when using make/Makefiles.

For example this:

cpp_cc_build_time_copy(INPUT "{source}/file1.txt" OUTPUT "{build}/file1.txt")
cpp_cc_build_time_copy(INPUT "{source}/file2.txt" OUTPUT "{build}/file2.txt")

creates two top-level rules while

cpp_cc_build_time_copy(INPUT "{source}/file1.txt" OUTPUT "{build}/file1.txt" NO_TARGET)
cpp_cc_build_time_copy(INPUT "{source}/file2.txt" OUTPUT "{build}/file2.txt" NO_TARGET)
add_custom_target(copy-files ALL DEPENDS "{build}/file1.txt" "{build}/file2.txt")

creates just one. When large numbers of files are being copied then this difference can be significant.

Note that if NO_TARGET is passed and no dependency is declared on the OUTPUT file in the same CMakeLists.txt the file will not be copied. See, for example, https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#how-can-i-add-a-dependency-to-a-source-file-which-is-generated-in-a-subdirectory and https://cmake.org/cmake/help/latest/command/add_custom_command.html.