Skip to content

Commit

Permalink
[xbuild] Provide "user_info" from every context (#7266)
Browse files Browse the repository at this point in the history
* provide user_info from both contexts

* remove extra blank

* sort ouptput

* do not add 'user_info_host'
  • Loading branch information
jgsogo authored Jun 30, 2020
1 parent 023b881 commit a312aef
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
7 changes: 7 additions & 0 deletions conans/client/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from conans.model.graph_lock import GraphLockNode
from conans.model.info import PACKAGE_ID_UNKNOWN
from conans.model.ref import PackageReference
from conans.model.user_info import DepsUserInfo
from conans.model.user_info import UserInfo
from conans.paths import BUILD_INFO, CONANINFO, RUN_LOG_NAME
from conans.util.conan_v2_mode import CONAN_V2_MODE_ENVVAR
Expand Down Expand Up @@ -532,6 +533,10 @@ def _propagate_info(self, node, using_build_profile, fixed_package_id):
if it.require.build_require_context == CONTEXT_HOST:
br_host.extend(it.dst.transitive_closure.values())

# Initialize some members if we are using different contexts
if using_build_profile:
conan_file.user_info_build = DepsUserInfo()

for n in node_order:
if n not in transitive:
conan_file.output.info("Applying build-requirement: %s" % str(n.ref))
Expand All @@ -542,8 +547,10 @@ def _propagate_info(self, node, using_build_profile, fixed_package_id):
conan_file.deps_env_info.update(n.conanfile.env_info, n.ref.name)
else:
if n in transitive or n in br_host:
conan_file.deps_user_info[n.ref.name] = n.conanfile.user_info
conan_file.deps_cpp_info.add(n.ref.name, n.conanfile._conan_dep_cpp_info)
else:
conan_file.user_info_build[n.ref.name] = n.conanfile.user_info
env_info = EnvInfo()
env_info._values_ = n.conanfile.env_info._values_.copy()
# Add cpp_info.bin_paths/lib_paths to env_info (it is needed for runtime)
Expand Down
2 changes: 1 addition & 1 deletion conans/model/conan_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def initialize(self, settings, env):

# user declared variables
self.user_info = None
# Keys are the package names, and the values a dict with the vars
# Keys are the package names (only 'host' if different contexts)
self.deps_user_info = DepsUserInfo()

# user specified env variables
Expand Down
83 changes: 83 additions & 0 deletions conans/test/functional/cross_building/user_info_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import textwrap
import unittest

from conans.test.utils.tools import TestClient


class UserInfoTestCase(unittest.TestCase):
""" When using several contexts (xbuild feature) the user information from each of the
contexts should be accessible, Conan will provide 'deps_user_info' attribute to access
information from the 'host' context (like it is doing with 'deps_cpp_info'), but
information from other contexts will be accessible via attributes 'user_info_<context>'.
"""

library = textwrap.dedent("""
from conans import ConanFile
class Library(ConanFile):
name = "library"
settings = "os"
def package_info(self):
self.user_info.DATA = "{}-{}".format(self.name, self.settings.os)
""")

br = textwrap.dedent("""
from conans import ConanFile
class BuildRequires(ConanFile):
settings = "os"
requires = "library/1.0"
def package_info(self):
self.user_info.DATA = "{}-{}".format(self.name, self.settings.os)
""")

app = textwrap.dedent("""
from conans import ConanFile
class Library(ConanFile):
name = "app"
settings = "os"
def requirements(self):
self.requires("library/1.0")
def build_requirements(self):
self.build_requires("br_build/1.0")
self.build_requires("br_host/1.0", force_host_context=True)
def build(self):
_info = self.output.info
_info("[deps] {}".format(', '.join(sorted(self.deps_user_info.keys()))))
_info("[deps] library.DATA={}".format(self.deps_user_info["library"].DATA))
_info("[deps] br_host.DATA={}".format(self.deps_user_info["br_host"].DATA))
_info("[build] {}".format(', '.join(sorted(self.user_info_build.keys()))))
_info("[build] library.DATA={}".format(self.user_info_build["library"].DATA))
_info("[build] br_build.DATA={}".format(self.user_info_build["br_build"].DATA))
""")

def test_user_info_from_requirements(self):
t = TestClient()
t.save({'library.py': self.library,
'build_requires.py': self.br,
'app.py': self.app,
'host': '[settings]\nos=Windows',
'build': '[settings]\nos=Linux', })
t.run("create library.py library/1.0@ --profile=host")
t.run("create library.py library/1.0@ --profile=build")
t.run("create build_requires.py br_host/1.0@ --profile=host")
t.run("create build_requires.py br_build/1.0@ --profile=build")
t.run("create app.py app/1.0@ --profile:host=host --profile:build=build")

# Check information from the host context (using deps_user_info attribute)
self.assertIn("app/1.0: [deps] br_host, library", t.out)
self.assertIn("app/1.0: [deps] library.DATA=library-Windows", t.out)
self.assertIn("app/1.0: [deps] br_host.DATA=br_host-Windows", t.out)

# Check information from the build context (using user_info_build attribute)
self.assertIn("app/1.0: [build] br_build, library", t.out)
self.assertIn("app/1.0: [build] library.DATA=library-Linux", t.out)
self.assertIn("app/1.0: [build] br_build.DATA=br_build-Linux", t.out)

0 comments on commit a312aef

Please sign in to comment.