Skip to content

Commit

Permalink
(conan-io#18636) asn1c: migrate to Conan v2
Browse files Browse the repository at this point in the history
* asn1c: migrate to Conan v2

* asn1c: add support for non-MSVC Windows builds

* asn1c: bump deps

* asn1c: fix test_package

* asn1c: skip printing of PATH
  • Loading branch information
valgur committed Jan 1, 2024
1 parent dd8ec0b commit 0e9427c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 60 deletions.
111 changes: 65 additions & 46 deletions recipes/asn1c/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,101 @@
from conans import AutoToolsBuildEnvironment, tools
import os

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.files import get, rmdir, chdir
import os
from conan.tools.build import cross_building
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
from conan.tools.files import copy, get, rmdir, chdir
from conan.tools.gnu import Autotools, AutotoolsToolchain
from conan.tools.layout import basic_layout
from conan.tools.microsoft import is_msvc

required_conan_version = ">=1.47.0"
required_conan_version = ">=1.53.0"


class Asn1cConan(ConanFile):
name = "asn1c"
description = "The ASN.1 Compiler"
license = "BSD-2-Clause"
topics = ("asn.1", "compiler")
homepage = "https://lionet.info/asn1c"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://lionet.info/asn1c"
topics = ("asn.1", "compiler")

package_type = "application"
settings = "os", "arch", "compiler", "build_type"

_autotools = None

@property
def _source_subfolder(self):
return "source_subfolder"

@property
def _datarootdir(self):
return os.path.join(self.package_folder, "res")
def _settings_build(self):
return getattr(self, "settings_build", self.settings)

def configure(self):
del self.settings.compiler.libcxx
del self.settings.compiler.cppstd
self.settings.rm_safe("compiler.libcxx")
self.settings.rm_safe("compiler.cppstd")

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

def package_id(self):
del self.info.settings.compiler

def build_requirements(self):
self.tool_requires("bison/3.7.6")
self.tool_requires("flex/2.6.4")
if self._settings_build.os == "Windows":
self.win_bash = True
if not self.conf.get("tools.microsoft.bash:path", check_type=str):
self.tool_requires("msys2/cci.latest")
if is_msvc(self):
self.tool_requires("automake/1.16.5")
self.tool_requires("winflexbison/2.5.24")
else:
self.tool_requires("bison/3.8.2")
self.tool_requires("flex/2.6.4")
self.tool_requires("libtool/2.4.7")

def validate(self):
if self.settings.compiler == "Visual Studio":
if is_msvc(self):
raise ConanInvalidConfiguration("Visual Studio is not supported")

def package_id(self):
del self.info.settings.compiler

def source(self):
get(self, **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)
conf_args = [
"--datarootdir={}".format(tools.unix_path(self._datarootdir)),
]
self._autotools.configure(args=conf_args, configure_dir=self._source_subfolder)
return self._autotools
get(self, **self.conan_data["sources"][self.version], strip_root=True)

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

if not cross_building(self):
env = VirtualRunEnv(self)
env.generate(scope="build")

tc = AutotoolsToolchain(self)
tc.configure_args += ["--datarootdir=${prefix}/res"]
tc.generate()

def build(self):
with chdir(self, self._source_subfolder):
self.run("{} -fiv".format(tools.get_env("AUTORECONF")), win_bash=tools.os_info.is_windows)
autotools = self._configure_autotools()
autotools.make()
with chdir(self, self.source_folder):
autotools = Autotools(self)
autotools.autoreconf()
autotools.configure()
autotools.make()

def package(self):
self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
autotools = self._configure_autotools()
autotools.install()
copy(self, "LICENSE", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
with chdir(self, self.source_folder):
autotools = Autotools(self)
autotools.install()
rmdir(self, os.path.join(self.package_folder, "res", "doc"))
rmdir(self, os.path.join(self.package_folder, "res", "man"))

def package_info(self):
bin_path = os.path.join(self.package_folder, "bin")
self.output.info("Appending PATH environment variable: {}".format(bin_path))
self.env_info.PATH.append(bin_path)
self.cpp_info.includedirs = []
self.cpp_info.libdirs = []
self.cpp_info.frameworkdirs = []
self.cpp_info.resdirs = ["res"]

# asn1c cannot use environment variables to specify support files path
# so `SUPPORT_PATH` should be propagated to command line invocation to `-S` argument
self.env_info.SUPPORT_PATH = os.path.join(self.package_folder, "res/asn1c")
support_path = os.path.join(self.package_folder, "res", "asn1c")
self.buildenv_info.define_path("SUPPORT_PATH", support_path)

self.cpp_info.includedirs = []
# TODO: to remove in conan v2
bin_path = os.path.join(self.package_folder, "bin")
self.env_info.PATH.append(bin_path)
self.env_info.SUPPORT_PATH = support_path
9 changes: 5 additions & 4 deletions recipes/asn1c/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.15)
project(test_package C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
if (NOT DEFINED ENV{SUPPORT_PATH})
message(FATAL_ERROR "SUPPORT_PATH environment variable not defined")
endif()

set(STANDARD_ASN1_FILES
BIT_STRING.c
Expand Down Expand Up @@ -39,7 +40,7 @@ set(GENERATED_FILES

add_custom_command(
OUTPUT ${GENERATED_FILES} ${STANDARD_ASN1_FILES}
COMMAND asn1c -S ${SUPPORT_PATH} MyModule.asn1
COMMAND asn1c -S "$ENV{SUPPORT_PATH}" MyModule.asn1
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS MyModule.asn1
)
Expand Down
23 changes: 13 additions & 10 deletions recipes/asn1c/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
from conans import CMake
import os

from conan import ConanFile
from conan.tools.build import can_run, cross_building
from conan.tools.cmake import cmake_layout, CMake
from conan.tools.files import copy
from conan.tools.build import cross_building
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"

generators = "cmake"
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv", "VirtualBuildEnv"
test_type = "explicit"

def build_requirements(self):
self.tool_requires(str(self.requires["asn1c"]))
self.tool_requires(self.tested_reference_str)

def layout(self):
cmake_layout(self)

def build(self):
if not cross_building(self):
copy(self, "MyModule.asn1", src=self.source_folder, dst=self.build_folder)
cmake = CMake(self)
cmake.definitions["SUPPORT_PATH"] = self.deps_env_info["asn1c"].SUPPORT_PATH
cmake.configure()
cmake.build()

def test(self):
if not cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")

0 comments on commit 0e9427c

Please sign in to comment.