summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlejandro Sior <aho@sior.be>2022-08-07 11:53:38 +0200
committerAlejandro Sior <aho@sior.be>2022-08-07 11:53:38 +0200
commit5a11f2675177074db05965e366196166943ed725 (patch)
tree0c36cf32089242863b5257eaf6443b09a76e14f2
parent13c7009df18a4c86086bf270d125d23ee9b849e2 (diff)
cook: add a basic C modules system
-rw-r--r--.build3
-rwxr-xr-xcook.py81
-rw-r--r--test/.build7
-rw-r--r--test/hai.c1
-rw-r--r--test/hai.h2
-rw-r--r--test/main.c77
6 files changed, 157 insertions, 14 deletions
diff --git a/.build b/.build
index f7f817c..cef5b7a 100644
--- a/.build
+++ b/.build
@@ -1 +1,2 @@
-subdir("test") \ No newline at end of file
+mod.submodule("test")
+mod.mkexe("vol") \ No newline at end of file
diff --git a/cook.py b/cook.py
index 3c4588f..b3f4622 100755
--- a/cook.py
+++ b/cook.py
@@ -6,6 +6,7 @@ from pathlib import Path
from threading import local
import shutil
import argparse
+from xml.etree.ElementInclude import include
def error(*args):
print(*args, file = sys.stderr)
@@ -404,15 +405,89 @@ def CToolchain(name="system", suffix="", c_args=[], ld_args=[]):
try:
return t(suffix, c_args, ld_args)
except ProgramNotFoundException as e:
- pass
+ continue
except Exception as e:
raise e
raise Exception("No C toolchain found on system")
-global cc, exe, lib, shlib
+# NOTE: this implementation is very simple and bad but does what
+# i need
+class CModule:
+ def __init__(self, name, cc, exe, lib, shlib, root = None):
+ self.name = name
+ self.cc = cc
+ self.exe = exe
+ self.lib = lib
+ self.shlib = shlib
+
+ self.root = root
+
+ self.exes = dict()
+ self.submodules = dict()
+ self.objects = []
+ self.includes = []
+ self.links = []
+ self.shared_includes = []
+ self.shared_links = []
+ self.ld_args = []
+ self.cc_args = []
+ pass
+
+ def eval(self):
+ subdir(self.name)
+
+ def submodule(self, name, toolchain = None):
+ global mod
+ prev = mod
+
+ root = self.root if self.root else self
+ cc = prev.cc
+ exe = prev.exe
+ lib = prev.lib
+ shlib = prev.shlib
+ if toolchain:
+ cc, exe, lib, shlib = toolchain
+ root = None
+
+ mod = CModule(name, cc, exe, lib, shlib, root)
+ mod.eval()
+
+ # Perhaps store everything the object created
+ prev.objects += [mod.lib(name, mod.objects)]
+ prev.shared_includes += mod.shared_includes
+ prev.shared_links += mod.shared_links
+
+ mod, prev = prev, mod
+
+ mod.submodules[name] = prev
+
+ return prev
+
+
+ def src(self, *args, extra_args = [], include_dirs = []):
+ obj = self.cc(*args, extra_args = [extra_args, self.cc_args], include_dirs = [include_dirs, self.shared_includes])
+ self.objects += obj
+
+ def mkdep(self, include_dirs = [], libs = [], ld_args = []):
+ include_dirs = flatten(include_dirs)
+ libs = flatten(libs)
+ ld_args = flatten(ld_args)
+
+ self.ld_args += ld_args
+ self.shared_includes += include_dirs
+ self.shared_links += libs
+
+ def mkexe(self, name, extra_args = [], libs = []):
+ self.exes[name] = self.exe(name, self.objects, extra_args = [extra_args, self.ld_args], libs = [libs, self.shared_links])
+ return self.exes[name]
+
cc, exe, lib, shlib = CToolchain()
+global root, mod
+root = CModule("root", cc, exe, lib, shlib)
+mod = root
+
def find_one(names):
for name in names:
program = find_program(name, required=False)
@@ -431,7 +506,7 @@ def subdir(dir):
# Set helper path
global CWD
CWD = Path.cwd().relative_to(ROOT)
- exec(open('.build').read())
+ exec(open('.build').read(), globals())
# Revert the current directory;
os.chdir(PWD)
diff --git a/test/.build b/test/.build
index 8dfcf25..1faa6e9 100644
--- a/test/.build
+++ b/test/.build
@@ -1,5 +1,2 @@
-maino = cc("main.c")
-haio = cc("hai.c")
-hailib = lib("hai", haio)
-
-aout = exe("vol", maino, hailib) \ No newline at end of file
+mod.src("main.c", "hai.c")
+mod.mkdep(libs = ["Mrm", "Uil", "Xm", "Xt"]) \ No newline at end of file
diff --git a/test/hai.c b/test/hai.c
index 263a8de..25c25ae 100644
--- a/test/hai.c
+++ b/test/hai.c
@@ -2,7 +2,6 @@
#include <stdio.h>
-__declspec(dllexport)
void hai() {
printf("lol\n");
} \ No newline at end of file
diff --git a/test/hai.h b/test/hai.h
index 02201d9..da27064 100644
--- a/test/hai.h
+++ b/test/hai.h
@@ -1,4 +1,4 @@
-__declspec(dllexport)
void hai();
+
// test \ No newline at end of file
diff --git a/test/main.c b/test/main.c
index 3763706..f33d282 100644
--- a/test/main.c
+++ b/test/main.c
@@ -1,9 +1,80 @@
+
#include <stdio.h>
-#include "hai.h"
+#include <Xm/Xm.h>
+#include <Xm/MainW.h>
+#include <Xm/PushB.h>
+#include <Xm/ArrowB.h>
+#include <Xm/RowColumn.h>
+#include <Xm/Form.h>
+
+int lol() {
+ printf("yes!\n");
+}
int main(int argc, char **argv) {
- printf("Hello world\n");
- hai();
+ XtAppContext app;
+ Widget toplevel = XtVaAppInitialize(&app, "Demo", NULL, 0, &argc, argv, NULL, NULL);
+
+ /*Widget mainw = XtVaCreateManagedWidget("main_window",
+ xmMainWindowWidgetClass, toplevel,
+ XmNscrollBarDisplayPolicy, XmAS_NEEDED,
+ XmNscrollingPolicy, XmAUTOMATIC,
+ NULL);
+
+ XmString file = XmStringCreateLocalized("File");
+ XmString edit = XmStringCreateLocalized("Edit");
+ Widget menu = XmVaCreateSimpleMenuBar(mainw, "menubar",
+ XmVaCASCADEBUTTON, file, 'F',
+ XmVaCASCADEBUTTON, edit, 'E',
+ NULL);
+ XmStringFree(edit);
+ XmStringFree(file);
+
+ XtManageChild(menu); */
+
+ Widget form = XmVaCreateForm(toplevel, "form",
+ XmNfractionBase, 90,
+ NULL);
+ XtManageChild(form);
+
+ Widget left = XmVaCreateArrowButton(form, "voldown",
+ XmNarrowDirection, XmARROW_LEFT,
+ XmNtopAttachment, XmATTACH_FORM,
+ XmNleftAttachment, XmATTACH_FORM,
+ XmNbottomAttachment, XmATTACH_FORM,
+ XmNrightAttachment, XmATTACH_POSITION,
+ XmNrightPosition, 30,
+ NULL);
+ XtAddCallback(left, XmNactivateCallback, (XtCallbackProc)lol, NULL);
+ XtAddCallback(left, XmNdisarmCallback, (XtCallbackProc)lol, NULL);
+ XtManageChild(left);
+
+
+ Widget pb = XmVaCreatePushButton(form, "Volume",
+ XmNleftAttachment, XmATTACH_WIDGET,
+ XmNleftWidget, left,
+ XmNtopAttachment, XmATTACH_FORM,
+ XmNbottomAttachment, XmATTACH_FORM,
+ XmNrightAttachment, XmATTACH_POSITION,
+ XmNrightPosition, 60,
+ NULL);
+ XtAddCallback(pb, XmNactivateCallback, (XtCallbackProc)lol, NULL);
+ XtManageChild(pb);
+
+ Widget right = XmVaCreateArrowButton(form, "volup",
+ XmNarrowDirection, XmARROW_RIGHT,
+ XmNleftAttachment, XmATTACH_WIDGET,
+ XmNleftWidget, pb,
+ XmNrightAttachment, XmATTACH_FORM,
+ XmNtopAttachment, XmATTACH_FORM,
+ XmNbottomAttachment, XmATTACH_FORM,
+ NULL);
+ XtAddCallback(right, XmNactivateCallback, (XtCallbackProc)lol, NULL);
+ XtAddCallback(right, XmNdisarmCallback, (XtCallbackProc)lol, NULL);
+ XtManageChild(right);
+
+ XtRealizeWidget(toplevel);
+ XtAppMainLoop(app);
return 0;
}