Skip to content

Commit

Permalink
conan v2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceIm committed Oct 2, 2022
1 parent ea21ca0 commit 9569dbf
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 66 deletions.
3 changes: 1 addition & 2 deletions recipes/libtasn1/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ sources:
sha256: "0e0fb0903839117cb6e3b56e68222771bebf22ad7fc2295a0ed7d576e8d4329d"
patches:
"4.16.0":
- base_path: "source_subfolder"
patch_file: "patches/0001-do-not-add-static-to-functions-meant-to-be-built.patch"
- patch_file: "patches/0001-do-not-add-static-to-functions-meant-to-be-built.patch"
122 changes: 72 additions & 50 deletions recipes/libtasn1/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
from conans import ConanFile, tools, AutoToolsBuildEnvironment
from conans.errors import ConanInvalidConfiguration
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.env import VirtualBuildEnv
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, replace_in_file, rm, rmdir
from conan.tools.gnu import Autotools, AutotoolsToolchain
from conan.tools.layout import basic_layout
from conan.tools.microsoft import is_msvc, unix_path
import os

required_conan_version = ">=1.33.0"
required_conan_version = ">=1.52.0"


class LibTasn1Conan(ConanFile):
name = "libtasn1"
homepage = "https://www.gnu.org/software/libtasn1/"
description = "Libtasn1 is the ASN.1 library used by GnuTLS, p11-kit and some other packages."
topics = ("conan", "libtasn", "ASN.1", "cryptography")
topics = ("libtasn", "ASN.1", "cryptography")
url = "https://github.com/conan-io/conan-center-index"
settings = "os", "compiler", "arch", "build_type"
license = "LGPL-2.1-or-later"
exports_sources = "patches/**"

settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
Expand All @@ -23,74 +28,91 @@ class LibTasn1Conan(ConanFile):
"fPIC": True,
}

_autotools = None

@property
def _source_subfolder(self):
return "source_subfolder"
def _settings_build(self):
return getattr(self, "settings_build", self.settings)

def export_sources(self):
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
del self.settings.compiler.cppstd
del self.settings.compiler.libcxx
try:
del self.options.fPIC
except Exception:
pass
try:
del self.settings.compiler.cppstd
except Exception:
pass
try:
del self.settings.compiler.libcxx
except Exception:
pass

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

def validate(self):
if self.settings.compiler == "Visual Studio":
raise ConanInvalidConfiguration("Visual Studio is unsupported by libtasn1")

@property
def _settings_build(self):
return self.settings_build if hasattr(self, "settings_build") else self.settings
if is_msvc(self):
raise ConanInvalidConfiguration(f"{self.ref} doesn't support Visual Studio")

def build_requirements(self):
self.build_requires("bison/3.5.3")
if self._settings_build.os == "Windows" and not tools.get_env("CONAN_BASH_PATH"):
self.build_requires("msys2/cci.latest")
self.tool_requires("bison/3.8.2")
if self._settings_build.os == "Windows":
self.win_bash = True
if not self.conf.get("tools.microsoft.bash:path", default=False, check_type=bool):
self.tool_requires("msys2/cci.latest")

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

def _configure_autotools(self):
if self._autotools:
return self._autotools
self._autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows)
if self.settings.compiler != "Visual Studio":
self._autotools.flags.append("-std=c99")
conf_args = [
"--disable-doc",
]
if self.options.shared:
conf_args.extend(["--enable-shared", "--disable-static"])
else:
conf_args.extend(["--disable-shared", "--enable-static"])
self._autotools.configure(configure_dir=self._source_subfolder, args=conf_args)
return self._autotools
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)

def generate(self):
env = VirtualBuildEnv(self)
env.generate()

tc = AutotoolsToolchain(self)
if not is_msvc(self):
tc.extra_cflags.append("-std=c99")
tc.configure_args.append("--disable-doc")
# Workaround against SIP on macOS
if self.settings.os == "Macos" and self.options.shared:
tc.extra_ldflags.append("-Wl,-rpath,@loader_path/../lib")
tc.generate()

def _patch_sources(self):
apply_conandata_patches(self)
# TODO: use fix_apple_shared_install_name(self) instead, once https://github.com/conan-io/conan/issues/12107 fixed
replace_in_file(self, os.path.join(self.source_folder, "configure"),
"-install_name \\$rpath/",
"-install_name @rpath/")

def build(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
autotools = self._configure_autotools()
self._patch_sources()
autotools = Autotools(self)
autotools.configure()
autotools.make()

def package(self):
self.copy(pattern="LICENSE", src=self._source_subfolder, dst="licenses")
autotools = self._configure_autotools()
autotools.install()
tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
os.remove(os.path.join(self.package_folder, "lib", "libtasn1.la"))
copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
autotools = Autotools(self)
# TODO: replace by autotools.install() once https://github.com/conan-io/conan/issues/12153 fixed
autotools.install(args=[f"DESTDIR={unix_path(self, self.package_folder)}"])
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
rm(self, "*.la", os.path.join(self.package_folder, "lib"))

def package_info(self):
self.cpp_info.names["pkg_config"] = "libtasn1"
self.cpp_info.set_property("pkg_config_name", "libtasn1")
self.cpp_info.libs = ["tasn1"]
if not self.options.shared:
self.cpp_info.defines = ["ASN1_STATIC"]

# TODO: to remove in conan v2
bindir = os.path.join(self.package_folder, "bin")
self.output.info("Appending PATH environment variable: {}".format(bindir))
self.output.info(f"Appending PATH environment variable: {bindir}")
self.env_info.PATH.append(bindir)
25 changes: 17 additions & 8 deletions recipes/libtasn1/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)
project(test_package LANGUAGES C)

include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
conan_basic_setup()
find_package(libtasn1 REQUIRED CONFIG)
find_program(ASN1_PARSER NAMES asn1Parser)
if(NOT ASN1_PARSER)
message(FATAL_ERROR "asn1Parser not found")
endif()

add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/pkix_asn1_tab.c"
COMMAND asn1Parser -o "${CMAKE_CURRENT_BINARY_DIR}/pkix_asn1_tab.c" "${CMAKE_CURRENT_SOURCE_DIR}/pkix.asn"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/pkix.asn")
set(PKIX_ASN1_TAB_C "${CMAKE_CURRENT_BINARY_DIR}/pkix_asn1_tab.c")
set(PKIX_ASN_IN "${CMAKE_CURRENT_SOURCE_DIR}/pkix.asn")
add_custom_command(
COMMAND ${ASN1_PARSER} -o ${PKIX_ASN1_TAB_C} ${PKIX_ASN_IN}
OUTPUT ${PKIX_ASN1_TAB_C}
DEPENDS ${PKIX_ASN_IN}
)
add_custom_target(pkix_asn DEPENDS ${PKIX_ASN1_TAB_C})

add_executable(${PROJECT_NAME} test_package.c "${CMAKE_CURRENT_BINARY_DIR}/pkix_asn1_tab.c")
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
add_executable(${PROJECT_NAME} test_package.c ${PKIX_ASN1_TAB_C})
target_link_libraries(${PROJECT_NAME} PRIVATE libtasn1::libtasn1)
add_dependencies(${PROJECT_NAME} pkix_asn)
24 changes: 18 additions & 6 deletions recipes/libtasn1/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
import os


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv", "VirtualBuildEnv"
test_type = "explicit"

def layout(self):
cmake_layout(self)

def requirements(self):
self.requires(self.tested_reference_str)

def build_requirements(self):
self.tool_requires(self.tested_reference_str)

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

def test(self):
if not tools.cross_building(self.settings):
bin_path = os.path.join("bin", "test_package")
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
asn = os.path.join(self.source_folder, "pkix.asn")
self.run("{} {}".format(bin_path, asn), run_environment=True)
self.run(f"{bin_path} {asn}", env="conanrun")
24 changes: 24 additions & 0 deletions recipes/libtasn1/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.1)
project(test_package LANGUAGES C)

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

find_package(libtasn1 REQUIRED CONFIG)
find_program(ASN1_PARSER NAMES asn1Parser)
if(NOT ASN1_PARSER)
message(FATAL_ERROR "asn1Parser not found")
endif()

set(PKIX_ASN1_TAB_C "${CMAKE_CURRENT_BINARY_DIR}/pkix_asn1_tab.c")
set(PKIX_ASN_IN "${CMAKE_CURRENT_SOURCE_DIR}/../test_package/pkix.asn")
add_custom_command(
COMMAND ${ASN1_PARSER} -o ${PKIX_ASN1_TAB_C} ${PKIX_ASN_IN}
OUTPUT ${PKIX_ASN1_TAB_C}
DEPENDS ${PKIX_ASN_IN}
)
add_custom_target(pkix_asn DEPENDS ${PKIX_ASN1_TAB_C})

add_executable(${PROJECT_NAME} ../test_package/test_package.c ${PKIX_ASN1_TAB_C})
target_link_libraries(${PROJECT_NAME} PRIVATE libtasn1::libtasn1)
add_dependencies(${PROJECT_NAME} pkix_asn)
25 changes: 25 additions & 0 deletions recipes/libtasn1/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str)

def build_requirements(self):
self.build_requires(self.tested_reference_str)

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

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
asn = os.path.join(self.source_folder, os.pardir, "test_package", "pkix.asn")
self.run(f"{bin_path} {asn}", run_environment=True)

0 comments on commit 9569dbf

Please sign in to comment.