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

Support cmake_target_namespace in legacy cmake_find_package, cmake_find_package_multi generators #10005

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
48 changes: 26 additions & 22 deletions conans/client/generators/cmake_find_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ class CMakeFindPackageGenerator(GeneratorComponentsMixin, Generator):
{find_libraries_block}
if(NOT ${{CMAKE_VERSION}} VERSION_LESS "3.0")
# Target approach
if(NOT TARGET {name}::{name})
add_library({name}::{name} INTERFACE IMPORTED)
if(NOT TARGET {namespace}::{name})
add_library({namespace}::{name} INTERFACE IMPORTED)
if({name}_INCLUDE_DIRS)
set_target_properties({name}::{name} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
set_target_properties({namespace}::{name} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
"${{{name}_INCLUDE_DIRS}}")
endif()
set_property(TARGET {name}::{name} PROPERTY INTERFACE_LINK_LIBRARIES
set_property(TARGET {namespace}::{name} PROPERTY INTERFACE_LINK_LIBRARIES
"${{{name}_LIBRARIES_TARGETS}};${{{name}_LINKER_FLAGS_LIST}}")
set_property(TARGET {name}::{name} PROPERTY INTERFACE_COMPILE_DEFINITIONS
set_property(TARGET {namespace}::{name} PROPERTY INTERFACE_COMPILE_DEFINITIONS
${{{name}_COMPILE_DEFINITIONS}})
set_property(TARGET {name}::{name} PROPERTY INTERFACE_COMPILE_OPTIONS
set_property(TARGET {namespace}::{name} PROPERTY INTERFACE_COMPILE_OPTIONS
"${{{name}_COMPILE_OPTIONS_LIST}}")
{find_dependencies_block}
endif()
Expand Down Expand Up @@ -76,7 +76,7 @@ class CMakeFindPackageGenerator(GeneratorComponentsMixin, Generator):

if({{ pkg_name }}_FIND_COMPONENTS)
foreach(_FIND_COMPONENT {{ '${'+pkg_name+'_FIND_COMPONENTS}' }})
list(FIND {{ pkg_name }}_COMPONENTS "{{ pkg_name }}::${_FIND_COMPONENT}" _index)
list(FIND {{ pkg_name }}_COMPONENTS "{{ pkg_namespace }}::${_FIND_COMPONENT}" _index)
czoido marked this conversation as resolved.
Show resolved Hide resolved
if(${_index} EQUAL -1)
conan_message(FATAL_ERROR "Conan: Component '${_FIND_COMPONENT}' NOT found in package '{{ pkg_name }}'")
else()
Expand Down Expand Up @@ -172,17 +172,17 @@ class CMakeFindPackageGenerator(GeneratorComponentsMixin, Generator):

if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
# Target approach
if(NOT TARGET {{ pkg_name }}::{{ comp_name }})
add_library({{ pkg_name }}::{{ comp_name }} INTERFACE IMPORTED)
set_target_properties({{ pkg_name }}::{{ comp_name }} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
if(NOT TARGET {{ pkg_namespace }}::{{ comp_name }})
add_library({{ pkg_namespace }}::{{ comp_name }} INTERFACE IMPORTED)
set_target_properties({{ pkg_namespace }}::{{ comp_name }} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
"{{ '${'+pkg_name+'_'+comp_name+'_INCLUDE_DIRS}' }}")
set_target_properties({{ pkg_name }}::{{ comp_name }} PROPERTIES INTERFACE_LINK_DIRECTORIES
set_target_properties({{ pkg_namespace }}::{{ comp_name }} PROPERTIES INTERFACE_LINK_DIRECTORIES
"{{ '${'+pkg_name+'_'+comp_name+'_LIB_DIRS}' }}")
set_target_properties({{ pkg_name }}::{{ comp_name }} PROPERTIES INTERFACE_LINK_LIBRARIES
set_target_properties({{ pkg_namespace }}::{{ comp_name }} PROPERTIES INTERFACE_LINK_LIBRARIES
"{{ '${'+pkg_name+'_'+comp_name+'_LINK_LIBS}' }};{{ '${'+pkg_name+'_'+comp_name+'_LINKER_FLAGS_LIST}' }}")
set_target_properties({{ pkg_name }}::{{ comp_name }} PROPERTIES INTERFACE_COMPILE_DEFINITIONS
set_target_properties({{ pkg_namespace }}::{{ comp_name }} PROPERTIES INTERFACE_COMPILE_DEFINITIONS
"{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_DEFINITIONS}' }}")
set_target_properties({{ pkg_name }}::{{ comp_name }} PROPERTIES INTERFACE_COMPILE_OPTIONS
set_target_properties({{ pkg_namespace }}::{{ comp_name }} PROPERTIES INTERFACE_COMPILE_OPTIONS
"{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_C}' }};{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_CXX}' }}")
endif()
endif()
Expand All @@ -192,10 +192,10 @@ class CMakeFindPackageGenerator(GeneratorComponentsMixin, Generator):
########## GLOBAL TARGET ####################################################################

if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
if(NOT TARGET {{ pkg_name }}::{{ pkg_name }})
add_library({{ pkg_name }}::{{ pkg_name }} INTERFACE IMPORTED)
if(NOT TARGET {{ pkg_namespace }}::{{ pkg_name }})
add_library({{ pkg_namespace }}::{{ pkg_name }} INTERFACE IMPORTED)
endif()
set_property(TARGET {{ pkg_name }}::{{ pkg_name }} APPEND PROPERTY
set_property(TARGET {{ pkg_namespace }}::{{ pkg_name }} APPEND PROPERTY
INTERFACE_LINK_LIBRARIES "{{ '${'+pkg_name+'_COMPONENTS}' }}")
endif()

Expand Down Expand Up @@ -227,10 +227,12 @@ def content(self):
for pkg_name, cpp_info in self.deps_build_info.dependencies:
pkg_filename = self._get_filename(cpp_info)
pkg_findname = self._get_name(cpp_info)
pkg_namespace = self._get_namespace(cpp_info) or pkg_findname
ret["Find%s.cmake" % pkg_filename] = self._find_for_dep(
pkg_name=pkg_name,
pkg_findname=pkg_findname,
pkg_filename=pkg_filename,
pkg_namespace=pkg_namespace,
cpp_info=cpp_info
)
return ret
Expand All @@ -245,7 +247,7 @@ def _get_components(self, pkg_name, cpp_info):
ret.append((comp_genname, deps_cpp_cmake))
return ret

def _find_for_dep(self, pkg_name, pkg_findname, pkg_filename, cpp_info):
def _find_for_dep(self, pkg_name, pkg_findname, pkg_filename, pkg_namespace, cpp_info):
# return the content of the FindXXX.cmake file for the package "pkg_name"
self._validate_components(cpp_info)

Expand All @@ -263,14 +265,15 @@ def _find_for_dep(self, pkg_name, pkg_findname, pkg_filename, cpp_info):
if cpp_info.components:
components = self._get_components(pkg_name, cpp_info)
# Note these are in reversed order, from more dependent to less dependent
pkg_components = " ".join(["{p}::{c}".format(p=pkg_findname, c=comp_findname) for
pkg_components = " ".join(["{p}::{c}".format(p=pkg_namespace, c=comp_findname) for
comp_findname, _ in reversed(components)])
pkg_info = DepsCppCmake(cpp_info, self.name)
global_target_variables = target_template.format(name=pkg_findname, deps=pkg_info,
build_type_suffix="",
deps_names=deps_names)
return self.find_components_tpl.render(
pkg_name=pkg_findname,
pkg_namespace=pkg_namespace,
pkg_filename=pkg_filename,
pkg_version=pkg_version,
pkg_components=pkg_components,
Expand All @@ -296,8 +299,8 @@ def _find_for_dep(self, pkg_name, pkg_findname, pkg_filename, cpp_info):

# The find_libraries_block, all variables for the package, and creation of targets
deps = DepsCppCmake(dep_cpp_info, self.name)
find_libraries_block = target_template.format(name=pkg_findname, deps=deps,
build_type_suffix="",
find_libraries_block = target_template.format(name=pkg_findname,
deps=deps, build_type_suffix="",
deps_names=deps_names)

# The find_transitive_dependencies block
Expand All @@ -309,7 +312,8 @@ def _find_for_dep(self, pkg_name, pkg_findname, pkg_filename, cpp_info):
find_dependencies_block = ''.join(" " + line if line.strip() else line
for line in f.splitlines(True))

return self.find_template.format(name=pkg_findname, version=pkg_version,
return self.find_template.format(name=pkg_findname, namespace=pkg_namespace,
version=pkg_version,
filename=pkg_filename,
find_libraries_block=find_libraries_block,
find_dependencies_block=find_dependencies_block,
Expand Down
45 changes: 25 additions & 20 deletions conans/client/generators/cmake_find_package_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator):
""")

targets_template = textwrap.dedent("""
if(NOT TARGET {name}::{name})
add_library({name}::{name} INTERFACE IMPORTED)
if(NOT TARGET {namespace}::{name})
add_library({namespace}::{name} INTERFACE IMPORTED)
endif()

# Load the debug and release library finders
Expand All @@ -47,23 +47,23 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator):
# This template takes the "name" of the target name::name and configs = ["Release", "Debug"..]
target_properties = Template("""
# Assign target properties
set_property(TARGET {{name}}::{{name}}
set_property(TARGET {{namespace}}::{{name}}
PROPERTY INTERFACE_LINK_LIBRARIES
{%- for config in configs %}
$<$<CONFIG:{{config}}>:${{'{'}}{{name}}_LIBRARIES_TARGETS_{{config.upper()}}}
${{'{'}}{{name}}_LINKER_FLAGS_{{config.upper()}}_LIST}>
{%- endfor %})
set_property(TARGET {{name}}::{{name}}
set_property(TARGET {{namespace}}::{{name}}
PROPERTY INTERFACE_INCLUDE_DIRECTORIES
{%- for config in configs %}
$<$<CONFIG:{{config}}>:${{'{'}}{{name}}_INCLUDE_DIRS_{{config.upper()}}}>
{%- endfor %})
set_property(TARGET {{name}}::{{name}}
set_property(TARGET {{namespace}}::{{name}}
PROPERTY INTERFACE_COMPILE_DEFINITIONS
{%- for config in configs %}
$<$<CONFIG:{{config}}>:${{'{'}}{{name}}_COMPILE_DEFINITIONS_{{config.upper()}}}>
{%- endfor %})
set_property(TARGET {{name}}::{{name}}
set_property(TARGET {{namespace}}::{{name}}
PROPERTY INTERFACE_COMPILE_OPTIONS
{%- for config in configs %}
$<$<CONFIG:{{config}}>:${{'{'}}{{name}}_COMPILE_OPTIONS_{{config.upper()}}_LIST}>
Expand Down Expand Up @@ -166,14 +166,14 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator):
components_targets_tpl = Template(textwrap.dedent("""\
{%- for comp_name, comp in components %}

if(NOT TARGET {{ pkg_name }}::{{ comp_name }})
add_library({{ pkg_name }}::{{ comp_name }} INTERFACE IMPORTED)
if(NOT TARGET {{ pkg_namespace }}::{{ comp_name }})
add_library({{ pkg_namespace }}::{{ comp_name }} INTERFACE IMPORTED)
endif()

{%- endfor %}

if(NOT TARGET {{ pkg_name }}::{{ pkg_name }})
add_library({{ pkg_name }}::{{ pkg_name }} INTERFACE IMPORTED)
if(NOT TARGET {{ pkg_namespace }}::{{ pkg_name }})
add_library({{ pkg_namespace }}::{{ pkg_name }} INTERFACE IMPORTED)
endif()

# Load the debug and release library finders
Expand All @@ -186,7 +186,7 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator):

if({{ pkg_name }}_FIND_COMPONENTS)
foreach(_FIND_COMPONENT {{ '${'+pkg_name+'_FIND_COMPONENTS}' }})
list(FIND {{ pkg_name }}_COMPONENTS_{{ build_type }} "{{ pkg_name }}::${_FIND_COMPONENT}" _index)
list(FIND {{ pkg_name }}_COMPONENTS_{{ build_type }} "{{ pkg_namespace }}::${_FIND_COMPONENT}" _index)
if(${_index} EQUAL -1)
conan_message(FATAL_ERROR "Conan: Component '${_FIND_COMPONENT}' NOT found in package '{{ pkg_name }}'")
else()
Expand Down Expand Up @@ -237,20 +237,20 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator):

########## COMPONENT {{ comp_name }} TARGET PROPERTIES ######################################

set_property(TARGET {{ pkg_name }}::{{ comp_name }} PROPERTY INTERFACE_LINK_LIBRARIES
set_property(TARGET {{ pkg_namespace }}::{{ comp_name }} PROPERTY INTERFACE_LINK_LIBRARIES
{%- for config in configs %}
$<$<CONFIG:{{config}}>:{{tvalue(pkg_name, comp_name, 'LINK_LIBS', config)}}
{{tvalue(pkg_name, comp_name, 'LINKER_FLAGS_LIST', config)}}>
{%- endfor %})
set_property(TARGET {{ pkg_name }}::{{ comp_name }} PROPERTY INTERFACE_INCLUDE_DIRECTORIES
set_property(TARGET {{ pkg_namespace }}::{{ comp_name }} PROPERTY INTERFACE_INCLUDE_DIRECTORIES
{%- for config in configs %}
$<$<CONFIG:{{config}}>:{{tvalue(pkg_name, comp_name, 'INCLUDE_DIRS', config)}}>
{%- endfor %})
set_property(TARGET {{ pkg_name }}::{{ comp_name }} PROPERTY INTERFACE_COMPILE_DEFINITIONS
set_property(TARGET {{ pkg_namespace }}::{{ comp_name }} PROPERTY INTERFACE_COMPILE_DEFINITIONS
{%- for config in configs %}
$<$<CONFIG:{{config}}>:{{tvalue(pkg_name, comp_name, 'COMPILE_DEFINITIONS', config)}}>
{%- endfor %})
set_property(TARGET {{ pkg_name }}::{{ comp_name }} PROPERTY INTERFACE_COMPILE_OPTIONS
set_property(TARGET {{ pkg_namespace }}::{{ comp_name }} PROPERTY INTERFACE_COMPILE_OPTIONS
{%- for config in configs %}
$<$<CONFIG:{{config}}>:
{{tvalue(pkg_name, comp_name, 'COMPILE_OPTIONS_C', config)}}
Expand All @@ -263,7 +263,7 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator):
########## GLOBAL TARGET PROPERTIES #########################################################

if(NOT {{ pkg_name }}_{{ pkg_name }}_TARGET_PROPERTIES)
set_property(TARGET {{ pkg_name }}::{{ pkg_name }} APPEND PROPERTY INTERFACE_LINK_LIBRARIES
set_property(TARGET {{ pkg_namespace }}::{{ pkg_name }} APPEND PROPERTY INTERFACE_LINK_LIBRARIES
{%- for config in configs %}
$<$<CONFIG:{{config}}>:{{ '${'+pkg_name+'_COMPONENTS_'+config.upper()+'}'}}>
{%- endfor %})
Expand Down Expand Up @@ -312,6 +312,7 @@ def content(self):
self._validate_components(cpp_info)
pkg_filename = self._get_filename(cpp_info)
pkg_findname = self._get_name(cpp_info)
pkg_namespace = self._get_namespace(cpp_info) or pkg_findname
pkg_version = cpp_info.version

public_deps = self.get_public_deps(cpp_info)
Expand All @@ -329,11 +330,12 @@ def content(self):
ret[self._config_filename(pkg_filename)] = self._config(
filename=pkg_filename,
name=pkg_findname,
namespace=pkg_namespace,
version=cpp_info.version,
public_deps_names=pkg_public_deps_filenames
)
ret["{}Targets.cmake".format(pkg_filename)] = self.targets_template.format(
filename=pkg_filename, name=pkg_findname)
filename=pkg_filename, name=pkg_findname, namespace=pkg_namespace)

# If any config matches the build_type one, add it to the cpp_info
dep_cpp_info = extend(cpp_info, build_type.lower())
Expand All @@ -347,7 +349,7 @@ def content(self):
pkg_info = DepsCppCmake(cpp_info, self.name)
components = self._get_components(pkg_name, cpp_info)
# Note these are in reversed order, from more dependent to less dependent
pkg_components = " ".join(["{p}::{c}".format(p=pkg_findname, c=comp_findname) for
pkg_components = " ".join(["{p}::{c}".format(p=pkg_namespace, c=comp_findname) for
comp_findname, _ in reversed(components)])
global_target_variables = target_template.format(name=pkg_findname, deps=pkg_info,
build_type_suffix=build_type_suffix,
Expand All @@ -364,13 +366,15 @@ def content(self):
ret["{}Target-{}.cmake".format(pkg_filename, build_type.lower())] = variables
targets = self.components_targets_tpl.render(
pkg_name=pkg_findname,
pkg_namespace=pkg_namespace,
pkg_filename=pkg_filename,
components=components,
build_type=build_type
)
ret["{}Targets.cmake".format(pkg_filename)] = targets
target_config = self.components_config_tpl.render(
pkg_name=pkg_findname,
pkg_namespace=pkg_namespace,
pkg_filename=pkg_filename,
components=components,
pkg_public_deps=pkg_public_deps_filenames,
Expand All @@ -392,7 +396,7 @@ def _config_version_filename(self, pkg_filename):
else:
return "{}ConfigVersion.cmake".format(pkg_filename)

def _config(self, filename, name, version, public_deps_names):
def _config(self, filename, name, namespace, version, public_deps_names):
# Builds the XXXConfig.cmake file for one package

# The common macros
Expand All @@ -403,7 +407,8 @@ def _config(self, filename, name, version, public_deps_names):
])

# Define the targets properties
targets_props = self.target_properties.render(name=name, configs=self.configurations)
targets_props = self.target_properties.render(name=name, namespace=namespace,
configs=self.configurations)
# Add build modules
build_modules_block = self.build_modules.render(name=name, configs=self.configurations)
# The find_dependencies_block
Expand Down
7 changes: 7 additions & 0 deletions conans/model/build_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ def get_name(self, generator, default_name=True):
return self.get_property(property_name, generator) \
or self.names.get(generator, self._name if default_name else None)

# To provide compatibility for legacy generators with cmake_target_namespace
# and make migration easier
def get_namespace(self, generator):
if generator == "cmake_find_package" or "cmake_find_package_multi":
czoido marked this conversation as resolved.
Show resolved Hide resolved
namespace = self.get_property("cmake_target_namespace", generator)
return namespace

# TODO: Deprecate for 2.0. Only cmake generators should access this. Use get_property for 2.0
def get_filename(self, generator, default_name=True):
if generator == "cmake_find_package" and self.get_property("cmake_module_file_name", generator):
Expand Down
8 changes: 7 additions & 1 deletion conans/model/conan_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def __init__(self, conanfile):
def _get_name(cls, obj):
return obj.get_name(cls.name)

@classmethod
def _get_namespace(cls, obj):
return obj.get_namespace(cls.name)

@property
def deps_build_info(self):
return self._deps_build_info
Expand Down Expand Up @@ -84,11 +88,13 @@ def _get_require_name(self, pkg_name, req):
pkg, cmp = req.split(COMPONENT_SCOPE) if COMPONENT_SCOPE in req else (pkg_name, req)
pkg_build_info = self.deps_build_info[pkg]
pkg_name = self._get_name(pkg_build_info)
# fallback namespace to pkg_name if not defined
pkg_namespace = self._get_namespace(pkg_build_info) or pkg_name
if cmp in pkg_build_info.components:
cmp_name = self._get_name(pkg_build_info.components[cmp])
else:
cmp_name = pkg_name
return pkg_name, cmp_name
return pkg_namespace, cmp_name

def _get_components(self, pkg_name, cpp_info):
ret = []
Expand Down
Loading