Skip to content

Commit

Permalink
#16427. Make MSBuildDeps generation with deployer relocatable (#16441)
Browse files Browse the repository at this point in the history
* #16427. Make ConanlibXyzRoot variable relative to the generated props file when deployer is used.

Copied the logic from conan\tools\cmake\cmakedeps\templates\target_data.py

* Imports in lexicographical order...

* #16427. Added test that MSBuildDeps generates relative paths in props with deployer, and absolute paths without deployer.

Note: this test does not invoke msbuild, and thus does not depend on MSVC / Windows.

* Disabled test on Windows, converted to parametrized test.
  • Loading branch information
stgatilov authored Jun 9, 2024
1 parent 1a75618 commit 994f271
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion conan/tools/microsoft/msbuilddeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from conan.internal import check_duplicated_generator
from conan.errors import ConanException
from conans.client.generators import relativize_path
from conans.model.dependencies import get_transitive_requires
from conans.util.files import load, save

Expand Down Expand Up @@ -178,6 +179,9 @@ def join_paths(paths):

root_folder = dep.recipe_folder if dep.package_folder is None else dep.package_folder
root_folder = escape_path(root_folder)
# Make the root_folder relative to the generated conan_vars_xxx.props file
relative_root_folder = relativize_path(root_folder, self._conanfile,
"$(MSBuildThisFileDirectory)")

bin_dirs = join_paths(cpp_info.bindirs)
res_dirs = join_paths(cpp_info.resdirs)
Expand Down Expand Up @@ -205,7 +209,7 @@ def join_paths(paths):

fields = {
'name': name,
'root_folder': root_folder,
'root_folder': relative_root_folder,
'bin_dirs': bin_dirs,
'res_dirs': res_dirs,
'include_dirs': include_dirs,
Expand Down
51 changes: 51 additions & 0 deletions test/integration/toolchains/microsoft/test_msbuilddeps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import glob
import os
import platform
import textwrap
Expand Down Expand Up @@ -122,3 +123,53 @@ def package_info(self):
assert "conan_liba" not in libb
libb = c.load("app/conan_libb_mycomp_vars_release_x64.props")
assert "conan_liba" not in libb


@pytest.mark.skipif(platform.system() != "Windows", reason="MSBuildDeps broken with POSIX paths")
@pytest.mark.parametrize("withdepl", [False, True])
def test_msbuilddeps_relocatable(withdepl):
c = TestClient()
c.save({
"libh/conanfile.py": GenConanfile("libh", "0.1")
.with_package_type("header-library"),
"libs/conanfile.py": GenConanfile("libs", "0.2")
.with_package_type("static-library")
.with_requires("libh/0.1"),
"libd/conanfile.py": GenConanfile("libd", "0.3")
.with_package_type("shared-library"),
"app/conanfile.py": GenConanfile()
.with_requires("libh/0.1")
.with_requires("libs/0.2")
.with_requires("libd/0.3")
.with_settings("arch", "build_type"),
})

c.run("create libh")
c.run("create libs")
c.run("create libd")
c.run("install app -g MSBuildDeps" + (" -d full_deploy" if withdepl else ""))

for dep in ["libh", "libs", "libd"]:
text = c.load(f"app/conan_{dep}_vars_release_x64.props")
marker = f"Conan{dep}RootFolder"
value = text.split(f"<{marker}>")[1].split(f"</{marker}>")[0]
if withdepl:
# path should be relative, since artifacts are moved along with project
prefix = '$(MSBuildThisFileDirectory)/'
assert value.startswith(prefix)
tail = value[len(prefix):]
assert not os.path.isabs(tail)
else:
# path should be absolute, since conan cache does not move with project
assert os.path.isabs(value)
assert '$(' not in value

if withdepl:
# extra checks: no absolute paths allowed anywhere in props
propsfiles = glob.glob(os.path.join(c.current_folder, "app/*.props"))
assert len(propsfiles) > 0
for fn in propsfiles:
text = c.load(fn)
text = text.replace('\\', '/')
dir = c.current_folder.replace('\\', '/')
assert dir not in text

0 comments on commit 994f271

Please sign in to comment.