summaryrefslogtreecommitdiff
path: root/cook.py
diff options
context:
space:
mode:
authorAlejandro W. Sior <aho@sior.be>2023-01-23 18:19:48 +0100
committerAlejandro W. Sior <aho@sior.be>2023-01-23 18:19:48 +0100
commitd0759d3282025f310ecd566c55dcf214b0cbb1b8 (patch)
tree705f9cd43a9429ee264ce100381f7e76f7dca8a1 /cook.py
parentddedac846b0788e3cff90a24b2c8109308b1e840 (diff)
cook: module reorganization
Diffstat (limited to 'cook.py')
-rwxr-xr-xcook.py62
1 files changed, 26 insertions, 36 deletions
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))