Skip to content

Instantly share code, notes, and snippets.

@tru
Created February 14, 2018 13:30
Show Gist options
  • Save tru/5032a0b9da141d8d482e23133c740060 to your computer and use it in GitHub Desktop.
Save tru/5032a0b9da141d8d482e23133c740060 to your computer and use it in GitHub Desktop.
import shlex
import os
from pathlib import Path
from conans.model import Generator
from conans import ConanFile
class CMakeToolchain(Generator):
def __init__(self, conanfile):
self.conanfile = conanfile
super(CMakeToolchain, self).__init__(conanfile)
@property
def filename(self):
return ""
def cmake_system_name(self):
names = {
"Macos": "Darwin",
"iOS": "Darwin",
}
osname = str(self.conanfile.settings.os)
if osname in names:
return names[osname]
return osname
@property
def content(self):
def make_absolute(prg):
if Path(prg).is_absolute():
return prg
for path in os.environ.get("PATH", "").split(os.pathsep):
binpath = Path(path) / Path(prg)
if binpath.exists():
return str(binpath)
return prg
def split_ccache(env, default=""):
envstr = self.conanfile.env.get(env, default)
if "ccache" in envstr:
lexsplit = shlex.split(envstr)
lexsplit = [make_absolute(a) for a in lexsplit]
argstr = ""
if len(lexsplit) > 1:
argstr = " ".join(lexsplit[1:])
return (make_absolute(lexsplit[0]), argstr)
return (envstr, "")
cc, ccarg1 = split_ccache("CC", "cc")
cxx, cxxarg1 = split_ccache("CXX", "c++")
cmakevars = {
"CMAKE_SYSTEM_PROCESSOR": str(self.conanfile.settings.arch),
"CMAKE_SYSTEM_NAME": self.cmake_system_name(),
"CMAKE_C_COMPILER": cc,
"CMAKE_C_COMPILER_ARG1": ccarg1,
"CMAKE_CXX_COMPILER": cxx,
"CMAKE_CXX_COMPILER_ARG1": cxxarg1,
"CMAKE_SYSTEM_VERSION": "1",
}
ldflags = self.conanfile.env.get("LDFLAGS", "")
cmakeforcedvars = {
"CMAKE_C_FLAGS": self.conanfile.env.get("CFLAGS", ""),
"CMAKE_CXX_FLAGS": self.conanfile.env.get("CXXFLAGS", ""),
"CMAKE_SHARED_LINKER_FLAGS": ldflags,
"CMAKE_EXE_LINKER_FLAGS": ldflags,
"CMAKE_MODULE_LINKER_FLAGS": ldflags,
"CMAKE_STATIC_LINKER_FLAGS": ldflags
}
if self.conanfile.settings.os == "iOS":
cmakevars["IOS"] = "TRUE"
cmakedata = "\n".join("set({key} \"{value}\")".format(key=key, value=value) for key, value in cmakevars.items())
cmakedata += "\n"
cmakedata += "\n".join("set({key} \"{value}\" CACHE STRING \"\" FORCE)".format(key=key, value=value) for key, value in cmakeforcedvars.items())
cmakedata += "\n"
return {"cmake-conan-toolchain.cmake": cmakedata}
class CMakeToolchainGenerator(PlexConanFile):
name = "cmaketoolchain"
version = "1.0"
url = "https://conan.io"
license = "MIT"
description = "A Conan generator that generates a CMake Toolchain file based on the profile"
def build(self):
pass
def package_info(self):
self.cpp_info.includedirs = []
self.cpp_info.libdirs = []
self.cpp_info.bindirs = []
def package(self):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment