CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Embed Svn revision number in code

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mds



Joined: 06 Nov 2005
Posts: 29

View user's profile Send private message

Embed Svn revision number in code
PostPosted: Tue Aug 16, 2011 7:37 pm     Reply with quote

Hi all,
I was hoping to embed the svn revision number into my PIC source code.
I have two problems though.

Code:
#define ver_str "$Revision: 47 $"


this line allows svn on commit to update the number(47)
I can then printf the number whenever the PIC is interrogated.

1)Normal course of action tho is to:
change code,compile, then commit. Otherwise the commit may not hold the latest STA and hex etc.

This sequence however would mean the latest HEX would include 47, "one behind" the latest svn commit revision.

2)The string contains the svn:keyword. I would rather it just be the number.

My first thought was take ver_str and after basic PIC power up config code. Strip the keyword etc out and even add 1 to the number, thus solving both my problems.

My other thoughts were "trapping" the compile trigger (ide compile button) event somehow and run a quick script to run svnrevision and then "edit" the ver_str define text.
a little complex.

any other thoughts?
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Aug 22, 2011 5:14 am     Reply with quote

I've been looking into this sort of "watermarking" problem and while I've not yet tried to get anything working, there is a way of doing some useful things.

In Options->Tools there is a list of User Defined Tools. These are command lines tools that run at specifed event. The relevant events for this are "Before Build" and "After Build". These, hopefully (as I've not yet tried it in anger) allow a user script or command line tool to be run before and after all builds. Typically you'd put the svn check-in and immediate read back in the before build script, and that would update the revision number, so no need for adding 1 or whatever. This is especially useful when you're dealing with several code modules as each has its own revision code, and the project as a whole has a different rev code.

In other IDEs (specifically Keil uVision 3 for ARM) the team I headed used the "After Build" event to add a CRC calculated from the hex image with which the code could check itself at runtime. We also marked debug/development builds (unlike the PIC the ARM doesn't need debug handler code) by not giving them a CRC. So builds could identify themselves as dev or release.

This doesn't solve the "getting rid of the $Revision" issue of course. As far as I can see the pre-processor (integrated into the compiler and not an actual separate pass) won't string slice/index so that's not possible at compile time, only as run time. In other words you have to assign your ver_str to a string in ram and extract the relevant bits in code. Messy and wasteful on memory, but I can't find any other way to do it :-( I've already had to do that for compilation dates and time, which I encoded into a 32 bit Timecode; at runtime of course. I needed a timecode as it was for sending over CAN with its max 8 byte messages. Thinking about it, you could get your "Before" script to extract the number for you into a place holder define. That should work.

I hope this helps. As I say I've looked at this, but not yet implemented it, mainly as getting a Subversion server up and running is proving to be politically tricky :-(

RF Developer.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Aug 22, 2011 6:39 am     Reply with quote

PS: Before and After Build events only work for *builds*! Compiles, including F9s, don't appear to trigger the scripts. Also the Before doesn't appear to wait for the spawned process to finish. That negates the whole purpose of the event.
mds



Joined: 06 Nov 2005
Posts: 29

View user's profile Send private message

PostPosted: Mon Aug 22, 2011 5:21 pm     Reply with quote

Yes it is all a little messy and Im amazed it has not been "dealt" with previously. Im also amazed at the lack of responses to the post as I was expecting it to be something that everyone would be up against...But then tis said I live on a different planet.

I did look at those script triggers and yes its build not F9 and strange things happen.

I know its code, thus takes up PIC mem and cpu cycles on power up. But its the simplest solution I came up with. I dont always commit after a compile(F9) sometimes its little text changes or I want to try b4 I commit. So the system below works for me.

do{
change code
compile
test // Code reports as 1ver in front of commit
}While(not happy with code)


commit // commit version now matches hex

snip of my define header file
Code:
#define REL_NUM 1   /// Release number has to be put in manually.
#define ver_strd "$Revision: 49 $"
#define aut_str "MDS"


Where REL_NUM is manually put in by me and matches the branch/ tag etc. There probably is a way to auto make this too?

Code:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// *  strip number from string        *
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** \brief Strips a number from a string 
   \details
    - takes the ver_strd "$Revision: 49 $"
    - strips the number out of it
    - Note would concatenate all digits within a str
*/
int8 get_num_frm_str(int8 *str)
{
 int8 retval,i=0;
 int8 numstr[8];
                           
  while(*str!=0)                      /// check string till end
  {
    if(isdigit(*str))              /// if the character is a number
    {
      numstr[i]=*str;               /// concatenate it to numbrstr
      i++;
    }
  *str++;   
  }
  retval=atoi(numstr);            /// convert str to number
  return(retval);
}


Code:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// *  make revision        *
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** \brief Simplify Convert the SVN revision string
   \details
    - takes the ver_strd "$Revision: 49 $"
    - strips the text and adds 1 to the number
      - work sequnece is modify,compile,commit
      - Creating a HEX.file that is 1 behind the commit number.
*/
void fixver()
{
 int8 wstr[20];     //working string
 int8 vernum;
  strcpy(wstr,ver_strd);            /// copy svn revision str to wroking str
  vernum=get_num_frm_str(wstr);     /// strip out the number from the text
  vernum++;                         /// add one to emulate next commit.
  sprintf(ver_str,"v%u.%u ",REL_NUM,vernum);   /// create a ver_str with the number
}


snip reporting the vernumber
Code:
fprintf(PC,ver_str);
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Tue Aug 23, 2011 7:26 am     Reply with quote

mds wrote:
Yes it is all a little messy and Im amazed it has not been "dealt" with previously. Im also amazed at the lack of responses to the post as I was expecting it to be something that everyone would be up against...But then tis said I live on a different planet.


At my present job, version control used to consist of a collection of subdirectories - one for each version of the code. Then we hired a guy who installed subversion because he was familiar with it, and he got the rest of us on the same page. But we still reverted to the tried & true new subdirectory for each revision, just in case.

...Now he's gone and we're stuck with subversion and no one really knows how it works, nor do we care to be honest. And we're glad we stuck with the old method of version control since we'd be quite screwed now that our subversion guru is no longer with the company.

In my experience, all you need is storage space (which is cheap), backups (our IT people take care of them), and a decent comparison program like beyond compare.

I'm not a fan of subversion and I suspect that most people here either feel the same way or simply don't know about it.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Aug 23, 2011 7:37 am     Reply with quote

When I worked for Benthos, now Teledyne, the IT people insisted we use some version control software none of us understood or trusted. It was worse than having nothing at all as we each then used our own individual backup system nobody else knew about. And my boss insisted the only "official" copy of the source code was the one on the disk in his filing cabinet! Glad to be out of that racket.
_________________
The search for better is endless. Instead simply find very good and get the job done.
mds



Joined: 06 Nov 2005
Posts: 29

View user's profile Send private message

PostPosted: Tue Aug 23, 2011 5:58 pm     Reply with quote

Hi new guy,
subdirs etc was the way I worked for years, so I understand exactly where youre at.
Then I started getting numerous projects, numerous releases of said projects and more than one person working on it, the projects involved firmware,hardware, manuals, CAD.... it became a nightmare to keep track of. I swallowed hard and went looking.
TortoiseSVN solved most of the problems and didnt need a guru. There is no way I would ever go back to folders now.

the only real copy is the one in the filling cabinet... how true!
and that filling cabinet is on a server and pointed at by SVN ;)

Each to their own I recognise its not everyones cupa.
barryg



Joined: 04 Dec 2006
Posts: 41

View user's profile Send private message

PostPosted: Tue Aug 23, 2011 6:42 pm     Reply with quote

I used a slightly enhanced version of the "subdirs" for many years. Namely, a *.zip file of those subdirs Very Happy This works well if you do everything yourself, and on one computer. We got into svn about 6 years back. And I'd say it is slightly better than the zip solution. It's nice that it doesn't store endless copies of things that don't change. It's nice, now that I have a work machine, a home machine, and a netbook, and I can't remember which one has the latest version, that svn can keep it sorted. It's nice that the repo is on another computer, thus serving as an independent backup. And it's nice that I can go back and find "the code that was used for that demo a year and a half ago", though admittdly, the zip solution can do that, too. svn has a lot of nice potential features, but unfortunately, I don't know much about configuration management. Having svn does not magically turn me into one.

As to the original issue, I am content that the number changes each time I commit. That way I can tell if I'm running the same thing or not. When I had manual build codes, I never remembered to change them. Now they change by themselves. Your situation probably demands tighter and more accurate control. I depend on my comments in the svn log (or stuff copied to a tag) to identify significant changes.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Aug 24, 2011 7:00 am     Reply with quote

I don't think that its worthwhile or productive getting into the argument about whether Subversion of things like it are the right thing to use. All I will say is that where I've used such things it's been worthwhile, for a number of often at the time seemingly arcane but in reality very much practical reasons.

One issue with the "commit, fudge numbers, compile" sequence is that technically the code you've compiled is not the code you've commited. Afer fudging teh numbers, the difference may be small or nothing, but its there all the same, particularly if the svn inserted stuff is actually used in live code, as against merely being in comments. The "commit, readback, compile" sequence is better as it avoids fudging and it compiles the actual committed code. Its what it says in the tin.

There is no need to commit every compile. Any pre-build script can ask the user whether they want a development build (not committed) or release (commited), or whatever distinction fits best in your workflow. In our case a release also generated a pdf report showing the times, dates, issues and latest labels/comments/changes of all included files. That involved a database and was quite a lot of work, but we cut the release process from about 5 hours down to less than 5 minutes, and it was more accurate and reliable as it removed most sources of human error and way less of a PITA.

Its difficult to persuade many folks that these things are worthwhile, but as has just happened here, stuff happens: I've spent days trying and failing to track down source for a release of code that now only exists as a hex image - code that is essential for a repair/fault rectification. Its not my code. It's my predecessor's. He was a firm "subdir" guy, but he simply didn't bother to stash everything away, and then renamed hex images, often making it impossible to pin it down to the source from which it came :-(((!

I too naively thought many folks would have come up across this problem. But no, they appear to be mainly stuck on very basic problems such as making serial ports work, and this sort of thing is like looking after our own ivory towers. Ah well.

I don't think the CCS IDE is upto this. Running CCS in MPLAB, which has some very basic svn integration built in, is another possibility, but that has its own problems and I otherwise far prefer the CCS working environment.

RF Developer.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group