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

feat: Add CONAN_RUNTIME_LIB_DIRS to the conan_toolchain.cmake #15914

Merged
merged 20 commits into from May 17, 2024
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
36 changes: 31 additions & 5 deletions conan/tools/cmake/toolchain/blocks.py
Expand Up @@ -446,6 +446,14 @@ def to_apple_archs(conanfile, default=None):

class FindFiles(Block):
template = textwrap.dedent("""
{% macro multiconfig_generator(dict) %}
{% set output = namespace(str='') %}
{% for c, v in dict.items() %}
{% set output.str = output.str + '$<$<CONFIG:' + c + '>:' + v|string + '>' %}
{% endfor %}
{{ output.str }}
{% endmacro %}

memsharded marked this conversation as resolved.
Show resolved Hide resolved
{% if find_package_prefer_config %}
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG {{ find_package_prefer_config }})
{% endif %}
Expand Down Expand Up @@ -481,7 +489,7 @@ class FindFiles(Block):
list(PREPEND CMAKE_INCLUDE_PATH {{ cmake_include_path }})
{% endif %}
{% if host_lib_dirs %}
list(PREPEND CONAN_RUNTIME_LIB_DIRS {{ host_lib_dirs }})
list(PREPEND CONAN_RUNTIME_LIB_DIRS {{ multiconfig_generator(host_lib_dirs) }} )
{% endif %}

{% if cross_building %}
Expand All @@ -505,6 +513,22 @@ class FindFiles(Block):
{% endif %}
""")

def _get_host_lib_dirs_mc(self):
settings = self._conanfile.settings
host_lib_dirs = {}
if os.path.exists(CONAN_TOOLCHAIN_FILENAME):
jcar87 marked this conversation as resolved.
Show resolved Hide resolved
existing_include = load(CONAN_TOOLCHAIN_FILENAME)
pattern_lib_dris = r"list\(PREPEND CONAN_RUNTIME_LIB_DIRS ([^)]*)\)"
jcar87 marked this conversation as resolved.
Show resolved Hide resolved
msvc_runtime_value = re.search(pattern_lib_dris, existing_include)
jcar87 marked this conversation as resolved.
Show resolved Hide resolved
if msvc_runtime_value:
capture = msvc_runtime_value.group(1)
matches = re.findall(r"\$<\$<CONFIG:(.*)>:(.*)>", capture)
host_lib_dirs = dict(matches)
build_type = settings.get_safe("build_type")
host_lib_dirs[build_type] = []
memsharded marked this conversation as resolved.
Show resolved Hide resolved
return host_lib_dirs


@staticmethod
def _join_paths(paths):
return " ".join(['"{}"'.format(p.replace('\\', '/')
Expand All @@ -522,13 +546,14 @@ def context(self):

is_apple_ = is_apple_os(self._conanfile)
is_win = self._conanfile.settings.get_safe("os") == "Windows"
memsharded marked this conversation as resolved.
Show resolved Hide resolved
build_type = self._conanfile.settings.get_safe("build_type")

# Read information from host context
# TODO: Add here in 2.0 the "skip": False trait
host_req = self._conanfile.dependencies.filter({"build": False}).values()
build_paths = []
host_lib_paths = []
host_lib_dirs = []
host_lib_dirs = self._get_host_lib_dirs_mc()
memsharded marked this conversation as resolved.
Show resolved Hide resolved
host_framework_paths = []
host_include_paths = []
for req in host_req:
Expand All @@ -539,9 +564,10 @@ def context(self):
host_framework_paths.extend(cppinfo.frameworkdirs)
host_include_paths.extend(cppinfo.includedirs)
if is_win:
host_lib_dirs.extend(cppinfo.bindirs)
host_lib_dirs[build_type].extend(cppinfo.bindirs)
else:
host_lib_dirs.extend(cppinfo.libdirs)
host_lib_dirs[build_type].extend(cppinfo.libdirs)
host_lib_dirs[build_type] = self._join_paths(host_lib_dirs[build_type])
memsharded marked this conversation as resolved.
Show resolved Hide resolved

# Read information from build context
build_req = self._conanfile.dependencies.build.values()
Expand All @@ -561,7 +587,7 @@ def context(self):
"cmake_include_path": self._join_paths(host_include_paths),
"is_apple": is_apple_,
"cross_building": cross_building(self._conanfile),
"host_lib_dirs": self._join_paths(host_lib_dirs)
"host_lib_dirs": host_lib_dirs
}


Expand Down
22 changes: 17 additions & 5 deletions conans/test/integration/toolchains/cmake/test_cmaketoolchain.py
Expand Up @@ -371,7 +371,7 @@ def package_info(self):
.with_settings("os", "arch", "compiler", "build_type"))

client.save({"conanfile.py": conanfile})
client.run("install . ")
client.run("install . -s build_type=Release")
return client


Expand All @@ -380,8 +380,8 @@ def test_lib_dirs_windows(lib_dir_setup):
client = lib_dir_setup

contents = client.load("conan_toolchain.cmake")
pattern_lib_path = r'list\(PREPEND CMAKE_LIBRARY_PATH "(.*?)"\)'
pattern_lib_dirs = r'list\(PREPEND CONAN_RUNTIME_LIB_DIRS "(.*?)"\)'
pattern_lib_path = r'list\(PREPEND CMAKE_LIBRARY_PATH "(.*)"\)'
pattern_lib_dirs = r'PREPEND CONAN_RUNTIME_LIB_DIRS \$<\$<CONFIG:Release>:"(.*)">'
lib_path_group = re.search(pattern_lib_path, contents).groups()
lib_dirs_group = re.search(pattern_lib_dirs, contents).groups()

Expand All @@ -394,14 +394,26 @@ def test_lib_dirs_no_windows(lib_dir_setup):
client = lib_dir_setup

contents = client.load("conan_toolchain.cmake")
pattern_lib_path = r'list\(PREPEND CMAKE_LIBRARY_PATH (.*)\)'
pattern_lib_dirs = r'list\(PREPEND CONAN_RUNTIME_LIB_DIRS (.*)\)'
pattern_lib_path = r'list\(PREPEND CMAKE_LIBRARY_PATH "(.*)"\)'
pattern_lib_dirs = r'PREPEND CONAN_RUNTIME_LIB_DIRS \$<\$<CONFIG:Release>:"(.*)">'
lib_path = re.search(pattern_lib_path, contents).group(1)
lib_dirs = re.search(pattern_lib_dirs, contents).group(1)

assert lib_path == lib_dirs


def test_lib_dirs_multiconf(lib_dir_setup):
client = lib_dir_setup
client.run("install . -s build_type=Debug")

contents = client.load("conan_toolchain.cmake")
pattern_lib_dris = r"list\(PREPEND CONAN_RUNTIME_LIB_DIRS ([^)]*)\)"
msvc_runtime_value = re.search(pattern_lib_dris, contents).group(1)

assert "<CONFIG:Release>" in msvc_runtime_value
assert "<CONFIG:Debug>" in msvc_runtime_value


@pytest.mark.skipif(platform.system() != "Darwin", reason="Only OSX")
def test_cmaketoolchain_cmake_system_processor_cross_apple():
"""
Expand Down