integrate C/LDFLAGS into LunaBuild flow
[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"define {name}")
13         for v in values:
14             r.append(v)
15         r.append("endef")
16         return r
17
18     def generate(self, env: BuildEnvironment):
19         path = env.to_wspath(self.__path)
20         lines = []
21
22         opts = env.get_object("CC_OPTS", [])
23         lines.append("CFLAGS += %s"%(" ".join(opts)))
24         
25         opts = env.get_object("LD_OPTS", [])
26         lines.append("LDFLAGS += %s"%(" ".join(opts)))
27
28         arr = self.emit_makearray("_LBUILD_SRCS", env.srcs())
29         lines += arr
30
31         arr = self.emit_makearray("_LBUILD_HDRS", env.headers())
32         lines += arr
33
34         arr = self.emit_makearray("_LBUILD_INCS", env.includes())
35         lines += arr
36
37         with open(path, 'w') as f:
38             f.write("\n".join(lines))
39         
40
41 def install_lbuild_functions(_env: BuildEnvironment):
42     def set_opts(env: BuildEnvironment, name, opts, override):
43         if not isinstance(opts, list):
44             opts = [opts]
45         
46         _opts = env.get_object(name, [])
47
48         if override:
49             _opts = opts
50         else:
51             _opts += opts
52
53         env.set_object(name, _opts)
54
55     def compile_opts(env: BuildEnvironment, opts, override=False):
56         set_opts(env, "CC_OPTS", opts, override)
57
58     def linking_opts(env: BuildEnvironment, opts, override=False):
59         set_opts(env, "LD_OPTS", opts, override)
60
61     def env(env, name, default=None):
62         return getenv(name, default)
63
64
65     _env.add_external_func(compile_opts)
66     _env.add_external_func(linking_opts)
67     _env.add_external_func(env)