Skip to content

Commit

Permalink
[feature] MSBuildDeps | Configuration and Platform keys for property …
Browse files Browse the repository at this point in the history
…sheets (#17076)

* Added configuration and platform keys in MSBuildDeps

* Reverted filename key. Could not find a use case. Also removed "Configuration" and "Platform" from function to avoid confusion as it was useless.

* Added test for configuration key and platform key.
  • Loading branch information
vermz99 authored Oct 14, 2024
1 parent 890f000 commit f9ced79
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
14 changes: 9 additions & 5 deletions conan/tools/microsoft/msbuilddeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ def __init__(self, conanfile):
self._conanfile = conanfile
#: Defines the build type. By default, ``settings.build_type``.
self.configuration = conanfile.settings.build_type
#: Defines the configuration key used to conditionate on which property sheet to import.
self.configuration_key = "Configuration"
# TODO: This platform is not exactly the same as ``msbuild_arch``, because it differs
# in x86=>Win32
#: Platform name, e.g., ``Win32`` if ``settings.arch == "x86"``.
self.platform = {'x86': 'Win32',
'x86_64': 'x64',
'armv7': 'ARM',
'armv8': 'ARM64'}.get(str(conanfile.settings.arch))
#: Defines the platform key used to conditionate on which property sheet to import.
self.platform_key = "Platform"
ca_exclude = "tools.microsoft.msbuilddeps:exclude_code_analysis"
#: List of packages names patterns to add Visual Studio ``CAExcludePath`` property
#: to each match as part of its ``conan_[DEP]_[CONFIG].props``. By default, value given by
Expand All @@ -124,14 +128,14 @@ def generate(self):
save(generator_file, content)

def _config_filename(self):
props = [("Configuration", self.configuration),
("Platform", self.platform)]
name = "".join("_%s" % v for _, v in props)
props = [self.configuration,
self.platform]
name = "".join("_%s" % v for v in props)
return name.lower()

def _condition(self):
props = [("Configuration", self.configuration),
("Platform", self.platform)]
props = [(self.configuration_key, self.configuration),
(self.platform_key, self.platform)]
condition = " And ".join("'$(%s)' == '%s'" % (k, v) for k, v in props)
return condition

Expand Down
53 changes: 52 additions & 1 deletion test/integration/toolchains/microsoft/test_msbuilddeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,57 @@ def test_msbuilddeps_maps_architecture_to_platform(arch, exp_platform):
assert expected_import in toolchain


@pytest.mark.parametrize(
"build_type,arch,configuration,exp_platform",
[
("Release", "x86", "Release - Test", "Win32"),
("Debug", "x86_64", "Debug - Test", "x64"),
],
)
@pytest.mark.parametrize(
"config_key,platform_key",
[
("GlobalConfiguration", "GlobalPlatform"),
(None, None),
],
)
def test_msbuilddeps_import_condition(build_type, arch, configuration, exp_platform, config_key, platform_key):
client = TestClient()
app = textwrap.dedent(f"""
from conan import ConanFile
from conan.tools.microsoft import MSBuildDeps
class App(ConanFile):
requires = ("lib/0.1")
settings = "arch", "build_type"
options = {{"configuration": ["ANY"],
"platform": ["Win32", "x64"]}}
def generate(self):
ms = MSBuildDeps(self)
ms.configuration_key = "{config_key}"
ms.configuration = self.options.configuration
ms.platform_key = "{platform_key}"
ms.platform = self.options.platform
ms.generate()
""")

# Remove custom set of keys to test default values
app = app.replace(f'ms.configuration_key = "{config_key}"', "") if config_key is None else app
app = app.replace(f'ms.platform_key = "{platform_key}"', "") if platform_key is None else app
config_key_expected = "Configuration" if config_key is None else config_key
platform_key_expected = "Platform" if platform_key is None else platform_key

client.save({"app/conanfile.py": app,
"lib/conanfile.py": GenConanfile("lib", "0.1").with_package_type("header-library")})
client.run("create lib")
client.run(f'install app -s build_type={build_type} -s arch={arch} -o *:platform={exp_platform} -o *:configuration="{configuration}"')

assert os.path.exists(os.path.join(client.current_folder, "app", "conan_lib.props"))
dep = client.load(os.path.join(client.current_folder, "app", "conan_lib.props"))
expected_import = f"""<Import Condition="'$({config_key_expected})' == '{configuration}' And '$({platform_key_expected})' == '{exp_platform}'" Project="conan_lib_{configuration.lower()}_{exp_platform.lower()}.props"/>"""
assert expected_import in dep


def test_msbuilddeps_format_names():
c = TestClient()
conanfile = textwrap.dedent("""
Expand Down Expand Up @@ -172,4 +223,4 @@ def test_msbuilddeps_relocatable(withdepl):
text = c.load(fn)
text = text.replace('\\', '/')
dir = c.current_folder.replace('\\', '/')
assert dir not in text
assert dir not in text

0 comments on commit f9ced79

Please sign in to comment.