diff options
| -rwxr-xr-x | cook.py | 78 | ||||
| -rw-r--r-- | test/.build | 8 |
2 files changed, 76 insertions, 10 deletions
@@ -1,5 +1,6 @@ #!/usr/bin/python +import subprocess import sys import os from pathlib import Path @@ -416,23 +417,36 @@ def CToolchain(name="system", suffix="", c_args=[], ld_args=[]): class CModule: def __init__(self, name, cc, exe, lib, shlib, root = None): self.name = name + + # The toolchain that the module uses self.cc = cc self.exe = exe self.lib = lib self.shlib = shlib + # The root module. Is this needed? self.root = root + # Executables instanciated by the module. Add shared libraries when applicable self.exes = dict() + # Submodules instanciated by the module self.submodules = dict() + + # Objects accumulator self.objects = [] + + # Private includes, libs and args self.includes = [] + self.libs = [] + self.ld_args = [] + self.cc_args = [] + + # Shared includes, libs and args self.shared_includes = [] self.shared_libs = [] self.bcast_includes = [] self.shared_ld_args = [] self.shared_cc_args = [] - pass def eval(self): subdir(self.name) @@ -458,32 +472,80 @@ class CModule: mod.eval() # Perhaps store everything the object created - prev.objects += [mod.lib(name, mod.objects)] - prev.shared_includes += mod.shared_includes - prev.shared_libs += mod.shared_libs - prev.bcast_includes += mod.bcast_includes + self.link(mod) mod, prev = prev, mod mod.submodules[name] = prev return prev + + def mklib(self): + if self.objects == [] or self.lib == None: + return None + return self.lib(self.name, self.objects) + + def link(self, *mods): + mods = flatten(mods) + for mod in mods: + if type(mod) == str: + mod = CDependency(mod) + + obj = mod.mklib() + if obj: + self.objects += [obj] + self.shared_includes += mod.shared_includes + self.shared_libs += mod.shared_libs + self.bcast_includes += mod.bcast_includes + self.shared_ld_args += mod.shared_ld_args + self.shared_cc_args += mod.shared_cc_args def src(self, *args, extra_args = [], include_dirs = []): - obj = self.cc(*args, extra_args = [extra_args, self.shared_cc_args], include_dirs = [include_dirs, self.includes, self.bcast_includes, self.shared_includes]) + obj = self.cc(*args, extra_args = [extra_args, self.cc_args, self.shared_cc_args], include_dirs = [include_dirs, self.includes, self.bcast_includes, self.shared_includes]) self.objects += obj - def mkdep(self, shared_includes = [], shared_libs = [], bcast_includes = [], shared_ld_args = []): + def mkdep(self, shared_includes = [], shared_libs = [], bcast_includes = [], shared_ld_args = [], shared_cc_args = []): self.shared_includes += flatten(shared_includes) self.shared_libs += flatten(shared_libs) self.shared_ld_args += flatten(shared_ld_args) + self.shared_cc_args += flatten(shared_cc_args) self.bcast_includes += flatten(bcast_includes) def mkexe(self, name, extra_args = [], libs = []): - self.exes[name] = self.exe(name, self.objects, extra_args = [extra_args, self.shared_ld_args], libs = [libs, self.shared_libs]) + self.exes[name] = self.exe(name, self.objects, extra_args = [extra_args, self.ld_args, self.shared_ld_args], libs = [libs, self.libs, self.shared_libs]) return self.exes[name] +pkgconfig = find_program("pkg-config", False) + +def subproc(*args, **kwargs): + return subprocess.check_output(*args, **kwargs, stderr=subprocess.DEVNULL) + +# XXX: make name a variadic +def CDependency(name): + #if type(name) == list or type(name) is tuple: + # return [CDependency(n) for n in name] + + module = CModule(name, None, None, None, None, None) + + if pkgconfig: + try: + inc = subproc([pkgconfig.path, "--cflags", name]).strip().decode("utf-8") + lib = subproc([pkgconfig.path, "--libs", name]).strip().decode("utf-8") + module.mkdep(shared_ld_args = lib, shared_cc_args = inc) + return module + except subprocess.CalledProcessError: + pass + + #if prog: + # XXX: pkg-config + # XXX: windows stuff + + # Use the system way of getting to know libraries (no includes specified) + module.mkdep(shared_libs = name) + return module + + cc, exe, lib, shlib = CToolchain() global root, mod diff --git a/test/.build b/test/.build index b76f85b..f616f70 100644 --- a/test/.build +++ b/test/.build @@ -1,2 +1,6 @@ -mod.src("main.c", "hai.c") -mod.mkdep(shared_libs = ["Mrm", "Uil", "Xm", "Xt"])
\ No newline at end of file +mod.link( + "Mrm", "Uil", "Xm", "Xt", + "libpcre" +) + +mod.src("main.c", "hai.c")
\ No newline at end of file |
