From 1588470bc764a75f66a67e1ee979f4145e35015a Mon Sep 17 00:00:00 2001
From: Davy Durham <ddurham@bomgar.com>
Date: Thu, 17 Mar 2016 16:12:02 -0400
Subject: [PATCH 1/4] Add VS_STARTUP_PROJECT directory property (#15578)

---
 Help/manual/cmake-properties.7.rst         |  1 +
 Help/prop_dir/VS_STARTUP_PROJECT.rst       | 12 ++++++++++++
 Source/cmGlobalVisualStudio71Generator.cxx |  2 +-
 Source/cmGlobalVisualStudio8Generator.cxx  |  2 +-
 Source/cmGlobalVisualStudioGenerator.cxx   | 22 ++++++++++++++++++++++
 Source/cmGlobalVisualStudioGenerator.h     |  2 ++
 Source/cmVisualStudio10TargetGenerator.cxx |  2 +-
 7 files changed, 40 insertions(+), 3 deletions(-)
 create mode 100644 Help/prop_dir/VS_STARTUP_PROJECT.rst

diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst
index fbde4eb..dadbc6e 100644
--- a/Help/manual/cmake-properties.7.rst
+++ b/Help/manual/cmake-properties.7.rst
@@ -76,6 +76,7 @@ Properties on Directories
    /prop_dir/VARIABLES
    /prop_dir/VS_GLOBAL_SECTION_POST_section
    /prop_dir/VS_GLOBAL_SECTION_PRE_section
+   /prop_dir/VS_STARTUP_PROJECT
 
 .. _`Target Properties`:
 
diff --git a/Help/prop_dir/VS_STARTUP_PROJECT.rst b/Help/prop_dir/VS_STARTUP_PROJECT.rst
new file mode 100644
index 0000000..edd4832
--- /dev/null
+++ b/Help/prop_dir/VS_STARTUP_PROJECT.rst
@@ -0,0 +1,12 @@
+VS_STARTUP_PROJECT
+------------------
+
+Specify the default startup project in a Visual Studio solution.
+
+The property must be set to the name of an existing target.  This
+will cause that project to be listed first in the generated solution
+file causing Visual Studio to make it the startup project if the
+solution has never been opened before.
+
+If this property is not specified, then the "ALL_BUILD" project
+will be the default.
diff --git a/Source/cmGlobalVisualStudio71Generator.cxx b/Source/cmGlobalVisualStudio71Generator.cxx
index 8227b82..0600e6d 100644
--- a/Source/cmGlobalVisualStudio71Generator.cxx
+++ b/Source/cmGlobalVisualStudio71Generator.cxx
@@ -94,7 +94,7 @@ void cmGlobalVisualStudio71Generator
   TargetDependSet projectTargets;
   TargetDependSet originalTargets;
   this->GetTargetSets(projectTargets, originalTargets, root, generators);
-  OrderedTargetDependSet orderedProjectTargets(projectTargets, "ALL_BUILD");
+  OrderedTargetDependSet orderedProjectTargets(projectTargets, this->GetStartupProjectName(root));
 
   this->WriteTargetsToSolution(fout, root, orderedProjectTargets);
 
diff --git a/Source/cmGlobalVisualStudio8Generator.cxx b/Source/cmGlobalVisualStudio8Generator.cxx
index 3abff6c..43a233d 100644
--- a/Source/cmGlobalVisualStudio8Generator.cxx
+++ b/Source/cmGlobalVisualStudio8Generator.cxx
@@ -450,7 +450,7 @@ void cmGlobalVisualStudio8Generator::WriteProjectDepends(
         cmGeneratorTarget const* gt)
 {
   TargetDependSet const& unordered = this->GetTargetDirectDepends(gt);
-  OrderedTargetDependSet depends(unordered, std::string());
+  OrderedTargetDependSet depends(unordered, this->GetStartupProjectName(NULL));
   for(OrderedTargetDependSet::const_iterator i = depends.begin();
       i != depends.end(); ++i)
     {
diff --git a/Source/cmGlobalVisualStudioGenerator.cxx b/Source/cmGlobalVisualStudioGenerator.cxx
index 00bb511..acc919f 100644
--- a/Source/cmGlobalVisualStudioGenerator.cxx
+++ b/Source/cmGlobalVisualStudioGenerator.cxx
@@ -519,6 +519,22 @@ cmGlobalVisualStudioGenerator::GetUtilityDepend(
 }
 
 //----------------------------------------------------------------------------
+std::string
+cmGlobalVisualStudioGenerator::GetStartupProjectName(const cmLocalGenerator* root) const
+{
+  if (root) {
+    const char* n = root->GetMakefile()->GetProperty("VS_STARTUP_PROJECT");
+      if (n && strcmp(n, "") != 0) {
+        // should we verify that this target even exists or report error?
+        return n;
+      }
+  }
+
+  // default, if not specified
+  return this->GetAllTargetName();
+}
+
+//----------------------------------------------------------------------------
 #include <windows.h>
 
 //----------------------------------------------------------------------------
@@ -853,6 +869,12 @@ cmGlobalVisualStudioGenerator::TargetIsFortranOnly(cmGeneratorTarget const* gt)
 }
 
 //----------------------------------------------------------------------------
+cmGlobalVisualStudioGenerator::TargetCompare
+::TargetCompare(const std::string& FirstProject_)
+: FirstProject(FirstProject_)
+{
+}
+
 bool
 cmGlobalVisualStudioGenerator::TargetCompare
 ::operator()(cmGeneratorTarget const* l, cmGeneratorTarget const* r) const
diff --git a/Source/cmGlobalVisualStudioGenerator.h b/Source/cmGlobalVisualStudioGenerator.h
index ac9111e..812fe61 100644
--- a/Source/cmGlobalVisualStudioGenerator.h
+++ b/Source/cmGlobalVisualStudioGenerator.h
@@ -106,6 +106,8 @@ public:
 
   void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const;
 
+  std::string GetStartupProjectName(const cmLocalGenerator* root) const;
+
   void AddSymbolExportCommand(
     cmGeneratorTarget*, std::vector<cmCustomCommand>& commands,
     std::string const& configName);
diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx
index 31873b5..32657b6 100644
--- a/Source/cmVisualStudio10TargetGenerator.cxx
+++ b/Source/cmVisualStudio10TargetGenerator.cxx
@@ -2962,7 +2962,7 @@ void cmVisualStudio10TargetGenerator::WriteProjectReferences()
     = this->GlobalGenerator->GetTargetDirectDepends(this->GeneratorTarget);
   typedef cmGlobalVisualStudioGenerator::OrderedTargetDependSet
     OrderedTargetDependSet;
-  OrderedTargetDependSet depends(unordered, CMAKE_CHECK_BUILD_SYSTEM_TARGET);
+  OrderedTargetDependSet depends(unordered, this->GlobalGenerator->GetStartupProjectName(NULL));
   this->WriteString("<ItemGroup>\n", 1);
   for( OrderedTargetDependSet::const_iterator i = depends.begin();
        i != depends.end(); ++i)
-- 
2.7.2.windows.1

