diff --git a/Auxiliary/vim/syntax/cmake.vim b/Auxiliary/vim/syntax/cmake.vim index b80d3e5529..adcfed4c15 100644 --- a/Auxiliary/vim/syntax/cmake.vim +++ b/Auxiliary/vim/syntax/cmake.vim @@ -433,6 +433,7 @@ syn keyword cmakeProperty contained \ UNITY_BUILD_BATCH_SIZE \ UNITY_BUILD_CODE_AFTER_INCLUDE \ UNITY_BUILD_CODE_BEFORE_INCLUDE + \ UNITY_BUILD_FILENAME_PREFIX \ UNITY_BUILD_MODE \ UNITY_BUILD_RELOCATABLE \ UNITY_BUILD_UNIQUE_ID diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst index c0ee1cdfcb..baaff01c87 100644 --- a/Help/manual/cmake-properties.7.rst +++ b/Help/manual/cmake-properties.7.rst @@ -424,6 +424,7 @@ Properties on Targets /prop_tgt/UNITY_BUILD_BATCH_SIZE /prop_tgt/UNITY_BUILD_CODE_AFTER_INCLUDE /prop_tgt/UNITY_BUILD_CODE_BEFORE_INCLUDE + /prop_tgt/UNITY_BUILD_FILENAME_PREFIX /prop_tgt/UNITY_BUILD_MODE /prop_tgt/UNITY_BUILD_RELOCATABLE /prop_tgt/UNITY_BUILD_UNIQUE_ID diff --git a/Help/prop_tgt/UNITY_BUILD_FILENAME_PREFIX.rst b/Help/prop_tgt/UNITY_BUILD_FILENAME_PREFIX.rst new file mode 100644 index 0000000000..d6c1e6ab03 --- /dev/null +++ b/Help/prop_tgt/UNITY_BUILD_FILENAME_PREFIX.rst @@ -0,0 +1,25 @@ +UNITY_BUILD_FILENAME_PREFIX +--------------------------- + +.. versionadded:: 4.2 + +By default, the unity file generated when :prop_tgt:`UNITY_BUILD` is enabled +is of the form ``unity__``, where ```` is language-specific. + +If several targets are using unity builds, the build output may give no +indication which target a unity file belongs to. This property allows +customizing the prefix of the generated unity file name. If unset, +the default prefix ``unity_`` is used. + +Example usage: + +.. code-block:: cmake + + add_library(example_library + source1.cxx + source2.cxx + source3.cxx) + + set_target_properties(example_library PROPERTIES + UNITY_BUILD True + UNITY_BUILD_FILENAME_PREFIX "example_") diff --git a/Help/release/dev/unity-filename-prefix.rst b/Help/release/dev/unity-filename-prefix.rst new file mode 100644 index 0000000000..e03ca7ebbe --- /dev/null +++ b/Help/release/dev/unity-filename-prefix.rst @@ -0,0 +1,5 @@ +unity-filename-prefix +--------------------- + +* The :prop_tgt:`UNITY_BUILD_FILENAME_PREFIX` target property was added + to control names of source files generated by :prop_tgt:`UNITY_BUILD`. diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index a46b36da3c..2bdaa68df2 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -2943,9 +2943,8 @@ void cmLocalGenerator::CopyPchCompilePdb( << from_file << "\"))\n"; file << " file(MAKE_DIRECTORY \"" << to_dir << "\")\n"; file << " execute_process(COMMAND ${CMAKE_COMMAND} -E copy"; - file << " \"" << from_file << "\"" - << " \"" << to_dir << "\" RESULT_VARIABLE result " - << " ERROR_QUIET)\n"; + file << " \"" << from_file << "\"" << " \"" << to_dir + << "\" RESULT_VARIABLE result " << " ERROR_QUIET)\n"; file << " if (NOT result EQUAL 0)\n" << " execute_process(COMMAND ${CMAKE_COMMAND}" << " -E sleep 1)\n" @@ -2953,8 +2952,7 @@ void cmLocalGenerator::CopyPchCompilePdb( file << " break()\n" << " endif()\n"; file << " elseif(NOT EXISTS \"" << from_file << "\")\n" - << " execute_process(COMMAND ${CMAKE_COMMAND}" - << " -E sleep 1)\n" + << " execute_process(COMMAND ${CMAKE_COMMAND}" << " -E sleep 1)\n" << " endif()\n"; file << "endforeach()\n"; outputs.push_back(configGenex(dest_file)); @@ -3173,6 +3171,14 @@ std::string unity_file_extension(std::string const& lang) } return extension; } + +char const* unity_file_prefix(cmGeneratorTarget* target) +{ + if (cmValue val = target->GetProperty("UNITY_BUILD_FILENAME_PREFIX")) { + return val->c_str(); + } + return "unity_"; +} } std::vector @@ -3186,15 +3192,15 @@ cmLocalGenerator::AddUnityFilesModeAuto( if (batchSize == 0) { batchSize = filtered_sources.size(); } - + char const* filename_prefix = unity_file_prefix(target); std::vector unity_files; for (size_t itemsLeft = filtered_sources.size(), chunk, batch = 0; itemsLeft > 0; itemsLeft -= chunk, ++batch) { chunk = std::min(itemsLeft, batchSize); - std::string filename = - cmStrCat(filename_base, "unity_", batch, unity_file_extension(lang)); + std::string filename = cmStrCat(filename_base, filename_prefix, batch, + unity_file_extension(lang)); auto const begin = filtered_sources.begin() + batch * batchSize; auto const end = begin + chunk; unity_files.emplace_back(this->WriteUnitySource( @@ -3230,10 +3236,11 @@ cmLocalGenerator::AddUnityFilesModeGroup( } } + char const* filename_prefix = unity_file_prefix(target); for (auto const& item : explicit_mapping) { auto const& name = item.first; - std::string filename = - cmStrCat(filename_base, "unity_", name, unity_file_extension(lang)); + std::string filename = cmStrCat(filename_base, filename_prefix, name, + unity_file_extension(lang)); unity_files.emplace_back(this->WriteUnitySource( target, configs, cmMakeRange(item.second), beforeInclude, afterInclude, std::move(filename), filename_base, pathMode)); diff --git a/Tests/RunCMake/UnityBuild/RunCMakeTest.cmake b/Tests/RunCMake/UnityBuild/RunCMakeTest.cmake index 55bbe98b3d..f023299437 100644 --- a/Tests/RunCMake/UnityBuild/RunCMakeTest.cmake +++ b/Tests/RunCMake/UnityBuild/RunCMakeTest.cmake @@ -19,6 +19,7 @@ run_cmake(unitybuild_cxx_group) run_cmake(unitybuild_c_and_cxx_absolute_path) run_cmake(unitybuild_c_and_cxx_relocatable_path) run_cmake(unitybuild_c_and_cxx) +run_cmake(unitybuild_c_and_cxx_filename_prefix) run_cmake(unitybuild_c_and_cxx_group) if(CMake_TEST_OBJC) run_cmake(unitybuild_objc) diff --git a/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_filename_prefix-check.cmake b/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_filename_prefix-check.cmake new file mode 100644 index 0000000000..f2f4e2c812 --- /dev/null +++ b/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_filename_prefix-check.cmake @@ -0,0 +1,11 @@ +set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/custom_prefix_0_c.c") +if(NOT EXISTS "${unitybuild_c}") + set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c} does not exist.") + return() +endif() + +set(unitybuild_cxx "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/custom_prefix_0_cxx.cxx") +if(NOT EXISTS "${unitybuild_cxx}") + set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_cxx} does not exist.") + return() +endif() diff --git a/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_filename_prefix.cmake b/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_filename_prefix.cmake new file mode 100644 index 0000000000..13af7c890a --- /dev/null +++ b/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_filename_prefix.cmake @@ -0,0 +1,21 @@ +set(CMAKE_INTERMEDIATE_DIR_STRATEGY FULL CACHE STRING "" FORCE) + +project(unitybuild_c_and_cxx_filename_prefix C CXX) + +set(srcs "") +foreach(s RANGE 1 8) + set(src_c "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c") + file(WRITE "${src_c}" "int s${s}(void) { return 0; }\n") + + set(src_cxx "${CMAKE_CURRENT_BINARY_DIR}/s${s}.cxx") + file(WRITE "${src_cxx}" "int s${s}(void) { return 0; }\n") + + list(APPEND srcs "${src_c}") + list(APPEND srcs "${src_cxx}") +endforeach() + +add_library(tgt SHARED ${srcs}) + +set_target_properties(tgt PROPERTIES + UNITY_BUILD ON + UNITY_BUILD_FILENAME_PREFIX "custom_prefix_")