|
| 1 | +import os |
| 2 | + |
| 3 | +# Import build configuration |
| 4 | +Import('build_config') |
| 5 | + |
| 6 | +# Get project path: this is the path of the SConscript file |
| 7 | +project_path = Dir('#').rel_path(Dir('.')) |
| 8 | + |
| 9 | +# >>> Project Configuration --------------------------------------------------- |
| 10 | + |
| 11 | +# Specify the build directory and its subfolders |
| 12 | +build_dir = os.path.join('build', build_config['name']) |
| 13 | +obj_subdir = 'obj' |
| 14 | + |
| 15 | +# Specify the target outputs |
| 16 | +target_name = 'stm32-bootloader' |
| 17 | +target_exe = os.path.join(build_dir, target_name) |
| 18 | +target_hex = os.path.join(build_dir, target_name) + '.hex' |
| 19 | +target_bin = os.path.join(build_dir, target_name) + '.bin' |
| 20 | + |
| 21 | +# Microcontroller flags |
| 22 | +mcu_flags = [ |
| 23 | + '-mcpu=cortex-m4', |
| 24 | + '-mthumb', |
| 25 | + '-mfpu=fpv4-sp-d16', |
| 26 | + '-mfloat-abi=hard', |
| 27 | +] |
| 28 | + |
| 29 | +# Common compiler flags |
| 30 | +common_flags = [ |
| 31 | + '-Wall', |
| 32 | + '-pedantic', |
| 33 | + '-fdata-sections', |
| 34 | + '-ffunction-sections', |
| 35 | +] |
| 36 | + |
| 37 | +# Assembler flags |
| 38 | +a_flags = [ |
| 39 | + '-c', |
| 40 | +] |
| 41 | + |
| 42 | +# C/C++ compiler flags |
| 43 | +cc_flags = [ |
| 44 | + '-MMD', |
| 45 | + '-MP', |
| 46 | +] |
| 47 | + |
| 48 | +# Linker flags |
| 49 | +link_flags = [ |
| 50 | + '-T{}'.format(os.path.join(project_path, 'stm32l496xx_flash.ld')), |
| 51 | + '-specs=nano.specs', |
| 52 | + '-specs=nosys.specs', |
| 53 | + '-lc', |
| 54 | + '-lm', |
| 55 | + '-lnosys', |
| 56 | + '-Wl,-Map={}.map,--cref'.format(os.path.join(project_path, target_exe)), |
| 57 | + '-Wl,--gc-sections', |
| 58 | +] |
| 59 | + |
| 60 | +# Defines |
| 61 | +defines = [ |
| 62 | + 'USE_HAL_DRIVER', |
| 63 | + 'STM32L496xx', |
| 64 | +] |
| 65 | + |
| 66 | +# Include locations |
| 67 | +includes = [ |
| 68 | + '../include', |
| 69 | + '#/lib/stm32-bootloader', |
| 70 | + '#/lib/fatfs', |
| 71 | + '#/drivers/CMSIS/Include', |
| 72 | + '#/drivers/CMSIS/Device/ST/STM32L4xx/Include', |
| 73 | + '#/drivers/STM32L4xx_HAL_Driver/Inc', |
| 74 | +] |
| 75 | + |
| 76 | +# Source files |
| 77 | +sources = Glob('#/lib/stm32-bootloader/*.c') |
| 78 | +sources += Glob('#/lib/fatfs/*.c') |
| 79 | +sources += Glob('#/drivers/STM32L4xx_HAL_Driver/Src/*.c') |
| 80 | +sources += Glob('#/lib/fatfs/option/unicode.c') |
| 81 | +sources += Glob('../source/*.c') |
| 82 | +sources += Glob('startup_stm32l496xx.s') |
| 83 | + |
| 84 | +# <<< End of configuration ---------------------------------------------------- |
| 85 | + |
| 86 | +# Create environment |
| 87 | +env = Environment( |
| 88 | + ENV={'PATH': os.environ['PATH']} |
| 89 | +) |
| 90 | + |
| 91 | +# Specify the cross compiler toolchain |
| 92 | +cross_compiler = 'arm-none-eabi-' |
| 93 | +for (tool, name) in [ |
| 94 | + ('AS', 'gcc -x assembler-with-cpp'), |
| 95 | + ('CC', 'gcc'), |
| 96 | + ('CXX', 'g++'), |
| 97 | + ('LINK', 'g++'), |
| 98 | + ('OBJCOPY', 'objcopy'), |
| 99 | + ('SIZE', 'size'), |
| 100 | +]: |
| 101 | + env[tool] = cross_compiler + name |
| 102 | + |
| 103 | +# Overwrite the default assembler command variables |
| 104 | +env['ASCOMSTR'] = 'Assembling static object $TARGET' |
| 105 | + |
| 106 | +# Overwrite the default compiler command variables |
| 107 | +env['CCCOMSTR'] = 'Compiling static object $TARGET' |
| 108 | + |
| 109 | +# Overwrite the default linker command variables |
| 110 | +env['LINKCOM'] = '$LINK $LINKFLAGS -o $TARGET $SOURCES' |
| 111 | +env['LINKCOMSTR'] = 'Linking $TARGET' |
| 112 | + |
| 113 | +# Overwrite the default objcopy command string |
| 114 | +env['OBJCOPYCOMSTR'] = 'Generating $TARGET' |
| 115 | + |
| 116 | +# Overwrite the default size command string |
| 117 | +env['SIZECOMSTR'] = 'Generating size information' |
| 118 | + |
| 119 | +# Clear all COMSTR variables if verbose mode is requested |
| 120 | +if GetOption('verbose'): |
| 121 | + for key, value in env.items(): |
| 122 | + if key.endswith('COMSTR'): |
| 123 | + env[key] = '' |
| 124 | + |
| 125 | +# Set the executable file suffix |
| 126 | +env['PROGSUFFIX'] = ".elf" |
| 127 | + |
| 128 | +# Set the object file suffix |
| 129 | +env['OBJSUFFIX'] = ".o" |
| 130 | + |
| 131 | +# Set the assembler flags |
| 132 | +env['ASFLAGS'] = a_flags + mcu_flags + common_flags + build_config['optimization_flags'] |
| 133 | + |
| 134 | +# Set the C/C++ compiler flags |
| 135 | +env['CCFLAGS'] = cc_flags + mcu_flags + common_flags + build_config['optimization_flags'] |
| 136 | + |
| 137 | +# Set the linker flags |
| 138 | +env['LINKFLAGS'] = mcu_flags + link_flags |
| 139 | + |
| 140 | +# Set the defines |
| 141 | +env['CPPDEFINES'] = defines |
| 142 | + |
| 143 | +# Set the include locations |
| 144 | +env['CPPPATH'] = includes |
| 145 | + |
| 146 | +# Build object files |
| 147 | +obj_list = [] |
| 148 | +for src in sources: |
| 149 | + filename = (os.path.splitext(os.path.basename(str(src)))[0]) |
| 150 | + obj = os.path.join(build_dir, obj_subdir, (filename + '.o')) |
| 151 | + extra_flags = [ |
| 152 | + '-Wa,-a,-ad,-alms={}.lst'.format( |
| 153 | + os.path.join(project_path, build_dir, obj_subdir, filename)), |
| 154 | + ] |
| 155 | + env.Object(target=obj, source=src, CCFLAGS=(env['CCFLAGS'] + extra_flags)) |
| 156 | + obj_list.append(obj) |
| 157 | + |
| 158 | +# Build executable |
| 159 | +program = env.Program(target_exe, obj_list) |
| 160 | + |
| 161 | +# Create hex output |
| 162 | +hexfile = env.Command(target_hex, program, Action( |
| 163 | + '$OBJCOPY -O ihex $SOURCE $TARGET', env['OBJCOPYCOMSTR'])) |
| 164 | + |
| 165 | +# Create binary output |
| 166 | +binary = env.Command(target_bin, program, Action( |
| 167 | + '$OBJCOPY -O binary -S $SOURCE $TARGET', env['OBJCOPYCOMSTR'])) |
| 168 | + |
| 169 | +# Generate size information |
| 170 | +size = env.Command(None, program, Action('$SIZE $SOURCE', env['SIZECOMSTR'])) |
| 171 | + |
| 172 | +# Define build targets |
| 173 | +build_targets = [program, hexfile, binary, size] |
| 174 | + |
| 175 | +# Specify additional files and directories that should be removed during clean |
| 176 | +env.Clean(build_targets, 'build') |
| 177 | + |
| 178 | +# Define default target |
| 179 | +env.Default(build_targets) |
| 180 | + |
| 181 | +# Define target aliases |
| 182 | +env.Alias('build', build_targets) |
0 commit comments