Skip to content

Commit

Permalink
refactor around components (#13993)
Browse files Browse the repository at this point in the history
Changelog: Omit
Docs: Omit

Some work previous to #13758
  • Loading branch information
AbrilRBS authored May 30, 2023
2 parents 460a632 + bbe5724 commit 9a00e9f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 42 deletions.
9 changes: 0 additions & 9 deletions conan/tools/cmake/cmakedeps/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,11 @@ def suffix(self):
return ""
return self.cmakedeps.build_context_suffix.get(self.conanfile.ref.name, "")

@property
def build_modules_activated(self):
if self.require.build:
return self.conanfile.ref.name in self.cmakedeps.build_context_build_modules
else:
return self.conanfile.ref.name not in self.cmakedeps.build_context_build_modules

def render(self):
try:
context = self.context
except Exception as e:
raise ConanException("error generating context for '{}': {}".format(self.conanfile, e))
if context is None:
return

# Cache the template instance as a class attribute to greatly speed up the rendering
# NOTE: this assumes that self.template always returns the same string
Expand Down
25 changes: 16 additions & 9 deletions conan/tools/cmake/cmakedeps/templates/target_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ def filename(self):
data_fname += "-data.cmake"
return data_fname

@property
def _build_modules_activated(self):
if self.require.build:
return self.conanfile.ref.name in self.cmakedeps.build_context_build_modules
else:
return self.conanfile.ref.name not in self.cmakedeps.build_context_build_modules

@property
def context(self):
global_cpp = self._get_global_cpp_cmake()
if not self.build_modules_activated:
if not self._build_modules_activated:
global_cpp.build_modules_paths = ""

components = self._get_required_components_cpp()
Expand Down Expand Up @@ -195,22 +202,22 @@ def _get_required_components_cpp(self):
self.is_host_windows)

public_comp_deps = []
for require in comp.requires:
if "::" in require: # Points to a component of a different package
pkg, cmp_name = require.split("::")
for required_pkg, required_comp in comp.parsed_requires():
if required_pkg is not None: # Points to a component of a different package
try: # Make sure the declared dependency is at least in the recipe requires
self.conanfile.dependencies[pkg]
self.conanfile.dependencies[required_pkg]
except KeyError:
raise ConanException(f"{self.conanfile}: component '{comp_name}' required "
f"'{require}', but '{pkg}' is not a direct dependency")
f"'{required_pkg}::{required_comp}', "
f"but '{required_pkg}' is not a direct dependency")
try:
req = transitive_requires[pkg]
req = transitive_requires[required_pkg]
except KeyError: # The transitive dep might have been skipped
pass
else:
public_comp_deps.append(self.get_component_alias(req, cmp_name))
public_comp_deps.append(self.get_component_alias(req, required_comp))
else: # Points to a component of same package
public_comp_deps.append(self.get_component_alias(self.conanfile, require))
public_comp_deps.append(self.get_component_alias(self.conanfile, required_comp))
deps_cpp_cmake.public_deps = " ".join(public_comp_deps)
component_target_name = self.get_component_alias(self.conanfile, comp_name)
ret.append((component_target_name, deps_cpp_cmake))
Expand Down
6 changes: 3 additions & 3 deletions conan/tools/gnu/autotoolsdeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class AutotoolsDeps:
def __init__(self, conanfile):
self._conanfile = conanfile
self._environment = None
self._ordered_deps = []
self._ordered_deps = None

@property
def ordered_deps(self):
if not self._ordered_deps:
if self._ordered_deps is None:
deps = self._conanfile.dependencies.host.topological_sort
self._ordered_deps = [dep for dep in reversed(deps.values())]
return self._ordered_deps
Expand Down Expand Up @@ -55,7 +55,7 @@ def environment(self):
ldflags.extend(flags.framework_paths)
ldflags.extend(flags.lib_paths)

## set the rpath in Macos so that the library are found in the configure step
# set the rpath in Macos so that the library are found in the configure step
if self._conanfile.settings.get_safe("os") == "Macos":
ldflags.extend(self._rpaths_flags())

Expand Down
12 changes: 5 additions & 7 deletions conan/tools/microsoft/msbuilddeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,19 @@ def _package_props_files(self, require, dep, build=False):
if dep.cpp_info.has_components:
pkg_aggregated_content = None
for comp_name, comp_info in dep.cpp_info.components.items():
if comp_name is None:
continue
full_comp_name = "{}_{}".format(dep_name, self._get_valid_xml_format(comp_name))
vars_filename = "conan_%s_vars%s.props" % (full_comp_name, conf_name)
activate_filename = "conan_%s%s.props" % (full_comp_name, conf_name)
comp_filename = "conan_%s.props" % full_comp_name
pkg_filename = "conan_%s.props" % dep_name

public_deps = [] # To store the xml dependencies/file names
for r in comp_info.requires:
if "::" in r: # Points to a component of a different package
pkg, cmp_name = r.split("::")
public_deps.append(pkg if pkg == cmp_name else "{}_{}".format(pkg, cmp_name))
for required_pkg, required_comp in comp_info.parsed_requires():
if required_pkg is not None: # Points to a component of a different package
public_deps.append(required_pkg if required_pkg == required_comp
else "{}_{}".format(required_pkg, required_comp))
else: # Points to a component of same package
public_deps.append("{}_{}".format(dep_name, r))
public_deps.append("{}_{}".format(dep_name, required_comp))
public_deps = [self._get_valid_xml_format(d) for d in public_deps]
result[vars_filename] = self._vars_props_file(require, dep, full_comp_name,
comp_info, build=build)
Expand Down
30 changes: 16 additions & 14 deletions conans/model/build_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class _Component:

def __init__(self, set_defaults=False):
# ###### PROPERTIES
self._generator_properties = None
self._properties = None

# ###### DIRECTORIES
self._includedirs = None # Ordered list of include paths
Expand Down Expand Up @@ -109,7 +109,7 @@ def serialize(self):
"objects": self._objects,
"sysroot": self._sysroot,
"requires": self._requires,
"properties": self._generator_properties
"properties": self._properties
}

@property
Expand Down Expand Up @@ -321,15 +321,15 @@ def required_component_names(self):
return [r for r in self.requires if "::" not in r]

def set_property(self, property_name, value):
if self._generator_properties is None:
self._generator_properties = {}
self._generator_properties[property_name] = value
if self._properties is None:
self._properties = {}
self._properties[property_name] = value

def get_property(self, property_name):
if self._generator_properties is None:
if self._properties is None:
return None
try:
return self._generator_properties[property_name]
return self._properties[property_name]
except KeyError:
pass

Expand Down Expand Up @@ -362,16 +362,16 @@ def merge_list(o, d):
current_values = self.get_init("requires", [])
merge_list(other.requires, current_values)

if other._generator_properties:
current_values = self.get_init("_generator_properties", {})
current_values.update(other._generator_properties)
if other._properties:
current_values = self.get_init("_properties", {})
current_values.update(other._properties)

def set_relative_base_folder(self, folder):
for varname in _DIRS_VAR_NAMES:
origin = getattr(self, varname)
if origin is not None:
origin[:] = [os.path.join(folder, el) for el in origin]
properties = self._generator_properties
properties = self._properties
if properties is not None:
modules = properties.get("cmake_build_modules") # Only this prop at this moment
if modules is not None:
Expand All @@ -387,19 +387,21 @@ def relocate(el):
origin = getattr(self, varname)
if origin is not None:
origin[:] = [relocate(f) for f in origin]
properties = self._generator_properties
properties = self._properties
if properties is not None:
modules = properties.get("cmake_build_modules") # Only this prop at this moment
if modules is not None:
assert isinstance(modules, list), "cmake_build_modules must be a list"
properties["cmake_build_modules"] = [relocate(f) for f in modules]

def parsed_requires(self):
return [r.split("::", 1) if "::" in r else (None, r) for r in self.requires]


class CppInfo:

def __init__(self, set_defaults=False):
self.components = defaultdict(lambda: _Component(set_defaults))
# Main package is a component with None key
self._package = _Component(set_defaults)
self._aggregated = None # A _NewComponent object with all the components aggregated

Expand Down Expand Up @@ -498,7 +500,7 @@ def aggregated_components(self):
# component like "cmake_target_name" describing the target name FOR THE component
# not the namespace.
# FIXME: What to do about sysroot?
result._generator_properties = copy.copy(self._package._generator_properties)
result._properties = copy.copy(self._package._properties)
else:
result = copy.copy(self._package)
self._aggregated = CppInfo()
Expand Down

0 comments on commit 9a00e9f

Please sign in to comment.