REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
build_plugin_system.py
Go to the documentation of this file.
1# This PlatformIO Scons script is supposed to gather all information to make plugins
2# work (i.e. compile but also actually work) against the firmware built at the time
3# this Scons script you are currently viewing runs.
4#
5# That means, ideally this file does the following:
6# - Make sure an import library file (*.elf.a) is built from the static firmware
7# because it is needed to link the plugin against.
8# - "Freeze" the full build environment, ideally including all Cflags *and* all
9# relevant header files with their correct path structure. That is, this script
10# is supposed to prepare/set up the build system for plugins that can compile
11# and run against exactly this firmware version. Basically that is part of the
12# definition of a Compilation Database.
13#
14# Currently, the script only creates the *.elf.a file, nothing else.
15
16# this is a POST script, cf. https://docs.platformio.org/en/latest/scripting/launch_types.html#scripting-launch-types
17# cf. ~/.platformio/platforms/teensy/builder/main.py
18
19import os
20from os.path import join, basename
21from SCons.Script import AlwaysBuild
22
23Import("env")
24
25understand_the_build_system = False
26if understand_the_build_system:
27
28 fh = open(join("/tmp/", "dump.txt"), "w")
29 fh.write(env.Dump())
30 fh.close()
31
32 fh = open(join("/tmp/", "expanded.txt"), "w")
33 for k,v in env.Dictionary().items():
34 fh.write("%s = %s = %s\n" % (k, v, env.subst('$'+k)))
35 fh.close()
36
37me = "controller/build_plugin_system.py"
38
39
42
43target_elf = join("$BUILD_DIR", "${PROGNAME}.elf")
44target_elf_a = target_elf + ".a"
45base_target_elf_a = basename(env.subst(target_elf_a))
46if "BUILD_ELF_A" in os.environ:
47 # The following makes sure something like firmware.elf.a is produced,
48 # which is required to have something to link against, because the static compiled
49 # executable (firmware) contains no more symbol table.
50 #
51 # Note: This is GCC Syntax and won't work with clang. Typically this means
52 # that this isn't working on mac but linux only.
53 env.Append(
54 LINKFLAGS=[
55 "-Wl,--out-implib," + target_elf_a
56 ],
57 )
58 print(me, "will also build", base_target_elf_a)
59else:
60 print(me, "skips building", base_target_elf_a)
61
62
65
66# Note:
67# This step is actually *not* needed for the plugin system but instead for OTA updating.
68# It is, however, pretty easy to do this anytime after build time since it just requires
69# running an objcopy on the elf or (i)hex file.
70
71if 0:
72 # The following is a makefile'y way to execute something like
73 # objcopy --input-target=ihex --output-target=binary firmware.hex firmware.bin
74 # at the correct time.
75
76 # target_bin = env.ElfToBin(join("$BUILD_DIR", "${PROGNAME}"), target_elf) # doesn't work
77 # AlwaysBuild(target_bin) # doesn't work
78
79 Import("projenv") # defines projenv
80
81 global_env = DefaultEnvironment()
82
83 # well, this used to work as an extra_scripts = post:lib/loader/...py
84 # within the platformio.ini, but it doesn't from library.json.
85 global_env.AddPostAction(
86 "$BUILD_DIR/${PROGNAME}.elf",
87 global_env.VerboseAction(" ".join([
88 "$OBJCOPY", "-O", "binary", "-I", "ihex",
89 "$BUILD_DIR/${PROGNAME}.elf", "$BUILD_DIR/${PROGNAME}.bin"
90 ]), "Building $BUILD_DIR/${PROGNAME}.bin")
91 )
92
93 def post_program_action(source, target, env):
94 print("Program has been built, yeah")
95 program_path = target[0].get_abspath()
96 print("Program path ", target)
97
98 global_env.AddPostAction("$PROGPATH", post_program_action)
99
100 # arghl, I hate pio.
101
102 print(me, "will also TRY to build", "firmware.bin")
103
104
107
108if understand_the_build_system:
109 expand = lambda var: env.subst('$'+var)
110
111 # still missing: The crazy amount of include paths
112
113 fh = open(join("/tmp/", "Makefile.txt"), "w")
114 for var in "CCFLAGS CXXFLAGS CPPFLAGS _CPPDEFFLAGS _CPPINCFLAGS CC CXX OBJCOPY PROGPATH".split():
115 fh.write("%s = %s\n" % (var, expand(var)))
116
117 # path contains path to arm-none-eabi-gcc and friends
118 path = env['ENV']['PATH'] # same as os.environ['PATH']
119 fh.write("PATH = "+path)
120
121 fh.close()
post_program_action(source, target, env)
env expand
Job 3) Extract CFlags, etc for a custom Makefile.