AddBuildMiddleware - missing include flags (-I)

Default build flags in my platformio.ini are -O3. I.e. build_flags = -O3. I would like some source files to be compiled with -Os, so I followed the instructions in https://community.platformio.org/t/granular-optimisation-settings/20267.

However, during the AddBuildMiddleware callback the CCFLAGS option is missing the -I flags for any referenced libraries. Which produces a fatal error: EEPROM.h: No such file or directory compilation error.

platformio.ini:
[env:megaatmega2560]
platform=atmelavr
board=megaatmega2560
framework=arduino
build_unflags = -Os
build_flags = -O3 -ffast-math -funroll-loops -Wall -Wextra -std=c99
lib_deps = EEPROM, Time
test_build_project_src = true
debug_tool = simavr
optimize_for_size = storage.cpp, page_crc.cpp
extra_scripts = pre:build\compiler_flag_override.py

compiler_flag_override.py:

Import("env")

size_optimized_files = [f.strip() for f in env.GetProjectOption("optimize_for_size").split(',')]

def optimize_for_size(node):
    if node.name in size_optimized_files:
        print(env['CCFLAGS'])
        return env.Object(node,
            CCFLAGS=[opt for opt in env['CCFLAGS'] if not opt.startswith("-O")] + ["-Os"])
    else:
        return node

env.AddBuildMiddleware(optimize_for_size)

Example CCFLAGS from print(env['CCFLAGS']) in the script:
-O3 -ffast-math -funroll-loops -Wall -Wextra -Wall -ffunction-sections -fdata-sections -flto -mmcu=$BOARD_MCU

Missing flags:
-I.platformio\packages\framework-arduino-avr\libraries\EEPROM\src