I’m manually setting build flags for compiler, linker, and assembler since I’m using a custom toolchain. Default GCC options are removed and custom ones added.
# Run "pio run -t envdump" to display whoe "env" object with all its contents
# to verify whether data written here is also written in there
import os
from SCons.Script import Import
Import("env")
Import("projenv")
platform = env.PioPlatform()
# Filter out excluded build options from flag variable
def FilterFlags(flagVar, filteredOptions):
filteredFlags = [flag for flag in flagVar if flag not in filteredOptions]
return filteredFlags
# Define list of flag variables for option exclude
filteredBuildFlags = ["CPPFLAGS", "CFLAGS", "CCFLAGS", "CXXFLAGS", "ASFLAGS", "LINKFLAGS", "_LIBFLAGS"]
# Define default flags to exclude from all flag variables
filteredOptions = [
"-Wall",
"-ffunction-sections",
"-fdata-sections",
"-mcpu=cortex-m3",
"nostdlib",
"-l",
"-mthumb",
"-Wl,--gc-sections,--relax",
"-specs=nano.specs",
"-ggdb3",
"-ggdb2",
"-ggdb",
"-g2",
"-O1",
"-O2",
"-O3",
"-Os",
"-Og"
# Add more flags to exclude as needed
]
# Modify flags globally (as if through "build_flags")
# Whole list [env, projenv, DefaultEnvironment()]
for e in [env, projenv, DefaultEnvironment()]:
# ARMCC toolchain executables
e.Replace(
AR = "armar",
AS = "armasm",
CC = "armcc",
CXX = "armcc",
LD = "armlink",
LINK= "armlink",
GDB = "arm-none-eabi-gdb", # to retain compatibility
OBJCOPY = "fromelf --bin --output $TARGET $SOURCE && echo",
# For displaying ROM/RAM usage
SIZEPROGREGEXP=r"^(?:ER_IROM1)\s+(\d+).*",
SIZEDATAREGEXP=r"^(?:RW_IRAM1)\s+(\d+).*",
)
# Preprocessor flags
e.Replace(CPPFLAGS = [])
e.Append(CPPFLAGS = [
"-DUSE_HAL_DRIVER",
"-DSTM32F103xE",
"-DUSER_VECT_TAB_ADDRESS",
"-DHAL_WWDG_MODULE_ENABLED",
"-DHAL_IWDG_MODULE_ENABLED"
# Add more flags to exclude as needed
])
# Compiler flags
#e.Replace(CCFLAGS = [])
e.Append(CCFLAGS = [
"--c99",
"-c",
"--cpu=Cortex-M3",
"-g",
"-O0",
"--thumb"
# Add more flags to exclude as needed
])
# Assembler flags
#e.Replace(ASFLAGS = [])
e.Append(ASFLAGS = [
"--cpu=Cortex-M3",
"-g",
"--apcs=interwork"
# Add more flags to exclude as needed
])
# Linker flags
#e.Replace(LINKFLAGS = [])
e.Append(LINKFLAGS = [
"--cpu=Cortex-M3",
#"--scatter=PCV2-SES.sct"
#"--list=$BUILD_DIR/$PROGNAME.map" # causes errors for some reason
# Add more flags to exclude as needed
])
# Replace "-T" with "--scatter" for linker (scatter) scirpt
old_flags = e["LINKFLAGS"].copy()
try:
i = old_flags.index("-T")
old_flags[i] = "--scatter"
e.Replace(LINKFLAGS=old_flags)
except:
pass
# Remove "-Wl,..." which are GCC specific
e.Replace(_LIBFLAGS = [])
# Prevent searching for default linker file
e.Replace(LIBPATH = [])
# Filter out excluded flags from all flag variables
for flag in filteredBuildFlags:
existingFlags = e.get(flag, [])
filteredFlags = FilterFlags(existingFlags, filteredOptions)
e.Replace(**{flag: filteredFlags})
# Add new toolchain to path
pkg = platform.get_package("toolchain-armcc")
e.PrependENVPath(
"PATH",
os.path.join(pkg.path, "bin")
if os.path.isdir(os.path.join(pkg.path, "bin"))
else pkg.path,
)
# Remove duplicates, because modifying env, envproj, and DefaultEnvironment() objects
# adds redundant (repeated) flags into build options
e['CPPFLAGS'] = list(set(e['CPPFLAGS']))
e['CFLAGS'] = list(set(e['CFLAGS']))
e['CCFLAGS'] = list(set(e['CCFLAGS']))
e['CXXFLAGS'] = list(set(e['CXXFLAGS']))
e['ASFLAGS'] = list(set(e['ASFLAGS']))
Even though I’m modifying build flags globally it seems the -Og
flag is still present somewhere in the system. When I run application, it runs without issues. When I run application with debugger, application doesn’t run because some parts of code fail due to optimizations enabled. What I see under “Variables” window is the following:
var_name:<optimized out>
This should be the consequence of the -Og
optimization. But as you can see from script, where build flags are set, -Og
flag is filtered out (and other optimization levels) and -O0
in added instead.
It seems the build differs when I try and debug application. To check the modified build flags, I’m using the following command:
pio run --target envdump
And after inspecting all flags, there is only -O0
option and no sign of -Og
option. Therefore, what I’m trying to find out is the source of -Og
option and how to access it and, of course, remove it. Any ideas?