Skip to content

Commit

Permalink
Add cpp_info.generators
Browse files Browse the repository at this point in the history
  • Loading branch information
madebr committed Feb 25, 2020
1 parent 39849e9 commit 954379a
Show file tree
Hide file tree
Showing 7 changed files with 546 additions and 213 deletions.
14 changes: 7 additions & 7 deletions conans/client/generators/cmake_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,24 +216,24 @@ def generate_targets_section(dependencies, generator_name):
class CMakeCommonMacros:
# Group definition of CMake macros and functions used for many different generators
conan_message = textwrap.dedent("""
function(conan_message MESSAGE_OUTPUT)
function(conan_message)
if(NOT CONAN_CMAKE_SILENT_OUTPUT)
message(${ARGV${0}})
message(${ARGV})
endif()
endfunction()
""")

conan_find_libraries_abs_path = textwrap.dedent("""
function(conan_find_libraries_abs_path libraries package_libdir libraries_abs_path)
foreach(_LIBRARY_NAME ${libraries})
find_library(CONAN_FOUND_LIBRARY NAME ${_LIBRARY_NAME} PATHS ${package_libdir}
foreach(LIBRARY_NAME ${libraries})
find_library(CONAN_FOUND_LIBRARY NAME ${LIBRARY_NAME} PATHS ${package_libdir}
NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
if(CONAN_FOUND_LIBRARY)
conan_message(STATUS "Library ${_LIBRARY_NAME} found ${CONAN_FOUND_LIBRARY}")
conan_message(STATUS "Library ${LIBRARY_NAME} found ${CONAN_FOUND_LIBRARY}")
set(CONAN_FULLPATH_LIBS ${CONAN_FULLPATH_LIBS} ${CONAN_FOUND_LIBRARY})
else()
conan_message(STATUS "Library ${_LIBRARY_NAME} not found in package, might be system one")
set(CONAN_FULLPATH_LIBS ${CONAN_FULLPATH_LIBS} ${_LIBRARY_NAME})
conan_message(STATUS "Library ${LIBRARY_NAME} not found in package, might be system one")
set(CONAN_FULLPATH_LIBS ${CONAN_FULLPATH_LIBS} ${LIBRARY_NAME})
endif()
unset(CONAN_FOUND_LIBRARY CACHE)
endforeach()
Expand Down
103 changes: 88 additions & 15 deletions conans/client/generators/cmake_find_package.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import textwrap


from conans.client.generators.cmake import DepsCppCmake
from conans.client.generators.cmake_find_package_common import target_template, CMakeFindPackageCommonMacros
from conans.client.generators.cmake_multi import extend
Expand All @@ -19,32 +22,86 @@

assign_target_properties = """
if({name}_INCLUDE_DIRS)
set_target_properties({name}::{name} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${{{name}_INCLUDE_DIRS}}")
set_target_properties({name}::{name} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${{{name}_INCLUDE_DIRS}}")
endif()
set_property(TARGET {name}::{name} PROPERTY INTERFACE_LINK_LIBRARIES "${{{name}_LIBRARIES_TARGETS}};${{{name}_LINKER_FLAGS_LIST}}")
set_property(TARGET {name}::{name} PROPERTY INTERFACE_COMPILE_DEFINITIONS ${{{name}_COMPILE_DEFINITIONS}})
set_property(TARGET {name}::{name} PROPERTY INTERFACE_COMPILE_OPTIONS "${{{name}_COMPILE_OPTIONS_LIST}}")
"""

# FIXME: copy/paste from cmake_find_package_common. This code can extracted and shared.
assign_geninfo_target_properties = """
set({safe_target_name}_INCLUDE_DIRS {deps.include_paths})
set({safe_target_name}_COMPILE_DEFINITIONS {deps.compile_definitions})
set({safe_target_name}_COMPILE_OPTIONS_LIST "{deps.cxxflags_list}" "{deps.cflags_list}")
class CMakeFindPackageGenerator(Generator):
template = """
{macros_and_functions}
{find_package_header_block}
{find_libraries_block}
if(NOT ${{CMAKE_VERSION}} VERSION_LESS "3.0")
# Target approach
if(NOT TARGET {name}::{name})
add_library({name}::{name} INTERFACE IMPORTED)
{assign_target_properties_block}
{find_dependencies_block}
endif()
endif()
set({safe_target_name}_LIBRARY_LIST{build_type_suffix} {deps.libs})
set({safe_target_name}_LIB_DIRS{build_type_suffix} {deps.lib_paths})
set({safe_target_name}_LINKER_FLAGS{build_type_suffix}_LIST "{deps.sharedlinkflags_list}" "{deps.exelinkflags_list}")
set({safe_target_name}_SYSTEM_LIBS{build_type_suffix} {deps.system_libs})
set({safe_target_name}_FRAMEWORK_DIRS{build_type_suffix} {deps.framework_paths})
set({safe_target_name}_FRAMEWORKS{build_type_suffix} {deps.frameworks})
conan_find_apple_frameworks({safe_target_name}_FRAMEWORKS_FOUND{build_type_suffix} "${{{safe_target_name}_FRAMEWORKS{build_type_suffix}}}" "${{{safe_target_name}_FRAMEWORK_DIRS{build_type_suffix}}}")
# Gather all the libraries that should be linked to the targets (do not touch existing variables):
set(_{safe_target_name}_DEPENDENCIES{build_type_suffix} "${{{safe_target_name}_FRAMEWORKS_FOUND{build_type_suffix}}} ${{{safe_target_name}_SYSTEM_LIBS{build_type_suffix}}} {deps_names}")
conan_package_library_targets("${{{safe_target_name}_LIBRARY_LIST{build_type_suffix}}}" # libraries
"${{{safe_target_name}_LIB_DIRS{build_type_suffix}}}" # package_libdir
"${{_{safe_target_name}_DEPENDENCIES{build_type_suffix}}}" # deps
{safe_target_name}_LIBRARIES{build_type_suffix} # out_libraries
{safe_target_name}_LIBRARIES_TARGETS{build_type_suffix} # out_libraries_targets
"{build_type_suffix}" # build_type
"{safe_target_name}")
if({safe_target_name}_INCLUDE_DIRS)
set_target_properties({target_name} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${{{safe_target_name}_INCLUDE_DIRS}}")
endif()
set_property(TARGET {target_name} PROPERTY INTERFACE_LINK_LIBRARIES "${{{safe_target_name}_LIBRARIES_TARGETS}};${{{safe_target_name}_LINKER_FLAGS_LIST}}")
set_property(TARGET {target_name} PROPERTY INTERFACE_COMPILE_DEFINITIONS ${{{safe_target_name}_COMPILE_DEFINITIONS}})
set_property(TARGET {target_name} PROPERTY INTERFACE_COMPILE_OPTIONS "${{{safe_target_name}_COMPILE_OPTIONS_LIST}}")
"""


class CMakeFindPackageGenerator(Generator):
template = textwrap.dedent("""
{macros_and_functions}
{find_package_header_block}
{find_libraries_block}
if(NOT ${{CMAKE_VERSION}} VERSION_LESS "3.0")
# Target approach
if(NOT TARGET {name}::{name})
add_library({name}::{name} INTERFACE IMPORTED)
{assign_target_properties_block}
{find_dependencies_block}
endif()
{generator_targets}
endif()
""")

target_template = textwrap.dedent("""
if(NOT TARGET {target_name})
add_library({target_name} INTERFACE IMPORTED)
{assign_target_properties_block}
{find_dependencies_block}
endif()
""")

assign_target_properties = """
if({name}_INCLUDE_DIRS)
set_target_properties({target_name} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${{{name}_INCLUDE_DIRS}}")
endif()
set_property(TARGET {target_name} PROPERTY INTERFACE_LINK_LIBRARIES "${{{name}_LIBRARIES_TARGETS}};${{{name}_LINKER_FLAGS_LIST}}")
set_property(TARGET {target_name} PROPERTY INTERFACE_COMPILE_DEFINITIONS ${{{name}_COMPILE_DEFINITIONS}})
set_property(TARGET {target_name} PROPERTY INTERFACE_COMPILE_OPTIONS "${{{name}_COMPILE_OPTIONS_LIST}}")
"""

@property
def filename(self):
pass
return None

@property
def content(self):
Expand All @@ -71,12 +128,28 @@ def _find_for_dep(self, name, cpp_info):
deps_names = ";".join(["{n}::{n}".format(n=n) for n in public_deps_names])
find_libraries_block = target_template.format(name=name, deps=deps, build_type_suffix="", deps_names=deps_names)
target_props = assign_target_properties.format(name=name, deps=deps, deps_names=deps_names)
generator_targets_blocks = []

for target in cpp_info.generators["cmake_find_package"].targets:
gen_info_assign_target_properties = assign_geninfo_target_properties.format(
safe_target_name=target.name.replace(":", "_"),
target_name=target.name,
build_type_suffix="",
deps=DepsCppCmake(target),
deps_names=deps_names,
)
gen_block = self.target_template.format(target_name=target.name,
assign_target_properties_block=gen_info_assign_target_properties,
find_dependencies_block="")
generator_targets_blocks.append(gen_block)

tmp = self.template.format(name=name, deps=deps,
version=dep_cpp_info.version,
find_dependencies_block="\n ".join(lines),
find_libraries_block=find_libraries_block,
find_package_header_block=find_package_header_block,
assign_target_properties_block=target_props,
generator_targets="\n".join(generator_targets_blocks),
macros_and_functions="\n".join([
CMakeFindPackageCommonMacros.conan_message,
CMakeFindPackageCommonMacros.apple_frameworks_macro,
Expand Down
4 changes: 2 additions & 2 deletions conans/client/generators/cmake_find_package_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ class CMakeFindPackageCommonMacros:
endif()
unset(CONAN_FOUND_LIBRARY CACHE)
endforeach()
if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
# Add all dependencies to all targets
string(REPLACE " " ";" deps_list "${deps}")
foreach(_CONAN_ACTUAL_TARGET ${_CONAN_ACTUAL_TARGETS})
set_property(TARGET ${_CONAN_ACTUAL_TARGET} PROPERTY INTERFACE_LINK_LIBRARIES "${_CONAN_FOUND_SYSTEM_LIBS};${deps_list}")
endforeach()
endif()
set(${out_libraries} ${_out_libraries} PARENT_SCOPE)
set(${out_libraries_target} ${_out_libraries_target} PARENT_SCOPE)
endfunction()
Expand Down
10 changes: 7 additions & 3 deletions conans/model/build_info.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os
from collections import OrderedDict

import deprecation


from conans.model.gen_info import GenInfo


DEFAULT_INCLUDE = "include"
DEFAULT_LIB = "lib"
DEFAULT_BIN = "bin"
Expand All @@ -20,6 +23,7 @@ class _CppInfo(object):
def __init__(self):
self.name = None
self.names = {}
self.generators = GenInfo(self)
self.system_libs = [] # Ordered list of system libraries
self.includedirs = [] # Ordered list of include paths
self.srcdirs = [] # Ordered list of source paths
Expand Down Expand Up @@ -126,7 +130,7 @@ def set_cppflags(self, value):

class CppInfo(_CppInfo):
""" Build Information declared to be used by the CONSUMERS of a
conans. That means that consumers must use this flags and configs i order
conans. That means that consumers must use this flags and configs in order
to build properly.
Defined in user CONANFILE, directories are relative at user definition time
"""
Expand Down Expand Up @@ -228,7 +232,7 @@ def framework_paths(self):
class DepsCppInfo(_BaseDepsCppInfo):
""" Build Information necessary to build a given conans. It contains the
flags, directories and options if its dependencies. The conans CONANFILE
should use these flags to pass them to the underlaying build system (Cmake, make),
should use these flags to pass them to the underlying build system (Cmake, make),
so deps info is managed
"""

Expand Down
5 changes: 0 additions & 5 deletions conans/model/conan_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def __init__(self, conanfile):
self.conanfile = conanfile
self._deps_build_info = conanfile.deps_cpp_info
self._deps_env_info = conanfile.deps_env_info
self._env_info = conanfile.env_info
self._deps_user_info = conanfile.deps_user_info

@property
Expand All @@ -25,10 +24,6 @@ def deps_env_info(self):
def deps_user_info(self):
return self._deps_user_info

@property
def env_info(self):
return self._env_info

@property
def settings(self):
return self.conanfile.settings
Expand Down
Loading

0 comments on commit 954379a

Please sign in to comment.