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

CMakeDeps with custom namespace #9513

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions conan/tools/cmake/cmakedeps/templates/__init__.py
Expand Up @@ -20,6 +20,10 @@ def pkg_name(self):
def target_namespace(self):
return get_target_namespace(self.conanfile) + self.suffix

@property
def global_target_name(self):
return get_global_target_name(self.conanfile) + self.suffix

@property
def file_name(self):
return get_file_name(self.conanfile) + self.suffix
Expand Down Expand Up @@ -82,6 +86,11 @@ def get_file_name(self):


def get_target_namespace(req):
ret = req.new_cpp_info.get_property("cmake_target_namespace", "CMakeDeps")
return ret or get_global_target_name(req)


def get_global_target_name(req):
ret = req.new_cpp_info.get_property("cmake_target_name", "CMakeDeps")
if not ret:
ret = req.cpp_info.get_name("cmake_find_package_multi", default_name=False)
Expand Down
13 changes: 7 additions & 6 deletions conan/tools/cmake/cmakedeps/templates/target_configuration.py
@@ -1,7 +1,7 @@
import textwrap

from conan.tools.cmake.cmakedeps.templates import CMakeDepsFileTemplate, get_component_alias, \
get_target_namespace
get_target_namespace, get_global_target_name

"""

Expand All @@ -23,6 +23,7 @@ def context(self):
if not self.conanfile.is_build_context else []
return {"pkg_name": self.pkg_name,
"target_namespace": self.target_namespace,
"global_target_name": self.global_target_name,
"config_suffix": self.config_suffix,
"deps_targets_names": ";".join(deps_targets_names),
"components_names": self.get_required_components_names(),
Expand Down Expand Up @@ -106,17 +107,17 @@ def template(self):


########## GLOBAL TARGET PROPERTIES {{ configuration }} ########################################
set_property(TARGET {{target_namespace}}::{{target_namespace}}
set_property(TARGET {{target_namespace}}::{{global_target_name}}
PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_LIBRARIES_TARGETS{{config_suffix}}}
${{'{'}}{{pkg_name}}_LINKER_FLAGS{{config_suffix}}}> APPEND)
set_property(TARGET {{target_namespace}}::{{target_namespace}}
set_property(TARGET {{target_namespace}}::{{global_target_name}}
PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_INCLUDE_DIRS{{config_suffix}}}> APPEND)
set_property(TARGET {{target_namespace}}::{{target_namespace}}
set_property(TARGET {{target_namespace}}::{{global_target_name}}
PROPERTY INTERFACE_COMPILE_DEFINITIONS
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_COMPILE_DEFINITIONS{{config_suffix}}}> APPEND)
set_property(TARGET {{target_namespace}}::{{target_namespace}}
set_property(TARGET {{target_namespace}}::{{global_target_name}}
PROPERTY INTERFACE_COMPILE_OPTIONS
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_COMPILE_OPTIONS{{config_suffix}}}> APPEND)

Expand Down Expand Up @@ -173,6 +174,6 @@ def get_deps_targets_names(self):
ret.append("{}::{}".format(dep_name, component_name))
elif self.conanfile.dependencies.direct_host:
# Regular external "conanfile.requires" declared, not cpp_info requires
ret = ["{p}::{p}".format(p=get_target_namespace(r))
ret = ["{p}::{n}".format(p=get_target_namespace(r), n=get_global_target_name(r))
for r in self.conanfile.dependencies.direct_host.values()]
return ret
7 changes: 4 additions & 3 deletions conan/tools/cmake/cmakedeps/templates/targets.py
Expand Up @@ -19,6 +19,7 @@ def filename(self):
def context(self):
ret = {"pkg_name": self.pkg_name,
"target_namespace": self.target_namespace,
"global_target_name": self.global_target_name,
"file_name": self.file_name}
return ret

Expand All @@ -41,9 +42,9 @@ def template(self):
endif()
endforeach()

if(NOT TARGET {{ target_namespace }}::{{ target_namespace }})
add_library({{ target_namespace }}::{{ target_namespace }} INTERFACE IMPORTED)
conan_message(STATUS "Conan: Target declared '{{ target_namespace }}::{{ target_namespace }}'")
if(NOT TARGET {{ target_namespace }}::{{ global_target_name }})
add_library({{ target_namespace }}::{{ global_target_name }} INTERFACE IMPORTED)
conan_message(STATUS "Conan: Target declared '{{ target_namespace }}::{{ global_target_name }}'")
endif()

# Load the debug and release library finders
Expand Down
Expand Up @@ -310,6 +310,47 @@ def test_custom_names(setup_client_with_greetings):
create_chat(client, "custom", package_info, cmake_find, test_cmake_find)


def test_different_namespace(setup_client_with_greetings):
client = setup_client_with_greetings

package_info = textwrap.dedent("""
self.cpp_info.set_property("cmake_target_namespace", "MyChat")
self.cpp_info.set_property("cmake_target_name", "MyGlobalChat")
self.cpp_info.set_property("cmake_file_name", "MyChat")

self.cpp_info.components["sayhello"].set_property("cmake_target_name", "MySay")

self.cpp_info.components["sayhello"].requires = ["greetings::hello"]
self.cpp_info.components["sayhello"].libs = ["sayhello"]
self.cpp_info.components["sayhellobye"].set_property("cmake_target_name", "MySayBye")

self.cpp_info.components["sayhellobye"].requires = ["sayhello", "greetings::bye"]
self.cpp_info.components["sayhellobye"].libs = ["sayhellobye"]
""")

cmake_find = textwrap.dedent("""
find_package(MYG COMPONENTS MyHello MyBye)

add_library(sayhello sayhello.cpp)
target_link_libraries(sayhello MyGreetings::MyHello)

add_library(sayhellobye sayhellobye.cpp)
target_link_libraries(sayhellobye sayhello MyGreetings::MyBye)
""")

test_cmake_find = textwrap.dedent("""
find_package(MyChat)

add_executable(example example.cpp)
target_link_libraries(example MyChat::MySayBye)

add_executable(example2 example.cpp)
target_link_libraries(example2 MyChat::MyGlobalChat)
""")
create_chat(client, "custom", package_info, cmake_find, test_cmake_find)



def test_no_components(setup_client_with_greetings):
client = setup_client_with_greetings

Expand Down