fix dangling pointer issues in ext2dr
[lunaix-os.git] / lunaix-os / scripts / build-tools / integration / build_gen.py
1 from lbuild.api import BuildGenerator
2 from lbuild.common import BuildEnvironment
3 from lib.utils import join_path
4 from os import getenv
5
6 class MakefileBuildGen(BuildGenerator):
7     def __init__(self, out_dir, name = "lbuild.mkinc") -> None:
8         self.__path = join_path(out_dir, name)
9     
10     def emit_makearray(self, name, values):
11         r = []
12         r.append(f"{name} :=")
13         for v in values:
14             r.append(f"{v}")
15         return [" ".join(r)]
16
17     def generate(self, env: BuildEnvironment):
18         path = env.to_wspath(self.__path)
19         lines = []
20
21         opts = env.get_object("CC_OPTS", [])
22         lines.append("CFLAGS += %s"%(" ".join(opts)))
23         
24         opts = env.get_object("LD_OPTS", [])
25         lines.append("LDFLAGS += %s"%(" ".join(opts)))
26
27         arr = self.emit_makearray("_LBUILD_SRCS", env.srcs())
28         lines += arr
29
30         arr = self.emit_makearray("_LBUILD_HDRS", env.headers())
31         lines += arr
32
33         arr = self.emit_makearray("_LBUILD_INCS", env.includes())
34         lines += arr
35
36         with open(path, 'w') as f:
37             f.write("\n".join(lines))
38         
39
40 def install_lbuild_functions(_env: BuildEnvironment):
41     def set_opts(env: BuildEnvironment, name, opts, override):
42         if not isinstance(opts, list):
43             opts = [opts]
44         
45         _opts = env.get_object(name, [])
46
47         if override:
48             _opts = opts
49         else:
50             _opts += opts
51
52         env.set_object(name, _opts)
53
54     def compile_opts(env: BuildEnvironment, opts, override=False):
55         set_opts(env, "CC_OPTS", opts, override)
56
57     def linking_opts(env: BuildEnvironment, opts, override=False):
58         set_opts(env, "LD_OPTS", opts, override)
59
60     def env(env, name, default=None):
61         return getenv(name, default)
62
63
64     _env.add_external_func(compile_opts)
65     _env.add_external_func(linking_opts)
66     _env.add_external_func(env)