Skip to content

Instantly share code, notes, and snippets.

@puetzk
Created November 30, 2018 04:37
Show Gist options
  • Save puetzk/12b902fd2ee469852d2ebd331a927a2c to your computer and use it in GitHub Desktop.
Save puetzk/12b902fd2ee469852d2ebd331a927a2c to your computer and use it in GitHub Desktop.
SystemPackageConan
from conans import ConanFile, tools
from configparser import ConfigParser
import os
def remove_prefix(s, prefix):
return s[len(prefix):] if s.startswith(prefix) else s
class SystemPackageConan(ConanFile):
settings = 'os', 'arch'
options = { "pkg_config" : "ANY" }
def config_options(self):
if not self.develop: # pkg_config option is only for `conan create`
del self.options.pkg_config
def build(self):
build_options = ConfigParser()
if self.options.pkg_config:
build_options['pkg_config'] = {}
build_options['pkg_config']['module'] = str(self.options.pkg_config)
if self.env.get('PKG_CONFIG_SYSROOT_DIR'):
build_options['pkg_config']['sysroot'] = self.env.get('PKG_CONFIG_SYSROOT_DIR')
with open('build_options.txt','w') as options_file:
build_options.write(options_file)
def package(self):
self.copy('build_options.txt')
def package_id(self):
del self.info.options.pkg_config
def package_info(self):
build_options = ConfigParser()
if hasattr(self, 'package_folder'):
build_options.read(os.path.join(self.package_folder, 'build_options.txt'))
if 'pkg_config' in build_options:
pkg_config_env = self.env
build_sysroot=build_options['pkg_config'].get('sysroot', None)
usage_sysroot=pkg_config_env.get('PKG_CONFIG_SYSROOT_DIR', None)
if build_sysroot and (build_sysroot != usage_sysroot):
self.output.warn("Inconsistent PKG_CONFIG_SYSROOT_DIR (%s != %s), forcing absolute paths" % (build_sysroot,usage_sysroot))
pkg_config_env['PKG_CONFIG_SYSROOT_DIR'] = build_sysroot
pkg_config_env['PKG_CONFIG_ALLOW_SYSTEM_CFLAGS'] = '1'
pkg_config_env['PKG_CONFIG_ALLOW_SYSTEM_LIBS'] = '1'
with tools.environment_append(pkg_config_env):
pkg = tools.PkgConfig(build_options['pkg_config']['module'])
self.cpp_info.includedirs = [remove_prefix(x,'-I') for x in pkg.cflags_only_I]
self.cpp_info.libdirs = [remove_prefix(x,'-L') for x in pkg.libs_only_L]
self.cpp_info.libs = [remove_prefix(x,'-l') for x in pkg.libs_only_l]
self.cpp_info.defines = [remove_prefix(x,'-D') for x in pkg.cflags_only_other if x.startswith('-D')]
self.cpp_info.cflags = [x for x in pkg.cflags_only_other if not x.startswith('-D')]
self.cpp_info.cppflags = [x for x in pkg.cflags_only_other if not x.startswith('-D')]
self.cpp_info.sharedlinkflags = pkg.libs_only_other
self.cpp_info.exelinkflags = pkg.libs_only_other
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment