-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I had to switch from a simple `conanfile.txt` to a more involved `conanfile.py` because the "import" functionality has been removed in Conan 2. Initially this seemed like a hassle, but in the end, it turned out to be beneficial: * We no longer have to maintain the list of imports by hand. Instead, we dynamically iterate over the dependencies list in the `generate()` method. * It allowed me to adjust the RPATH of the imported files from within the conanfile, rather than doing it in the GitHub Actions workflow file as before. Thus, people who build locally also get libaries and executables with an appropriate RPATH.
- Loading branch information
1 parent
697ccf9
commit 71b7bf3
Showing
6 changed files
with
155 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
*.swp | ||
build/ | ||
CMakeUserPresets.json | ||
|
||
# JetBrains CLion: | ||
.idea/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import os | ||
|
||
from conan import ConanFile | ||
from conan.tools.cmake import CMakeDeps, CMakeToolchain, cmake_layout | ||
from conan.tools.env import VirtualBuildEnv | ||
from conan.tools.files import copy | ||
|
||
class CosimCLIConan(ConanFile): | ||
settings = "os", "compiler", "build_type", "arch" | ||
default_options = { "*:shared": True } | ||
|
||
def requirements(self): | ||
self.tool_requires("cmake/[>=3.19]") | ||
if self.settings.os == "Linux": | ||
self.tool_requires("patchelf/[<0.18]") | ||
self.requires("libcosim/0.10.3@osp/stable") | ||
self.requires("boost/[>=1.71]") | ||
|
||
def layout(self): | ||
cmake_layout(self) | ||
|
||
def generate(self): | ||
# Import shared libraries and executables from dependency packages | ||
# to the 'dist/' folder. | ||
bindir = os.path.join(self.build_folder, "dist", "bin") | ||
dldir = (bindir if self.settings.os == "Windows" else | ||
os.path.join(self.build_folder, "dist", "lib")) | ||
dependency_libs = { | ||
# For some dependencies, we only want a subset of the libraries | ||
"boost" : [ | ||
"boost_atomic*", | ||
"boost_chrono*", | ||
"boost_container*", | ||
"boost_context*", | ||
"boost_date_time*", | ||
"boost_filesystem*", | ||
"boost_locale*", | ||
"boost_log*", | ||
"boost_log_setup*", | ||
"boost_program_options*", | ||
"boost_random*", | ||
"boost_regex*", | ||
"boost_serialization*", | ||
"boost_system*", | ||
"boost_thread*"], | ||
"thrift": ["thrift", "thriftd"], | ||
} | ||
for req, dep in self.dependencies.items(): | ||
self._import_dynamic_libs(dep, dldir, dependency_libs.get(req.ref.name, ["*"])) | ||
if self.dependencies["libcosim"].options.proxyfmu: | ||
self._import_executables(self.dependencies["proxyfmu"], bindir, ["*"]) | ||
|
||
# Generate build system | ||
tc = CMakeToolchain(self) | ||
tc.generate() | ||
CMakeDeps(self).generate() | ||
|
||
def _import_dynamic_libs(self, dependency, target_dir, patterns): | ||
if dependency.options.get_safe("shared", False): | ||
if self.settings.os == "Windows": | ||
depdirs = dependency.cpp_info.bindirs | ||
else: | ||
depdirs = dependency.cpp_info.libdirs | ||
for depdir in depdirs: | ||
for pattern in patterns: | ||
patternx = pattern+".dll" if self.settings.os == "Windows" else "lib"+pattern+".so*" | ||
files = copy(self, patternx, depdir, target_dir, keep_path=False) | ||
self._update_rpath(files, "$ORIGIN") | ||
|
||
def _import_executables(self, dependency, target_dir, patterns=["*"]): | ||
for bindir in dependency.cpp_info.bindirs: | ||
for pattern in patterns: | ||
patternx = pattern+".exe" if self.settings.os == "Windows" else pattern | ||
files = copy(self, patternx, bindir, target_dir, keep_path=False) | ||
self._update_rpath(files, "$ORIGIN/../lib") | ||
|
||
def _update_rpath(self, files, new_rpath): | ||
if files and self.settings.os == "Linux": | ||
with VirtualBuildEnv(self).environment().vars(self).apply(): | ||
self.run("patchelf --set-rpath '" + new_rpath + "' '" + ("' '".join(files)) + "'") |
Oops, something went wrong.