title: Eclipse: How to make build-config variables available at compile time?
body: <p>In an Arduino project, I have build configurations to compile for, and download to, two devices: an Arduino Mega2560 and a standalone ATmega 328P DIP chip on a breadboard.  Both configs compile, download and run on their respective devices, at least for device independent code like a Hello program.  But most programs, f/ex, blinky, would need some conditional device-dependent code.  I could make up and define some symbols on (Project Properties > C/C++ Build > AVR Compiler (and AVR C++ Compiler) > Symbols) per configuration, which would get added to the compiler command-lines.  </p>
  
  <p>But somewhere within the IDE, the part and other choices I selected for each configuration are known and assigned to environment variables in (Project Properties > C/C++ Build > Environment), and some of these are passed on the compiler command-line but not as -D options.  I'd prefer to use these existing ones since 1) they're created automatically; 2) I won't typo one of them differently for one of the compilers; 3) the same convention could be followed by other writers - or by me, on some other project - making code more portable.</p>
  
  <p>Is there a way to automate passing them to the compiler as #defines?  </p>
  
  <p>(Eclipse Indigo, CrossPack-AVR-20100115, &amp; Mac OS 10.6.8)</p>
  
  <hr>
  
  <p>Edit: </p>
  
  <p>As a work-around I've written a pre-build script that creates a header file to #define the build-time environment variables: </p>
  
  <pre><code>printenv | awk 'BEGIN{print "/***  AUTO-GENERATED FILE -- DO NOT EDIT.  ***/\n";} {sub(/^/, "#define "); sub(/=/, "  "); print; }' &gt;${CWD}/../src/buildtime-environment.h
  </code></pre>
  
  <p><br><br>
  <img src="https://i.stack.imgur.com/XXNhM.png" alt="Screenshot of Build Steps dialog"></p>
  
  <p>It works well enough with two caveats:</p>
  
  <ol>
  <li>It's fragile - it has "special-knowledge" of my project directory structure.</li>
  <li>Hovering over one of those macros in the IDE editor, the macro-expansion tool-tip may show an out-of-date value for the macro, potentially causing you to chase a ghost if you forget this and believe the tool-tip.</li>
  </ol>
  
  <p><em>(I'll leave this a comment until/unless I give up on finding a solution).</em></p>