Use -D symbols in Post-build?

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wonder if it's possible to use a Symbol from the -D compiler directive as a macro in the Post-Build?

 

Example:

-DMAJOR=1 -DMINOR=05

 

In Post-Build I would like to use these defines as macros:

copy $(Name).fwb ..\..\$(name)_$(MAJOR)$(MINOR).fwb

 

If the $Name is 1002 the expanded Post-Build would be:

copy 1002.fwb ..\..\1002_105.fwb

 

Is this possible at all?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Your best bet may be to try and define these in the "enviroment" and then import the values in both places. For example:

C:\SysGCC\avr\bin>set FOO=79

C:\SysGCC\avr\bin>type Makefile
all:
        avr-gcc -mmcu=atmega16 -Os -DSOMEVAR=$(FOO) avr.c -o avr.elf

C:\SysGCC\avr\bin>type avr.c
#include <avr/io.h>

int main(void)
{
        PORTB = SOMEVAR;

    return 0;
}
C:\SysGCC\avr\bin>make
avr-gcc -mmcu=atmega16 -Os -DSOMEVAR=79 avr.c -o avr.elf

C:\SysGCC\avr\bin>avr-objdump -S avr.elf | tail -n 12
0000006c <main>:
  6c:   8f e4           ldi     r24, 0x4F       ; 79
  6e:   88 bb           out     0x18, r24       ; 24
  70:   80 e0           ldi     r24, 0x00       ; 0
  72:   90 e0           ldi     r25, 0x00       ; 0
  74:   08 95           ret

00000076 <_exit>:
  76:   f8 94           cli

00000078 <__stop_program>:
  78:   ff cf           rjmp    .-2             ; 0x78 <__stop_program>

here I have a variable called "FOO" in my environment that I set to 79. I use this in my Makefile as $(FOO) and it is assigned in a -D to "SOMEVAR". then the C code writes the SOMEVAR value to PORTB and finally in the generated code we see:

  6c:   8f e4           ldi     r24, 0x4F       ; 79
  6e:   88 bb           out     0x18, r24       ; 24

where that 79 I started with is being used.

 

In AS7 post build commands I think you can also access env-vars as $(FOO) so now you should be able use that 79 value in the two places.

 

What you cannot do is simply lift a -DSOMEVAR=79 from the Makefile options.

 

(or rather you could but you would need to wite a file processing utility in C or Python or your other language of choice that actually read through the Makefile and found the invocation and then parsed 79 out of the -D)

 

It seems better to set such a value (FOO=79) at a higher level then take that as your input in two places.

 

BTW it remains to be shown whether AS7's post build can access env-vars.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok so I don't have AS7 here but I do have Visual Studio (which I expect to work in a similar way). I set up a Post Build event as:

 

 

and when I build that I get...

 

 

So %FOO% is the magic you need to get access to a environment variable in VS (and hence probably AS7).

 

HOWEVER this:

Had to be set in the same environment I started VS from. if I just start with my normal toolbar icon it clearly just inherits the "system" environment. So I had to "SET FOO=79" in a command prompt and then launch VS

C:\SysGCC\avr\bin>C:\LegacyApp\VisualStudio2010\Common7\IDE\devenv.exe

from there for it to "inherit" that value.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So the short answer is; NO :)

 

We use the AS7 IDE and don't tamper with the auto generated makefile. It is easier for those who might come in to the project with less knowledge, and it's less prone to mess things up.

The work around you presented requires to set an environment variables before AS7 launch. The problem here is that the solution contains several projects, all with their individual versions. So I tried out a few solutions.

 

To keep things simple I made a GetVersion.bat file:

SET MAJOR=1
SET MINOR=05
ECHO Version:%MAJOR%.%MINOR%

As a test the bat file is run in the Post-Build event. $(Name) is 1001

..\GetVersion.bat
..\..\..\tools\mot2bin.exe -efwb -c -t -f98,2 $(Name).srec
copy $(Name).fwb ..\..\$(Name)_%MAJOR%.fwb

The output from GetVersion.bat looks fine

Version:1.05

Proving that the environmental variables are set. But the copy command ends up with:

1001_.fwb

It seems like each command is run in a separate environment and therefore can't share the variables.

 

So I don't see an easy solution to this. What I can do, is to write an external application to read the project file and grab it from there and rename the final output.

Thank you very much for your time and effort.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MaxKielland wrote:
We use the AS7 IDE and don't tamper with the auto generated makefile.
I wasn't suggesting it either. But somewhere in the IDE you currently entry -DSOMEVAR=n, i was merely suggesting that be changed to -SSOMEVAR=$(FOO) which the IDE will pass through into the Makefile when it creates it.

 

But yeah, I see the issue if they can't be set when AS7 is launched so could be inherited from the environment.