Go Down

Topic: Arduino building process and per-sketch compiler options. (Read 2 times) previous topic - next topic

hugo75

    Hello everyone,

I'm an electronics & microcontroller newbie but do have a decent coding background. Something that I am used to doing, is using defines for configuration values that only need to be set at compile-time. Imagine, for simplicity, that I have a "LED" class and I want to be able to override the default LED_PIN on a per-sketch basis -- you can imagine something like this in the relevant .h file:

Code: [Select]
#ifndef LED_PIN
#define LED_PIN 13
#endif

...and wanting to compile that with a -DLED_PIN=7 for situations that require it.

I don't like to reserve variables for a thing that does not change and is really only used once. (I'm old school and prefer small memory footprints.)

I searched through the Arduino IDE (v0022) code, but there seems to be no facility to actually pass some CFLAGS down to the command that compiles the library .cpp file.

Simplest solution seemed to hack Sketch.java to include a getCompilerFlags() routine and Compiler.java to call it, so it'll search for a "cflags.txt" file in the sketch's folder, read that file's contents, and pass that as-is appended to the compile commands. (A hacky 5m job -- but it works like a charm.)

Now, having just done this; I wonder if I just re-invented the wheel [I am known to do that].

Perhaps there are existing enhancements or patches available that already accomplish the same, in a more elegant manner?

Also, I am "happily unaware" of any unexpected consequences this change could have, although it does seem that every time a sketch compiles, the libraries are also re-compiled, so it seems safe. Or is there perhaps something that I really should know? :)

Thanks for your time!

Coding Badly

#1
Mar 09, 2011, 07:41 pm Last Edit: Mar 09, 2011, 07:43 pm by Coding Badly Reason: 1
Quote
Now, having just done this; I wonder if I just re-invented the wheel [I am known to do that].


Sort of.  A similar feature is planned for "Arduino 1.0".  The plan is to allow CFLAGS to be defined in boards.txt.  Selecting a different board would potentially select a different set of CFLAGS.

Quote
Perhaps there are existing enhancements or patches available that already accomplish the same, in a more elegant manner?


I'm not aware of any.

Quote
Also, I am "happily unaware" of any unexpected consequences this change could have, although it does seem that every time a sketch compiles, the libraries are also re-compiled, so it seems safe. Or is there perhaps something that I really should know?


Seems like a reasonably good solution to me.

Graynomad

Rob Gray aka the GRAYnomad www.robgray.com

Coding Badly

The first official release of the Arduino IDE.  And the irony of having 22 versions already released is not lost on me.

maniacbug

So just curious...  If you're ok with cflags.txt in sketch dir, why not config.h and put your defines in there??

Go Up