Skip to content

Commit

Permalink
cmake_build_module only from global
Browse files Browse the repository at this point in the history
  • Loading branch information
lasote committed Jul 19, 2022
1 parent 3733be8 commit e733137
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 52 deletions.
16 changes: 6 additions & 10 deletions conan/tools/cmake/cmakedeps/templates/target_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def context(self):
package_folder = package_folder.replace('\\', '/').replace('$', '\\$').replace('"', '\\"')

return {"global_cpp": global_cpp,
"has_components": self.conanfile.cpp_info.has_components,
"pkg_name": self.pkg_name,
"file_name": self.file_name,
"package_folder": package_folder,
Expand Down Expand Up @@ -84,8 +85,9 @@ def template(self):
########### VARIABLES #######################################################################
#############################################################################################
set({{ pkg_name }}_PACKAGE_FOLDER{{ config_suffix }} "{{ package_folder }}")
set({{ pkg_name }}_BUILD_MODULES_PATHS{{ config_suffix }} {{ global_cpp.build_modules_paths }})
{% if global_cpp %}
{% if not has_components %}
set({{ pkg_name }}_INCLUDE_DIRS{{ config_suffix }} {{ global_cpp.include_paths }})
set({{ pkg_name }}_RES_DIRS{{ config_suffix }} {{ global_cpp.res_paths }})
Expand All @@ -101,7 +103,6 @@ def template(self):
set({{ pkg_name }}_SYSTEM_LIBS{{ config_suffix }} {{ global_cpp.system_libs }})
set({{ pkg_name }}_FRAMEWORK_DIRS{{ config_suffix }} {{ global_cpp.framework_paths }})
set({{ pkg_name }}_FRAMEWORKS{{ config_suffix }} {{ global_cpp.frameworks }})
set({{ pkg_name }}_BUILD_MODULES_PATHS{{ config_suffix }} {{ global_cpp.build_modules_paths }})
set({{ pkg_name }}_BUILD_DIRS{{ config_suffix }} {{ global_cpp.build_paths }})
{% else %}
Expand All @@ -128,21 +129,16 @@ def template(self):
$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>{{ ':${' }}{{ pkg_name }}_{{ comp_variable_name }}_SHARED_LINK_FLAGS{{ config_suffix }}}>
$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>{{ ':${' }}{{ pkg_name }}_{{ comp_variable_name }}_EXE_LINK_FLAGS{{ config_suffix }}}>
)
list(APPEND {{ pkg_name }}_BUILD_MODULES_PATHS{{ config_suffix }} {{ cpp.build_modules_paths }})
{%- endfor %}
{%- endif %}
""")
return ret

def _get_global_cpp_cmake(self):
sorted_comps = self.conanfile.cpp_info.get_sorted_components()
if sorted_comps: # There are components
return None
else:
pfolder_var_name = "{}_PACKAGE_FOLDER{}".format(self.pkg_name, self.config_suffix)
return _TargetDataContext(self.conanfile.cpp_info, pfolder_var_name,
self.conanfile.package_folder)
pfolder_var_name = "{}_PACKAGE_FOLDER{}".format(self.pkg_name, self.config_suffix)
return _TargetDataContext(self.conanfile.cpp_info, pfolder_var_name,
self.conanfile.package_folder)

def _get_required_components_cpp(self):
"""Returns a list of (component_name, DepsCppCMake)"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@


@pytest.mark.tool_cmake
@pytest.mark.parametrize("use_components", [False, True])
def test_build_modules_alias_target(use_components):
def test_build_modules_alias_target():
client = TestClient()
conanfile = textwrap.dedent("""
import os
Expand All @@ -24,21 +23,13 @@ def package(self):
def package_info(self):
module = os.path.join("share", "cmake", "target-alias.cmake")
{}
self.cpp_info.set_property("cmake_build_modules", [module])
""")
if use_components:
info = """
self.cpp_info.components["comp"].set_property("cmake_build_modules", [module])
"""
else:
info = """
self.cpp_info.set_property("cmake_build_modules", [module])
"""

target_alias = textwrap.dedent("""
add_library(otherhello INTERFACE IMPORTED)
target_link_libraries(otherhello INTERFACE {target_name})
""").format(target_name="namespace::comp" if use_components else "hello::hello")
conanfile = conanfile.format(info)
target_link_libraries(otherhello INTERFACE hello::hello)
""")
client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias})
client.run("create .")

Expand Down Expand Up @@ -66,20 +57,13 @@ def build(self):
""")
client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists})
client.run("create .")
if use_components:
assert "otherhello link libraries: namespace::comp" in client.out
else:
assert "otherhello link libraries: hello::hello" in client.out
assert "otherhello link libraries: hello::hello" in client.out


@pytest.mark.tool_cmake
def test_build_modules_components_selection_is_not_possible():
def test_build_modules_components_is_not_possible():
"""
If openssl declares different cmake_build_modules on ssl and crypto, in the consumer both
are included even if the cpp_info of the consumer declares:
def package_info(self):
self.cpp_info.requires = ["openssl::crypto"]
Because that information is defined later, not at "generate" time (building time).
The "cmake_build_module" property declared in the components is useless
"""
client = TestClient()
conanfile = textwrap.dedent("""
Expand All @@ -90,27 +74,19 @@ class Conan(ConanFile):
name = "openssl"
version = "1.0"
settings = "os", "arch", "compiler", "build_type"
exports_sources = ["ssl.cmake", "crypto.cmake", "root.cmake"]
exports_sources = ["crypto.cmake", "root.cmake"]
def package(self):
self.copy("*.cmake", dst="share/cmake")
def package_info(self):
ssl_module = os.path.join("share", "cmake", "ssl.cmake")
self.cpp_info.components["ssl"].set_property("cmake_build_modules", [ssl_module])
crypto_module = os.path.join("share", "cmake", "crypto.cmake")
self.cpp_info.components["crypto"].set_property("cmake_build_modules", [crypto_module])
root_module = os.path.join("share", "cmake", "root.cmake")
self.cpp_info.set_property("cmake_build_modules", [root_module])
""")

ssl_cmake = textwrap.dedent("""
function(ssl_message MESSAGE_OUTPUT)
message("SSL MESSAGE:${ARGV${0}}")
endfunction()
""")
crypto_cmake = textwrap.dedent("""
function(crypto_message MESSAGE_OUTPUT)
message("CRYPTO MESSAGE:${ARGV${0}}")
Expand All @@ -122,7 +98,6 @@ def package_info(self):
endfunction()
""")
client.save({"conanfile.py": conanfile,
"ssl.cmake": ssl_cmake,
"crypto.cmake": crypto_cmake,
"root.cmake": root_cmake})
client.run("create .")
Expand Down Expand Up @@ -151,17 +126,15 @@ def package_info(self):
project(test)
find_package(openssl CONFIG)
crypto_message("hello!")
ssl_message("hello!")
# The root build_module is not taken into account because there are components instead
# FIXME: Another alternative could be passing the global_cpp but without aggregating
# the components.
# root_message("hello!")
root_message("hello!")
""")
client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists})
# As we are requiring only "crypto" but it doesn't matter, it is not possible to include
# only crypto build_modules
client.run("create .")
assert "SSL MESSAGE:hello!" in client.out
assert "CRYPTO MESSAGE:hello!" in client.out
client.run("create .", assert_error=True)
assert 'Unknown CMake command "crypto_message"' in client.out

# Comment the function call
client.save({"CMakeLists.txt": cmakelists.replace("crypto", "#crypto")})
assert "ROOT MESSAGE:hello!" not in client.out

0 comments on commit e733137

Please sign in to comment.