Merge topic 'JOB_POOL_COMPILE-FileSet-property'

8e6eec8125 FILE_SET: add JOB_POOL_COMPILE property support

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !12005
diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst
index 73f880f1..aec1892 100644
--- a/Help/manual/cmake-properties.7.rst
+++ b/Help/manual/cmake-properties.7.rst
@@ -545,6 +545,7 @@
    /prop_fs/COMPILE_DEFINITIONS
    /prop_fs/COMPILE_OPTIONS
    /prop_fs/CXX_SCAN_FOR_MODULES
+   /prop_fs/JOB_POOL_COMPILE
    /prop_fs/INCLUDE_DIRECTORIES
    /prop_fs/INDEPENDENT_FILES
    /prop_fs/INTERFACE_COMPILE_DEFINITIONS
diff --git a/Help/prop_fs/JOB_POOL_COMPILE.rst b/Help/prop_fs/JOB_POOL_COMPILE.rst
new file mode 100644
index 0000000..b460aff
--- /dev/null
+++ b/Help/prop_fs/JOB_POOL_COMPILE.rst
@@ -0,0 +1,28 @@
+JOB_POOL_COMPILE
+----------------
+
+.. versionadded:: 4.4
+
+Ninja only: Pool used for compiling.
+
+The number of parallel compile processes could be limited by defining
+pools with the global :prop_gbl:`JOB_POOLS`
+property and then specifying here the pool name.
+
+This allows to override the :prop_sf:`JOB_POOL_COMPILE` and
+:prop_tgt:`JOB_POOL_COMPILE` values for specific source files within a same
+target.
+
+For instance:
+
+.. code-block:: cmake
+
+  set_property(FILE_SET my_fileset TYPE SOURCE PROPERTY JOB_POOL_COMPILE two_jobs)
+
+This property is undefined by default.
+
+See Also
+^^^^^^^^
+
+* :prop_sf:`JOB_POOL_COMPILE` source file property
+* :prop_tgt:`JOB_POOL_COMPILE` target property
diff --git a/Help/prop_sf/JOB_POOL_COMPILE.rst b/Help/prop_sf/JOB_POOL_COMPILE.rst
index 79409da..2c9a8d4 100644
--- a/Help/prop_sf/JOB_POOL_COMPILE.rst
+++ b/Help/prop_sf/JOB_POOL_COMPILE.rst
@@ -19,3 +19,9 @@
   set_property(SOURCE main.cc PROPERTY JOB_POOL_COMPILE two_jobs)
 
 This property is undefined by default.
+
+See Also
+^^^^^^^^
+
+* :prop_fs:`JOB_POOL_COMPILE` file set property
+* :prop_tgt:`JOB_POOL_COMPILE` target property
diff --git a/Help/prop_tgt/JOB_POOL_COMPILE.rst b/Help/prop_tgt/JOB_POOL_COMPILE.rst
index 5d8e940..d70b010 100644
--- a/Help/prop_tgt/JOB_POOL_COMPILE.rst
+++ b/Help/prop_tgt/JOB_POOL_COMPILE.rst
@@ -15,3 +15,9 @@
 
 This property is initialized by the value of
 :variable:`CMAKE_JOB_POOL_COMPILE`.
+
+See Also
+^^^^^^^^
+
+* :prop_fs:`JOB_POOL_COMPILE` file set property
+* :prop_sf:`JOB_POOL_COMPILE` source file property
diff --git a/Help/release/dev/FILE_SET-JOB_POOL_COMPILE.rst b/Help/release/dev/FILE_SET-JOB_POOL_COMPILE.rst
new file mode 100644
index 0000000..fe30570
--- /dev/null
+++ b/Help/release/dev/FILE_SET-JOB_POOL_COMPILE.rst
@@ -0,0 +1,5 @@
+FILE_SET-JOB_POOL_COMPILE
+-------------------------
+
+* File sets gained the support of the :prop_fs:`JOB_POOL_COMPILE` file set
+  property.
diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx
index c962997..556d713 100644
--- a/Source/cmNinjaNormalTargetGenerator.cxx
+++ b/Source/cmNinjaNormalTargetGenerator.cxx
@@ -1041,7 +1041,8 @@
                               vars["LINK_FLAGS"], frameworkPath, linkPath,
                               genTarget);
 
-  this->addPoolNinjaVariable("JOB_POOL_LINK", genTarget, nullptr, vars);
+  this->addPoolNinjaVariable("JOB_POOL_LINK", config, genTarget, nullptr,
+                             vars);
 
   vars["MANIFESTS"] = this->GetManifests(config);
 
@@ -1384,7 +1385,7 @@
                            this->TargetLinkLanguage(config), "CURRENT", false);
   }
 
-  this->addPoolNinjaVariable("JOB_POOL_LINK", gt, nullptr, vars);
+  this->addPoolNinjaVariable("JOB_POOL_LINK", config, gt, nullptr, vars);
 
   this->UseLWYU = this->GetLocalGenerator()->AppendLWYUFlags(
     vars["LINK_FLAGS"], this->GetGeneratorTarget(),
diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx
index c20ceb5..a07fe1c 100644
--- a/Source/cmNinjaTargetGenerator.cxx
+++ b/Source/cmNinjaTargetGenerator.cxx
@@ -1787,8 +1787,9 @@
       scanningFiles.ScanningOutput = cmStrCat(objectFileName, ".ddi");
     }
 
-    this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
-                               source, ppBuild.Variables);
+    this->addPoolNinjaVariable("JOB_POOL_COMPILE", config,
+                               this->GetGeneratorTarget(), source,
+                               ppBuild.Variables);
 
     this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
                                            ppBuild, commandLineLengthLimit);
@@ -1821,13 +1822,13 @@
     this->ConvertToOutputFormatForShell(targetSupportDir);
   vars["OBJECT_FILE_DIR"] = this->ConvertToOutputFormatForShell(objectFileDir);
 
-  this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
-                             source, vars);
+  this->addPoolNinjaVariable("JOB_POOL_COMPILE", config,
+                             this->GetGeneratorTarget(), source, vars);
 
   if (!pchSources.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
     auto pchIt = pchSources.find(source->GetFullPath());
     if (pchIt != pchSources.end()) {
-      this->addPoolNinjaVariable("JOB_POOL_PRECOMPILE_HEADER",
+      this->addPoolNinjaVariable("JOB_POOL_PRECOMPILE_HEADER", config,
                                  this->GetGeneratorTarget(), nullptr, vars);
     }
   }
@@ -2024,8 +2025,9 @@
       scanningFiles.ScanningOutput = cmStrCat(bmiFileName, ".ddi");
     }
 
-    this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
-                               source, ppBuild.Variables);
+    this->addPoolNinjaVariable("JOB_POOL_COMPILE", config,
+                               this->GetGeneratorTarget(), source,
+                               ppBuild.Variables);
 
     this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
                                            ppBuild, commandLineLengthLimit);
@@ -2053,8 +2055,8 @@
     this->ConvertToOutputFormatForShell(targetSupportDir);
   vars["OBJECT_FILE_DIR"] = this->ConvertToOutputFormatForShell(bmiFileDir);
 
-  this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
-                             source, vars);
+  this->addPoolNinjaVariable("JOB_POOL_COMPILE", config,
+                             this->GetGeneratorTarget(), source, vars);
 
   bmiBuild.RspFile = cmStrCat(bmiFileName, ".rsp");
 
@@ -2775,15 +2777,22 @@
 }
 
 void cmNinjaTargetGenerator::addPoolNinjaVariable(
-  std::string const& pool_property, cmGeneratorTarget* target,
-  cmSourceFile const* source, cmNinjaVars& vars)
+  std::string const& pool_property, std::string const& config,
+  cmGeneratorTarget* target, cmSourceFile const* source, cmNinjaVars& vars)
 {
-  // First check the current source properties, then if not found, its target
-  // ones. Allows to override a target-wide compile pool with a source-specific
-  // one.
+  // First check file set properties, then if not found the current source
+  // properties, then if not found, its target ones. Allows to override a
+  // target-wide compile pool with file set-specific or source-specific one.
   cmValue pool = {};
   if (source) {
-    pool = source->GetProperty(pool_property);
+    cmGeneratorFileSet const* fileSet =
+      target->GetFileSetForSource(config, source);
+    if (fileSet) {
+      pool = fileSet->GetProperty(pool_property);
+    }
+    if (!pool) {
+      pool = source->GetProperty(pool_property);
+    }
   }
   if (!pool) {
     pool = target->GetProperty(pool_property);
diff --git a/Source/cmNinjaTargetGenerator.h b/Source/cmNinjaTargetGenerator.h
index 41f3f7c..a019df2 100644
--- a/Source/cmNinjaTargetGenerator.h
+++ b/Source/cmNinjaTargetGenerator.h
@@ -247,6 +247,7 @@
 
   /// @param source may be nullptr.
   void addPoolNinjaVariable(std::string const& pool_property,
+                            std::string const& config,
                             cmGeneratorTarget* target,
                             cmSourceFile const* source, cmNinjaVars& vars);
 
diff --git a/Tests/RunCMake/Ninja/FileSetJobPool-check.cmake b/Tests/RunCMake/Ninja/FileSetJobPool-check.cmake
new file mode 100644
index 0000000..0998a22
--- /dev/null
+++ b/Tests/RunCMake/Ninja/FileSetJobPool-check.cmake
@@ -0,0 +1,17 @@
+set(log "${RunCMake_BINARY_DIR}/FileSetJobPool-build/build.ninja")
+file(READ "${log}" build_file)
+if(NOT "${build_file}" MATCHES "pool = file_set_compile_pool")
+  string(CONCAT RunCMake_TEST_FAILED "Log file:\n ${log}\n" "does not have expected line: pool = file_set_compile_pool")
+endif()
+if(NOT "${build_file}" MATCHES "pool = target_link_pool")
+  string(CONCAT RunCMake_TEST_FAILED "Log file:\n ${log}\n" "does not have expected line: pool = target_link_pool")
+endif()
+# Even though `source_file_compile_pool` and `target_compile_pool` were defined as the target's compile jobs pool,
+# since the file set property overrides it with `file_set_compile_pool` pool, the source file compile job
+# and the target compile job pool should not exist in the generated Ninja file.
+if("${build_file}" MATCHES "pool = source_file_compile_pool")
+  string(CONCAT RunCMake_TEST_FAILED "Log file:\n ${log}\n" "have unexpected line: pool = source_file_compile_pool")
+endif()
+if("${build_file}" MATCHES "pool = target_compile_pool")
+  string(CONCAT RunCMake_TEST_FAILED "Log file:\n ${log}\n" "have unexpected line: pool = target_compile_pool")
+endif()
diff --git a/Tests/RunCMake/Ninja/FileSetJobPool.cmake b/Tests/RunCMake/Ninja/FileSetJobPool.cmake
new file mode 100644
index 0000000..cd9dfad
--- /dev/null
+++ b/Tests/RunCMake/Ninja/FileSetJobPool.cmake
@@ -0,0 +1,16 @@
+set_property(GLOBAL PROPERTY JOB_POOLS source_file_compile_tool=2 file_set_compile_pool=2
+                                       target_compile_pool=4 target_link_pool=1)
+
+enable_language(C)
+
+set_property(SOURCE hello.c PROPERTY JOB_POOL_COMPILE "source_file_compile_pool")
+
+add_executable(hello)
+target_sources(hello PRIVATE FILE_SET SOURCES FILES hello.c)
+
+set_property(FILE_SET SOURCES TARGET hello PROPERTY JOB_POOL_COMPILE "file_set_compile_pool")
+
+set_property(TARGET hello PROPERTY JOB_POOL_COMPILE "target_compile_pool")
+set_property(TARGET hello PROPERTY JOB_POOL_LINK "target_link_pool")
+
+include(CheckNoPrefixSubDir.cmake)
diff --git a/Tests/RunCMake/Ninja/RunCMakeTest.cmake b/Tests/RunCMake/Ninja/RunCMakeTest.cmake
index 32eda9d..65202f7 100644
--- a/Tests/RunCMake/Ninja/RunCMakeTest.cmake
+++ b/Tests/RunCMake/Ninja/RunCMakeTest.cmake
@@ -121,6 +121,7 @@
 run_cmake_with_options(CustomCommandDepfileAsByproduct -DCMAKE_BUILD_TYPE=Debug)
 run_cmake(CustomCommandJobPool)
 run_cmake(SourceFileJobPool)
+run_cmake(FileSetJobPool)
 run_cmake(JobPoolUsesTerminal)
 
 run_cmake(RspFileC)