From d0759d3282025f310ecd566c55dcf214b0cbb1b8 Mon Sep 17 00:00:00 2001 From: "Alejandro W. Sior" Date: Mon, 23 Jan 2023 18:19:48 +0100 Subject: cook: module reorganization --- .build | 10 +++++----- cook.py | 62 ++++++++++++++++++++++++++----------------------------------- test/.build | 13 +++++-------- test/hai.c | 9 ++++----- 4 files changed, 40 insertions(+), 54 deletions(-) diff --git a/.build b/.build index 2b5b4dd..fca48dd 100644 --- a/.build +++ b/.build @@ -1,6 +1,6 @@ -mod["ld_args"] = [] -mod["deps"] = [] +this.c = cook.c -mod["vol"] = mod.exe("vol", - mod("test").link, - deps = mod.deps) \ No newline at end of file +this.ld_args = [] +this.deps = [] + +this.c.exe("vol", this('test').link) diff --git a/cook.py b/cook.py index 002ab04..50957b2 100755 --- a/cook.py +++ b/cook.py @@ -15,6 +15,11 @@ def error(*args): class ProgramNotFoundException(Exception): pass +class AttrDict(dict): + def __init__(self, *args, **kwargs): + super(AttrDict, self).__init__(*args, **kwargs) + self.__dict__ = self + def find_program(prog, required=True): path = shutil.which(prog) if path == None and required: @@ -333,7 +338,7 @@ def CCStyleToolchain(name, gccname, arname, suffix="", c_args=[], ld_args=[]): mkdesc=lambda o, i, cmd: "SO %s" % (o), mkout=lambda n: "lib%s.so" % (n) ) - return cc, exe, lib, shlib + return AttrDict({'cc': cc, 'exe': exe, 'lib': lib, 'shlib': shlib}) def GccToolchain(*args, **kwargs): return CCStyleToolchain("gcc", "gcc", "gcc-ar", *args, **kwargs) @@ -390,7 +395,7 @@ def MSVCStyleToolchain(name, gccname, arname, ldname, suffix="", c_args=[], ld_a mkdesc=lambda o, i, cmd: "SO %s" % (o), mkout=lambda n: "%s.lib" % (n) ) - return cc, exe, lib, shlib + return AttrDict({'cc': cc, 'exe': exe, 'lib': lib, 'shlib': shlib}) def MSVCToolchain(*args, **kwargs): return MSVCStyleToolchain("msvc", "cl", "lib", "link", *args, **kwargs) @@ -459,7 +464,6 @@ class CMod: if self.parent: self.parent.children[name] = self self.children = dict() - self.objects = dict() def __call__(self, path): components = path.split("/") @@ -482,30 +486,18 @@ class CMod: return resolved - def __getitem__(self, key): - if key in self.objects: - return self.objects[key] - elif self.parent: - return self.parent[key] + def __getattr__(self, attr): + if self.parent: + return getattr(self.parent, attr) else: return None - def __setitem__(self, key, val): - self.objects[key] = val - - def __delitem__(self, key): - if key in self.objects: - del self.objects[key] - - def __getattr__(self, attr): - return self[attr] - def dive(self): - global mod + global this # Save the previous location PWD = Path.cwd() - pmod = mod - mod = self + previous_this = this + this = self # Go into subdir sub = self.path @@ -515,21 +507,19 @@ class CMod: # Revert the current directory; os.chdir(PWD) - mod = pmod + this = previous_this global _root _root = CMod(None, "") -global mod -mod = _root - -def set_toolchain(t): - mod(".")["cc"] = t[0] - mod(".")["exe"] = t[1] - mod(".")["lib"] = t[2] - mod(".")["shlib"] = t[3] +global this +this = _root -set_toolchain(CToolchain()) +global cook +cook = AttrDict({ + 'c': CToolchain(), + 'cpp': CppToolchain() +}) pkgconfig = find_program("pkg-config", False) @@ -548,8 +538,8 @@ def CDependency(name): inc = subproc([pkgconfig.path, "--cflags", name]).strip().decode("utf-8") lib = subproc([pkgconfig.path, "--libs", name]).strip().decode("utf-8") - module["cc_args"] = inc if inc != "" else [] - module["ld_args"] = lib if lib != "" else [] + module.cc_args = inc if inc != "" else [] + module.ld_args = lib if lib != "" else [] return module except subprocess.CalledProcessError: @@ -560,7 +550,7 @@ def CDependency(name): # XXX: windows stuff # Use the system way of getting to know libraries (no includes specified) - module["libs"] = name + module.libs = name return module @@ -593,7 +583,7 @@ def subdir(dir): # Revert the current directory; os.chdir(PWD) -mod.dive() +this.dive() def gen_bld(): return [target.path.parent.mkdir(parents=True, exist_ok=True) @@ -627,4 +617,4 @@ if backend == "ninja": elif backend == "makefile": open(ROOT / 'makefile', 'w').write(gen_makefile()) else: - print("No such backend %s" % (backend)) \ No newline at end of file + print("No such backend %s" % (backend)) diff --git a/test/.build b/test/.build index 60aadcb..d313d01 100644 --- a/test/.build +++ b/test/.build @@ -1,8 +1,5 @@ -deps = CDependency(["Mrm", "Xm", "Xt", "libpcre"]) - -mod["link"] = mod.lib("test", mod.cc( - "main.c", - "hai.c", - deps = deps)) - -mod.deps += deps \ No newline at end of file +this.link = this.c.lib("test", + this.c.cc( + "hai.c" + ) +) diff --git a/test/hai.c b/test/hai.c index 25c25ae..7791fbb 100644 --- a/test/hai.c +++ b/test/hai.c @@ -1,7 +1,6 @@ -#include "hai.h" - #include -void hai() { - printf("lol\n"); -} \ No newline at end of file +int main() { + printf("Hello world!\n"); + return 0; +} -- cgit v1.2.3