Przeglądaj źródła

Merge topic 'unity-filename-prefix'

5723e65215 Unity: Add a target property to control generated filenames

Acked-by: Kitware Robot <[email protected]>
Tested-by: buildbot <[email protected]>
Merge-request: !11068
Brad King 1 miesiąc temu
rodzic
commit
032b091c3e

+ 1 - 0
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

+ 1 - 0
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

+ 25 - 0
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_<index>_<suffix>``, where ``<suffix>`` 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_")

+ 5 - 0
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`.

+ 17 - 10
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<cmLocalGenerator::UnitySource>
@@ -3186,15 +3192,15 @@ cmLocalGenerator::AddUnityFilesModeAuto(
   if (batchSize == 0) {
     batchSize = filtered_sources.size();
   }
-
+  char const* filename_prefix = unity_file_prefix(target);
   std::vector<UnitySource> 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));

+ 1 - 0
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)

+ 11 - 0
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()

+ 21 - 0
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_")