Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] MSBuildDeps | Configuration and Platform keys for property sheets #17076

Merged
merged 4 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The None, None case could have been simplified, as this would already be covered by other tests.
But no prob, once it is done, we can keep it!

],
)
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