Skip to content

Commit

Permalink
CMakeDeps with custom namespace (#9513)
Browse files Browse the repository at this point in the history
* CMakeDeps with custom namespace

* cmake_module_target_namespace
  • Loading branch information
lasote committed Sep 2, 2021
1 parent 313552e commit e869976
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
13 changes: 13 additions & 0 deletions conan/tools/cmake/cmakedeps/templates/__init__.py
Expand Up @@ -21,6 +21,10 @@ def pkg_name(self):
def target_namespace(self):
return self.get_target_namespace(self.conanfile) + self.suffix

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

@property
def file_name(self):
return get_file_name(self.conanfile, self.find_module_mode) + self.suffix
Expand Down Expand Up @@ -79,6 +83,15 @@ def get_file_name(self):
return get_file_name(self.conanfile, find_module_mode=self.find_module_mode)

def get_target_namespace(self, req):
if self.find_module_mode:
ret = req.new_cpp_info.get_property("cmake_module_target_namespace", "CMakeDeps")
if ret:
return ret

ret = req.new_cpp_info.get_property("cmake_target_namespace", "CMakeDeps")
return ret or self.get_global_target_name(req)

def get_global_target_name(self, req):
if self.find_module_mode:
ret = req.new_cpp_info.get_property("cmake_module_target_name", "CMakeDeps")
if ret:
Expand Down
11 changes: 6 additions & 5 deletions conan/tools/cmake/cmakedeps/templates/target_configuration.py
Expand Up @@ -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 @@ -172,6 +173,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=self.get_target_namespace(r))
ret = ["{p}::{n}".format(p=self.get_target_namespace(r), n=self.get_global_target_name(r))
for r in self.conanfile.dependencies.direct_host.values()]
return ret
9 changes: 6 additions & 3 deletions conan/tools/cmake/cmakedeps/templates/targets.py
Expand Up @@ -27,8 +27,11 @@ 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,
"data_pattern": data_pattern,
"target_pattern": target_pattern}

return ret

@property
Expand All @@ -50,9 +53,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
Expand Up @@ -48,6 +48,7 @@ def package_info(self):
self.cpp_info.set_property("cmake_module_file_name", "mi_dependencia")
self.cpp_info.set_property("cmake_module_target_name", "mi_dependencia_target")
self.cpp_info.set_property("cmake_module_target_namespace", "mi_dependencia_namespace")
self.cpp_info.components["crispin"].libs = ["mydep"]
self.cpp_info.components["crispin"].set_property("cmake_target_name", "MyCrispinTarget")
Expand Down Expand Up @@ -81,7 +82,7 @@ def test_reuse_with_modules_and_config(client):
add_executable(myapp2 main.cpp)
find_package(mi_dependencia) # This one will find the module
target_link_libraries(myapp2 mi_dependencia_target::mi_crispin_target)
target_link_libraries(myapp2 mi_dependencia_namespace::mi_crispin_target)
"""
conanfile = GenConanfile().with_name("myapp")\
Expand Down

0 comments on commit e869976

Please sign in to comment.