Global predefine (build.source.path) returns strange characters. Full pathname desired- ESP8266 Arduino IDE 1.8.13

Using IDE 1.8.13 on W10
Target WEMOS D1 R2 MINI

For me having the final application display the full path has proven to be very handy when I revisit an old project or during development as I bounce between about 6 un-connected shared computers some of which do not have internet access.

As previously discussed __FILE__ no longer returns the full path so a work around was provided by creating platform.txt file in the hardware directory of the Arduino IDE with the following contents

compiler.cpp.extra_flags=-D__PATH__="{build.source.path}"

however it returns escape characters and characters are missing. Example for this path

C:\data\arduino-1.8.13\portable\sketchbook\ESP8266_FX_951\ESP8266_OLED_MQTT_HX711_v106

I get

C:dataarduino-1.8.13portablesketchbookeSP8266_FX_951eSP8266_OLED_MQTT_HX711_v106

Note: Update, this is hard to describe - after re-reading this post I noticed that the text I pasted in above has been converted and does not display what I see. The square boxed question marks have been replaced with the first letter of the directory but lower case version. Here is a screen snip

image

The first character of the directory name is often missing with a \BELL (code 7) and \ESCAPE (code 27) chars where the '' backslash should be.

So I created a code snippet to examine the actual character codes returned as below. The code snippet is also below. I replace any none alpha numeric chars with'@@' to make sure they were not backspacing or deleting chars in the terminal session

Anyone have any suggestions as to what I can change or where I am going wrong.

 Symbol  Code
--->C<--->67<---
--->:<--->58<---
--->d<--->100<---
--->a<--->97<---
--->t<--->116<---
--->a<--->97<---
--->@@<--->7<---
--->r<--->114<---
--->d<--->100<---
--->u<--->117<---
--->i<--->105<---
--->n<--->110<---
--->o<--->111<---
--->@@<--->45<---
--->1<--->49<---
--->@@<--->46<---
--->8<--->56<---
--->@@<--->46<---
--->1<--->49<---
--->3<--->51<---
--->p<--->112<---
--->o<--->111<---
--->r<--->114<---
--->t<--->116<---
--->a<--->97<---
--->b<--->98<---
--->l<--->108<---
--->e<--->101<---
--->s<--->115<---
--->k<--->107<---
--->e<--->101<---
--->t<--->116<---
--->c<--->99<---
--->h<--->104<---
--->b<--->98<---
--->o<--->111<---
--->o<--->111<---
--->k<--->107<---
--->@@<--->27<---
--->S<--->83<---
--->P<--->80<---
--->8<--->56<---
--->2<--->50<---
--->6<--->54<---
--->6<--->54<---
--->_<--->95<---
--->F<--->70<---
--->X<--->88<---
--->_<--->95<---
--->9<--->57<---
--->5<--->53<---
--->1<--->49<---
--->@@<--->27<---
--->S<--->83<---
--->P<--->80<---
--->8<--->56<---
--->2<--->50<---
--->6<--->54<---
--->6<--->54<---
--->_<--->95<---
--->O<--->79<---
--->L<--->76<---
--->E<--->69<---
--->D<--->68<---
--->_<--->95<---
--->M<--->77<---
--->Q<--->81<---
--->T<--->84<---
--->T<--->84<---
--->_<--->95<---
--->H<--->72<---
--->X<--->88<---
--->7<--->55<---
--->1<--->49<---
--->1<--->49<---
--->_<--->95<---
--->v<--->118<---
--->1<--->49<---
--->0<--->48<---
--->6<--->54<---

//declared earlier is this 

const char __SKETCH__[] = __PATH__;

//followed by this 
  for (unsigned i = 0; i < strlen(__SKETCH__); ++i)
  {
    Serial.print("--->");
    if (__SKETCH__[i] < 48 || __SKETCH__[i] > 122  )
    {
      Serial.print("@@");
    }
    else
    {
      Serial.print(__SKETCH__[i]);
    }
    Serial.print("<--->");
    Serial.print((int)__SKETCH__[i]);
    Serial.println("<---");
  }

I think the issue will be more clear if you turn on verbose compilation output in the IDE:

  1. Select File > Preferences from the Arduino IDE menus.
  2. Uncheck the checkbox next to Show verbose output during: šŸ—¹ compilation
  3. Check the checkbox next to Show verbose output during: ☐ upload.
  4. Click the OK button.

After compiling your sketch, check the contents of the black output panel at the bottom of the Arduino IDE. There you will find the compilation commands that used the -D__PATH__ flag and see how the {build.source.path} property expansion was handled by the Arduino build system.

It will look something like this:

Using board 'mega' from platform in folder: C:\Users\asdf\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.4
Using core 'arduino' from platform in folder: C:\Users\asdf\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.4
Detecting libraries used...
"C:\\Users\\asdf\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-D__PATH__=\"C:\\Users\\asdf\\Documents\\Arduino\\Foo\"" "-IC:\\Users\\asdf\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.4\\cores\\arduino" "-IC:\\Users\\asdf\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.4\\variants\\mega" "C:\\Users\\asdf\\AppData\\Local\\Temp\\arduino-sketch-7A7F91B418D6E7BDFC42D5B629459A04\\sketch\\Foo.ino.cpp" -o nul
Generating function prototypes...

[...]

,,,and on and on. If you are having trouble seeing the flag in the wall of text, you can paste it into a text editor and do a search for it there.

In the example output above, you can see the flag expanded to this;

"-D__PATH__=\"C:\\Users\\asdf\\Documents\\Arduino\\Foo\""

So the build system does some amount of automagical quoting and escaping. This includes replacing the \ Windows path separator characters with \\. That is needed because this character Microsoft decided to use as a path separator also has a very different use as an escape character (e.g., \a is the escape code for bell). So if we really want a string with the character \ followed by the character a, it is necessary to escape the \: "\\a".

That automagic escaping got the annoying Windows path separator \ characters through the compilation commands, but we lose a level of escaping during that.

So in the end what you result with is the equivalent of this C++ code:

#define __PATH__ "C:\Users\asdf\Documents\Arduino\Foo"

So what would really be needed is this flag:

"-D__PATH__=\"C:\\\\Users\\\\asdf\\\\Documents\\\\Arduino\\\\Foo\""

So that the macro would be defined like this:

#define __PATH__ "C:\\Users\\asdf\\Documents\\Arduino\\Foo"

and then this sketch code:

Serial.print(__PATH__);

Would print this:

C:\Users\asdf\Documents\Arduino\Foo

Sadly, I don't know any way to force the property expansion system to do such a thing.

When I have been stuck on similar things in the past I ended up resorting to the use of a wrapper script bundled with the boards platform. So the compilation recipes (e.g., recipe.cpp.o.pattern) pass everything to the script, then the script does whatever additional work is needed, and then finally runs the actual command. This allows you to use the full powers of the scripting language.

In my case, I wanted to use the native script language to avoid a dependency on something like Python being installed on the user's system, and so I ended up having to write a shell script for Linux and macOS and an equivalent batch file for Windows. The Arduino platform system allows you to define platform-specific properties, so this is possible:
https://arduino.github.io/arduino-cli/latest/platform-specification/#automatic-property-override-for-specific-os

Some preprocessor trickery, not perfect, but better than the current situation.
The drawback is that the resulting string has quotes around the path.

#define STR(x) #x
#define STR2(x) STR(x)

#define __PATH__ "C:\some\path\to\a\file.ino"

__PATH__
STR2(__PATH__)
"C:\some\path\to\a\file.ino"
"\"C:\\some\\path\\to\\a\\file.ino\""

That is an awesome reply. Thanks. You have given me a lot of great information.

Turning on verbose is something I would not have considered and is a great starting point. From there I guess I need to look under the hood of the Arduino IDE and get busy.

Thanks again