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

Toolchain declare DIR find package variables #9032

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 1 addition & 7 deletions conan/tools/cmake/cmakedeps/templates/__init__.py
@@ -1,6 +1,7 @@
import jinja2
from jinja2 import Template

from conan.tools.cmake.utils import get_file_name
from conans.errors import ConanException


Expand Down Expand Up @@ -86,13 +87,6 @@ def get_target_namespace(req):
return ret or req.ref.name


def get_file_name(req):
ret = req.new_cpp_info.get_property("cmake_file_name", "CMakeDeps")
if not ret:
ret = req.cpp_info.get_filename("cmake_find_package_multi", default_name=False)
return ret or req.ref.name


def get_component_alias(req, comp_name):
if comp_name not in req.new_cpp_info.components:
# foo::foo might be referencing the root cppinfo
Expand Down
3 changes: 2 additions & 1 deletion conan/tools/cmake/cmakedeps/templates/target_data.py
Expand Up @@ -2,7 +2,8 @@
import textwrap

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

"""

Expand Down
17 changes: 15 additions & 2 deletions conan/tools/cmake/toolchain.py
Expand Up @@ -9,7 +9,7 @@

from conan.tools import CONAN_TOOLCHAIN_ARGS_FILE
from conan.tools._compilers import architecture_flag, use_win_mingw
from conan.tools.cmake.utils import is_multi_configuration
from conan.tools.cmake.utils import is_multi_configuration, get_file_name
from conan.tools.microsoft.toolchain import write_conanvcvars, vs_ide_version
from conans.errors import ConanException
from conans.util.files import load, save
Expand Down Expand Up @@ -383,6 +383,12 @@ class FindConfigFiles(Block):
{% if android_prefix_path %}
set(CMAKE_FIND_ROOT_PATH {{ android_prefix_path }} ${CMAKE_FIND_ROOT_PATH})
{% endif %}

# To support cross building to iOS, watchOS and tvOS where CMake looks for config files
# only in the system frameworks unless you declare the XXX_DIR variables
{% for name in find_names %}
set({{ name }}_DIR "{{ generators_folder }}")
{% endfor %}
""")

def context(self):
Expand All @@ -395,10 +401,17 @@ def context(self):

os_ = self._conanfile.settings.get_safe("os")
android_prefix = "${CMAKE_CURRENT_LIST_DIR}" if os_ == "Android" else None

host_req = self._conanfile.dependencies.transitive_host_requires
find_names_needed = os_ in ('iOS', "watchOS", "tvOS")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't happen in OSX, right, only for these systems?

find_names = [get_file_name(req) for req in host_req] if find_names_needed else []

return {"find_package_prefer_config": find_package_prefer_config,
"cmake_prefix_path": "${CMAKE_CURRENT_LIST_DIR}",
"cmake_module_path": "${CMAKE_CURRENT_LIST_DIR}",
"android_prefix_path": android_prefix}
"android_prefix_path": android_prefix,
"find_names": find_names,
"generators_folder": "${CMAKE_CURRENT_LIST_DIR}"}


class UserToolchain(Block):
Expand Down
11 changes: 11 additions & 0 deletions conan/tools/cmake/utils.py
Expand Up @@ -2,3 +2,14 @@ def is_multi_configuration(generator):
if not generator:
return False
return "Visual" in generator or "Xcode" in generator or "Multi-Config" in generator


def get_file_name(conanfile):
"""Get the name of the file for the find_package(XXX)"""
# This is used by the CMakeToolchain to adjust the XXX_DIR variables and the CMakeDeps. Both
# to know the file name that will have the XXX-config.cmake files.
ret = conanfile.new_cpp_info.get_property("cmake_file_name", "CMakeDeps")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not very great now, that it depends explicitly on CMakeDeps, while it has been made common and is being used not in CMakeDeps, but in CMakeToolchain. I understand why, but reads a bit confusing if you don't know it.

Copy link
Contributor Author

@lasote lasote Jun 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used in both of them. Maybe that property shouldn't be attached to the CMakeDeps generator by the recipe creator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment. Suggestion welcome.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not fully convinced on the proposition of both having the property name and the specific generator as a specialization. We might want to revisit that, but yes, I guess at the moment this is fine here.

if not ret:
ret = conanfile.cpp_info.get_filename("cmake_find_package_multi", default_name=False)
return ret or conanfile.ref.name

Expand Up @@ -193,17 +193,10 @@ class HELLO_EXPORT Hello
def test_apple_own_framework_cross_build(settings):
client = TestClient()

# FIXME: The crossbuild for iOS etc is failing with find_package because cmake ignore the
# cmake_prefix_path to point only to the Frameworks of the system. The only fix found
# would require to introduce something like "set (mylibrary_DIR "${CMAKE_BINARY_DIR}")"
# at the toolchain (but it would require the toolchain to know about the deps)
# https://stackoverflow.com/questions/65494246/cmakes-find-package-ignores-the-paths-option-when-building-for-ios#
test_cmake = textwrap.dedent("""
cmake_minimum_required(VERSION 3.15)
project(Testing CXX)
# set(CMAKE_FIND_DEBUG_MODE TRUE)

set (mylibrary_DIR "${CMAKE_BINARY_DIR}")
find_package(mylibrary REQUIRED)

add_executable(timer timer.cpp)
Expand Down
2 changes: 2 additions & 0 deletions conans/test/unittests/tools/cmake/test_cmaketoolchain.py
Expand Up @@ -24,6 +24,8 @@ def conanfile():
c.settings.compiler.libcxx = "libstdc++"
c.conf = Conf()
c.folders.set_base_generators(".")
c._conan_node = Mock()
c._conan_node.dependencies = []
return c


Expand Down