VS: Add option to import .props in .vcxproj.filters files

Adds new target property VS_FILTER_PROPS

Fixes: #25948
stage/master/nightly/2024/05/02
halx99 2024-05-02 00:02:06 +08:00
parent 01e138a7b7
commit 56a96d1f1f
7 changed files with 60 additions and 24 deletions

View File

@ -450,6 +450,7 @@ syn keyword cmakeProperty contained
\ VS_STARTUP_PROJECT
\ VS_TOOL_OVERRIDE
\ VS_USER_PROPS
\ VS_FILTER_PROPS
\ VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION
\ VS_WINRT_COMPONENT
\ VS_WINRT_EXTENSIONS

View File

@ -448,6 +448,7 @@ Properties on Targets
/prop_tgt/VS_SOURCE_SETTINGS_tool
/prop_tgt/VS_USE_DEBUG_LIBRARIES
/prop_tgt/VS_USER_PROPS
/prop_tgt/VS_FILTER_PROPS
/prop_tgt/VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION
/prop_tgt/VS_WINRT_COMPONENT
/prop_tgt/VS_WINRT_REFERENCES

View File

@ -0,0 +1,10 @@
VS_FILTER_PROPS
---------------
.. versionadded:: 3.30
Sets the filter props file to be included in the visual studio
C++ project filter file.
The ``*.filter.props`` files can be used for Visual Studio wide
configuration which is independent from cmake.

View File

@ -0,0 +1,6 @@
vs-filter-props
---------------
* A :prop_tgt:`VS_FILTER_PROPS` target property was added to tell
:ref:`Visual Studio Generators` for VS 2010 and above to use a
custom MSBuild filter ``.props`` file.

View File

@ -2074,6 +2074,18 @@ void cmVisualStudio10TargetGenerator::WriteGroups()
"gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms");
}
}
{
if (cmValue p = this->GeneratorTarget->GetProperty("VS_FILTER_PROPS")) {
auto props = *p;
if (!props.empty()) {
ConvertToWindowsSlash(props);
Elem(e0, "Import")
.Attribute("Project", props)
.Attribute("Condition", cmStrCat("exists('", props, "')"))
.Attribute("Label", "LocalAppDataPlatform");
}
}
}
}
fout << '\n';

View File

@ -1,25 +1,30 @@
set(vcProjectFile "${RunCMake_TEST_BINARY_DIR}/foo.vcxproj")
if(NOT EXISTS "${vcProjectFile}")
set(RunCMake_TEST_FAILED "Project file ${vcProjectFile} does not exist.")
return()
endif()
set(importFound FALSE)
set(props_file "${RunCMake_SOURCE_DIR}/my.props")
file(TO_NATIVE_PATH "${props_file}" check_file)
file(STRINGS "${vcProjectFile}" lines)
foreach(line IN LISTS lines)
if(line MATCHES "^ *<Import Project=\"([^\"]+)\".*Label=\"([^\"]+)\".*$")
if("${CMAKE_MATCH_1}" STREQUAL "${check_file}" AND
"${CMAKE_MATCH_2}" STREQUAL "LocalAppDataPlatform")
message(STATUS "foo.vcxproj is importing ${check_file}")
set(importFound TRUE)
endif()
macro(check_custom_prop suffix)
set(vcProjectFile "${RunCMake_TEST_BINARY_DIR}/foo.vcxproj${suffix}")
if(NOT EXISTS "${vcProjectFile}")
set(RunCMake_TEST_FAILED "Project file ${vcProjectFile} does not exist.")
return()
endif()
endforeach()
if(NOT importFound)
set(RunCMake_TEST_FAILED "Import of custom .props file not found.")
return()
endif()
set(importFound FALSE)
set(props_file "${RunCMake_SOURCE_DIR}/my.props")
file(TO_NATIVE_PATH "${props_file}" check_file)
file(STRINGS "${vcProjectFile}" lines)
foreach(line IN LISTS lines)
if(line MATCHES "^ *<Import Project=\"([^\"]+)\".*Label=\"([^\"]+)\".*$")
if("${CMAKE_MATCH_1}" STREQUAL "${check_file}" AND
"${CMAKE_MATCH_2}" STREQUAL "LocalAppDataPlatform")
message(STATUS "foo.vcxproj${suffix} is importing ${check_file}")
set(importFound TRUE)
endif()
endif()
endforeach()
if(NOT importFound)
set(RunCMake_TEST_FAILED "Import of custom .props file not found.")
return()
endif()
endmacro()
check_custom_prop("")
check_custom_prop(".filters")

View File

@ -4,4 +4,5 @@ add_library(foo foo.cpp)
set(props_file "${CMAKE_CURRENT_SOURCE_DIR}/my.props")
set_target_properties(foo PROPERTIES
VS_USER_PROPS "${props_file}")
VS_USER_PROPS "${props_file}"
VS_FILTER_PROPS "${props_file}")