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

[Bugfix] CMakeDeps and options for linking #9980

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions conan/tools/cmake/cmakedeps/templates/target_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ def template(self):
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}}}
${{'{'}}{{pkg_name}}_OBJECTS{{config_suffix}}}> APPEND)
set_property(TARGET {{target_namespace}}::{{global_target_name}}
PROPERTY INTERFACE_LINK_OPTIONS
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_LINKER_FLAGS{{config_suffix}}}> APPEND)
set_property(TARGET {{target_namespace}}::{{global_target_name}}
PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_INCLUDE_DIRS{{config_suffix}}}> APPEND)
Expand All @@ -129,8 +131,9 @@ def template(self):
########## COMPONENT {{ comp_name }} TARGET PROPERTIES ######################################
set_property(TARGET {{ target_namespace }}::{{ comp_name }} PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_name, 'LINK_LIBS', config_suffix)}}
{{tvalue(pkg_name, comp_name, 'LINKER_FLAGS', config_suffix)}}
{{tvalue(pkg_name, comp_name, 'OBJECTS', config_suffix)}}> APPEND)
set_property(TARGET {{ target_namespace }}::{{ comp_name }} PROPERTY INTERFACE_LINK_OPTIONS
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_name, 'LINKER_FLAGS', config_suffix)}}> APPEND)
set_property(TARGET {{ target_namespace }}::{{ comp_name }} PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_name, 'INCLUDE_DIRS', config_suffix)}}> APPEND)
set_property(TARGET {{ target_namespace }}::{{ comp_name }} PROPERTY INTERFACE_COMPILE_DEFINITIONS
Expand Down
11 changes: 7 additions & 4 deletions conan/tools/cmake/cmakedeps/templates/target_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ def join_paths(paths):

def join_flags(separator, values):
# Flags have to be escaped
return separator.join(v.replace('\\', '\\\\').replace('$', '\\$').replace('"', '\\"')
for v in values)
ret = separator.join(v.replace('\\', '\\\\').replace('$', '\\$').replace('"', '\\"')
for v in values)
return ret

def join_defines(values, prefix=""):
# Defines have to be escaped, included spaces
Expand Down Expand Up @@ -215,8 +216,10 @@ def join_paths_single_var(values):
# linker flags without magic: trying to mess with - and / =>
# https://github.com/conan-io/conan/issues/8811
# frameworks should be declared with cppinfo.frameworks not "-framework Foundation"
self.sharedlinkflags_list = join_flags(";", cpp_info.sharedlinkflags)
self.exelinkflags_list = join_flags(";", cpp_info.exelinkflags)
self.sharedlinkflags_list = '"{}"'.format(join_flags(";", cpp_info.sharedlinkflags)) \
if cpp_info.sharedlinkflags else ''
self.exelinkflags_list = '"{}"'.format(join_flags(";", cpp_info.exelinkflags)) \
if cpp_info.exelinkflags else ''

self.objects_list = join_paths(cpp_info.objects)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,12 @@ def package_info(self):
if build_type == "Release":
assert "System libs release: %s" % library_name in client.out
assert "Libraries to Link release: lib1" in client.out
target_libs = ("$<$<CONFIG:Release>:CONAN_LIB::Test_lib1_RELEASE;sys1;"
"$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>;"
"$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>;"
"$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>;>")
target_libs = "$<$<CONFIG:Release>:CONAN_LIB::Test_lib1_RELEASE;sys1;"
else:
assert "System libs debug: %s" % library_name in client.out
assert "Libraries to Link debug: lib1" in client.out
target_libs = ("$<$<CONFIG:Debug>:CONAN_LIB::Test_lib1_DEBUG;sys1d;"
"$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>;"
"$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>;"
"$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>;>")
target_libs = "$<$<CONFIG:Debug>:CONAN_LIB::Test_lib1_DEBUG;sys1d;"

assert "Target libs: %s" % target_libs in client.out


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import platform
import textwrap

import pytest

from conans.test.utils.tools import TestClient


@pytest.mark.skipif(platform.system() != "Linux", reason="Only Linux")
@pytest.mark.tool_cmake
def test_shared_link_flags():
"""
Testing CMakeDeps and linker flags injection

Issue: https://github.com/conan-io/conan/issues/9936
"""
conanfile = textwrap.dedent("""
franramirez688 marked this conversation as resolved.
Show resolved Hide resolved
from conans import ConanFile
from conan.tools.cmake import CMake
from conan.tools.layout import cmake_layout


class HelloConan(ConanFile):
name = "hello"
version = "1.0"
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False]}
default_options = {"shared": False}
exports_sources = "CMakeLists.txt", "src/*"
generators = "CMakeDeps", "CMakeToolchain"

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
cmake = CMake(self)
cmake.install()

def package_info(self):
self.cpp_info.libs = ["hello"]
self.cpp_info.sharedlinkflags = ["-z now", "-z relro"]
self.cpp_info.exelinkflags = ["-z now", "-z relro"]
""")

client = TestClient()
client.run("new hello/1.0 -m cmake_lib")
client.save({"conanfile.py": conanfile})
client.run("create .")
t = os.path.join("test_package", "cmake-build-release", "conan", "hello-release-x86_64-data.cmake")
target_data_cmake_content = client.load(t)
assert 'set(hello_SHARED_LINK_FLAGS_RELEASE "-z now;-z relro")' in target_data_cmake_content
assert 'set(hello_EXE_LINK_FLAGS_RELEASE "-z now;-z relro")' in target_data_cmake_content
assert "hello/1.0: Hello World Release!" in client.out
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,19 @@ def build(self):
project(consumer)
cmake_minimum_required(VERSION 3.1)
find_package(requirement)
get_target_property(tmp requirement::component INTERFACE_LINK_LIBRARIES)
message("component libs: ${tmp}")
get_target_property(tmp_libs requirement::component INTERFACE_LINK_LIBRARIES)
get_target_property(tmp_options requirement::component INTERFACE_LINK_OPTIONS)
message("component libs: ${tmp_libs}")
message("component options: ${tmp_options}")
""")

t.save({"conanfile.py": conanfile, "CMakeLists.txt": cmakelists})
t.run("create . --build missing -s build_type=Release")

assert ("component libs: "
"$<$<CONFIG:Release>:system_lib_component;"
"$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>;"
"$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>;"
"$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>;>") in t.out
assert 'component libs: $<$<CONFIG:Release>:system_lib_component;>' in t.out
assert ('component options: '
'$<$<CONFIG:Release>:'
'$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>;'
'$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>;'
'$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>>') in t.out
# NOTE: If there is no "conan install -s build_type=Debug", the properties won't contain the
# <CONFIG:Debug>
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,10 @@ def package_info(self):
content = f.read()
assert """set_property(TARGET hello::say PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:Release>:${hello_say_LINK_LIBS_RELEASE}
${hello_say_LINKER_FLAGS_RELEASE}
${hello_say_OBJECTS_RELEASE}> APPEND)""" in content
assert """set_property(TARGET hello::hello
PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:Release>:${hello_LIBRARIES_TARGETS_RELEASE}
${hello_LINKER_FLAGS_RELEASE}
${hello_OBJECTS_RELEASE}> APPEND)""" in content

with open(os.path.join(client.current_folder, "hello-release-x86_64-data.cmake")) as f:
Expand Down
4 changes: 2 additions & 2 deletions conans/test/unittests/tools/cmake/test_cmakedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ def test_cmake_deps_links_flags():
cmakedeps = CMakeDeps(conanfile)
files = cmakedeps.content
data_cmake = files["mypkg-release-x86-data.cmake"]
assert "set(mypkg_SHARED_LINK_FLAGS_RELEASE -NODEFAULTLIB;-OTHERFLAG)" in data_cmake
assert "set(mypkg_EXE_LINK_FLAGS_RELEASE -OPT:NOICF)" in data_cmake
assert 'set(mypkg_SHARED_LINK_FLAGS_RELEASE "-NODEFAULTLIB;-OTHERFLAG")' in data_cmake
assert 'set(mypkg_EXE_LINK_FLAGS_RELEASE "-OPT:NOICF")' in data_cmake
assert 'set(mypkg_OBJECTS_RELEASE "${mypkg_PACKAGE_FOLDER_RELEASE}/myobject.o")' in data_cmake


Expand Down