Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By mconsidine
#59351 I've searched this site for info on this and am oot really turning anything up. Apologies if this should be posted elsewhere...

Short version: Can someone point me to an example of how the boards.txt and/or platform.txt file can be modified to reflect a menu choice for compiler optimization, either for a given board or for all of them?

Longer version: I'd like to be able to select between -Os and -O3 (at the moment) when compiling for ESP8266. I see from an Arduino wiki that there is a structure to the boards and platform files and I can see how this is reflected in the ESP-specific files. The platform.txt file looks like the spot where the options could be set, e.g. something like this
Code: Select allcompiler.optimize_flags=-O3
compiler.optimize_flags.size=-Os

followed by something like this
Code: Select allcompiler.c.flags=-c {compiler.warning_flags} {compiler.optimize_flags} -g -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -falign-functions=4 -MMD -std=gnu99 -ffunction-sections -fdata-sections


What seems to be escaping me is how to get this carried over to a menu option, which seems to be governed by the boards.txt info.

Can anyone provide any pointers or examples on the best way to achieve this that would survive updates? Am I even thinking about this correctly (ie. are there compiler flags for the lx106 versions of gcc and g++ that aren't recognize or don't make sense to try to use for the ESP?)

Thanks in advance,
mconsidine
Last edited by mconsidine on Sun Dec 11, 2016 6:53 pm, edited 1 time in total.
User avatar
By mconsidine
#59377 Ok, I've figured this out.

First, I edited platform.txt so that this
Code: Select allcompiler.optimize_flags=-O2

was an entry along with the other compiler-related entries near the top of the file. (The reason for using this value was to ensure that what I chose was actually getting passed along, via a review of the compilation log.)

Then, I replaced every instance of
Code: Select all-Os
with
Code: Select all{compiler.optimize_flags}


While going through that I noticed a comment that said
Code: Select all# This can be overriden in boards.txt
build.extra_flags=-DESP8266


"hmmm" says I.

So I turned to boards.txt to see if I could figure out how that override could happen for that entry and then figured that maybe the one I just created for the compiler could be overridden in same way. Turns out it can.

To do that, I added the following two lines
Code: Select allmenu.CompilerDefs=Compiler Defs
menu.CompilerDirs=Compiler Directives

at the end of the first section of menu entries.

Then, in the entry for the board I wanted these options available for (Nodemcu V2), I added the following at the end of its section:
Code: Select allnodemcuv2.menu.CompilerDefs.BT=Bluetooth
nodemcuv2.menu.CompilerDefs.BT.build.extra_flags=-DBT -DESP8266

nodemcuv2.menu.CompilerDefs.WIFI=WiFi
nodemcuv2.menu.CompilerDefs.WIFI.build.extra_flags=-DWIFI -DESP8266

nodemcuv2.menu.CompilerDirs.Os=Os
nodemcuv2.menu.CompilerDirs.Os.compiler.optimize_flags=-Os

nodemcuv2.menu.CompilerDirs.O3=O3
nodemcuv2.menu.CompilerDirs.O3.compiler.optimize_flags=-O3


A quick test reviewing the compilation output and using this
Code: Select allvoid setup() {
  // put your setup code here, to run once:
delay(5000);
Serial.begin(9600);
#ifdef BT
  Serial.println("Bluetooth code would go here");
#elif WIFI
  Serial.println("WIFI stuff goes here");
#else
  Serial.println("Crud...");
#endif
}

void loop() {
  // put your main code here, to run repeatedly:
 
}

suggests that this works fine.

HTH,
mconsidine