mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-30 05:24:25 +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):
|
def build(self):
|
||||||
build_dir = self.get_build_dir()
|
build_dir = self.get_build_dir()
|
||||||
bootloader_obj = os.path.join(self.env['baremetal_build_lib_dir'], 'bootloader{}'.format(self.env['obj_ext']))
|
bootloader_obj = os.path.join(
|
||||||
common_obj = os.path.join(self.env['baremetal_build_lib_dir'], self.env['common_basename_noext'] + self.env['obj_ext'])
|
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_basename_noext = 'syscalls'
|
||||||
syscalls_src = os.path.join(self.env['baremetal_source_lib_dir'], syscalls_basename_noext + self.env['c_ext'])
|
syscalls_src = os.path.join(
|
||||||
syscalls_obj = os.path.join(self.env['baremetal_build_lib_dir'], syscalls_basename_noext + self.env['obj_ext'])
|
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]
|
common_objs = [common_obj, syscalls_obj]
|
||||||
cflags = [
|
cflags = [
|
||||||
'-I', self.env['baremetal_source_lib_dir'], LF,
|
'-I', self.env['baremetal_source_lib_dir'], LF,
|
||||||
@@ -49,7 +61,13 @@ Build the baremetal examples with crosstool-NG.
|
|||||||
uart_address = 0x09000000
|
uart_address = 0x09000000
|
||||||
os.makedirs(build_dir, exist_ok=True)
|
os.makedirs(build_dir, exist_ok=True)
|
||||||
os.makedirs(self.env['baremetal_build_lib_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):
|
if self.need_rebuild([src], bootloader_obj):
|
||||||
self.sh.run_cmd(
|
self.sh.run_cmd(
|
||||||
[gcc, LF] +
|
[gcc, LF] +
|
||||||
@@ -77,105 +95,63 @@ Build the baremetal examples with crosstool-NG.
|
|||||||
] +
|
] +
|
||||||
cflags_after
|
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',
|
'interactive',
|
||||||
gcc=gcc,
|
self.env['baremetal_source_arch_subpath'],
|
||||||
cflags=cflags,
|
os.path.join(self.env['baremetal_source_arch_subpath'], 'no_bootloader'),
|
||||||
cflags_after=cflags_after,
|
]:
|
||||||
entry_address=entry_address,
|
in_dir = os.path.join(self.env['baremetal_source_dir'], subpath)
|
||||||
bootloader_obj=bootloader_obj,
|
if os.path.isdir(in_dir):
|
||||||
common_objs=common_objs,
|
out_dir = os.path.join(self.env['baremetal_build_dir'], subpath)
|
||||||
)
|
os.makedirs(out_dir, exist_ok=True)
|
||||||
if os.path.isdir(os.path.join(self.env['baremetal_source_arch_dir'])):
|
common_objs_bootloader = common_objs.copy()
|
||||||
self._build_dir(
|
if os.path.basename(subpath) != 'no_bootloader':
|
||||||
self.env['baremetal_source_arch_subpath'],
|
common_objs_bootloader.append(bootloader_obj)
|
||||||
gcc=gcc,
|
for in_basename in sorted(os.listdir(in_dir)):
|
||||||
cflags=cflags,
|
in_path = os.path.join(in_dir, in_basename)
|
||||||
cflags_after=cflags_after,
|
in_name, in_ext = os.path.splitext(in_basename)
|
||||||
entry_address=entry_address,
|
if (
|
||||||
bootloader_obj=bootloader_obj,
|
os.path.isfile(in_path) and
|
||||||
common_objs=common_objs,
|
in_ext in (self.env['c_ext'], self.env['asm_ext'])
|
||||||
)
|
):
|
||||||
arch_dir = os.path.join('arch', self.env['arch'], 'no_bootloader')
|
main_obj = os.path.join(
|
||||||
if os.path.isdir(os.path.join(self.env['baremetal_source_dir'], arch_dir)):
|
out_dir,
|
||||||
self._build_dir(
|
'{}{}'.format(
|
||||||
arch_dir,
|
in_name,
|
||||||
gcc=gcc,
|
self.env['obj_ext']
|
||||||
cflags=cflags,
|
)
|
||||||
cflags_after=cflags_after,
|
)
|
||||||
entry_address=entry_address,
|
src = os.path.join(self.env['baremetal_source_dir'], in_path)
|
||||||
bootloader_obj=bootloader_obj,
|
if self.need_rebuild([src, self.env['common_h']], main_obj):
|
||||||
common_objs=common_objs,
|
self.sh.run_cmd(
|
||||||
bootloader=False,
|
[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):
|
def get_build_dir(self):
|
||||||
return self.env['baremetal_build_dir']
|
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__':
|
if __name__ == '__main__':
|
||||||
Main().cli()
|
Main().cli()
|
||||||
|
|||||||
@@ -718,6 +718,7 @@ Valid emulators: {}
|
|||||||
env['baremetal_source_arch_subpath'] = join('arch', env['arch'])
|
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_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_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':
|
if env['emulator'] == 'gem5':
|
||||||
env['simulator_name'] = 'gem5'
|
env['simulator_name'] = 'gem5'
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user