(#14801) libpcap: conan v2 support

* conan v2 support

* remove pcap-config script
This commit is contained in:
SpaceIm
2022-12-19 17:25:33 +01:00
committed by GitHub
parent 187b33c9ca
commit e23aef7381
6 changed files with 131 additions and 116 deletions

View File

@@ -1,7 +0,0 @@
cmake_minimum_required(VERSION 3.1)
project(cmake_wrapper)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(KEEP_RPATHS)
add_subdirectory(source_subfolder)

View File

@@ -1,11 +1,19 @@
from conan.tools.microsoft import msvc_runtime_flag
from conans import AutoToolsBuildEnvironment, tools, ConanFile, CMake
from conans.errors import ConanInvalidConfiguration
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.apple import fix_apple_shared_install_name, is_apple_os
from conan.tools.build import cross_building
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
from conan.tools.files import chdir, copy, get, rm, rmdir
from conan.tools.gnu import Autotools, AutotoolsDeps, AutotoolsToolchain
from conan.tools.layout import basic_layout
from conan.tools.microsoft import is_msvc, is_msvc_static_runtime
from conan.tools.scm import Version
import glob
import os
import shutil
required_conan_version = ">=1.36.0"
required_conan_version = ">=1.53.0"
class LibPcapConan(ConanFile):
@@ -21,33 +29,18 @@ class LibPcapConan(ConanFile):
"shared": [True, False],
"fPIC": [True, False],
"enable_libusb": [True, False],
"enable_universal": [True, False, "deprecated"],
}
default_options = {
"shared": False,
"fPIC": True,
"enable_libusb": False,
"enable_universal": "deprecated",
}
exports_sources = "CMakeLists.txt"
generators = "cmake"
_cmake = None
_autotools = None
# TODO: Add dbus-glib when available
# TODO: Add libnl-genl when available
# TODO: Add libbluetooth when available
# TODO: Add libibverbs when available
@property
def _source_subfolder(self):
return "source_subfolder"
@property
def _is_msvc(self):
return str(self.settings.compiler) in ["Visual Studio", "msvc"]
@property
def _settings_build(self):
return getattr(self, "settings_build", self.settings)
@@ -60,98 +53,93 @@ class LibPcapConan(ConanFile):
def configure(self):
if self.options.shared:
del self.options.fPIC
del self.settings.compiler.libcxx
del self.settings.compiler.cppstd
if self.options.enable_universal != "deprecated":
self.output.warn("enable_universal is a deprecated option. Do not use.")
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")
def layout(self):
if self.settings.os == "Windows":
cmake_layout(self, src_folder="src")
else:
basic_layout(self, src_folder="src")
def requirements(self):
if self.options.get_safe("enable_libusb"):
self.requires("libusb/1.0.24")
self.requires("libusb/1.0.26")
def validate(self):
if tools.Version(self.version) < "1.10.0" and self.settings.os == "Macos" and self.options.shared:
raise ConanInvalidConfiguration("libpcap {} can not be built as shared on OSX.".format(self.version))
if hasattr(self, "settings_build") and tools.cross_building(self) and \
self.options.shared and tools.is_apple_os(self.settings.os):
if Version(self.version) < "1.10.0" and self.settings.os == "Macos" and self.options.shared:
raise ConanInvalidConfiguration(f"{self.ref} can not be built as shared on OSX.")
if hasattr(self, "settings_build") and cross_building(self) and \
self.options.shared and is_apple_os(self):
raise ConanInvalidConfiguration("cross-build of libpcap shared is broken on Apple")
if tools.Version(self.version) < "1.10.1" and self.settings.os == "Windows" and not self.options.shared:
raise ConanInvalidConfiguration("libpcap can not be built static on Windows below version 1.10.1.")
def package_id(self):
del self.info.options.enable_universal
if Version(self.version) < "1.10.1" and self.settings.os == "Windows" and not self.options.shared:
raise ConanInvalidConfiguration(f"{self.ref} can not be built static on Windows")
def build_requirements(self):
if self._settings_build.os == "Windows":
self.build_requires("winflexbison/2.5.24")
if is_msvc(self, build_context=True):
self.tool_requires("winflexbison/2.5.24")
else:
self.build_requires("bison/3.7.6")
self.build_requires("flex/2.6.4")
self.tool_requires("bison/3.8.2")
self.tool_requires("flex/2.6.4")
def source(self):
tools.get(**self.conan_data["sources"][self.version],
destination=self._source_subfolder, strip_root=True)
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)
def _configure_autotools(self):
if self._autotools:
return self._autotools
self._autotools = AutoToolsBuildEnvironment(self)
self._autotools.libs = []
yes_no = lambda v: "yes" if v else "no"
configure_args = [
"--enable-shared={}".format(yes_no(self.options.shared)),
"--enable-static={}".format(yes_no(not self.options.shared)),
"--enable-usb={}".format(yes_no(self.options.get_safe("enable_libusb"))),
"--disable-universal",
"--without-libnl",
"--disable-bluetooth",
"--disable-packet-ring",
"--disable-dbus",
"--disable-rdma",
]
if tools.cross_building(self):
target_os = "linux" if self.settings.os == "Linux" else "null"
configure_args.append("--with-pcap=%s" % target_os)
elif "arm" in self.settings.arch and self.settings.os == "Linux":
configure_args.append("--host=arm-linux")
self._autotools.configure(args=configure_args, configure_dir=self._source_subfolder)
# Relocatable shared lib on macOS
tools.replace_in_file("Makefile", "-install_name $(libdir)/", "-install_name @rpath/")
return self._autotools
def generate(self):
VirtualBuildEnv(self).generate()
def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
if not self.options.shared:
self._cmake.definitions["ENABLE_REMOTE"] = False
if self._is_msvc:
self._cmake.definitions["USE_STATIC_RT"] = "MT" in msvc_runtime_flag(self)
if self.settings.os == "Windows":
tc = CMakeToolchain(self)
if not self.options.shared:
tc.variables["ENABLE_REMOTE"] = False
if is_msvc(self):
tc.variables["USE_STATIC_RT"] = is_msvc_static_runtime(self)
else:
# Don't force -static-libgcc for MinGW, because conan users expect
# to inject this compilation flag themselves
tc.variables["USE_STATIC_RT"] = False
tc.generate()
else:
# Don't force -static-libgcc for MinGW, because conan users expect
# to inject this compilation flag themselves
self._cmake.definitions["USE_STATIC_RT"] = False
self._cmake.configure()
return self._cmake
if not cross_building(self):
VirtualRunEnv(self).generate(scope="build")
tc = AutotoolsToolchain(self)
yes_no = lambda v: "yes" if v else "no"
tc.configure_args.extend([
f"--enable-usb={yes_no(self.options.get_safe('enable_libusb'))}",
"--disable-universal",
"--without-libnl",
"--disable-bluetooth",
"--disable-packet-ring",
"--disable-dbus",
"--disable-rdma",
])
if cross_building(self):
target_os = "linux" if self.settings.os == "Linux" else "null"
tc.configure_args.append(f"--with-pcap={target_os}")
elif "arm" in self.settings.arch and self.settings.os == "Linux":
tc.configure_args.append("--host=arm-linux")
tc.generate()
AutotoolsDeps(self).generate()
def build(self):
if self.settings.os == "Windows":
cmake = self._configure_cmake()
cmake = CMake(self)
cmake.configure()
cmake.build()
else:
autotools = self._configure_autotools()
autotools = Autotools(self)
autotools.configure()
autotools.make()
def package(self):
self.copy("LICENSE", src=self._source_subfolder, dst="licenses")
copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
if self.settings.os == "Windows":
cmake = self._configure_cmake()
cmake = CMake(self)
cmake.install()
tools.remove_files_by_mask(os.path.join(self.package_folder, "bin"), "*.pdb")
if self.options.shared:
tools.remove_files_by_mask(os.path.join(self.package_folder, "lib"), "pcap_static.lib")
def flatten_filetree(folder):
for file in glob.glob(folder + "/**/*"):
@@ -160,24 +148,27 @@ class LibPcapConan(ConanFile):
os.rmdir(subdir)
# libpcap installs into a subfolder like x64 or amd64
with tools.chdir(self.package_folder):
with chdir(self, self.package_folder):
flatten_filetree("bin")
flatten_filetree("lib")
else:
autotools = self._configure_autotools()
autotools.install()
tools.rmdir(os.path.join(self.package_folder, "share"))
tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
rm(self, "*.pdb", os.path.join(self.package_folder, "bin"))
if self.options.shared:
tools.remove_files_by_mask(os.path.join(self.package_folder, "lib"), "*.a")
rm(self, "pcap_static.lib", os.path.join(self.package_folder, "lib"))
rm(self, "libpcap.a", os.path.join(self.package_folder, "lib"))
else:
autotools = Autotools(self)
autotools.install()
rmdir(self, os.path.join(self.package_folder, "bin"))
rmdir(self, os.path.join(self.package_folder, "share"))
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
if self.options.shared:
rm(self, "*.a", os.path.join(self.package_folder, "lib"))
fix_apple_shared_install_name(self)
def package_info(self):
self.cpp_info.set_property("pkg_config_name", "libpcap")
suffix = "_static" if self.settings.os == "Windows" and not self.options.shared else ""
self.cpp_info.libs = ["pcap{}".format(suffix)]
self.cpp_info.libs = [f"pcap{suffix}"]
if self.settings.os == "Windows":
self.cpp_info.system_libs = ["ws2_32"]
bindir = os.path.join(self.package_folder, "bin")
self.output.info("Appending PATH environment variable: {}".format(bindir))
self.env_info.PATH.append(bindir)

View File

@@ -1,10 +1,7 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
project(test_package LANGUAGES C)
find_package(libpcap REQUIRED CONFIG)
add_executable(test_package test_package.c)
target_link_libraries(test_package libpcap::libpcap)
target_link_libraries(test_package PRIVATE libpcap::libpcap)

View File

@@ -1,10 +1,19 @@
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", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
test_type = "explicit"
def layout(self):
cmake_layout(self)
def requirements(self):
self.requires(self.tested_reference_str)
def build(self):
cmake = CMake(self)
@@ -12,6 +21,6 @@ class TestPackageConan(ConanFile):
cmake.build()
def test(self):
if not tools.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.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")

View File

@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.1)
project(test_package)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package
${CMAKE_CURRENT_BINARY_DIR}/test_package)

View File

@@ -0,0 +1,17 @@
from conans import ConanFile, CMake, tools
import os
class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
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")
self.run(bin_path, run_environment=True)