Skip to content

Commit

Permalink
[wip] improve behavior with toolchains
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Sep 27, 2022
1 parent 3c306db commit a7f2ef8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
13 changes: 5 additions & 8 deletions src/c4/cmany/architecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .build_item import BuildItem
from . import util
from .cmake import CMakeSysInfo


# -----------------------------------------------------------------------------
Expand All @@ -15,14 +16,10 @@ def default():

@staticmethod
def default_str():
# s = CMakeSysInfo.architecture()
# if s == "amd64":
# s = "x86_64"
# return s
if util.in_64bit():
return "x86_64"
elif util.in_32bit():
return "x86"
s = CMakeSysInfo.architecture()
if s == "amd64":
s = "x86_64"
return s

@property
def is64(self):
Expand Down
43 changes: 22 additions & 21 deletions src/c4/cmany/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from collections import OrderedDict as odict

from .conf import USER_DIR
from .util import cacheattr, setcwd, runsyscmd, logdbg, chkf
from .util import cacheattr, setcwd, runsyscmd, chkf
from .util import logdbg as dbg
from . import util
from . import err

Expand Down Expand Up @@ -67,14 +68,14 @@ def loadvars(builddir):
if os.path.exists(c):
with open(c, 'r') as f:
for line in f:
# logdbg("loadvars0", line.strip())
# dbg("loadvars0", line.strip())
if not re.match(_cache_entry, line):
continue
ls = line.strip()
name = re.sub(_cache_entry, r'\1', ls)
vartype = re.sub(_cache_entry, r'\2', ls)[1:]
value = re.sub(_cache_entry, r'\3', ls)
# logdbg("loadvars1", name, vartype, value)
# dbg("loadvars1", name, vartype, value)
v[name] = CMakeCacheVar(name, value, vartype)
return v

Expand All @@ -86,7 +87,7 @@ def get_cxx_compiler(builddir):
if len(files) != 1:
raise Exception(f"could not find compiler settings: {expr}")
cxxfile = files[0]
logdbg("")
dbg("")
with open(cxxfile) as f:
lines = f.readlines()
lookup = "set(CMAKE_CXX_COMPILER "
Expand Down Expand Up @@ -281,46 +282,46 @@ def info(which_generator="default"):
def _getstr(var_name, which_generator):
regex = r'^{} "(.*)"'.format(var_name)
for l in __class__.info(which_generator):
#logdbg(l.strip("\n"), l.startswith(var_name), var_name)
#dbg(l.strip("\n"), l.startswith(var_name), var_name)
if l.startswith(var_name):
l = l.strip("\n").lstrip(" ").rstrip(" ")
#logdbg(var_name, "startswith :", l)
#dbg(var_name, "startswith :", l)
if re.match(regex, l):
s = re.sub(regex, r'\1', l)
#logdbg(var_name, "result: '" + s + "'")
#dbg(var_name, "result: '" + s + "'")
return s
#logdbg("--------------------------------------\n", __class__.info(which_generator))
#dbg("--------------------------------------\n", __class__.info(which_generator))
msg = "could not find variable {} in the output of `cmake --system-information -G '{}'`"
raise err.Error(msg, var_name, which_generator)

@staticmethod
def system_info(gen):
"""gen can be a string or a cmany.Generator object"""
from .generator import Generator
logdbg("CMakeSystemInfo: asked info for", gen)
dbg("CMakeSystemInfo: asked info for", gen)
p = _genid(gen)
d = os.path.join(USER_DIR, 'cmake_info', p)
p = os.path.join(d, 'info')
logdbg("CMakeSystemInfo: path=", p)
dbg("CMakeSystemInfo: path=", p)
# https://stackoverflow.com/questions/7015587/python-difference-of-2-datetimes-in-months
if os.path.exists(p) and util.time_since_modification(p).months < 1:
logdbg("CMakeSystemInfo: asked info for", gen, "... found", p)
dbg("CMakeSystemInfo: asked info for", gen, "... found", p)
with open(p, "r") as f:
i = f.readlines()
if i:
return i
else:
logdbg("CMakeSystemInfo: info for gen", gen, "is empty...")
dbg("CMakeSystemInfo: info for gen", gen, "is empty...")
#
if isinstance(gen, Generator):
cmd = ['cmake'] + gen.configure_args() + ['--system-information']
logdbg("CMakeSystemInfo: from generator! '{}' ---> cmd={}".format(gen, cmd))
dbg("CMakeSystemInfo: from generator! '{}' ---> cmd={}".format(gen, cmd))
else:
if gen == "default" or gen == "":
logdbg("CMakeSystemInfo: default! '{}'".format(gen))
dbg("CMakeSystemInfo: default! '{}'".format(gen))
cmd = ['cmake', '--system-information']
else:
logdbg("CMakeSystemInfo: assume vs! '{}'".format(gen))
dbg("CMakeSystemInfo: assume vs! '{}'".format(gen))
from . import vsinfo
gen = vsinfo.to_gen(gen)
if isinstance(gen, list):
Expand All @@ -338,7 +339,7 @@ def system_info(gen):
os.makedirs(d)
with setcwd(d):
out = runsyscmd(cmd, echo_output=False, capture_output=True)
logdbg("cmany: finished generating information for generator '{}'\n".format(gen), out, cmd)
dbg("cmany: finished generating information for generator '{}'\n".format(gen), out, cmd)
out = out.strip()
if not out:
from .err import InvalidGenerator
Expand Down Expand Up @@ -384,10 +385,10 @@ def _genid(gen):
# -----------------------------------------------------------------------------
def get_toolchain_cache(toolchain):
d = os.path.join(USER_DIR, 'toolchains', re.sub(os.sep, '_', toolchain))
logdbg("toolchain cache: USER_DIR=", USER_DIR)
logdbg("toolchain cache: d=", d)
dbg("toolchain cache: USER_DIR=", USER_DIR)
dbg("toolchain cache: d=", d)
bd = os.path.join(d, 'build')
logdbg("toolchain cache: bd=", bd)
dbg("toolchain cache: bd=", bd)
if not os.path.exists(d):
os.makedirs(d)
with setcwd(d):
Expand All @@ -411,8 +412,8 @@ def extract_toolchain_compilers(toolchain):
cache = get_toolchain_cache(toolchain)
out = odict()
for k, v in cache.items():
logdbg(f"toolchain cache: {k}=={v}")
dbg(f"toolchain cache: {k}=={v}")
if k.startswith('CMAKE_') and k.endswith('_COMPILER'):
logdbg(f"toolchain cache: .................... {k}=={v}")
dbg(f"toolchain cache: .................... {k}=={v}")
out[k] = v.val
return out
34 changes: 18 additions & 16 deletions src/c4/cmany/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from . import util
from . import vsinfo
from . import err
from .util import logdbg as dbg


# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -56,17 +57,17 @@ def __init__(self, spec):
spl = [path]
p = util.which(path)
if p is None:
util.logdbg("not found:", path)
dbg("not found:", path)
shspl = shlex.split(path)
util.logdbg("trying split:", shspl)
dbg("trying split:", shspl)
if len(shspl) > 0:
p = util.which(shspl[0])
util.logdbg("trying split:", p)
dbg("trying split:", p)
if p is None:
util.logdbg("no compiler found", path)
dbg("no compiler found", path)
raise err.CompilerNotFound(path)
if p != path:
util.logdbg("compiler: selected {} for {}".format(p, path))
dbg("compiler: selected {} for {}".format(p, path))
if isinstance(path, str):
path = os.path.abspath(p)
else:
Expand Down Expand Up @@ -132,18 +133,18 @@ def get_version(self, path):
if hasattr(self, "vs"):
return self.vs.name, str(self.vs.year), self.vs.name
# other compilers
util.logdbg("cmp: found compiler:", path)
dbg("cmp: found compiler:", path)
if isinstance(path, str):
out = slntout([path, '--version'])
else:
out = slntout(path + ['--version'])
version_full = out.split("\n")[0]
splits = version_full.split(" ")
name = splits[0].lower()
# print("cmp: version:", name, "---", version_full, "---")
dbg("cmp: version:", name, "---", version_full, "---")
vregex = r'(\d+\.\d+)\.\d+'
base = os.path.basename(path)
# print(name, "cmp base:", base, name)
dbg(name, "cmp base:", base, name)
if base.startswith("c++") or base.startswith("cc"):
try: # if this fails, just go on. It's not really needed.
with tempfile.NamedTemporaryFile(suffix=".cpp", prefix="cmany.", delete=False) as f:
Expand All @@ -164,30 +165,31 @@ def get_version(self, path):
if re.search('Apple LLVM', version_full):
name = "apple_llvm"
version = re.sub(r'Apple LLVM version ' + vregex + '.*', r'\1', version_full)
print("apple_llvm version:", version, "---")
dbg("apple_llvm version:", version, "---")
else:
version = re.sub(r'clang version ' + vregex + '.*', r'\1', version_full)
# print("clang version:", version, "---")
dbg("clang version:", version, "---")
elif name.startswith("g++") or name.startswith("gcc") or name.endswith("g++") or name.endswith("gcc"):
# print("g++: version:", name, name.find('++'))
name = "g++" if name.find('++') != -1 else 'gcc'
# print("g++: version:", name, name.find('++'))
dbg("g++: version:", name, name.find('++'))
#name = "g++" if name.find('++') != -1 else 'gcc'
#dbg("g++: version:", name, name.find('++'))
version = slntout([path, '-dumpversion'])
dbg("g++: versiondump:", version)
version = re.sub(vregex, r'\1', version)
# print("gcc version:", version, "---")
dbg("gcc version:", version, "---")
elif name.startswith("icpc") or name.startswith("icc"):
name = "icc" if name.startswith("icc") else "icpc"
if re.search(r'icpc \(ICC\) ' + vregex + '.*', version_full):
version = re.sub(r'icpc \(ICC\) ' + vregex + '.*', r'\1', version_full)
else:
version = re.sub(r'icc \(ICC\) ' + vregex + '.*', r'\1', version_full)
# print("icc version:", version, "---")
dbg("icc version:", version, "---")
elif version_full.startswith("Intel(R) oneAPI"):
name = "intel"
rx = r'Intel\(R\) oneAPI .*? Compiler ([0-9.]*?) \(([0-9.]*?)\).*'
version = re.sub(rx, r'\1', version_full)
version_full = re.sub(rx, r'\2', version_full)
#print("intel version:", version, "---", version_full)
dbg("intel version:", version, "---", version_full)
else:
version = slntout([path, '-dumpversion'])
version = re.sub(vregex, r'\1', version)
Expand Down

0 comments on commit a7f2ef8

Please sign in to comment.