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
headers([
"includes"
+])
+
+compile_opts([
+ "-m32"
+])
+
+linking_opts([
+ "-m32"
])
\ No newline at end of file
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,$@)
.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
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):
@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
@$(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))
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 \
-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
--- /dev/null
+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
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
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
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
"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 = []
@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
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
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)
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)
print("failed to resolve root build file")
raise err
- env.export(opts.out_dir)
+ env.export()
def main():
parser = ArgumentParser()
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