From: Minep Date: Thu, 4 Jul 2024 18:44:47 +0000 (+0100) Subject: integrate C/LDFLAGS into LunaBuild flow X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/commitdiff_plain/29891c3ccec4f1d28e0440d87ea2e2708377d2ee integrate C/LDFLAGS into LunaBuild flow * allow LBuild framework to accept custom generation implementation * allow LBuild framework to accept custom built-in function * separate the compilation flags setting by arch --- diff --git a/lunaix-os/LBuild b/lunaix-os/LBuild index f4bb7c8..dcee304 100644 --- a/lunaix-os/LBuild +++ b/lunaix-os/LBuild @@ -6,4 +6,22 @@ use("hal") headers([ "includes", "includes/usr" +]) + +# compliation setting + +compile_opts([ + "-ffreestanding", + "-fno-pie" +]) + +linking_opts([ + "-nostdlib", + "-nolibc", + "-z noexecstack", + "-no-pie", +]) + +linking_opts([ + "-Wl,--build-id=none" ]) \ No newline at end of file diff --git a/lunaix-os/arch/i386/LBuild b/lunaix-os/arch/i386/LBuild index d4c9a96..8a7f155 100644 --- a/lunaix-os/arch/i386/LBuild +++ b/lunaix-os/arch/i386/LBuild @@ -41,4 +41,12 @@ sources([ headers([ "includes" +]) + +compile_opts([ + "-m32" +]) + +linking_opts([ + "-m32" ]) \ No newline at end of file diff --git a/lunaix-os/kernel.mk b/lunaix-os/kernel.mk index 9399e71..dff0de2 100644 --- a/lunaix-os/kernel.mk +++ b/lunaix-os/kernel.mk @@ -1,37 +1,35 @@ include os.mkinc include toolchain.mkinc -ksrc_files = $(shell cat .builder/sources.list) -kinc_dirs = $(shell cat .builder/includes.list) -khdr_files = $(shell cat .builder/headers.list) -khdr_files += .builder/configs.h +include .builder/lbuild.mkinc kbin_dir := $(BUILD_DIR) kbin := $(BUILD_NAME) -ksrc_objs := $(addsuffix .o,$(ksrc_files)) -ksrc_deps := $(addsuffix .d,$(ksrc_files)) -khdr_opts := $(addprefix -include ,$(khdr_files)) -kinc_opts := $(addprefix -I,$(kinc_dirs)) +ksrc_objs := $(addsuffix .o,$(_LBUILD_SRCS)) +ksrc_deps := $(addsuffix .d,$(_LBUILD_SRCS)) +khdr_opts := $(addprefix -include ,$(_LBUILD_HDRS)) +kinc_opts := $(addprefix -I,$(_LBUILD_INCS)) +config_h += -include.builder/configs.h tmp_kbin := $(BUILD_DIR)/tmpk.bin ksymtable := lunaix_ksyms.o -CFLAGS += $(khdr_opts) +CFLAGS += $(khdr_opts) $(kinc_opts) $(config_h) -MMD -MP -include $(ksrc_deps) %.S.o: %.S $(khdr_files) kernel.mk $(call status_,AS,$<) - @$(CC) $(CFLAGS) $(kinc_opts) -MMD -MP -c $< -o $@ + @$(CC) $(CFLAGS) -c $< -o $@ %.c.o: %.c $(khdr_files) kernel.mk $(call status_,CC,$<) - @$(CC) $(CFLAGS) $(kinc_opts) -MMD -MP -c $< -o $@ + @$(CC) $(CFLAGS) -c $< -o $@ $(tmp_kbin): $(ksrc_objs) $(call status_,LD,$@) - @$(CC) -T link/linker.ld $(LDFLAGS) -o $@ $^ + @$(CC) -T link/linker.ld $(config_h) $(LDFLAGS) -o $@ $^ $(ksymtable): $(tmp_kbin) $(call status_,KSYM,$@) @@ -41,7 +39,7 @@ $(ksymtable): $(tmp_kbin) .PHONY: __do_relink __do_relink: $(ksrc_objs) $(ksymtable) $(call status_,LD,$(kbin)) - @$(CC) -T link/linker.ld $(LDFLAGS) -o $(kbin) $^ + @$(CC) -T link/linker.ld $(config_h) $(LDFLAGS) -o $(kbin) $^ @rm $(tmp_kbin) .PHONY: all diff --git a/lunaix-os/makefile b/lunaix-os/makefile index 4e13211..09ee4b9 100644 --- a/lunaix-os/makefile +++ b/lunaix-os/makefile @@ -28,12 +28,6 @@ $(DEPS): echo "failed" && exit 1;\ fi -define builder_data - .builder/sources.list - .builder/headers.list - .builder/includes.list -endef - all_lconfigs = $(shell find . -name "LConfig") $(kbuild_dir): @@ -48,13 +42,13 @@ $(kbuild_dir): @echo @./scripts/build-tools/luna_build.py --config --lconfig-file LConfig -o $(@D) -.builder/%.list: .builder/configs.h +.builder/lbuild.mkinc: .builder/configs.h @./scripts/build-tools/luna_build.py LBuild --lconfig-file LConfig -o $(@D) .PHONY: kernel export BUILD_DIR=$(kbin_dir) export BUILD_NAME=$(kbin) -kernel: $(builder_data) +kernel: .builder/lbuild.mkinc $(call status,TASK,$(notdir $@)) @$(MAKE) $(MKFLAGS) -I $(mkinc_dir) -f kernel.mk all @@ -96,7 +90,7 @@ clean: @$(MAKE) -C usr clean -I $(mkinc_dir) @$(MAKE) -f kernel.mk clean -I $(mkinc_dir) @rm -rf $(kbuild_dir) || exit 1 - @rm -f .builder/*.list || exit 1 + @rm -f .builder/lbuild.mkinc || exit 1 run: all @qemu-system-i386 $(call get_qemu_options,$(kimg)) diff --git a/lunaix-os/makeinc/toolchain.mkinc b/lunaix-os/makeinc/toolchain.mkinc index c9d085f..552c2fd 100644 --- a/lunaix-os/makeinc/toolchain.mkinc +++ b/lunaix-os/makeinc/toolchain.mkinc @@ -3,10 +3,6 @@ CC := $(CX_PREFIX)gcc AS := $(CX_PREFIX)as AR := $(CX_PREFIX)ar -STRIP_OSDEP_CC := -ffreestanding -fno-pie -STRIP_OSDEP_LD := -nostdlib -nolibc -z noexecstack -no-pie -Wl,--build-id=none - -ARCH_OPT := -m32 -D__ARCH_IA32 O := -O2 W := -Wall -Wextra -Werror \ -Wno-unknown-pragmas \ @@ -27,15 +23,15 @@ OFLAGS := -fno-gcse\ -fno-indirect-inlining\ -fno-omit-frame-pointer -CFLAGS := $(ARCH_OPT) -std=gnu99 $(OFLAGS) $(W) +CFLAGS := -std=gnu99 $(OFLAGS) $(W) ifeq ($(BUILD_MODE),debug) O = -Og CFLAGS += -g endif -CFLAGS += $(O) $(STRIP_OSDEP_CC) +CFLAGS += $(O) -LDFLAGS := $(ARCH_OPT) $(O) $(STRIP_OSDEP_LD) +LDFLAGS := $(O) MKFLAGS := --no-print-directory \ No newline at end of file diff --git a/lunaix-os/scripts/build-tools/integration/build_gen.py b/lunaix-os/scripts/build-tools/integration/build_gen.py new file mode 100644 index 0000000..83c9904 --- /dev/null +++ b/lunaix-os/scripts/build-tools/integration/build_gen.py @@ -0,0 +1,67 @@ +from lbuild.api import BuildGenerator +from lbuild.common import BuildEnvironment +from lib.utils import join_path +from os import getenv + +class MakefileBuildGen(BuildGenerator): + def __init__(self, out_dir, name = "lbuild.mkinc") -> None: + self.__path = join_path(out_dir, name) + + def emit_makearray(self, name, values): + r = [] + r.append(f"define {name}") + for v in values: + r.append(v) + r.append("endef") + return r + + def generate(self, env: BuildEnvironment): + path = env.to_wspath(self.__path) + lines = [] + + opts = env.get_object("CC_OPTS", []) + lines.append("CFLAGS += %s"%(" ".join(opts))) + + opts = env.get_object("LD_OPTS", []) + lines.append("LDFLAGS += %s"%(" ".join(opts))) + + arr = self.emit_makearray("_LBUILD_SRCS", env.srcs()) + lines += arr + + arr = self.emit_makearray("_LBUILD_HDRS", env.headers()) + lines += arr + + arr = self.emit_makearray("_LBUILD_INCS", env.includes()) + lines += arr + + with open(path, 'w') as f: + f.write("\n".join(lines)) + + +def install_lbuild_functions(_env: BuildEnvironment): + def set_opts(env: BuildEnvironment, name, opts, override): + if not isinstance(opts, list): + opts = [opts] + + _opts = env.get_object(name, []) + + if override: + _opts = opts + else: + _opts += opts + + env.set_object(name, _opts) + + def compile_opts(env: BuildEnvironment, opts, override=False): + set_opts(env, "CC_OPTS", opts, override) + + def linking_opts(env: BuildEnvironment, opts, override=False): + set_opts(env, "LD_OPTS", opts, override) + + def env(env, name, default=None): + return getenv(name, default) + + + _env.add_external_func(compile_opts) + _env.add_external_func(linking_opts) + _env.add_external_func(env) \ No newline at end of file diff --git a/lunaix-os/scripts/build-tools/lbuild/api.py b/lunaix-os/scripts/build-tools/lbuild/api.py index 2bc91fa..9f6f8d4 100644 --- a/lunaix-os/scripts/build-tools/lbuild/api.py +++ b/lunaix-os/scripts/build-tools/lbuild/api.py @@ -6,4 +6,11 @@ class ConfigProvider: raise ValueError(f"config '{name}' is undefined or disabled") def has_config(self, name): - return False \ No newline at end of file + return False + +class BuildGenerator: + def __init__(self) -> None: + pass + + def generate(self, env): + pass \ No newline at end of file diff --git a/lunaix-os/scripts/build-tools/lbuild/common.py b/lunaix-os/scripts/build-tools/lbuild/common.py index c559b0a..a973760 100644 --- a/lunaix-os/scripts/build-tools/lbuild/common.py +++ b/lunaix-os/scripts/build-tools/lbuild/common.py @@ -2,12 +2,15 @@ from lib.utils import join_path import os class BuildEnvironment: - def __init__(self, workspace_dir) -> None: + def __init__(self, workspace_dir, generator) -> None: self.__config_provider = None self.__sources = [] self.__headers = [] self.__inc_dir = [] self.__ws_dir = workspace_dir + self.__ext_object = {} + self.__ext_function = {} + self.__generator = generator def set_config_provider(self, provider): self.__config_provider = provider @@ -29,15 +32,28 @@ class BuildEnvironment: path = join_path(self.__ws_dir, file) return os.path.relpath(path, self.__ws_dir) - 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)) - - path = os.path.join(out_dir, "headers.list") - with open(path, "w") as f: - f.write("\n".join(self.__headers)) - - 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 + def export(self): + self.__generator.generate(self) + + def get_object(self, key, _default=None): + return _default if key not in self.__ext_object else self.__ext_object[key] + + def set_object(self, key, object): + self.__ext_object[key] = object + + def srcs(self): + return list(self.__sources) + + def headers(self): + return list(self.__headers) + + def includes(self): + return list(self.__inc_dir) + + def add_external_func(self, function): + name = function.__name__ + invk = lambda *args, **kwargs: function(self, *args, **kwargs) + self.__ext_function[name] = invk + + def external_func_table(self): + return self.__ext_function \ No newline at end of file diff --git a/lunaix-os/scripts/build-tools/lbuild/contract.py b/lunaix-os/scripts/build-tools/lbuild/contract.py index 1b818ab..643be14 100644 --- a/lunaix-os/scripts/build-tools/lbuild/contract.py +++ b/lunaix-os/scripts/build-tools/lbuild/contract.py @@ -18,7 +18,8 @@ class LunaBuildFile(Sandbox): "config": lambda name: self.read_config(name), "use": - lambda file: self.import_buildfile(file) + lambda file: self.import_buildfile(file), + **env.external_func_table() }) self.__srcs = [] diff --git a/lunaix-os/scripts/build-tools/lcfg/builtins.py b/lunaix-os/scripts/build-tools/lcfg/builtins.py index 9eb616f..7a46699 100644 --- a/lunaix-os/scripts/build-tools/lcfg/builtins.py +++ b/lunaix-os/scripts/build-tools/lcfg/builtins.py @@ -29,4 +29,8 @@ def parent(env, caller, ref): @contextual(caller_type=[LCTermNode]) def default(env, caller, val): - caller.set_default(val) \ No newline at end of file + caller.set_default(val) + +@builtin() +def env(env, key, default=None): + return os.getenv(key, default) \ No newline at end of file diff --git a/lunaix-os/scripts/build-tools/luna_build.py b/lunaix-os/scripts/build-tools/luna_build.py index 848ceb6..3d7f21a 100755 --- a/lunaix-os/scripts/build-tools/luna_build.py +++ b/lunaix-os/scripts/build-tools/luna_build.py @@ -7,6 +7,7 @@ from lcfg.common import LConfigEnvironment from integration.config_io import CHeaderConfigProvider from integration.lbuild_bridge import LConfigProvider from integration.render_ishell import InteractiveShell +from integration.build_gen import MakefileBuildGen, install_lbuild_functions import lcfg.types as lcfg_type import lcfg.builtins as builtin @@ -26,6 +27,7 @@ def prepare_lconfig_env(out_dir): env.register_builtin_func(builtin.parent) env.register_builtin_func(builtin.default) env.register_builtin_func(builtin.include) + env.register_builtin_func(builtin.env) env.type_factory().regitser(lcfg_type.PrimitiveType) env.type_factory().regitser(lcfg_type.MultipleChoiceType) @@ -46,7 +48,10 @@ def do_buildfile_gen(opts, lcfg_env): ws_path = dirname(root_path) root_name = basename(root_path) - env = BuildEnvironment(ws_path) + mkgen = MakefileBuildGen(opts.out_dir) + env = BuildEnvironment(ws_path, mkgen) + + install_lbuild_functions(env) cfg_provider = LConfigProvider(lcfg_env) env.set_config_provider(cfg_provider) @@ -59,7 +64,7 @@ def do_buildfile_gen(opts, lcfg_env): print("failed to resolve root build file") raise err - env.export(opts.out_dir) + env.export() def main(): parser = ArgumentParser() diff --git a/lunaix-os/usr/makefile b/lunaix-os/usr/makefile index e49d7d0..4dc8d0d 100644 --- a/lunaix-os/usr/makefile +++ b/lunaix-os/usr/makefile @@ -3,6 +3,10 @@ include toolchain.mkinc task := all +# TODO make this use LBuild +CFLAGS += -m32 -ffreestanding -fno-pie +LDFLAGS += -m32 -nostdlib -nolibc -z noexecstack -no-pie -Wl,--build-id=none + sys_include := $(CURDIR)/includes build_dir := $(CURDIR)/build libc_name := liblunac