mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
baremetal: refactor build to reduce duplication
This commit is contained in:
174
build-baremetal
174
build-baremetal
@@ -18,11 +18,23 @@ Build the baremetal examples with crosstool-NG.
|
||||
|
||||
def build(self):
|
||||
build_dir = self.get_build_dir()
|
||||
bootloader_obj = os.path.join(self.env['baremetal_build_lib_dir'], 'bootloader{}'.format(self.env['obj_ext']))
|
||||
common_obj = os.path.join(self.env['baremetal_build_lib_dir'], self.env['common_basename_noext'] + self.env['obj_ext'])
|
||||
bootloader_obj = os.path.join(
|
||||
self.env['baremetal_build_lib_dir'],
|
||||
'bootloader{}'.format(self.env['obj_ext'])
|
||||
)
|
||||
common_obj = os.path.join(
|
||||
self.env['baremetal_build_lib_dir'],
|
||||
self.env['common_basename_noext'] + self.env['obj_ext']
|
||||
)
|
||||
syscalls_basename_noext = 'syscalls'
|
||||
syscalls_src = os.path.join(self.env['baremetal_source_lib_dir'], syscalls_basename_noext + self.env['c_ext'])
|
||||
syscalls_obj = os.path.join(self.env['baremetal_build_lib_dir'], syscalls_basename_noext + self.env['obj_ext'])
|
||||
syscalls_src = os.path.join(
|
||||
self.env['baremetal_source_lib_dir'],
|
||||
syscalls_basename_noext + self.env['c_ext']
|
||||
)
|
||||
syscalls_obj = os.path.join(
|
||||
self.env['baremetal_build_lib_dir'],
|
||||
syscalls_basename_noext + self.env['obj_ext']
|
||||
)
|
||||
common_objs = [common_obj, syscalls_obj]
|
||||
cflags = [
|
||||
'-I', self.env['baremetal_source_lib_dir'], LF,
|
||||
@@ -49,7 +61,13 @@ Build the baremetal examples with crosstool-NG.
|
||||
uart_address = 0x09000000
|
||||
os.makedirs(build_dir, exist_ok=True)
|
||||
os.makedirs(self.env['baremetal_build_lib_dir'], exist_ok=True)
|
||||
src = os.path.join(self.env['baremetal_source_lib_dir'], '{}{}'.format(self.env['arch'], self.env['asm_ext']))
|
||||
src = os.path.join(
|
||||
self.env['baremetal_source_lib_dir'],
|
||||
'{}{}'.format(
|
||||
self.env['arch'],
|
||||
self.env['asm_ext']
|
||||
)
|
||||
)
|
||||
if self.need_rebuild([src], bootloader_obj):
|
||||
self.sh.run_cmd(
|
||||
[gcc, LF] +
|
||||
@@ -77,105 +95,63 @@ Build the baremetal examples with crosstool-NG.
|
||||
] +
|
||||
cflags_after
|
||||
)
|
||||
self._build_dir(
|
||||
for subpath in [
|
||||
'',
|
||||
gcc=gcc,
|
||||
cflags=cflags,
|
||||
cflags_after=cflags_after,
|
||||
entry_address=entry_address,
|
||||
bootloader_obj=bootloader_obj,
|
||||
common_objs=common_objs,
|
||||
)
|
||||
self._build_dir(
|
||||
'interactive',
|
||||
gcc=gcc,
|
||||
cflags=cflags,
|
||||
cflags_after=cflags_after,
|
||||
entry_address=entry_address,
|
||||
bootloader_obj=bootloader_obj,
|
||||
common_objs=common_objs,
|
||||
)
|
||||
if os.path.isdir(os.path.join(self.env['baremetal_source_arch_dir'])):
|
||||
self._build_dir(
|
||||
self.env['baremetal_source_arch_subpath'],
|
||||
gcc=gcc,
|
||||
cflags=cflags,
|
||||
cflags_after=cflags_after,
|
||||
entry_address=entry_address,
|
||||
bootloader_obj=bootloader_obj,
|
||||
common_objs=common_objs,
|
||||
)
|
||||
arch_dir = os.path.join('arch', self.env['arch'], 'no_bootloader')
|
||||
if os.path.isdir(os.path.join(self.env['baremetal_source_dir'], arch_dir)):
|
||||
self._build_dir(
|
||||
arch_dir,
|
||||
gcc=gcc,
|
||||
cflags=cflags,
|
||||
cflags_after=cflags_after,
|
||||
entry_address=entry_address,
|
||||
bootloader_obj=bootloader_obj,
|
||||
common_objs=common_objs,
|
||||
bootloader=False,
|
||||
)
|
||||
self.env['baremetal_source_arch_subpath'],
|
||||
os.path.join(self.env['baremetal_source_arch_subpath'], 'no_bootloader'),
|
||||
]:
|
||||
in_dir = os.path.join(self.env['baremetal_source_dir'], subpath)
|
||||
if os.path.isdir(in_dir):
|
||||
out_dir = os.path.join(self.env['baremetal_build_dir'], subpath)
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
common_objs_bootloader = common_objs.copy()
|
||||
if os.path.basename(subpath) != 'no_bootloader':
|
||||
common_objs_bootloader.append(bootloader_obj)
|
||||
for in_basename in sorted(os.listdir(in_dir)):
|
||||
in_path = os.path.join(in_dir, in_basename)
|
||||
in_name, in_ext = os.path.splitext(in_basename)
|
||||
if (
|
||||
os.path.isfile(in_path) and
|
||||
in_ext in (self.env['c_ext'], self.env['asm_ext'])
|
||||
):
|
||||
main_obj = os.path.join(
|
||||
out_dir,
|
||||
'{}{}'.format(
|
||||
in_name,
|
||||
self.env['obj_ext']
|
||||
)
|
||||
)
|
||||
src = os.path.join(self.env['baremetal_source_dir'], in_path)
|
||||
if self.need_rebuild([src, self.env['common_h']], main_obj):
|
||||
self.sh.run_cmd(
|
||||
[gcc, LF] +
|
||||
cflags +
|
||||
[
|
||||
'-c', LF,
|
||||
'-o', main_obj, LF,
|
||||
src, LF,
|
||||
] +
|
||||
cflags_after
|
||||
)
|
||||
objs = common_objs_bootloader + [main_obj]
|
||||
out = os.path.join(out_dir, in_name + self.env['baremetal_build_ext'])
|
||||
if self.need_rebuild(objs + [self.env['baremetal_link_script']], out):
|
||||
self.sh.run_cmd(
|
||||
[gcc, LF] +
|
||||
cflags +
|
||||
[
|
||||
'-Wl,--section-start=.text={:#x}'.format(entry_address), LF,
|
||||
'-o', out, LF,
|
||||
'-T', self.env['baremetal_link_script'], LF,
|
||||
] +
|
||||
self.sh.add_newlines(objs) +
|
||||
cflags_after
|
||||
)
|
||||
|
||||
|
||||
def get_build_dir(self):
|
||||
return self.env['baremetal_build_dir']
|
||||
|
||||
def _build_dir(
|
||||
self,
|
||||
subpath,
|
||||
gcc,
|
||||
cflags,
|
||||
cflags_after,
|
||||
entry_address,
|
||||
bootloader_obj,
|
||||
common_objs,
|
||||
bootloader=True
|
||||
):
|
||||
'''
|
||||
Build all .c and .S files in a given subpath of the baremetal source
|
||||
directory non recursively.
|
||||
|
||||
Place outputs on the same subpath or the output directory.
|
||||
'''
|
||||
in_dir = os.path.join(self.env['baremetal_source_dir'], subpath)
|
||||
out_dir = os.path.join(self.env['baremetal_build_dir'], subpath)
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
common_objs = common_objs.copy()
|
||||
if bootloader:
|
||||
common_objs.append(bootloader_obj)
|
||||
for in_basename in os.listdir(in_dir):
|
||||
in_path = os.path.join(in_dir, in_basename)
|
||||
if os.path.isfile(in_path) and os.path.splitext(in_basename)[1] in (self.env['c_ext'], self.env['asm_ext']):
|
||||
in_name = os.path.splitext(in_basename)[0]
|
||||
main_obj = os.path.join(self.env['baremetal_build_dir'], subpath, '{}{}'.format(in_name, self.env['obj_ext']))
|
||||
src = os.path.join(self.env['baremetal_source_dir'], in_path)
|
||||
if self.need_rebuild([src, self.env['common_h']], main_obj):
|
||||
self.sh.run_cmd(
|
||||
[gcc, LF] +
|
||||
cflags +
|
||||
[
|
||||
'-c', LF,
|
||||
'-o', main_obj, LF,
|
||||
src, LF,
|
||||
] +
|
||||
cflags_after
|
||||
)
|
||||
objs = common_objs + [main_obj]
|
||||
out = os.path.join(self.env['baremetal_build_dir'], subpath, in_name + self.env['baremetal_build_ext'])
|
||||
link_script = os.path.join(self.env['baremetal_source_dir'], 'link.ld')
|
||||
if self.need_rebuild(objs + [link_script], out):
|
||||
self.sh.run_cmd(
|
||||
[gcc, LF] +
|
||||
cflags +
|
||||
[
|
||||
'-Wl,--section-start=.text={:#x}'.format(entry_address), LF,
|
||||
'-o', out, LF,
|
||||
'-T', link_script, LF,
|
||||
] +
|
||||
self.sh.add_newlines(objs) +
|
||||
cflags_after
|
||||
)
|
||||
|
||||
if __name__ == '__main__':
|
||||
Main().cli()
|
||||
|
||||
@@ -718,6 +718,7 @@ Valid emulators: {}
|
||||
env['baremetal_source_arch_subpath'] = join('arch', env['arch'])
|
||||
env['baremetal_source_arch_dir'] = join(env['baremetal_source_dir'], env['baremetal_source_arch_subpath'])
|
||||
env['baremetal_source_lib_dir'] = join(env['baremetal_source_dir'], env['baremetal_lib_basename'])
|
||||
env['baremetal_link_script'] = os.path.join(env['baremetal_source_dir'], 'link.ld')
|
||||
if env['emulator'] == 'gem5':
|
||||
env['simulator_name'] = 'gem5'
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user