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

Add properties to MakeDeps generator #16613

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
22 changes: 21 additions & 1 deletion conan/tools/gnu/makedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import textwrap

from jinja2 import Template, StrictUndefined
from typing import Optional

from conan.internal import check_duplicated_generator
from conan.tools.files import save
Expand Down Expand Up @@ -67,6 +68,15 @@ def _makefy(name: str) -> str:
return re.sub(r'[^0-9A-Z_]', '_', name.upper())


def _makefy_properties(properties: Optional[dict]) -> dict:
"""
Convert property dictionary keys to Make-variable-friendly syntax
:param properties: The property dictionary to be converted (None is also accepted)
:return: Modified property dictionary with keys not including bad characters that are not parsed correctly
"""
return {_makefy(name): value for name, value in properties.items()} if properties else {}
vajdaz marked this conversation as resolved.
Show resolved Hide resolved


def _conan_prefix_flag(variable: str) -> str:
"""
Return a global flag to be used as prefix to any value in the makefile
Expand Down Expand Up @@ -131,6 +141,12 @@ def _jinja_format_list_values() -> str:
{%- endif -%}
{%- endmacro %}

{%- macro define_multiple_variable_value(var, values) -%}
{% for property_name, value in values.items() %}
{{ var }}_{{ property_name }} = {{ value }}
{% endfor %}
{%- endmacro %}

{%- macro define_variable_value(var, values) -%}
{%- if values is not none -%}
{%- if values|length > 0 -%}
Expand Down Expand Up @@ -332,6 +348,7 @@ class DepComponentContentGenerator:
{{- define_variable_value_safe("CONAN_FRAMEWORKS_{}_{}".format(dep_name, name), cpp_info_flags, 'frameworks') -}}
{{- define_variable_value_safe("CONAN_REQUIRES_{}_{}".format(dep_name, name), cpp_info_flags, 'requires') -}}
{{- define_variable_value_safe("CONAN_SYSTEM_LIBS_{}_{}".format(dep_name, name), cpp_info_flags, 'system_libs') -}}
{{- define_multiple_variable_value("CONAN_PROPERTY_{}_{}".format(dep_name, name), properties) -}}
""")

def __init__(self, dependency, component_name: str, dirs: dict, flags: dict):
Expand All @@ -356,7 +373,8 @@ def content(self) -> str:
"dep_name": _makefy(self._dep.ref.name),
"name": _makefy(self._name),
"cpp_info_dirs": self._dirs,
"cpp_info_flags": self._flags
"cpp_info_flags": self._flags,
"properties": _makefy_properties(self._dep.cpp_info.components[self._name]._properties),
}
template = Template(_jinja_format_list_values() + self.template, trim_blocks=True,
lstrip_blocks=True, undefined=StrictUndefined)
Expand Down Expand Up @@ -397,6 +415,7 @@ class DepContentGenerator:
{{- define_variable_value_safe("CONAN_REQUIRES_{}".format(name), cpp_info_flags, 'requires') -}}
{{- define_variable_value_safe("CONAN_SYSTEM_LIBS_{}".format(name), cpp_info_flags, 'system_libs') -}}
{{- define_variable_value("CONAN_COMPONENTS_{}".format(name), components) -}}
{{- define_multiple_variable_value("CONAN_PROPERTY_{}".format(name), properties) -}}
""")

def __init__(self, dependency, require, root: str, sysroot, dirs: dict, flags: dict):
Expand All @@ -420,6 +439,7 @@ def content(self) -> str:
"components": list(self._dep.cpp_info.get_sorted_components().keys()),
"cpp_info_dirs": self._dirs,
"cpp_info_flags": self._flags,
"properties": _makefy_properties(self._dep.cpp_info._properties),
}
template = Template(_jinja_format_list_values() + self.template, trim_blocks=True,
lstrip_blocks=True, undefined=StrictUndefined)
Expand Down
6 changes: 6 additions & 0 deletions test/integration/toolchains/gnu/test_makedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def package_info(self):
lib_dir2 = os.path.join(self.package_folder, "lib2")
self.cpp_info.includedirs = [include_dir]
self.cpp_info.libdirs = [lib_dir, lib_dir2]
self.cpp_info.set_property("my_prop", "my prop value")
""")
client = TestClient()
client.save({"conanfile.py": conanfile})
Expand All @@ -44,6 +45,7 @@ def package_info(self):
assert f'CONAN_LIB_DIRS_MYLIB = \\\n\t$(CONAN_LIB_DIR_FLAG){prefix}/my_absoulte_path/fake/mylib/lib \\\n\t$(CONAN_LIB_DIR_FLAG)$(CONAN_ROOT_MYLIB)/lib2' in makefile_content
assert f'CONAN_INCLUDE_DIRS_MYLIB = $(CONAN_INCLUDE_DIR_FLAG){prefix}/my_absoulte_path/fake/mylib/include' in makefile_content
assert 'CONAN_BIN_DIRS_MYLIB = $(CONAN_BIN_DIR_FLAG)$(CONAN_ROOT_MYLIB)/bin' in makefile_content
assert 'CONAN_PROPERTY_MYLIB_MY_PROP = my prop value' in makefile_content
memsharded marked this conversation as resolved.
Show resolved Hide resolved

lines = makefile_content.splitlines()
for line_no, line in enumerate(lines):
Expand Down Expand Up @@ -90,6 +92,7 @@ def package_info(self):
assert 'CONAN_BIN_DIRS' not in makefile_content
assert 'CONAN_LIBS' not in makefile_content
assert 'CONAN_FRAMEWORK_DIRS' not in makefile_content
assert 'CONAN_PROPERTY' not in makefile_content


def test_libs_and_system_libs():
Expand Down Expand Up @@ -197,6 +200,7 @@ class TestMakeDepsConan(ConanFile):
def package_info(self):
self.cpp_info.components["mycomponent"].requires.append("lib::cmp1")
self.cpp_info.components["myfirstcomp"].requires.append("mycomponent")
self.cpp_info.components["myfirstcomp"].set_property("my_prop", "my prop value")

""")
client.save({"conanfile.py": conanfile}, clean_first=True)
Expand Down Expand Up @@ -236,6 +240,8 @@ def package_info(self):
assert 'CONAN_REQUIRES = $(CONAN_REQUIRES_SECOND)\n' in makefile_content
assert 'CONAN_LIBS = $(CONAN_LIBS_LIB)\n' in makefile_content

assert 'CONAN_PROPERTY_SECOND_MYFIRSTCOMP_MY_PROP = my prop value\n' in makefile_content


def test_make_with_public_deps_and_component_requires_second():
"""
Expand Down