Skip to content

Commit

Permalink
(conan-io#14083) gperf: conan v2 support
Browse files Browse the repository at this point in the history
* conan v2 support

* ensure to build from source folder

* in source build for windows

* prevent msys2 doing erroneous path conversion

Co-authored-by: Luis Caro Campos <3535649+jcar87@users.noreply.github.com>

Co-authored-by: Luis Caro Campos <3535649+jcar87@users.noreply.github.com>
  • Loading branch information
2 people authored and StellaSmith committed Feb 2, 2023
1 parent 47b463f commit bf7beae
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 73 deletions.
1 change: 0 additions & 1 deletion recipes/gperf/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ sources:
patches:
"3.1":
- patch_file: "patches/0001-remove-register-keyword.patch"
base_path: "source_subfolder"
135 changes: 67 additions & 68 deletions recipes/gperf/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from conans import ConanFile, tools, AutoToolsBuildEnvironment
from contextlib import contextmanager
from conan import ConanFile
from conan.tools.env import Environment, VirtualBuildEnv
from conan.tools.files import apply_conandata_patches, chdir, copy, export_conandata_patches, get, rmdir
from conan.tools.gnu import Autotools, AutotoolsToolchain
from conan.tools.layout import basic_layout
from conan.tools.microsoft import is_msvc, unix_path
from conan.tools.scm import Version
import os

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


class GperfConan(ConanFile):
Expand All @@ -11,88 +16,82 @@ class GperfConan(ConanFile):
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://www.gnu.org/software/gperf"
description = "GNU gperf is a perfect hash function generator"
topics = ("gperf", "hash-generator", "hash")
topics = ("hash-generator", "hash")

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

exports_sources = "patches/*"
_autotools = None

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

@property
def _is_msvc(self):
return self.settings.compiler == "Visual Studio"
def export_sources(self):
export_conandata_patches(self)

def layout(self):
basic_layout(self, src_folder="src")
self.folders.build = self.folders.source

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

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

def build_requirements(self):
if self._is_msvc:
self.build_requires("automake/1.16.3")
if self._settings_build.os == "Windows" and not tools.get_env("CONAN_BASH_PATH"):
self.build_requires("msys2/cci.latest")
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")

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

def _configure_autotools(self):
if not self._autotools:
self._autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows)
if self._is_msvc and tools.Version(self.settings.compiler.version) >= "12":
self._autotools.flags.append("-FS")
self._autotools.configure()
return self._autotools

@property
def _user_info_build(self):
return getattr(self, "user_info_build", self.deps_user_info)

@contextmanager
def _build_context(self):
with tools.chdir(self._source_subfolder):
if self._is_msvc:
with tools.vcvars(self.settings):
env = {
"CC": "{} cl -nologo".format(tools.unix_path(self._user_info_build["automake"].compile)),
"CXX": "{} cl -nologo".format(tools.unix_path(self._user_info_build["automake"].compile)),
"CFLAGS": "-{}".format(self.settings.compiler.runtime),
"CXXLAGS": "-{}".format(self.settings.compiler.runtime),
"CPPFLAGS": "-D_WIN32_WINNT=_WIN32_WINNT_WIN8",
"LD": "link",
"NM": "dumpbin -symbols",
"STRIP": ":",
"AR": "{} lib".format(tools.unix_path(self._user_info_build["automake"].ar_lib)),
"RANLIB": ":",
}
with tools.environment_append(env):
yield
else:
yield
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 (self.settings.compiler == "Visual Studio" and Version(self.settings.compiler.version) >= "12") or \
(self.settings.compiler == "msvc" and Version(self.settings.compiler.version) >= "180"):
tc.extra_cflags.append("-FS")
tc.extra_cxxflags.append("-FS")
tc.generate()

if is_msvc(self):
env = Environment()
compile_wrapper = unix_path(self, os.path.join(self.source_folder, "build-aux", "compile"))
ar_wrapper = unix_path(self, os.path.join(self.source_folder, "build-aux", "ar-lib"))
env.define("CC", f"{compile_wrapper} cl -nologo")
env.define("CXX", f"{compile_wrapper} cl -nologo")
env.append("CPPFLAGS", "-D_WIN32_WINNT=_WIN32_WINNT_WIN8")
env.define("LD", "link -nologo")
env.define("AR", f"{ar_wrapper} \"lib -nologo\"")
env.define("NM", "dumpbin -symbols")
env.define("OBJDUMP", ":")
env.define("RANLIB", ":")
env.define("STRIP", ":")

#Prevent msys2 from performing erroneous path conversions for C++ files
# when invoking cl.exe as this is already handled by the compile wrapper.
env.define("MSYS2_ARG_CONV_EXCL", "-Tp")
env.vars(self).save_script("conanbuild_gperf_msvc")

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

def package(self):
self.copy("COPYING", dst="licenses", src=self._source_subfolder)
with self._build_context():
autotools = self._configure_autotools()
autotools.install()
tools.rmdir(os.path.join(self.package_folder, "share"))
copy(self, "COPYING", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
autotools = Autotools(self)
with chdir(self, self.source_folder):
# 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, "share"))

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

# TODO: to remove in conan v2
self.env_info.PATH.append(os.path.join(self.package_folder, "bin"))
12 changes: 8 additions & 4 deletions recipes/gperf/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from conans import ConanFile, tools
from conan import ConanFile


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

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

def test(self):
if not tools.cross_building(self):
self.run("gperf --version", run_environment=True)
self.run("gperf --version")
9 changes: 9 additions & 0 deletions recipes/gperf/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from conans import ConanFile, tools


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

def test(self):
if not tools.cross_building(self):
self.run("gperf --version", run_environment=True)

0 comments on commit bf7beae

Please sign in to comment.