LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2005, 09:03 AM   #1
BoldKiller
Member
 
Registered: Apr 2002
Location: Montreal, Quebec
Distribution: Debian, Gentoo, RedHat
Posts: 142

Rep: Reputation: 15
Automaticly incrementing Version Number


I was loking at a way to automaticly increment the build number in a gcc project written in C.

I could make a small program to re-write a header file containing the version number. Everytime make is run, the program parses the header, gets the version number, incremetns it, writes back the file (as a #define) , then the rest of the compilation process is ran.

It would work, but it is lot of work simply to have a build number!! Is there a better way to do it?

By the way, I could manualy update the build number, but seriously, who can remember to always change a number every time he presses the build button!!

Any ideas would be apreiated.
 
Old 03-25-2005, 10:25 AM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Hmm.. interesting idea.. can't say that I have any ideas offhand, except for something similar to the method you described. In projects where I'm maintaining builds, I typically just manually change the build number when I do major revisions.
 
Old 03-25-2005, 10:50 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Use awk?

Hi -

You might be able to do what you want with a simple one-line awk script.

For example:

1. Let's say your version is in a header file that looks like this:
less version.h =>
#define VERSION 2.0.1

2. Your awk script might then look something like this:
awk '/VERSION/ {
split ($3, a,.);
a[2] = a[2] + 1;
printf ("#define VERSION %d.%d.%d", a[0], a[1], a[2]);
}' version.h

'Hope that helps .. PSM

PS:
This is just a suggestion - I haven't tested or debugged it yet.
At a minimum, you'll need to update your "version.h" header with the
"awk" output.
 
Old 03-25-2005, 12:39 PM   #4
BoldKiller
Member
 
Registered: Apr 2002
Location: Montreal, Quebec
Distribution: Debian, Gentoo, RedHat
Posts: 142

Original Poster
Rep: Reputation: 15
Hmmm, interesting idea.

I think I will give it a try. Especially since gawk is available on both Linux and Windows. (we developp everything cross platform).

As for writing back to the file, if I used a file simply for this version number I could simply do a
"myAwkScript > version.h"
in the make file.

Be the way, if you are wondering why version numbers are so important, it is beacause, I'm developping for a microcontrolleur (using avr-gcc). This version is the only way I have to know what version of the code is running in the chip!

Thanks for the tip.
 
Old 03-26-2005, 11:15 AM   #5
BoldKiller
Member
 
Registered: Apr 2002
Location: Montreal, Quebec
Distribution: Debian, Gentoo, RedHat
Posts: 142

Original Poster
Rep: Reputation: 15
I tryed the awk script. I used
Code:
VERSION_NUMBER
{
   split ($3, a, ".")

   a[3] = a[3] + 1
   printf ("// VERSION_NUMBER %d.%d.%d\n", a[1], a[2], a[3] ) > "version.h"
   printf ("#define VERSION_CODE 0x%x%x%x\n", a[1], a[2], a[3]) > "version.h"
}
And the file version.h:
Code:
// VERSION_NUMBER 2.1.15
#define VERSION_CODE 0x21F
When I run the code, it does what I want but also execute the print statement even if the pattern does not match. For exemple, the above file will give this result
Code:
// VERSION_NUMBER 2.1.16
#define VERSION_CODE 0x2110
// VERSION_NUMBER 0.0.1
#define VERSION_CODE 0x001
If I put aditionnal lines (white lines or not) , the instructions will execute once for every line. The question is, its the pattern at the top of the bracket supposed to specify the code will only execute if the pattern is matched?

Thanks for any tips.
 
Old 03-26-2005, 11:33 AM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
as a user I'd be a little confused if your version numbers changed by 50 or 100 on each release : )
 
Old 03-26-2005, 11:46 AM   #7
BoldKiller
Member
 
Registered: Apr 2002
Location: Montreal, Quebec
Distribution: Debian, Gentoo, RedHat
Posts: 142

Original Poster
Rep: Reputation: 15
If you mean the last number, it is only the build number.

Version number will not actually be shown to the user. The version number is in the code of a microcontrolleur. I need it to let my drivers know what version of firmware the card is running.

The build number itself is more for myself. This way I dont have to ask myself: "did I reprogram the number 1 card or was it number 2?" With this build numbers, I can make sure all my cards are running the same version of firmware.
 
Old 03-26-2005, 12:08 PM   #8
BoldKiller
Member
 
Registered: Apr 2002
Location: Montreal, Quebec
Distribution: Debian, Gentoo, RedHat
Posts: 142

Original Poster
Rep: Reputation: 15
Perfect, I made it work!!

As I stated before, I had not really use awk before. I put an enter between the condition and the {

Here is my code for the moment, if someone needs it:
Code:
/VERSION_NUMBER/ { 
   split ($3, a, ".");
   a[3] = a[3] + 1;
   
   if ( a[3] > 255 )
      a[3] = 0;
}

END {
   printf ("// VERSION_NUMBER %d.%d.%d\n", a[1], a[2], a[3] ) > "version.h";
   printf ("#define VERSION_CODE 0x%x%x%.2x\n", a[1], a[2], a[3]) > "version.h";
}
Thanks everyone for your help!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting the Linux Distribution Name and Version Number s&j Linux - General 9 08-12-2012 06:22 PM
Is there a good way to find the OS version number? BrianK Linux - General 6 10-06-2004 10:54 PM
where is slackware version number? davidsrsb Slackware 3 09-08-2004 05:38 AM
unsupported btree version number 8 matttail Linux - Software 3 08-31-2004 09:19 PM
How do I specify a version number to use? Seventh Linux - Newbie 1 08-26-2004 04:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration