From f069be054833f465ab469b534fddbc52c5c9913b Mon Sep 17 00:00:00 2001 From: Taylor Braun-Jones Date: Mon, 21 Mar 2016 16:01:20 -0400 Subject: [PATCH] VS: Fix default target support for targets nested inside a folder It's not actually the first target in a `.sln` file that is treated as the default startup project, but rather the first fully defined target. --- Source/cmGlobalVisualStudio71Generator.cxx | 19 ++++++++++++++++++- Tests/RunCMake/CMakeLists.txt | 5 ++++- Tests/RunCMake/VSSolution/RunCMakeTest.cmake | 4 ++++ .../StartupProjectUseFolders-check.cmake | 9 +++++++++ .../VSSolution/StartupProjectUseFolders.cmake | 3 +++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 Tests/RunCMake/VSSolution/StartupProjectUseFolders-check.cmake create mode 100644 Tests/RunCMake/VSSolution/StartupProjectUseFolders.cmake diff --git a/Source/cmGlobalVisualStudio71Generator.cxx b/Source/cmGlobalVisualStudio71Generator.cxx index f6796a543..7b51fdf49 100644 --- a/Source/cmGlobalVisualStudio71Generator.cxx +++ b/Source/cmGlobalVisualStudio71Generator.cxx @@ -97,14 +97,31 @@ void cmGlobalVisualStudio71Generator OrderedTargetDependSet orderedProjectTargets( projectTargets, this->GetStartupProjectName(root)); - this->WriteTargetsToSolution(fout, root, orderedProjectTargets); + // Generate the targets specification to a string. We will put this in + // the actual .sln file later. As a side effect, this method also + // populates the set of folders. + std::ostringstream targetsSlnString; + this->WriteTargetsToSolution(targetsSlnString, root, orderedProjectTargets); + // VS 7 does not support folders specified first. + if (this->GetVersion() <= VS71) + { + fout << targetsSlnString.str(); + } + + // Generate folder specification. bool useFolderProperty = this->UseFolderProperty(); if (useFolderProperty) { this->WriteFolders(fout); } + // Now write the actual target specification content. + if (this->GetVersion() > VS71) + { + fout << targetsSlnString.str(); + } + // Write out the configurations information for the solution fout << "Global\n"; // Write out the configurations for the solution diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index c7fe6493e..0207753da 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -234,7 +234,10 @@ endif() if("${CMAKE_GENERATOR}" MATCHES "Visual Studio") add_RunCMake_test(include_external_msproject) - add_RunCMake_test(VSSolution) + if("${CMAKE_GENERATOR}" MATCHES "Visual Studio ([789]|10)" AND NOT CMAKE_VS_DEVENV_COMMAND) + set(NO_USE_FOLDERS 1) + endif() + add_RunCMake_test(VSSolution -DNO_USE_FOLDERS=${NO_USE_FOLDERS}) endif() if("${CMAKE_GENERATOR}" MATCHES "Visual Studio ([^789]|[789][0-9])") diff --git a/Tests/RunCMake/VSSolution/RunCMakeTest.cmake b/Tests/RunCMake/VSSolution/RunCMakeTest.cmake index 8ae959878..afd74a1cc 100644 --- a/Tests/RunCMake/VSSolution/RunCMakeTest.cmake +++ b/Tests/RunCMake/VSSolution/RunCMakeTest.cmake @@ -10,3 +10,7 @@ run_cmake(Override1) run_cmake(Override2) run_cmake(StartupProject) run_cmake(StartupProjectMissing) + +if(RunCMake_GENERATOR MATCHES "Visual Studio ([^7]|[7][0-9])" AND NOT NO_USE_FOLDERS) + run_cmake(StartupProjectUseFolders) +endif() diff --git a/Tests/RunCMake/VSSolution/StartupProjectUseFolders-check.cmake b/Tests/RunCMake/VSSolution/StartupProjectUseFolders-check.cmake new file mode 100644 index 000000000..c0a545a27 --- /dev/null +++ b/Tests/RunCMake/VSSolution/StartupProjectUseFolders-check.cmake @@ -0,0 +1,9 @@ +getProjectNames(projects) +list(GET projects 0 first_project) +if(NOT first_project STREQUAL "CMakePredefinedTargets") + error("CMakePredefinedTargets is not the first project") +endif() +list(GET projects 1 second_project) +if(NOT second_project STREQUAL "TestStartup") + error("TestStartup does not immediately follow the CMakePredefinedTargets project") +endif() diff --git a/Tests/RunCMake/VSSolution/StartupProjectUseFolders.cmake b/Tests/RunCMake/VSSolution/StartupProjectUseFolders.cmake new file mode 100644 index 000000000..8e422a495 --- /dev/null +++ b/Tests/RunCMake/VSSolution/StartupProjectUseFolders.cmake @@ -0,0 +1,3 @@ +add_custom_target(TestStartup) +set_property(GLOBAL PROPERTY USE_FOLDERS ON) +set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT "TestStartup")