|
| 1 | +# Copyright 2020 SiWi Embedded Solutions Pvt. Ltd. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +# |
| 5 | + |
| 6 | +import sys |
| 7 | +from platform import system |
| 8 | +from os import makedirs |
| 9 | +from os.path import basename, isdir, join |
| 10 | + |
| 11 | +from SCons.Script import (ARGUMENTS, COMMAND_LINE_TARGETS, AlwaysBuild, |
| 12 | + Builder, Default, DefaultEnvironment) |
| 13 | + |
| 14 | +env = DefaultEnvironment() |
| 15 | +platform = env.PioPlatform() |
| 16 | +board = env.BoardConfig() |
| 17 | + |
| 18 | +env.Replace( |
| 19 | + AR="arm-none-eabi-ar", |
| 20 | + AS="arm-none-eabi-as", |
| 21 | + CC="arm-none-eabi-gcc", |
| 22 | + CXX="arm-none-eabi-g++", |
| 23 | + GDB="arm-none-eabi-gdb", |
| 24 | + OBJCOPY="arm-none-eabi-objcopy", |
| 25 | + RANLIB="arm-none-eabi-ranlib", |
| 26 | + SIZETOOL="arm-none-eabi-size", |
| 27 | + |
| 28 | + ARFLAGS=["rc"], |
| 29 | + |
| 30 | + SIZEPROGREGEXP=r"^(?:\.text|\.data|\.rodata|\.text.align|\.ARM.exidx|\.ARM.extab|\.ll|\.initdata)\s+(\d+).*", |
| 31 | + SIZEDATAREGEXP=r"^(?:\.data|\.bss|\.noinit)\s+(\d+).*", |
| 32 | + SIZECHECKCMD="$SIZETOOL -A -d $SOURCES", |
| 33 | + SIZEPRINTCMD='$SIZETOOL -B -d $SOURCES', |
| 34 | + |
| 35 | + PROGSUFFIX=".elf", |
| 36 | + SIWIFLASHER=join(platform.get_package_dir( |
| 37 | + "tool-siwiflasher") or "", "siwiflasher"), |
| 38 | + REFLASH_FLAGS=[ |
| 39 | + "-r" |
| 40 | + ], |
| 41 | + REFLASH_CMD='"$SIWIFLASHER" $REFLASH_FLAGS' |
| 42 | +) |
| 43 | + |
| 44 | +# Allow user to override via pre:script |
| 45 | +if env.get("PROGNAME", "program") == "program": |
| 46 | + env.Replace(PROGNAME="firmware") |
| 47 | + |
| 48 | +# |
| 49 | +# Target: Build executable and linkable firmware |
| 50 | +# |
| 51 | + |
| 52 | +target_elf = None |
| 53 | +if "nobuild" in COMMAND_LINE_TARGETS: |
| 54 | + target_elf = join("$BUILD_DIR", "${PROGNAME}.elf") |
| 55 | + target_firm = join("$BUILD_DIR", "${PROGNAME}.bin") |
| 56 | + target_firm_fota = join("$BUILD_DIR", "fota_${PROGNAME}.bin") |
| 57 | +else: |
| 58 | + target_elf = env.BuildProgram() |
| 59 | + target_firm = env.ElfToBin(join("$BUILD_DIR", "${PROGNAME}"), target_elf) |
| 60 | + target_firm_fota = env.BinToFOTA( |
| 61 | + join("$BUILD_DIR", "fota_${PROGNAME}"), target_firm) |
| 62 | + |
| 63 | +AlwaysBuild(env.Alias("nobuild", target_firm)) |
| 64 | +target_buildprog = env.Alias("buildprog", target_firm, target_firm) |
| 65 | + |
| 66 | +# |
| 67 | +# Target: Build FOTA Binary |
| 68 | +# |
| 69 | + |
| 70 | +target_buildota = env.Alias("buildfota", target_firm_fota, target_firm_fota) |
| 71 | +AlwaysBuild(target_buildota) |
| 72 | + |
| 73 | +# |
| 74 | +# Target: Print binary size |
| 75 | +# |
| 76 | + |
| 77 | +target_size = env.Alias("size", target_elf, env.VerboseAction( |
| 78 | + "$SIZEPRINTCMD", "Calculating size $SOURCE")) |
| 79 | +AlwaysBuild(target_size) |
| 80 | + |
| 81 | +# |
| 82 | +# Target: Reflash core |
| 83 | +# |
| 84 | +AlwaysBuild( |
| 85 | + env.Alias("reflash", None, [ |
| 86 | + env.VerboseAction("$REFLASH_CMD", "Reflashing core...") |
| 87 | + ])) |
| 88 | + |
| 89 | +# |
| 90 | +# Target: Upload by default .bin file |
| 91 | +# |
| 92 | +env.Replace( |
| 93 | + UPLOADER="$SIWIFLASHER", |
| 94 | + UPLOADERFLAGS=[ |
| 95 | + "-b", "$UPLOAD_SPEED", |
| 96 | + "-p", '"$UPLOAD_PORT"', |
| 97 | + ], |
| 98 | + UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCE' |
| 99 | +) |
| 100 | +upload_source = target_firm |
| 101 | +upload_actions = [env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")] |
| 102 | + |
| 103 | +if env.subst("$UPLOAD_PORT") == "": |
| 104 | + env.Append( |
| 105 | + UPLOADERFLAGS=[ |
| 106 | + "-u" |
| 107 | + ], |
| 108 | + REFLASH_FLAGS=[ |
| 109 | + "-u" |
| 110 | + ] |
| 111 | + ) |
| 112 | + |
| 113 | +AlwaysBuild(env.Alias("upload", upload_source, upload_actions)) |
| 114 | + |
| 115 | +# |
| 116 | +# Setup default targets |
| 117 | +# |
| 118 | + |
| 119 | +Default([target_buildprog, target_buildota, target_size]) |
0 commit comments