Integrating CubeMX USB (USB_DEVICE, Middleware) into PlatformIO project

I created a project using CubeMX with the Makefile option, and I can successfully build and upload the code with the following platformio.ini file. Up to this point everything works fine:


[platformio]
src_dir = Core/Src  ; path for main.h

[env:genericSTM32F103C8]
platform = ststm32
board = genericSTM32F103C8
framework = stm32cube

; Programmer
upload_protocol = stlink
debug_tool = stlink

build_flags =
  -I Core/Inc
  -I Drivers/STM32F1xx_HAL_Driver/Inc
  -I Drivers/STM32F1xx_HAL_Driver/Inc/Legacy
  -I Drivers/CMSIS/Device/ST/STM32F1xx/Include
  -I Drivers/CMSIS/Include
  -I USB_DEVICE/App
  -I USB_DEVICE/Target
  -I Middlewares/ST/STM32_USB_Device_Library/Core/Inc
  -D USE_HAL_DRIVER
  -D HSE_VALUE=8000000U

However, when I enable USB in CubeMX, it generates the USB_DEVICE folder at the project root instead of inside Core/Src.
Similarly, the Drivers and Middlewares folders are also placed at the project root.

During the build process I could not manage to properly include these files. I tried several different approaches but none of them worked.

Is there a current recommended way / workflow to integrate CubeMX-generated code (with USB enabled, Drivers, and Middlewares at root level) into PlatformIO?

“It looks like it worked this way.”

[platformio]
src_dir = .

[env:genericSTM32F103C8]
platform = ststm32
board = genericSTM32F103C8
framework = stm32cube
upload_protocol = stlink
debug_tool = stlink

build_flags =
  -I Core/Inc
  -I Drivers/CMSIS/Device/ST/STM32F1xx/Include
  -I Drivers/CMSIS/Include
  -I USB_DEVICE/App
  -I USB_DEVICE/Target
  -I Middlewares/ST/STM32_USB_Device_Library/Core/Inc
  -I Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc
  -D USE_HAL_DRIVER
  -D HSE_VALUE=8000000U

build_src_filter =
  +<Core/Src/*.c>
  -<Core/Src/system_stm32f1xx.c>               
  +<USB_DEVICE/App/*.c>
  +<USB_DEVICE/Target/*.c>
  +<Middlewares/ST/STM32_USB_Device_Library/Core/Src/*.c>
  +<Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/*.c>
  -<Core/Startup/*>