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

aws-c-cal: modernize more for conan v2 + bump openssl #17100

Merged
merged 10 commits into from
Jun 1, 2023
90 changes: 52 additions & 38 deletions recipes/aws-c-cal/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from conan import ConanFile
from conan.tools.apple import is_apple_os
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, copy, get, rmdir
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, rmdir, save
from conan.tools.scm import Version
import os
import textwrap

required_conan_version = ">=1.51.3"
required_conan_version = ">=1.53.0"


class AwsCCal(ConanFile):
Expand All @@ -14,7 +15,8 @@ class AwsCCal(ConanFile):
topics = ("aws", "amazon", "cloud", "cal", "crypt", )
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/awslabs/aws-c-cal"
license = "Apache-2.0",
license = "Apache-2.0"
package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
Expand All @@ -27,42 +29,37 @@ class AwsCCal(ConanFile):

@property
def _needs_openssl(self):
return self.settings.os != "Windows" and not is_apple_os(self)
return not (self.settings.os == "Windows" or is_apple_os(self))

def export_sources(self):
for p in self.conan_data.get("patches", {}).get(self.version, []):
copy(self, p["patch_file"], self.recipe_folder, self.export_sources_folder)
export_conandata_patches(self)

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.shared:
del self.options.fPIC
try:
del self.settings.compiler.libcxx
except Exception:
pass
try:
del self.settings.compiler.cppstd
except Exception:
pass
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")

def layout(self):
cmake_layout(self, src_folder="src")

def requirements(self):
if Version(self.version) <= "0.5.11":
self.requires("aws-c-common/0.6.11")
# transitive_libs probably not strictly required, but it's impossible to write a robust test package
# without it for conan v2 (we would have to required aws-c-common in test package, but we can't know
# which version to require in test package)
self.requires("aws-c-common/0.6.11", transitive_headers=True, transitive_libs=True)
else:
self.requires("aws-c-common/0.8.2")
self.requires("aws-c-common/0.8.2", transitive_headers=True, transitive_libs=True)
if self._needs_openssl:
self.requires("openssl/1.1.1s")
self.requires("openssl/[>=1.1 <4]")

def source(self):
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
Expand All @@ -84,30 +81,43 @@ def package(self):
cmake.install()
rmdir(self, os.path.join(self.package_folder, "lib", "aws-c-cal"))

# TODO: to remove in conan v2 once legacy generators removed
self._create_cmake_module_alias_targets(
os.path.join(self.package_folder, self._module_file_rel_path),
{"AWS::aws-c-cal": "aws-c-cal::aws-c-cal"}
)

def _create_cmake_module_alias_targets(self, module_file, targets):
content = ""
for alias, aliased in targets.items():
content += textwrap.dedent(f"""\
if(TARGET {aliased} AND NOT TARGET {alias})
add_library({alias} INTERFACE IMPORTED)
set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased})
endif()
""")
save(self, module_file, content)

@property
def _module_file_rel_path(self):
return os.path.join("lib", "cmake", f"conan-official-{self.name}-targets.cmake")

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "aws-c-cal")
self.cpp_info.set_property("cmake_target_name", "AWS::aws-c-cal")

self.cpp_info.filenames["cmake_find_package"] = "aws-c-cal"
self.cpp_info.filenames["cmake_find_package_multi"] = "aws-c-cal"
self.cpp_info.names["cmake_find_package"] = "AWS"
self.cpp_info.names["cmake_find_package_multi"] = "AWS"
self.cpp_info.components["aws-c-cal-lib"].names["cmake_find_package"] = "aws-c-cal"
self.cpp_info.components["aws-c-cal-lib"].names["cmake_find_package_multi"] = "aws-c-cal"
self.cpp_info.components["aws-c-cal-lib"].set_property("cmake_target_name", "AWS::aws-c-cal")

self.cpp_info.components["aws-c-cal-lib"].libs = ["aws-c-cal"]
self.cpp_info.components["aws-c-cal-lib"].requires = ["aws-c-common::aws-c-common-lib"]
self.cpp_info.libs = ["aws-c-cal"]
self.cpp_info.requires = ["aws-c-common::aws-c-common"]
if self.options.shared:
self.cpp_info.defines.append("AWS_CAL_USE_IMPORT_EXPORT")
if self.settings.os == "Windows":
self.cpp_info.components["aws-c-cal-lib"].system_libs.append("ncrypt")
self.cpp_info.system_libs.append("ncrypt")
elif is_apple_os(self):
self.cpp_info.components["aws-c-cal-lib"].frameworks.extend(["CoreFoundation", "Security"])
self.cpp_info.frameworks.extend(["CoreFoundation", "Security"])
elif self.settings.os in ("FreeBSD", "Linux"):
self.cpp_info.components["aws-c-cal-lib"].system_libs.append("dl")
self.cpp_info.system_libs.append("dl")

self.user_info.with_openssl = self._needs_openssl
if self._needs_openssl:
self.cpp_info.components["aws-c-cal-lib"].requires.append("openssl::crypto")
self.cpp_info.requires.append("openssl::crypto")
if not self.dependencies["openssl"].options.shared:
# aws-c-cal does not statically link to openssl and searches dynamically for openssl symbols .
# Mark these as undefined so the linker will include them.
Expand All @@ -124,5 +134,9 @@ def package_info(self):
"HMAC_CTX_init", "HMAC_CTX_cleanup", "HMAC_CTX_reset",
])
crypto_link_flags = "-Wl," + ",".join(f"-u{symbol}" for symbol in crypto_symbols)
self.cpp_info.components["aws-c-cal-lib"].exelinkflags.append(crypto_link_flags)
self.cpp_info.components["aws-c-cal-lib"].sharedlinkflags.append(crypto_link_flags)
self.cpp_info.exelinkflags.append(crypto_link_flags)
self.cpp_info.sharedlinkflags.append(crypto_link_flags)

# TODO: to remove in conan v2 once cmake_find_package* generators removed
self.cpp_info.build_modules["cmake_find_package"] = [self._module_file_rel_path]
self.cpp_info.build_modules["cmake_find_package_multi"] = [self._module_file_rel_path]
3 changes: 2 additions & 1 deletion recipes/aws-c-cal/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.1)
project(test_package LANGUAGES C)

find_package(aws-c-cal REQUIRED CONFIG)
find_package(aws-c-common REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE AWS::aws-c-cal)
target_link_libraries(${PROJECT_NAME} PRIVATE AWS::aws-c-cal AWS::aws-c-common)
9 changes: 7 additions & 2 deletions recipes/aws-c-cal/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from conan import ConanFile
from conan.tools.apple import is_apple_os
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
import os
Expand All @@ -10,6 +11,10 @@ class TestPackageConan(ConanFile):
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"
test_type = "explicit"

@property
def _needs_openssl(self):
return not (self.settings.os == "Windows" or is_apple_os(self))

def layout(self):
cmake_layout(self)

Expand All @@ -25,7 +30,7 @@ def test(self):
if can_run(self):
stream = io.StringIO()
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
self.run(bin_path, env="conanrun", output=stream)
self.run(bin_path, stream, env="conanrun")
self.output.info(stream.getvalue())
if self.deps_user_info["aws-c-cal"].with_openssl == "True":
if self._needs_openssl:
assert "found static libcrypto" in stream.getvalue()
8 changes: 3 additions & 5 deletions recipes/aws-c-cal/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
cmake_minimum_required(VERSION 3.1)
project(test_package LANGUAGES C)
project(test_package)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(aws-c-cal REQUIRED CONFIG)

add_executable(${PROJECT_NAME} ../test_package/test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE AWS::aws-c-cal)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package
${CMAKE_CURRENT_BINARY_DIR}/test_package)
9 changes: 7 additions & 2 deletions recipes/aws-c-cal/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from conans import ConanFile, CMake, tools
from conan.tools.apple import is_apple_os
import os
import io

Expand All @@ -7,6 +8,10 @@ class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"

@property
def _needs_openssl(self):
return not (self.settings.os == "Windows" or is_apple_os(self))

def build(self):
cmake = CMake(self)
cmake.configure()
Expand All @@ -16,7 +21,7 @@ def test(self):
if not tools.cross_building(self):
stream = io.StringIO()
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True, output=stream)
self.run(bin_path, stream, run_environment=True)
self.output.info(stream.getvalue())
if self.deps_user_info["aws-c-cal"].with_openssl == "True":
if self._needs_openssl:
assert "found static libcrypto" in stream.getvalue()