Include search path order

How can I make sure that a specific “OVER_RIDE_DIR” will be searched first for include files?

adding it in platformio.ini just adds it some random position in the search list

I tried platfomio.ini with:

build_flags = -IOVER_RIDE_DIR

which will result in

avr-g++ -o .pio/build/tronxy_x1/src/src/HAL/AVR/Servo.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -fmax-errors=5 -g -fmerge-all-constants -Os -Wall -ffunction-sections -fdata-sections -flto -mmcu=atmega1284p -DPLATFORMIO=40301 -DARDUINO_AVR_SANGUINO -D__MARLIN_FIRMWARE__ -DLCD_I2C "-DCUSTOM_MACHINE_NAME=“Tronxy X1 24Apr2020"” -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/net/y/y/3D/Marlin-2.0.x-git/Marlin -I/t470/peter/.platformio/packages/framework-arduino-avr/libraries/SPI/src -I.pio/libdeps/tronxy_x1/LiquidCrystal_ID136 -I/t470/peter/.platformio/packages/framework-arduino-avr/libraries/Wire/src -I/t470/peter/.platformio/platforms/atmelavr/builder -IOVER_RIDE_DIR -I/t470/peter/.platformio/packages/framework-arduino-avr/cores/arduino -I/t470/peter/.platformio/packages/framework-arduino-avr/variants/sanguino /net/y/y/3D/Marlin-2.0.x-git/Marlin/src/HAL/AVR/Servo.cpp

Then I tried to make a small script “modify_cpppath.py” with these two lines:

import(“env”)
env.Append(CPPPATH=[“OVER_RIDE_DIR”])

and in platformio.ini I have
extra_scripts = pre:modify_cpppath.py

which results in

avr-g++ -o .pio/build/tronxy_x1/src/src/HAL/AVR/Servo.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -fmax-errors=5 -g -fmerge-all-constants -Os -Wall -ffunction-sections -fdata-sections -flto -mmcu=atmega1284p -DPLATFORMIO=40301 -DARDUINO_AVR_SANGUINO -D__MARLIN_FIRMWARE__ -DLCD_I2C "-DCUSTOM_MACHINE_NAME=“Tronxy X1 24Apr2020"” -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -I/net/y/y/3D/Marlin-2.0.x-git/Marlin -I/t470/peter/.platformio/packages/framework-arduino-avr/libraries/SPI/src -I.pio/libdeps/tronxy_x1/LiquidCrystal_ID136 -I/t470/peter/.platformio/packages/framework-arduino-avr/libraries/Wire/src -I/t470/peter/.platformio/platforms/atmelavr/builder/OVER_RIDE_DIR -I/t470/peter/.platformio/packages/framework-arduino-avr/cores/arduino -I/t470/peter/.platformio/packages/framework-arduino-avr/variants/sanguino /net/y/y/3D/Marlin-2.0.x-git/Marlin/src/HAL/AVR/Servo.cpp

It is still not first and now it it expects it to be somewhere under ~/.platformio

Please lead me in the right direction to find a solution for this.

Why would that be important? If the code is accessing your header file it doesn’t matter if the -I flag to the include directory is the last or the first.

Hmm answering a HOW question with a WHY question !

The search order do matter if there is include files with the same name !

Example 1:
I would like to use the same source-code-tree to compile Marlin for all my 3D printers, the only thing that differ between them is the two headerfiles Configuration.h and Configuration_adv.h, and their env section in platformio.ini so it would be very convenient to have specific dir per 3D printer with these two files,

So after introducing a fix/feature, “pio run” can compile a new firmware for all my 3D printers

Example 2
SoftI2CMaster is an i2c implementation on any iopins, out of the box the header-file does not allow this on cpu that do have dedicaded i2c-hw.
So if you wanted to do i2c on f.ex. Port D bit 6-7 on an atmega168 you need to patch the header file.

Both of the above would be trivial to implement i Makefile based system.

I had hoped that something similar would be possible with platformio,

Yes, since there’s always a possibility of it being an ‘XY Problem’.

Can’t you remove the Configuration*.h files from the main folder so that by default they are not found, and then introduce -I switches to your specific configuration directory in the specific environments?

You can also try to readout env['CPPPATH'], reorder the returned array and use env.Replace(CPPPATH=new_arr), see docs.

I must admit that this is my first encounter with PlatformIO, so I had no idea what is possible out of the box, so I just thought I would ask here before having to look through the doc/code. So to answer my own question:

  • No I do not think there is way to manipulate the include-search-path-order by setting a parameter in the platformio.ini file.

  • But you can change the behavior by writing a small python script to do it.

So the fix to my problem was to write a script:

import os
Import("env")
if os.path.isdir(env['PIOENV']):
    incdir = env['PIOENV']
else:
    incdir = "."
env['_CPPINCFLAGS'] = "-I%s $( ${_concat(INCPREFIX, CPPPATH, INCSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)" % (incdir)

in platformio.ini we will specify that the script should be run before compiling

[env]
extra_scripts = pre:prepend_cpppath.py

If you should be interested in further details I have shared my notes on my blog:

http://peter.lorenzen.us/3d-printer/platformio-and-marlin-and-how-to-have-separate-config-files-for-different-3dprinter-while-sharing-the-same-code