Go Down

Topic: IDE to Command Line Build, support -D (Read 1 time) previous topic - next topic

DrN

I am using the IDE to develop code for a few systems that will be deployed to aduino or teensy installed on different versions of a sensor board.

It is very tedious to open the IDE and manually change a #define statement in the code for the conditional compilations.

At the same time, it seems to be famously difficult to compile the project outside of the ide, or to generate a makefile to compile a project outside of the IDE.

What is needed is a feature in the IDE to output a makefile,  or,  output the commands that it runs to compile the project to a file.

pert

You can use the Arduino IDE from the command line. It has a `--pref` option that you can use to set build properties, including those like `compiler.cpp.extra_flags` that allow you to inject compiler flags such as the -D you desire.

You can find the documentation for the Arduino IDE's command line here:
https://github.com/arduino/Arduino/blob/master/build/shared/manpage.adoc

Better yet, use Arduino CLI. Its equivalent to `--pref` is `--build-properties`:
https://arduino.github.io/arduino-cli/latest/commands/arduino-cli_compile/#options

DrN

Thank you, that looks great.   In the present instance it's for a Teensy.   Presumably that is saved in the preferences from the previous run of the IDE.   So, the command would be like this?

        # arduino  --pref compiler.cpp.extra_flags=-Dsomething    myfile.ino

Is there any documentation on the available preference names?

And, is there a way to save it to a hex file instead of uploading it?  


pert

In the present instance it's for a Teensy.
OK, then Arduino CLI is not currently an option unfortunately:
https://github.com/arduino/arduino-cli/issues/700
but work is in progress to add support for Teensy to Arduino CLI.

But the Arduino IDE/Teensyduino command line works fine too.

So, the command would be like this?

        # arduino  --pref compiler.cpp.extra_flags=-Dsomething    myfile.ino
You need to add the --verify flag. Other than that, yes it's fine.

Is there any documentation on the available preference names?
There is some here:
https://arduino.github.io/arduino-cli/latest/platform-specification/
You can also set the properties you will find in the Arduino IDE's preferences.txt file (the path is shown at the bottom of the Arduino IDE's File > Preferences dialog).

A few are described in the CLI documentation:
https://github.com/arduino/Arduino/blob/master/build/shared/manpage.adoc#preferences

But you won't find mention of compiler.cpp.extra_flags in any of those. The reason is that, in addition to the properties that are used internally, the Arduino build system allows you to define arbitrary properties which have no special treatment by the Arduino IDE, but can be used in the user's build and upload recipes. compiler.cpp.extra_flags is one of these arbitrary properties.

By convention, Arduino boards platforms provide a set of these "extra flags" properties to allow the user to customize the build process by injecting arbitrary compiler flags, but there is nothing special about them and their presence is not enforced by the Arduino IDE. You can see an example of this in the Arduino AVR Boards platform here:
https://github.com/arduino/ArduinoCore-avr/blob/1.8.3/platform.txt#L42-L49
Unfortunately, the Teensy boards platform is one of the rare exceptions. The closest equivalent property seems to be `build.flags.defs`. However, this is already in use by the Teensy boards platform, so if you just define it as `-Dsomething` then you will be overwriting the definition of the property as used by the Teensy board platform. Here you can see the definition of it for the Teensy 4.1:
Code: [Select]
teensy41.build.flags.defs=-D__IMXRT1062__ -DTEENSYDUINO=153
(the `teensy41` part of the property is just associating that specific definition of the property with the Teensy 4.1 board selection. The property is named build.flags.defs when it's used in the compilation recipe)

So you would likely want to append your custom -D flag to the existing flags in your command so the existing flags are preserved:
Code: [Select]
arduino  --pref "build.flags.defs=-D__IMXRT1062__ -DTEENSYDUINO=153 -Dsomething" myfile.ino

I have made a proposal to give these properties more of an official, documented status here:
https://github.com/arduino/arduino-cli/issues/846

And, is there a way to save it to a hex file instead of uploading it?   
Yes. The `build.path` preference defines where the compiled binary will be saved. So this command:
Code: [Select]
arduino  --pref "build.flags.defs=-D__IMXRT1062__ -DTEENSYDUINO=153 -Dsomething" --pref build.path=/tmp/build-path myfile.ino
Will result in the .hex file being located at tmp/build-path/myfile.ino.hex after the command finishes.

Go Up