From ffe73df435efea8e6e78052a59361cba0e696189 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 7 Jan 2021 00:35:14 +0100 Subject: build: Reorganize build rules. --- .gitignore | 2 + build/env.py | 14 ++++ build/rules.py | 127 +++++++++++++++++++++++++++++++++ build/scons_tools/tool_firmware.py | 13 ++++ build/scons_tools/tool_selectmcu.py | 132 ++++++++++++++++++++++++++++++++++ build_rules | 137 ------------------------------------ 6 files changed, 288 insertions(+), 137 deletions(-) create mode 100644 build/env.py create mode 100644 build/rules.py create mode 100644 build/scons_tools/tool_firmware.py create mode 100644 build/scons_tools/tool_selectmcu.py delete mode 100644 build_rules diff --git a/.gitignore b/.gitignore index 499a618..f376939 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.o *.swp +*.pyc +__pycache__ \ No newline at end of file diff --git a/build/env.py b/build/env.py new file mode 100644 index 0000000..c43bb60 --- /dev/null +++ b/build/env.py @@ -0,0 +1,14 @@ +import os + +env = Environment( + ENV = os.environ, + + toolpath = ['scons_tools'], + tools = [ + 'default', + 'tool_selectmcu', + 'tool_firmware', + ], +) + +Return('env') diff --git a/build/rules.py b/build/rules.py new file mode 100644 index 0000000..80a4d10 --- /dev/null +++ b/build/rules.py @@ -0,0 +1,127 @@ +laks_dir = Dir('..') +ld_dir = Dir('../ld_scripts') +main_sconscript = File('../SConscript') + +def select_arm(env, family): + env.SetDefault( + TOOLCHAIN = 'arm-none-eabi-', + ) + + if family == 'cortex-m4f': + family = 'cortex-m4' + has_mcu = True + else: + has_mcu = False + + env.Replace( + CC = '${TOOLCHAIN}gcc', + CXX = '${TOOLCHAIN}g++', + AS = '${TOOLCHAIN}gcc', + LINK = '${TOOLCHAIN}gcc', + AR = '${TOOLCHAIN}ar', + RANLIB = '${TOOLCHAIN}ranlib', + + CCFLAGS = Split('-O2 -Wall -ggdb -mcpu=${CPU_FAMILY} -mthumb -ffunction-sections'), + CXXFLAGS = Split('-std=c++20 -fno-exceptions -fno-rtti -Wno-pmf-conversions'), + ASFLAGS = Split('-c -x assembler-with-cpp -mcpu=${CPU_FAMILY} -mthumb'), + LINKFLAGS = Split('-Wall -mcpu=${CPU_FAMILY} -mthumb -nostartfiles -Wl,-T${LINK_SCRIPT} -Wl,--gc-sections'), + + CPPPATH = [laks_dir], + LIBPATH = [ld_dir], + + LIB_SOURCES = [], + + CPU_FAMILY = family, + ) + + if has_mcu: + env.Append(CCFLAGS = Split('-mfloat-abi=hard -mfpu=fpv4-sp-d16')) + env.Append(LINKFLAGS = Split('-mfloat-abi=hard -mfpu=fpv4-sp-d16')) + env.Append(CPPDEFINES = ['HAS_FPU']) + +def select_stm32(env, variant): + family = variant[5:9] + pin_count = variant[9] + flash = variant[10] + + if family.startswith('f0'): + select_arm(env, 'cortex-m0') + env.Append(CPPDEFINES = ['STM32F0']) + + sram = family[2] + + env['LINK_SCRIPT'] = { + ('4', '6'): 'stm32_f04_6.ld', + ('5', '8'): 'stm32_f05_8.ld', + }[sram, flash] + + elif family == 'f103': + select_arm(env, 'cortex-m3') + env.Append(CPPDEFINES = ['STM32F1']) + + env['LINK_SCRIPT'] = { + '8': 'stm32_f1_8.ld', + 'b': 'stm32_f1_b.ld', + }[flash] + + elif family == 'f303': + select_arm(env, 'cortex-m4f') + env.Append(CPPDEFINES = ['STM32F3']) + + env['LINK_SCRIPT'] = { + 'b': 'stm32_f303_b.ld', + 'c': 'stm32_f303_c.ld', + }[flash] + + elif family == 'f373': + select_arm(env, 'cortex-m4f') + env.Append(CPPDEFINES = ['STM32F3']) + + env['LINK_SCRIPT'] = { + '8': 'stm32_f373_8.ld', + 'b': 'stm32_f373_b.ld', + 'c': 'stm32_f373_c.ld', + }[flash] + + elif family in ('f401'): + select_arm(env, 'cortex-m4f') + env.Append(CPPDEFINES = ['STM32F4']) + + env['LINK_SCRIPT'] = { + 'e': 'stm32_f401_e.ld', + }[flash] + + elif family in ('f405', 'f407'): + select_arm(env, 'cortex-m4f') + env.Append(CPPDEFINES = ['STM32F4']) + + env['LINK_SCRIPT'] = { + 'e': 'stm32_f4_e.ld', + 'g': 'stm32_f4_g.ld', + }[flash] + + elif family in ('l051', 'l052', 'l053'): + select_arm(env, 'cortex-m0plus') + env.Append(CPPDEFINES = ['STM32L0']) + + env['LINK_SCRIPT'] = { + '8': 'stm32_l0_8.ld', + }[flash] + + else: + print('Unknown stm32 family: %s' % family) + Exit(1) + +def SelectMCU(env, mcu, variant_dir = None): + mcu = mcu.lower() + + if mcu.startswith('stm32'): + select_stm32(env, mcu) + + else: + print('Unknown MCU: %s' % mcu) + Exit(1) + + SConscript(main_sconscript, variant_dir = variant_dir, exports = 'env') + +AddMethod(Environment, SelectMCU) diff --git a/build/scons_tools/tool_firmware.py b/build/scons_tools/tool_firmware.py new file mode 100644 index 0000000..b1ead1f --- /dev/null +++ b/build/scons_tools/tool_firmware.py @@ -0,0 +1,13 @@ +from SCons.Script import * + +def Firmware(env, target, sources, **kwargs): + objects = [env.Object(f) for f in Flatten([sources, env['LIB_SOURCES']])] + firmware = env.Program(target, objects, **kwargs) + #env.Depends(firmware, env['LINK_SCRIPT']) # TODO + return firmware + +def exists(): + return True + +def generate(env): + env.AddMethod(Firmware) \ No newline at end of file diff --git a/build/scons_tools/tool_selectmcu.py b/build/scons_tools/tool_selectmcu.py new file mode 100644 index 0000000..1e2b0ee --- /dev/null +++ b/build/scons_tools/tool_selectmcu.py @@ -0,0 +1,132 @@ +from SCons.Script import * + +laks_dir = Dir('..') +ld_dir = Dir('../ld_scripts') +main_sconscript = File('../SConscript') + +def select_arm(env, family): + env.SetDefault( + TOOLCHAIN = 'arm-none-eabi-', + ) + + if family == 'cortex-m4f': + family = 'cortex-m4' + has_mcu = True + else: + has_mcu = False + + env.Replace( + CC = '${TOOLCHAIN}gcc', + CXX = '${TOOLCHAIN}g++', + AS = '${TOOLCHAIN}gcc', + LINK = '${TOOLCHAIN}gcc', + AR = '${TOOLCHAIN}ar', + RANLIB = '${TOOLCHAIN}ranlib', + + CCFLAGS = Split('-O2 -Wall -ggdb -mcpu=${CPU_FAMILY} -mthumb -ffunction-sections'), + CXXFLAGS = Split('-std=c++20 -fno-exceptions -fno-rtti -Wno-pmf-conversions'), + ASFLAGS = Split('-c -x assembler-with-cpp -mcpu=${CPU_FAMILY} -mthumb'), + LINKFLAGS = Split('-Wall -mcpu=${CPU_FAMILY} -mthumb -nostartfiles -Wl,-T${LINK_SCRIPT} -Wl,--gc-sections'), + + CPPPATH = [laks_dir], + LIBPATH = [ld_dir], + + LIB_SOURCES = [], + + CPU_FAMILY = family, + ) + + if has_mcu: + env.Append(CCFLAGS = Split('-mfloat-abi=hard -mfpu=fpv4-sp-d16')) + env.Append(LINKFLAGS = Split('-mfloat-abi=hard -mfpu=fpv4-sp-d16')) + env.Append(CPPDEFINES = ['HAS_FPU']) + +def select_stm32(env, variant): + family = variant[5:9] + pin_count = variant[9] + flash = variant[10] + + if family.startswith('f0'): + select_arm(env, 'cortex-m0') + env.Append(CPPDEFINES = ['STM32F0']) + + sram = family[2] + + env['LINK_SCRIPT'] = { + ('4', '6'): 'stm32_f04_6.ld', + ('5', '8'): 'stm32_f05_8.ld', + }[sram, flash] + + elif family == 'f103': + select_arm(env, 'cortex-m3') + env.Append(CPPDEFINES = ['STM32F1']) + + env['LINK_SCRIPT'] = { + '8': 'stm32_f1_8.ld', + 'b': 'stm32_f1_b.ld', + }[flash] + + elif family == 'f303': + select_arm(env, 'cortex-m4f') + env.Append(CPPDEFINES = ['STM32F3']) + + env['LINK_SCRIPT'] = { + 'b': 'stm32_f303_b.ld', + 'c': 'stm32_f303_c.ld', + }[flash] + + elif family == 'f373': + select_arm(env, 'cortex-m4f') + env.Append(CPPDEFINES = ['STM32F3']) + + env['LINK_SCRIPT'] = { + '8': 'stm32_f373_8.ld', + 'b': 'stm32_f373_b.ld', + 'c': 'stm32_f373_c.ld', + }[flash] + + elif family in ('f401'): + select_arm(env, 'cortex-m4f') + env.Append(CPPDEFINES = ['STM32F4']) + + env['LINK_SCRIPT'] = { + 'e': 'stm32_f401_e.ld', + }[flash] + + elif family in ('f405', 'f407'): + select_arm(env, 'cortex-m4f') + env.Append(CPPDEFINES = ['STM32F4']) + + env['LINK_SCRIPT'] = { + 'e': 'stm32_f4_e.ld', + 'g': 'stm32_f4_g.ld', + }[flash] + + elif family in ('l051', 'l052', 'l053'): + select_arm(env, 'cortex-m0plus') + env.Append(CPPDEFINES = ['STM32L0']) + + env['LINK_SCRIPT'] = { + '8': 'stm32_l0_8.ld', + }[flash] + + else: + print('Unknown stm32 family: %s' % family) + Exit(1) + +def SelectMCU(env, mcu, variant_dir = None): + mcu = mcu.lower() + + if mcu.startswith('stm32'): + select_stm32(env, mcu) + + else: + print('Unknown MCU: %s' % mcu) + Exit(1) + + SConscript(main_sconscript, variant_dir = variant_dir, exports = 'env') +def exists(): + return True + +def generate(env): + env.AddMethod(SelectMCU) \ No newline at end of file diff --git a/build_rules b/build_rules deleted file mode 100644 index dfabcae..0000000 --- a/build_rules +++ /dev/null @@ -1,137 +0,0 @@ -laks_dir = Dir('.') -ld_dir = Dir('ld_scripts') -main_sconscript = File('SConscript') - -def select_arm(env, family): - env.SetDefault( - TOOLCHAIN = 'arm-none-eabi-', - ) - - if family == 'cortex-m4f': - family = 'cortex-m4' - has_mcu = True - else: - has_mcu = False - - env.Replace( - CC = '${TOOLCHAIN}gcc', - CXX = '${TOOLCHAIN}g++', - AS = '${TOOLCHAIN}gcc', - LINK = '${TOOLCHAIN}gcc', - AR = '${TOOLCHAIN}ar', - RANLIB = '${TOOLCHAIN}ranlib', - - CCFLAGS = Split('-O2 -Wall -ggdb -mcpu=${CPU_FAMILY} -mthumb -ffunction-sections'), - CXXFLAGS = Split('-std=gnu++11 -fno-exceptions -fno-rtti -Wno-pmf-conversions'), - ASFLAGS = Split('-c -x assembler-with-cpp -mcpu=${CPU_FAMILY} -mthumb'), - LINKFLAGS = Split('-Wall -mcpu=${CPU_FAMILY} -mthumb -nostartfiles -Wl,-T${LINK_SCRIPT} -Wl,--gc-sections'), - - CPPPATH = [laks_dir], - LIBPATH = [ld_dir], - - LIB_SOURCES = [], - - CPU_FAMILY = family, - ) - - if has_mcu: - env.Append(CCFLAGS = Split('-mfloat-abi=hard -mfpu=fpv4-sp-d16')) - env.Append(LINKFLAGS = Split('-mfloat-abi=hard -mfpu=fpv4-sp-d16')) - env.Append(CPPDEFINES = ['HAS_FPU']) - -def select_stm32(env, variant): - family = variant[5:9] - pin_count = variant[9] - flash = variant[10] - - if family.startswith('f0'): - select_arm(env, 'cortex-m0') - env.Append(CPPDEFINES = ['STM32F0']) - - sram = family[2] - - env['LINK_SCRIPT'] = { - ('4', '6'): 'stm32_f04_6.ld', - ('5', '8'): 'stm32_f05_8.ld', - }[sram, flash] - - elif family == 'f103': - select_arm(env, 'cortex-m3') - env.Append(CPPDEFINES = ['STM32F1']) - - env['LINK_SCRIPT'] = { - '8': 'stm32_f1_8.ld', - 'b': 'stm32_f1_b.ld', - }[flash] - - elif family == 'f303': - select_arm(env, 'cortex-m4f') - env.Append(CPPDEFINES = ['STM32F3']) - - env['LINK_SCRIPT'] = { - 'b': 'stm32_f303_b.ld', - 'c': 'stm32_f303_c.ld', - }[flash] - - elif family == 'f373': - select_arm(env, 'cortex-m4f') - env.Append(CPPDEFINES = ['STM32F3']) - - env['LINK_SCRIPT'] = { - '8': 'stm32_f373_8.ld', - 'b': 'stm32_f373_b.ld', - 'c': 'stm32_f373_c.ld', - }[flash] - - elif family in ('f401'): - select_arm(env, 'cortex-m4f') - env.Append(CPPDEFINES = ['STM32F4']) - - env['LINK_SCRIPT'] = { - 'e': 'stm32_f401_e.ld', - }[flash] - - elif family in ('f405', 'f407'): - select_arm(env, 'cortex-m4f') - env.Append(CPPDEFINES = ['STM32F4']) - - env['LINK_SCRIPT'] = { - 'e': 'stm32_f4_e.ld', - 'g': 'stm32_f4_g.ld', - }[flash] - - elif family in ('l051', 'l052', 'l053'): - select_arm(env, 'cortex-m0plus') - env.Append(CPPDEFINES = ['STM32L0']) - - env['LINK_SCRIPT'] = { - '8': 'stm32_l0_8.ld', - }[flash] - - else: - print('Unknown stm32 family: %s' % family) - Exit(1) - -def SelectMCU(env, mcu, variant_dir = None): - mcu = mcu.lower() - - if mcu.startswith('stm32'): - select_stm32(env, mcu) - - else: - print('Unknown MCU: %s' % mcu) - Exit(1) - - SConscript(main_sconscript, variant_dir = variant_dir, exports = 'env') - -AddMethod(Environment, SelectMCU) - -def Firmware(env, target, sources, **kwargs): - objects = [env.Object(f) for f in Flatten([sources, env['LIB_SOURCES']])] - firmware = env.Program(target, objects, **kwargs) - #env.Depends(firmware, env['LINK_SCRIPT']) # TODO - return firmware - -AddMethod(Environment, Firmware) - -# vim: syn=python -- cgit v1.2.3