Created
April 21, 2021 13:38
-
-
Save solvingj/1fb2cf7c09036fe526f97cebc0c09354 to your computer and use it in GitHub Desktop.
A custom Conan generator which produces cmake paths based on environment variables
This file contains hidden or 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 is intended to demonstrate a proof-of-concept | |
# for users/callers of Conan to easily override | |
# conan-generated paths for dependencies. | |
# The purpose was to show how users could manually | |
# implement "editable" packages. | |
# It works, however, sadly, not the way intended. | |
# Environment variables from profiles are not | |
# made available in generators. os.getenv() returns | |
# only environment variables set in the outer shell. | |
# so, to properly compose the editable packages | |
# would need to be done entirely in the shell, which | |
# is unfortunate, because the "shell" is not x-platform. | |
# It could also be done via python wrappers as well. | |
import os | |
from conans.model import Generator | |
class cmake_paths_from_env(Generator): | |
output = [] | |
@property | |
def filename(self): | |
return "conanbuildinfo.user.cmake" | |
@property | |
def content(self): | |
self.iterate_deps() | |
return "\n".join(self.output) | |
def iterate_deps(self): | |
for dep_name, _ in self.deps_build_info.dependencies: | |
self.process_dep(dep_name) | |
def process_dep(self, dep_name): | |
dep_root = os.getenv("CONAN_{0}_ROOT".format(dep_name.upper())) | |
if dep_root: | |
self.append_var(dep_name, "ROOT") | |
self.process_subdirs(dep_name, dep_root) | |
def process_subdirs(self, dep_name, dep_root): | |
for var in ["INCLUDE_DIRS", "LIB_DIRS", "BIN_DIRS"]: | |
var_name_full = "{0}_{1}".format(dep_name, var_name) | |
val = os.getenv(var_name_full) | |
if val: | |
self.append_var(dep_name, self.cmake_join(dep_root, var_name_full)) | |
def append_var(var, val): | |
self.output.append("set({0} {1})".format(var, val)) | |
@staticmethod | |
def cmake_join(path1, path2): | |
return "{0}/{1}".format(path1, path2).replace("//","/") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment