Skip to content

Commit aec5d7a

Browse files
committed
build: Clean up updating build params by adding functions
1 parent 2e54011 commit aec5d7a

File tree

1 file changed

+55
-33
lines changed

1 file changed

+55
-33
lines changed

scripts/embed_env_vars.py

+55-33
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
from utils import pioenv, sysenv, dotenv
22

3-
Import('env') # type: ignore
4-
pio = pioenv.PioEnv(env) # type: ignore
5-
63
# This file is invoked by PlatformIO during build.
74
# See 'extra_scripts' in 'platformio.ini'.
85

6+
#######################################################
7+
# DETERMINING THE ENVIRONMENT #
8+
#######################################################
9+
10+
# Import the PlatformIO env and initialize our PioEnv wrapper.
11+
Import('env') # type: ignore
12+
pio = pioenv.PioEnv(env) # type: ignore
13+
914
# Get PIO variables.
10-
build_flags = pio.get_string_array('BUILD_FLAGS', [])
1115
project_dir = pio.get_string('PROJECT_DIR')
1216

1317
# By default, the build is run in DEVELOP mode.
@@ -25,52 +29,70 @@
2529
pio_build_type = 'release' if is_release_build else 'debug'
2630
dotenv_type = 'production' if is_release_build else 'development'
2731

32+
# Read the correct .env file.
2833
dot = dotenv.DotEnv(project_dir, dotenv_type)
2934

30-
# Defines that will be passed to the compiler.
31-
cpp_defines: dict[str, str | int | bool] = {}
35+
#######################################################
36+
# UPDATING BUILD PARAMETERS #
37+
#######################################################
3238

33-
# Gets the build flags from the PIO ini file, removes the -D prefix, splits on the first '=', and then converts to a dictionary.
34-
# (Intent: Remove everything from buildflags, then add everything as cppdefines, and readd the non-key-value build flags.)
35-
leftover_flags = []
36-
for flag in build_flags:
37-
if flag.startswith('-D'):
38-
flag = flag[2::]
39-
if '=' in flag:
40-
(k, v) = flag.split('=', 1)
4139

42-
cpp_defines[k] = v
40+
# Parse PIO build flags.
41+
# All CPP Defines, i.e. flags starting with "-D", are parsed for key/value and stored in a dictionary (first value).
42+
# All other build flags are returned in a list (second value)
43+
def parse_pio_build_flags(raw_flags: list[str]) -> tuple[dict[str, str | int | bool], list[str]]:
44+
flag_dict = {}
45+
leftover_flags = []
46+
for flag in raw_flags:
47+
if flag.startswith('-D'):
48+
flag = flag[2::]
49+
if '=' in flag:
50+
(k, v) = flag.split('=', 1)
51+
flag_dict[k] = v
52+
else:
53+
flag_dict[flag] = True
4354
else:
44-
cpp_defines[flag] = True
45-
else:
46-
leftover_flags.append(flag)
55+
leftover_flags.append(flag)
56+
return (flag_dict, [])
4757

48-
# Gets all the environment variables prefixed with 'OPENSHOCK_'.
58+
59+
# Serialize CPP Defines.
60+
# Strings are escaped to be a correct CPP macro.
61+
# Booleans are turned into integers, True => 1 and False => 0.
62+
# Integers are kept as-is.
63+
def serialize_cpp_defines(raw_defines: dict[str, str | int | bool]) -> dict[str, str | int]:
64+
result_defines = {}
65+
for k, v in raw_defines.items():
66+
try:
67+
v = int(v)
68+
except ValueError:
69+
pass
70+
if isinstance(v, str):
71+
result_defines[k] = env.StringifyMacro(v)
72+
else:
73+
result_defines[k] = v
74+
return result_defines
75+
76+
77+
# Fetch the current build flags and group them into (CPP Defines, Other Flags).
78+
raw_build_flags = pio.get_string_array('BUILD_FLAGS', [])
79+
(cpp_defines, remaining_build_flags) = parse_pio_build_flags(raw_build_flags)
80+
81+
# Gets all the environment variables prefixed with 'OPENSHOCK_' and add them as CPP Defines.
4982
openshock_vars = dot.get_all_prefixed('OPENSHOCK_')
5083
cpp_defines.update(openshock_vars)
5184

5285
# Gets the log level from environment variables.
86+
# TODO: Delete get_loglevel and use... something more generic.
5387
log_level_int = dot.get_loglevel('LOG_LEVEL')
5488
if log_level_int is None:
5589
raise ValueError('LOG_LEVEL must be set in environment variables.')
5690
cpp_defines['CORE_DEBUG_LEVEL'] = log_level_int
5791

58-
# Convert all the string values to macros.
59-
for k, v in cpp_defines.items():
60-
try:
61-
v = int(v)
62-
except ValueError:
63-
pass
64-
65-
if isinstance(v, str):
66-
cpp_defines[k] = env.StringifyMacro(v)
67-
else:
68-
cpp_defines[k] = v
69-
7092
print('Build type: ' + pio_build_type)
7193
print('Build defines: ' + str(cpp_defines))
7294

7395
# Set PIO variables.
7496
env['BUILD_TYPE'] = pio_build_type
75-
env['BUILD_FLAGS'] = leftover_flags
76-
env.Append(CPPDEFINES=[(k, v) for k, v in cpp_defines.items()])
97+
env['BUILD_FLAGS'] = remaining_build_flags
98+
env.Append(CPPDEFINES=list(cpp_defines.items()))

0 commit comments

Comments
 (0)