Error: Unrecognized option when trying to debug

I have custom setup for setting building application project using ARMCC toolchain. Additionally, I provide compiler, linker, and assembler flags that differ from the default GCC ones. After some extensive troubleshooting, described here, I want to debug my application. However there is an “Fatal error: C3900U: Unrecognized option ‘-2’.” error comming up each time.

Now, the problem is that when I got these “Unrecognized option …” errors before, usually first character after hyphen is removed from this logged error. Even though, I was trying to locate any in-line option that has character “2” inside, but I find none. So, my guess would be this error is a consequence of some other issue, however I can’t determine it.

This is only in case of trying to debug application code. If I build, everything is okay. Where should I be looking at then? What different inline options are usually used, when trying to debug vs. when trying to build project code?

This is the whole script I use to re-direct toolchain paths and filter specific GCC non-compatible flags and add new ARMCC-specific flags.

# 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",
    "-Os",
    "nostdlib",
    "-l",
    "-mthumb",
    "-Wl,--gc-sections,--relax",
    "-specs=nano.specs",
    "-ggdb3",
    "-ggdb2",
    "-ggdb",
    "-D",
    "-2"
    # Add more flags to exclude as needed
]

# Modify flags globally (as if through "build_flags")
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"
        #"--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,
    )