Rewrite the lunabuild toolchain with enhanced feature (#60)
[lunaix-os.git] / lunaix-os / scripts / build-tools / lbuild / common.py
index c559b0aab6bfd10dfc7d468153af216378efcd3a..01a226cfa6f333849ca1e05eaa40b419fe420d05 100644 (file)
@@ -1,43 +1,57 @@
-from lib.utils import join_path
-import os
+import ast
+from pathlib import Path
 
-class BuildEnvironment:
-    def __init__(self, workspace_dir) -> None:
-        self.__config_provider = None
-        self.__sources = []
-        self.__headers = []
-        self.__inc_dir = []
-        self.__ws_dir = workspace_dir
+class DirectoryTracker:
+    def __init__(self):
+        self.__stack = []
 
-    def set_config_provider(self, provider):
-        self.__config_provider = provider
-    
-    def config_provider(self):
-        return self.__config_provider
-    
-    def add_sources(self, sources):
-        self.__sources += sources
+    def push(self, dir):
+        self.__stack.append(Path(dir))
 
-    def add_headers(self, sources):
-        for h in sources:
-            if not os.path.isdir(h):
-                self.__headers.append(h)
-            else:
-                self.__inc_dir.append(h)
+    def pop(self):
+        self.__stack.pop()
+    
+    def active_relative(self):
+        root = self.__stack[0]
+        return self.__stack[-1].relative_to(root)
 
-    def to_wspath(self, file):
-        path = join_path(self.__ws_dir, file)
-        return os.path.relpath(path, self.__ws_dir)
+    @staticmethod
+    def context_name():
+        return "__FILESTACK__"
+    
+    @staticmethod
+    def get(context):
+        return context[DirectoryTracker.context_name()]
     
-    def export(self, out_dir):
-        path = os.path.join(out_dir, "sources.list")
-        with open(path, "w") as f:
-            f.write("\n".join(self.__sources))
+    @staticmethod
+    def bind(context):
+        name = DirectoryTracker.context_name()
+        context[name] = DirectoryTracker()
 
-        path = os.path.join(out_dir, "headers.list")
-        with open(path, "w") as f:
-            f.write("\n".join(self.__headers))
+    @staticmethod
+    def emit_enter(dir):
+        name = DirectoryTracker.context_name()
+        return ast.Expr(
+                    ast.Call(
+                        func=ast.Attribute(
+                            value=ast.Name(name, ctx=ast.Load()),
+                            attr="push",
+                            ctx=ast.Load()
+                        ),
+                        args=[
+                            ast.Constant(str(dir))
+                        ],
+                        keywords=[]))
 
-        path = os.path.join(out_dir, "includes.list")
-        with open(path, "w") as f:
-            f.write("\n".join(self.__inc_dir))
\ No newline at end of file
+    @staticmethod
+    def emit_leave():
+        name = DirectoryTracker.context_name()
+        return ast.Expr(
+                    ast.Call(
+                        func=ast.Attribute(
+                            value=ast.Name(name, ctx=ast.Load()),
+                            attr="pop",
+                            ctx=ast.Load()
+                        ),
+                        args=[],
+                        keywords=[]))