migrate stock user space build to new build system
authorLunaixsky <lunaixsky@qq.com>
Sat, 10 May 2025 23:50:02 +0000 (00:50 +0100)
committerLunaixsky <lunaixsky@qq.com>
Sat, 10 May 2025 23:50:02 +0000 (00:50 +0100)
* fix corner cases in luna_build.py when no LConfig is used
* add back the missing arch_bits_* flags
* fix sometimes the shconfig being triggered multiple times

15 files changed:
lunaix-os/arch/LConfig
lunaix-os/install_headers.mk
lunaix-os/makefile
lunaix-os/makeinc/lunabuild.mkinc
lunaix-os/scripts/build-tools/lcfg2/builder.py
lunaix-os/scripts/build-tools/lcfg2/config.py
lunaix-os/scripts/build-tools/luna_build.py
lunaix-os/scripts/build-tools/shared/scopes.py
lunaix-os/usr/LBuild
lunaix-os/usr/LConfig [deleted file]
lunaix-os/usr/libc/LBuild
lunaix-os/usr/libc/arch/i386/LBuild
lunaix-os/usr/libc/arch/x86_64/LBuild
lunaix-os/usr/libc/makefile
lunaix-os/usr/makefile

index a78038fd947a8e6a2e2790e75dbe460def55c59d..e9eacee132de309e8b2131b5a1345478268f7c19 100644 (file)
@@ -8,16 +8,24 @@ def architecture_support():
 
     @flag
     def arch_x86_32() -> bool:
 
     @flag
     def arch_x86_32() -> bool:
-        when(arch is "i386")
+        when (arch is "i386")
     
     @flag
     def arch_x86_64() -> bool:
     
     @flag
     def arch_x86_64() -> bool:
-        when(arch is "x86_64")
+        when (arch is "x86_64")
     
     @flag
     def arch_x86() -> bool:
     
     @flag
     def arch_x86() -> bool:
-        when(arch is "i386")
-        when(arch is "x86_64")
+        when (arch is "i386")
+        when (arch is "x86_64")
+
+    @flag
+    def arch_bits_64() -> bool:
+        when (arch_bits is 64)
+
+    @flag
+    def arch_bits_32() -> bool:
+        when (arch_bits is 32)
 
     @"Architecture"
     def arch() -> "i386" | "x86_64":
 
     @"Architecture"
     def arch() -> "i386" | "x86_64":
index d63637548a2725efd7b4b4ebf602f195af9ef4b2..fee40f08962426ca062062e1350c3b2bc8f8b42c 100644 (file)
@@ -4,6 +4,8 @@ ifndef PREFIX
        $(error "Must specify PREFIX to header install location")
 endif
 
        $(error "Must specify PREFIX to header install location")
 endif
 
+CFLAGS += $(kcflags)
+
 USR_HEADER := includes/usr
 
 HEADERS := $(shell cat $(USR_HEADER)/headers)
 USR_HEADER := includes/usr
 
 HEADERS := $(shell cat $(USR_HEADER)/headers)
index 5d6d2af897512b574a17f21f3772dd2290a90198..ce65d6798795f09db3d3cd2480ae930b0d32c5b2 100644 (file)
@@ -76,7 +76,6 @@ clean:
        @$(MAKE) -f kernel.mk clean -I $(mkinc_dir)
        
        @rm -rf $(kbuild_dir) || exit 1
        @$(MAKE) -f kernel.mk clean -I $(mkinc_dir)
        
        @rm -rf $(kbuild_dir) || exit 1
-       @rm -rf .builder || exit 1
 
 
 # --- begin misc --- #
 
 
 # --- begin misc --- #
index f4cd27048a531713888b918350c0c0af029c8621..2e08b388254948fa93e2519d41cc2fd483086140 100644 (file)
@@ -16,7 +16,7 @@ endef
 
 define __gen_build
        @echo restarting configuration...
 
 define __gen_build
        @echo restarting configuration...
-       @$(LBUILD) --gen-config $(lbuild_dir)
+       @$(LBUILD) --gen-build $(lbuild_dir)
 endef
 
 define __gen_both
 endef
 
 define __gen_both
index 235126c2d3d10387bfe5f1e442b800386dad43c6..832807afbe12f62381ea0f8ede2288d879dfa834 100644 (file)
@@ -1,5 +1,6 @@
 import ast
 import textwrap
 import ast
 import textwrap
+import os
 
 from lib.utils  import ConfigAST, ConfigASTVisitor
 from .common     import NodeProperty, ConfigNodeError, ValueTypeConstrain
 
 from lib.utils  import ConfigAST, ConfigASTVisitor
 from .common     import NodeProperty, ConfigNodeError, ValueTypeConstrain
@@ -99,6 +100,10 @@ class NodeBuilder(ConfigASTVisitor):
 
     @staticmethod
     def build(env, rootfile):
 
     @staticmethod
     def build(env, rootfile):
+        if not os.path.exists(rootfile):
+            print(f"warning: config file '{rootfile}' not detected, skipped")
+            return
+
         build = NodeBuilder(env)
         ast = ConfigAST(rootfile)
         
         build = NodeBuilder(env)
         ast = ConfigAST(rootfile)
         
index 9eda9e0e36718a98828abe7d2a5d68ff15bb698a..7ca19422e3b3fe1ac346027b772554752a3c9a91 100644 (file)
@@ -85,6 +85,9 @@ class ConfigEnvironment:
         return self.__globals
         
     def refresh(self):
         return self.__globals
         
     def refresh(self):
+        if not self.__exec:
+            return
+
         self.__update_globals()
 
         for name, node in self.__node_table.items():
         self.__update_globals()
 
         for name, node in self.__node_table.items():
@@ -109,4 +112,7 @@ class ConfigEnvironment:
     def terms(self):
         for node in self.__node_table.values():
             if isinstance(node, TermNode):
     def terms(self):
         for node in self.__node_table.values():
             if isinstance(node, TermNode):
-                yield node
\ No newline at end of file
+                yield node
+
+    def loaded(self):
+        return self.__exec is not None
\ No newline at end of file
index aafdb0ae73cc3ade0100bc6b82460620238f1d5c..0f96e3e4dfd5901e99bf01e5a0d66b353140aff2 100755 (executable)
@@ -12,7 +12,7 @@ from shared.export      import ExportJsonFile
 from shared.export      import ExportHeaderFile
 from shared.export      import ExportMakefileRules
 from shared.export      import restore_config_value
 from shared.export      import ExportHeaderFile
 from shared.export      import ExportMakefileRules
 from shared.export      import restore_config_value
-from shared.scopes      import ConfigScope
+from shared.scopes      import ConfigScope, EnvScope
 from shared.build_gen   import BuildScriptGenerator
 from shared.shconfig    import shconfig
 
 from shared.build_gen   import BuildScriptGenerator
 from shared.shconfig    import shconfig
 
@@ -35,6 +35,8 @@ class LunaBuild:
         scope.subscope("ld")
         self.__lbuilder.register_scope(scope)
 
         scope.subscope("ld")
         self.__lbuilder.register_scope(scope)
 
+        self.__lbuilder.register_scope(EnvScope())
+
         self.__json  = ExportJsonFile(self.__lconfig)
         self.__make  = ExportMakefileRules(self.__lconfig)
         self.__headr = ExportHeaderFile(self.__lconfig)
         self.__json  = ExportJsonFile(self.__lconfig)
         self.__make  = ExportMakefileRules(self.__lconfig)
         self.__headr = ExportHeaderFile(self.__lconfig)
@@ -76,6 +78,13 @@ class LunaBuild:
             self.__headr.export(outdir / "config.h")
 
     def visual_config(self):
             self.__headr.export(outdir / "config.h")
 
     def visual_config(self):
+        if not self.__lconfig.loaded():
+            print("no config file loaded, skipped interactive config")
+            return
+        
+        if not self.__opt.gen_config:
+            return
+        
         if not shconfig(self.__lconfig):
             print("configuration process aborted")
             exit(1)
         if not shconfig(self.__lconfig):
             print("configuration process aborted")
             exit(1)
index a56c3425bc7bf203b48d39baf63760f0a33fe8e6..507280b9ed7d59d421f1191f403b4104ec2de83a 100644 (file)
@@ -1,3 +1,5 @@
+import os
+
 from lbuild.scope import ScopeProvider
 from lcfg2.common import NodeProperty
 
 from lbuild.scope import ScopeProvider
 from lcfg2.common import NodeProperty
 
@@ -11,4 +13,14 @@ class ConfigScope(ScopeProvider):
         if node is None:
             raise Exception(f"config '{name}' not exists")
         
         if node is None:
             raise Exception(f"config '{name}' not exists")
         
-        return NodeProperty.Value[node]
\ No newline at end of file
+        if not NodeProperty.Enabled[node]:
+            return None
+
+        return NodeProperty.Value[node]
+
+class EnvScope(ScopeProvider):
+    def __init__(self):
+        super().__init__("env")
+
+    def __getitem__(self, name):
+        return os.environ.get(name)
\ No newline at end of file
index 0561206b9c4d4987227391bd211ce641577577ef..05a12461912f94768e8e0c88e6a2e436039b32a8 100644 (file)
@@ -1,4 +1,4 @@
-sources([
+src.c += (
     "testp",
     "ls",
     "signal_demo",
     "testp",
     "ls",
     "signal_demo",
@@ -10,36 +10,43 @@ sources([
     "mkdir",
     "rm",
     "fragfile",
     "mkdir",
     "rm",
     "fragfile",
-])
+)
 
 
-compile_opts([
+flag.cc += (
     "-ffreestanding",
     "-ffreestanding",
-    "-fno-pie"
-])
+    "-fno-pie",
+    "-Werror"
+)
 
 
-linking_opts([
+flag.ld += (
     "-nostdlib", 
     "-nolibc", 
     "-z noexecstack", 
     "-no-pie", 
     "-nostdlib", 
     "-nolibc", 
     "-z noexecstack", 
     "-no-pie", 
-])
+)
 
 
-linking_opts([
+flag.ld += (
     "-Wl,--build-id=none"
     "-Wl,--build-id=none"
-])
+)
 
 
-if config("arch") == "x86_64":
-    compile_opts([
-        "-m64", 
-        "-fno-unwind-tables", 
-        "-fno-asynchronous-unwind-tables",
-        "-mcmodel=large"
-    ])
-    linking_opts([
-        "-m64",
-    ])
-else:
-    compile_opts("-m32")
-    linking_opts("-m32")
+match env.ARCH:
+    case "x86_64":
+        flag.cc += (
+            "-m64", 
+            "-fno-unwind-tables", 
+            "-fno-asynchronous-unwind-tables",
+            "-mcmodel=large",
+            "-DCONFIG_ARCH_X86_64"
+        )
+        flag.ld += (
+            "-m64",
+        )
+    case "i386":
+        flag.cc += "-m32", "-DCONFIG_ARCH_X86_32"
+        flag.ld += "-m32"
 
 
-compile_opts("-mno-sse")
\ No newline at end of file
+flag.cc += "-mno-sse"
+
+flag.cc += (
+    "-Wno-discarded-qualifiers"
+)
\ No newline at end of file
diff --git a/lunaix-os/usr/LConfig b/lunaix-os/usr/LConfig
deleted file mode 100644 (file)
index ad2709c..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-@"Architecture"
-def arch() -> "i386" | "x86_64" | "aarch64" | "rv64":
-    """
-        set the ISA target
-    """
-
-    _arch = env("ARCH")
-    return _arch if _arch else "x86_64"
index 5c77fe202462e59c7616be63aca777b3360f9b43..148835f5b5dab9ca634c2e45577f9ee113d86586 100644 (file)
@@ -1,4 +1,4 @@
-sources([
+src.c += (
     "src/string.c",
     "src/termios.c",
     "src/itoa.c",
     "src/string.c",
     "src/termios.c",
     "src/itoa.c",
@@ -6,9 +6,9 @@ sources([
     "src/readdir.c",
     "src/pthread.c",
     "src/printf.c"
     "src/readdir.c",
     "src/pthread.c",
     "src/printf.c"
-])
+)
 
 
-sources([
+src.c += (
     "src/posix/signal.c",
     "src/posix/mount.c",
     "src/posix/errno.c",
     "src/posix/signal.c",
     "src/posix/mount.c",
     "src/posix/errno.c",
@@ -18,11 +18,10 @@ sources([
     "src/posix/unistd.c",
     "src/posix/mann.c",
     "src/posix/lunaix.c"
     "src/posix/unistd.c",
     "src/posix/mann.c",
     "src/posix/lunaix.c"
-])
+)
 
 
-use({
-    env("ARCH"): {
-        "i386": "arch/i386",
-        "x86_64": "arch/x86_64",
-    }
-})
\ No newline at end of file
+match env.ARCH:
+    case "i386":
+        from .arch import i386
+    case "x86_64":
+        from .arch import x86_64
index be547c8168596fc09f297259b5224237ef93fbbb..6dfebc1e3e1ed3d6a098639d58678dc18b4b2b44 100644 (file)
@@ -1,8 +1,8 @@
-sources([
+src.c += (
     "crt0.S",
     "syscall.S",
     "trampoline.S",
     "crt0.S",
     "syscall.S",
     "trampoline.S",
-])
+)
 
 
-compile_opts("-m32")
-linking_opts("-m32")
\ No newline at end of file
+flag.cc += "-m32"
+flag.ld += "-m32"
\ No newline at end of file
index 8e0ed54d4a1e3b38a92323bede9429ab72d2c84b..e13a4beefa32453393d680f3fd6900dbfc10d98c 100644 (file)
@@ -1,8 +1,8 @@
-sources([
+src.c += (
     "crt0.S",
     "syscall.S",
     "trampoline.S",
     "crt0.S",
     "syscall.S",
     "trampoline.S",
-])
+)
 
 
-compile_opts("-m64")
-linking_opts("-m64")
\ No newline at end of file
+flag.cc += "-m64"
+flag.ld += "-m64"
\ No newline at end of file
index 0c03770226d4a59d63f19b0bf67e0d6cbe7b5700..223ce4089ad89b58bc06408da3caccc263e2cb8b 100644 (file)
@@ -10,12 +10,12 @@ BUILD_NAME ?= liblunac
 src_dirs := src
 src_dirs += arch/$(ARCH)
 
 src_dirs := src
 src_dirs += arch/$(ARCH)
 
-obj_files := $(addsuffix .o, $(_LBUILD_SRCS))
+obj_files := $(addsuffix .o, $(BUILD_SRCS))
 
 build_lib := $(BUILD_DIR)/lib
 
 libc_include_opt = $(addprefix -I, $(libc_include))
 
 build_lib := $(BUILD_DIR)/lib
 
 libc_include_opt = $(addprefix -I, $(libc_include))
-global_include_opt = $(addprefix -I, $(INCLUDES) $(_LBUILD_INCS))
+global_include_opt = $(addprefix -I, $(INCLUDES) $(BUILD_INC))
 
 check_folders := $(src_dirs)
 check_folders += $(build_lib) $(LIBC_INCLUDE)
 
 check_folders := $(src_dirs)
 check_folders += $(build_lib) $(LIBC_INCLUDE)
index 49036fc5629967a2832284f464d8d4ec58912336..8fa1c316e75ba6d076e9e4ab4546f594849d4322 100644 (file)
@@ -1,8 +1,6 @@
 include utils.mkinc
 include toolchain.mkinc
 
 include utils.mkinc
 include toolchain.mkinc
 
-LCONFIG_FLAGS := --quiet
-
 include lunabuild.mkinc
 
 include $(lbuild_mkinc)
 include lunabuild.mkinc
 
 include $(lbuild_mkinc)
@@ -11,6 +9,9 @@ ifndef ARCH
 $(error ARCH is not set)
 endif
 
 $(error ARCH is not set)
 endif
 
+CFLAGS += $(BUILD_CFLAGS)
+LDFLAGS += $(BUILD_LDFLAGS)
+
 task := all
 
 sys_include := $(CURDIR)/includes
 task := all
 
 sys_include := $(CURDIR)/includes
@@ -29,7 +30,7 @@ BUILD_NAME := $(libc_name).a
 LIBC_INCLUDE := $(build_dir)/usr/includes
 
 mkapp-list := $(addprefix app-, $(shell cat apps.list))
 LIBC_INCLUDE := $(build_dir)/usr/includes
 
 mkapp-list := $(addprefix app-, $(shell cat apps.list))
-mkexec-list := $(addprefix $(build_dir)/bin/, $(_LBUILD_SRCS))
+mkexec-list := $(addprefix $(build_dir)/bin/, $(BUILD_SRCS))
 
 uexec_ld := $(CURDIR)/uexec.ld
 
 
 uexec_ld := $(CURDIR)/uexec.ld
 
@@ -51,7 +52,7 @@ $(build_dir)/$(libc_name).a: $(build_dir)/bin \
        @$(MAKE) $(MKFLAGS) -C libc/ $(task)
 
 $(uexec_ld): $(uexec_ld)x
        @$(MAKE) $(MKFLAGS) -C libc/ $(task)
 
 $(uexec_ld): $(uexec_ld)x
-       @$(CC) -include $(lbuild_config_h) -x c -P -E $< -o $@
+       @$(CC) $(CFLAGS) -x c -P -E $< -o $@
 
 # Application (with standalone makefile)
 export LD_SCRIPT := $(uexec_ld)
 
 # Application (with standalone makefile)
 export LD_SCRIPT := $(uexec_ld)