Compiler option defines overriding ESP32 and not compiling

I have the same code base for ESP-32 and M5-Stick-C-Plus. But there are a couple #ifdef around specific sections (such as M5.Lcd.print).

I have a global Defines.h that specifies which configuration is being compiled.
I now want to specify the option at the compile level (but in a makefile).

I want to define ** ESP_M5_SMART_CLICKER_CONFIGURATION** which is specific to the M5.

compileM5:
        /opt/homebrew/bin/arduino-cli compile "build.extra_flags=\"-DESP_M5_SMART_CLICKER_CONFIGURATION\"" --fqbn esp32:esp32:m5stick-c | tee output

But after trying the various build.extra_flag syntax, when compiling for the M5 the ESP32 doesn't get defined, and a compile error shows up in library file: M5StickCPlus.h

#error “This library only supports boards with ESP32 processor.”

Example Defines.h file included in all my code. Currently I comment out the option I want.

//turn on or off which configuration is desired..
//#define ESP_M5_SMART_CLICKER_CONFIGURATION
//#define ESP_32_FEEDER

If instead this is modified in the file included, the compile works fine.

So somehow the build.extra_flags is wiping out other defines. One comment in my research showed that these flags should only be used by the platform builders (or things like I'm seeing show up).

With GitHub, I wanted a single install without having to modify that Defines.h file for each different build.

Any help would be appreciated.
thanks.
scott

Hi @concurrents. You can see the reason here:

https://github.com/espressif/arduino-esp32/blob/2.0.3/platform.txt#L136

build.extra_flags=-DESP32 -DCORE_DEBUG_LEVEL={build.code_debug} {build.loop_core} {build.event_core} {build.defines} {build.extra_flags.{build.mcu}}

When you define a custom build.extra_flags property via an arduino-cli compile --build-property flag, you overwrite the one defined in the platform.

Flags, no, but as for the build.extra_flags property that is also the impression I get about its purpose, based on this comment:

https://github.com/espressif/arduino-esp32/blob/2.0.3/platform.txt#L122

# This can be overriden in boards.txt

boards.txt is the domain of the platform author, so if the purpose of the flag is to make board-specific build command customizations defined in the individual boards definitions, then that property is not intended to be used by the user and you are risking just such a usage collision if you decide to modify it as a user.

There is another set of "extra_flags" properties, and the comment on those indicates to me they are intended for the user:

https://github.com/espressif/arduino-esp32/blob/2.0.3/platform.txt#L112-L120

# These can be overridden in platform.local.txt
compiler.c.extra_flags=
compiler.c.elf.extra_flags=
compiler.S.extra_flags=
compiler.cpp.extra_flags=
compiler.ar.extra_flags=
compiler.objcopy.eep.extra_flags=
compiler.elf2hex.extra_flags=
compiler.libraries.ldflags=

The platform.local.txt file is intended to allow the user to modify a platform configuration without touching the primary files. The platform author would have no need to use that file since platform.txt is their domain. So properties intended for use in platform.local.txt must be reserved for the user. They are both more flexible and more inconvenient than the build.extra_flags property in that they are specific to the compilation commands for a given language (e.g., compiler.cpp.extra_flags is only used when compiling C++ and Arduino Language files), but you can always define multiple if you need to do something like set a define for C++ and C files.

Something you should be aware of is that some platform authors have been naughty and also used these properties internally (even though there is no need for that since they are able to define an unlimited number of such arbitrary properties for their own use. In those platforms, you will run into this same problem of a use collision when your custom definition overwrites the one from the platform. You are of course welcome to copy the definition from the platform and append your additional flags to that.

Arduino holds much of the blame for such encroachment on the user's properties because they have never documented these flags beyond the obscure and vague comments in the platform.txt file. There is discussion on this situation here:

Thanks for the long reply. After a couple test options I found out that compiler.cpp.extra_flags was the winning ticket.

Now my makefile can compile both:

compile:
        /opt/homebrew/bin/arduino-cli compile --build-property compiler.cpp.extra_flags=-DESP_32_FEEDER --fqbn esp32:esp32:esp32 | tee output

compileM5:
        /opt/homebrew/bin/arduino-cli compile --build-property compiler.cpp.extra_flags=-DESP_M5_SMART_CLICKER_CONFIGURATION --fqbn esp32:esp32:m5stick-c | tee output

Next, do you know how to have GitHub run the arduino-cli compiler?

One more question: How do I provide these -D commands to the Arduino SDK? As I switch back and forth often, changing platforms.txt isn't a good solution. Or even the preferences.txt file requires re-starting Ardunio.

thanks.

It is not clear to me what you mean by "GitHub". Please provide more details about what you are hoping to accomplish.

By "Arduino SDK", do you mean the Arduino IDE? If so, which version?

Arduino SDK (the main graphic editor, download to boards, etc), version 1.8.19

As for GitHub, I can ask that in another thread. This is the GitHub that has continuous integration and can compile after files are checked in.

We call that the "Arduino IDE". It will avoid a lot of confusion if you use that term here instead of "Arduino SDK".

You can set build properties via the Arduino IDE 1.x command line using the --pref flag (like how you used --build-property with Arduino CLI):

OK, I'll answer over there. FYI, the thing you are talking about is called "GitHub Actions"

The Arduino IDE provides a nice buttons to compile, upload, etc. When using the IDE I don't see a command line option, only the buttons.

So my question is how to provide those -D switches from the IDE.

thanks.

You can't provide arbitrary compilation command flags via the Arduino IDE GUI. You can only do it from the command line or the boards platform configuration files.

This was done intentionally by Arduino to encourage library authors to provide users with friendly interfaces rather than preprocessor-based messes.

"friendly" ?? That's subjective.

Anyway, in my original question, I have the same code base for two distinct executables. Arduino setup the environment constraints (eg. src folder) which is fine.

But I want to be able to easily, and friendly without messes, compile between the two options using the Arduino IDE (not command line).

Are you saying that's not possible?

thanks,

Make a board definition with custom board options that define the -D flags you need. Then you can select which you want from the Tools menu in the Arduino IDE:

https://arduino.github.io/arduino-cli/latest/platform-specification/#custom-board-options

I'll look into that. In that case my other developers would need those boards too.

I'll probably just stick with commenting out my define for the 2 cases.

thanks.

You can make it easy to install and update your custom boards platform by providing Boards Manager installation support:

https://arduino.github.io/arduino-cli/latest/package_index_json-specification/

You can reference all the resources from the stock ESP32 boards platform (which they can also install via Boards Manager), so your custom platform can consist of a boards.txt file alone.

Or if you are not interested in going the Boards Manager route, you can distribute a boards.local.txt file to them with instructions for them to drop it into the ESP32 platform installation folder.

thanks, will look into it.

Per, it doesn't work for esp32 and esp8266 core. problem is the tools folder. {it should be renamed to system). and the other problem is that tools are in tools (system) and not installed in the right tools folder.

Thanks @Juraj. I remember the ESP8266 issue, but didn't extrapolate that to ESP32 platform as well.

The boards.local.txt solution is unaffected by the problem.

As for the creation of a referencing platform, it seems that it will work fine if you copy the entire tools folder from the stock ESP32 platform to your own platform. Other than that, the only file needed is a boards.txt.

Have you found success with this endeavor @concurrents?

Thanks for the query. Yes the GitHub continuous integration is what I'm looking for. I haven't worked it yet, but actually don't see how unless the arduino compiler can be placed at the GitHub server. That would be powerful.

Anyway, I did have good success with the makefile compiling various options as stated earlier. That at least lets me send of a couple compiles to make sure the code works with various define configurations.
thanks.

It can be done easily, and is already done by hundreds of Arduino project repositories.

GitHub actions provides you with a selection of virtual machines. You can run anything you like on those machines. "Actions" are pre-packaged reusable components you can build your workflow from, and so these are the most convenient way to use GitHub Actions, but you are also welcome to just run shell commands directly as you need or prefer. So essentially anything you can do in a headless manner on your local machine you can do on GitHub Actions as well.

I'll share some options in order of my own preference:

arduino/compile-sketches action

This action provides a simple yet flexible interface to:

  • Install sketch dependencies
  • Compile sketches
  • Collect compilation results data

This is the approach used in all of the official Arduino firmware repositories.

High level introduction:

Example of a workflow compiling the examples of the "Arduino_ConnectionHandler" library for a selection of boards including ESP32:

https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/.github/workflows/compile-examples.yml

arduino/setup-arduino-cli action

This action installs Arduino CLI in the GitHub Actions runner machine. It is then up to you to use Arduino CLI however you like in subsequent steps of the job.

High level introduction:

Workflow demonstrating its use:

https://github.com/arduino/arduino-cli-example/blob/master/.github/workflows/test.yaml

Install and use Arduino CLI directly from the runner shell

As I mentioned, you can execute commands in the runner shell just the same as you would on your machine. This approach will tend to result in a more complex workflow that takes more effort to maintain, especially if you are using it in multiple projects. But you have full control over the behavior.

Documentation here:

https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

And of course the installation and command line usage instructions for Arduino CLI are also relevant:

https://arduino.github.io/arduino-cli/latest/

Honorable mentions

I don't have personal experience with these, but they are popular alternatives created by talented members of the Arduino community so worth a mention:

ArminJo/arduino-test-compile action

Adafruit's build script

This is not an action, but a script Adafruit installs and runs via GitHub Actions workflows in their ~200 Arduino project repositories. It seems somewhat specific to their own requirements, but quite a few 3rd party projects are using it.

The origins of this project go all the way back to the days when Travis CI was the go to automation service. Adafruit was a pioneer in the use of Travis CI for automated testing of Arduino projects and their work served as a valuable reference even though I found the scripts did not meet my own specific needs.

Well there you go.. thanks that's awesome.

I'll work on that soon.

thanks,