Skip to content

Commit

Permalink
(#12835) libmodbus: conan v2 support
Browse files Browse the repository at this point in the history
* modernize libmodbus for conanv2

* fix some minor linter warnings

* add test_v1_package

* temporarely disable vcvars envs definition

* fix missing assignment

* fix wrong cross_building import

* try requiring automake for macos builds

* Remove project name from topics

Co-authored-by: Uilian Ries <uilianries@gmail.com>

* Use is_msvc

Co-authored-by: Uilian Ries <uilianries@gmail.com>

* Remove unneeded comments

Co-authored-by: Uilian Ries <uilianries@gmail.com>

* Fix conan.tools.microsoft imports

* revert Macos check

* add MSVC specific stuff ported following PR #12916

* add apple and destdir fixes

* fix linting

* remove autoreconf, not really needed

* set win_bash in configure stage

* use deps_user_info instead of conf to get ar_lib

* add -FS to extra_cflags

* replace wsock32 system_libs with ws2_32 (see libmodbus configure.ac)

* minor changes

* fix Windows AR arguments

* Apply suggestions for test_v1_package

Co-authored-by: Uilian Ries <uilianries@gmail.com>

* revert removal of _settings_build for checking if msys2 is needed

* Update recipes/libmodbus/all/conanfile.py

Co-authored-by: Uilian Ries <uilianries@gmail.com>

* Update recipes/libmodbus/all/conanfile.py

Co-authored-by: Uilian Ries <uilianries@gmail.com>

* Re-use test_package.c

Co-authored-by: Uilian Ries <uilianries@gmail.com>
  • Loading branch information
dubvulture and uilianries authored Sep 30, 2022
1 parent f5d763e commit a484011
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 85 deletions.
1 change: 0 additions & 1 deletion recipes/libmodbus/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ sources:
patches:
"3.1.6":
- patch_file: "patches/0001-msvc-fixes.patch"
base_path: "source_subfolder"
174 changes: 100 additions & 74 deletions recipes/libmodbus/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
from conans import AutoToolsBuildEnvironment, ConanFile, tools
from contextlib import contextmanager
from conan import ConanFile
from conan.tools.apple import is_apple_os, fix_apple_shared_install_name
from conan.tools.build import check_min_cppstd
from conan.tools.env import VirtualBuildEnv
from conan.tools.files import apply_conandata_patches, copy, get, replace_in_file, rename, rm, rmdir
from conan.tools.gnu import Autotools, AutotoolsToolchain, AutotoolsDeps, PkgConfigDeps
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.51.3"


class LibmodbusConan(ConanFile):
name = "libmodbus"
description = "libmodbus is a free software library to send/receive data according to the Modbus protocol"
homepage = "https://libmodbus.org/"
topics = ("conan", "libmodbus", "modbus", "protocol", "industry", "automation")
topics = ("modbus", "protocol", "industry", "automation")
license = "LGPL-2.1"
url = "https://github.com/conan-io/conan-center-index"
exports_sources = "patches/**"
Expand All @@ -23,12 +31,6 @@ class LibmodbusConan(ConanFile):
"fPIC": True,
}

_autotools = None

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

@property
def _settings_build(self):
return self.settings_build if hasattr(self, "settings_build") else self.settings
Expand All @@ -39,85 +41,109 @@ def config_options(self):

def configure(self):
if self.options.shared:
del self.options.fPIC
del self.settings.compiler.libcxx
del self.settings.compiler.cppstd
try:
del self.options.fPIC # once removed by config_options, need try..except for a second del
except Exception:
pass
try:
del self.settings.compiler.libcxx # for plain C projects only
except Exception:
pass
try:
del self.settings.compiler.cppstd # for plain C projects only
except Exception:
pass
if self.settings.os == "Windows":
self.win_bash = True

def build_requirements(self):
if self.settings.compiler == "Visual Studio":
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 is_msvc(self):
self.tool_requires("automake/1.16.3")
# see https://github.com/conan-io/conan/issues/11969
bash_path = os.getenv("CONAN_BASH_PATH") or self.conf.get("tools.microsoft.bash:path")
if self._settings_build.os == "Windows" and not bash_path:
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)
conf_args = [
"--disable-tests",
"--without-documentation",
"--prefix={}".format(tools.unix_path(self.package_folder)),
]
if self.options.shared:
conf_args.extend(["--enable-shared", "--disable-static"])
else:
conf_args.extend(["--enable-static", "--disable-shared"])
if self.settings.compiler == "Visual Studio" and tools.Version(self.settings.compiler.version) >= "12":
if self.settings.build_type in ("Debug", "RelWithDebInfo"):
self._autotools.flags.append("-FS")
self._autotools.configure(args=conf_args, configure_dir=self._source_subfolder)
return self._autotools

@contextmanager
def _build_context(self):
if self.settings.compiler == "Visual Studio":
with tools.vcvars(self.settings):
env = {
"CC": "cl -nologo",
"CXX": "cl -nologo",
"LD": "link -nologo",
"AR": "{} \"lib -nologo -verbose\"".format(tools.unix_path(self.deps_user_info["automake"].ar_lib)),
"RANLIB": ":",
"STRING": ":",
"NM": "dumpbin -symbols"}
with tools.environment_append(env):
yield
else:
yield
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)

def validate(self):
# validate the minimum cpp standard supported
if self.info.settings.compiler.cppstd:
check_min_cppstd(self, 11)

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

def generate(self):
tc = AutotoolsToolchain(self)
tc.configure_args.append("--without-documentation")
tc.configure_args.append("--disable-tests")

# the following MSVC specific part has been ported from conan v1 following https://github.com/conan-io/conan-center-index/pull/12916
if is_msvc(self) and Version(self.settings.compiler.version) >= "12":
tc.extra_cflags.append("-FS")

env = tc.environment()

if is_msvc(self):
ar_lib = unix_path(self, self.deps_user_info['automake'].ar_lib)
env.define("CC", "cl -nologo")
env.define("CXX", "cl -nologo")
env.define("LD", "link -nologo")
env.define("AR", f"{ar_lib} \"lib -nologo -verbose\"")
env.define("RANLIB", ":")
env.define("STRING", ":")
env.define("NM", "dumpbin -symbols")

tc.generate(env)

# generate dependencies for pkg-config
tc = PkgConfigDeps(self)
tc.generate()

# generate dependencies for autotools
tc = AutotoolsDeps(self)
tc.generate()

# inject tools_require env vars in build context
ms = VirtualBuildEnv(self)
ms.generate()

def _patch_sources(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
apply_conandata_patches(self)
if not self.options.shared:
for decl in ("__declspec(dllexport)", "__declspec(dllimport)"):
tools.replace_in_file(os.path.join(self._source_subfolder, "src", "modbus.h"), decl, "")
replace_in_file(self, os.path.join(self.source_folder, "src", "modbus.h"), decl, "")

def build(self):
self._patch_sources()
with self._build_context():
autotools = self._configure_autotools()
autotools.make()
autotools = Autotools(self)
autotools.configure()
autotools.make()

def package(self):
self.copy("COPYING*", src=self._source_subfolder, dst="licenses")
with self._build_context():
autotools = self._configure_autotools()
autotools.install()

os.unlink(os.path.join(self.package_folder, "lib", "libmodbus.la"))
tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
tools.rmdir(os.path.join(self.package_folder, "share"))
copy(self, pattern="COPYING*", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
autotools = Autotools(self)
# see https://github.com/conan-io/conan/issues/12006
autotools.install(args=[f"DESTDIR={unix_path(self, self.package_folder)}"])

if is_apple_os(self):
fix_apple_shared_install_name(self)

rm(self, "*.la", os.path.join(self.package_folder, "lib"))
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
rmdir(self, os.path.join(self.package_folder, "share"))
if self.settings.compiler == "Visual Studio" and self.options.shared:
tools.rename(os.path.join(self.package_folder, "lib", "modbus.dll.lib"),
os.path.join(self.package_folder, "lib", "modbus.lib"))
rename(self,
os.path.join(self.package_folder, "lib", "modbus.dll.lib"),
os.path.join(self.package_folder, "lib", "modbus.lib"))

def package_info(self):
self.cpp_info.names["pkg_config"] = "libmodbus"
self.cpp_info.includedirs.append(os.path.join("include", "modbus"))
self.cpp_info.libs = ["modbus"]

self.cpp_info.set_property("pkg_config_name", "libmodbus")

if self.settings.os == "Windows" and not self.options.shared:
self.cpp_info.system_libs = ["wsock32"]
self.cpp_info.system_libs = ["ws2_32"]
1 change: 0 additions & 1 deletion recipes/libmodbus/all/patches/0001-msvc-fixes.patch
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
oldincludedir = @oldincludedir@
pdfdir = @pdfdir@
prefix = @prefix@
diff -ur source_original/source_subfolder/src/modbus-private.h source/source_subfolder/src/modbus-private.h
--- src/modbus-private.h
+++ src/modbus-private.h
@@ -13,7 +13,7 @@
Expand Down
7 changes: 3 additions & 4 deletions recipes/libmodbus/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.8)
project(test_package C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
find_package(libmodbus REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
target_link_libraries(${PROJECT_NAME} PRIVATE libmodbus::libmodbus)

19 changes: 14 additions & 5 deletions recipes/libmodbus/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os


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

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

def layout(self):
cmake_layout(self)

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

def test(self):
if not tools.cross_building(self.settings):
self.run(os.path.join("bin", "test_package"), 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")
10 changes: 10 additions & 0 deletions recipes/libmodbus/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)

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

find_package(libmodbus CONFIG REQUIRED)

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


# legacy validation with Coann 1.x
class TestPackageV1Conan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"

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

def test(self):
if not cross_building(self.settings):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)

0 comments on commit a484011

Please sign in to comment.