From c48d2d8480402a75e1509b19c145f004126dda10 Mon Sep 17 00:00:00 2001
From: Brad King <brad.king@kitware.com>
Date: Thu, 3 Jun 2021 10:36:06 -0400
Subject: [PATCH] VS: Place per-source preprocessor definitions after
 target-wide ones

When the VS 2010+ generators were first implemented in commit 7491f52992
(ENH: first pass at VS 10, can bootstrap CMake, but many tests still
fail, 2009-06-25, v2.8.0~546), the per-source preprocessor definitions
were placed before target-wide preprocessor definitions for consistency
with the behavior of VS 9 2008 and below.  However, those generators
are not used much anymore.  Instead prefer consistency with the order
used by the Ninja and Makefile generators.
---
 Help/release/dev/compile-options-order.rst |  7 +++++++
 Source/cmVisualStudioGeneratorOptions.cxx  | 10 ++++------
 2 files changed, 11 insertions(+), 6 deletions(-)
 create mode 100644 Help/release/dev/compile-options-order.rst

diff --git a/Help/release/dev/compile-options-order.rst b/Help/release/dev/compile-options-order.rst
new file mode 100644
index 0000000000..2e182cdf06
--- /dev/null
+++ b/Help/release/dev/compile-options-order.rst
@@ -0,0 +1,7 @@
+compile-options-order
+---------------------
+
+* The :ref:`Visual Studio Generators` for VS 2010 and above now place
+  per-source preprocessor definitions after target-wide preprocssor
+  definitions.  This makes VS consistent with the :ref:`Ninja Generators`
+  and the :ref:`Makefile Generators`.
diff --git a/Source/cmVisualStudioGeneratorOptions.cxx b/Source/cmVisualStudioGeneratorOptions.cxx
index 937b4ce4ac..505d58ac3a 100644
--- a/Source/cmVisualStudioGeneratorOptions.cxx
+++ b/Source/cmVisualStudioGeneratorOptions.cxx
@@ -418,7 +418,9 @@ void cmVisualStudioGeneratorOptions::OutputPreprocessorDefinitions(
   }
 
   std::ostringstream oss;
-  const char* sep = "";
+  if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
+    oss << "%(" << tag << ")";
+  }
   std::vector<std::string>::const_iterator de =
     cmRemoveDuplicates(this->Defines);
   for (std::string const& di : cmMakeRange(this->Defines.cbegin(), de)) {
@@ -437,11 +439,7 @@ void cmVisualStudioGeneratorOptions::OutputPreprocessorDefinitions(
       }
     }
     // Store the flag in the project file.
-    oss << sep << define;
-    sep = ";";
-  }
-  if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
-    oss << ";%(" << tag << ")";
+    oss << ';' << define;
   }
 
   this->OutputFlag(fout, indent, tag, oss.str());
-- 
GitLab