Hot!Automatically incrementing a build or software version into my compiled code

Author
swmcl
Super Member
  • Total Posts : 244
  • Reward points : 0
  • Joined: 2014/05/10 13:54:42
  • Location: Queensland
  • Status: offline
2019/04/12 16:12:26 (permalink)
0

Automatically incrementing a build or software version into my compiled code

Hi all,
What is the best way to get the build number or some other software versioning into the code for a PIC ? ( Assuming a very late version of MPLAB X ).  At the moment I would manually be updating a global variable but I forget to do it now and then.
 
I'm actually a newbie as far as versioning is concerned in general.  Any youtube tutorials on the subject that are relevant or useful would be good too if you know of any.
 
Cheers,
Steve
#1

12 Replies Related Threads

    pcbbc
    Super Member
    • Total Posts : 786
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/13 05:37:38 (permalink)
    0
    I have a simple program I wrote (coded in C#, because that’s what I had to hand) which runs as a prebuilt step. This auto increments my version number on every build and outputs to a header file that I #include in my C source.

    Python script would work just as well.
    #2
    qhb
    Superb Member
    • Total Posts : 9609
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/13 05:56:29 (permalink)
    0
    Which compiler are you using?
    #3
    swmcl
    Super Member
    • Total Posts : 244
    • Reward points : 0
    • Joined: 2014/05/10 13:54:42
    • Location: Queensland
    • Status: offline
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/13 13:37:00 (permalink)
    0
    XC8 and MPLAB 5.10 but a technique as explained by pcbbc would be handy.  Its a shame this kind of thing isn't part of MPLAB as an option.  It can't be hard to do.
    #4
    qhb
    Superb Member
    • Total Posts : 9609
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/13 15:16:41 (permalink)
    +1 (1)
    In XC8, I just embed the compile date into my build using the __DATE__ macro.
     
    #5
    Antipodean
    Super Member
    • Total Posts : 1700
    • Reward points : 0
    • Joined: 2008/12/09 10:19:08
    • Location: Didcot, United Kingdom
    • Status: offline
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/13 16:04:43 (permalink)
    +2 (2)
    swmcl
    XC8 and MPLAB 5.10 but a technique as explained by pcbbc would be handy.  Its a shame this kind of thing isn't part of MPLAB as an option.  It can't be hard to do.




    I believe there is a facility to run a script before a compile (haven't used it myself), so that is probably what he does. If that script generated a suitable include file that had some #defines in it then that would do the trick.
     

    Do not use my alias in your message body when replying, your message will disappear ...

    Alan
    #6
    pcbbc
    Super Member
    • Total Posts : 786
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/13 23:39:50 (permalink)
    +1 (1)
    Yes, I’m using MPLAB and XC8. If you look in the project properties and under compiler you will see there are options to add pre- and post-build steps. Just add a script to do whatever you want.

    Here’s a previous thread with a readyrolled solution for you:
    https://www.microchip.com/forums/m899720.aspx
    #7
    swmcl
    Super Member
    • Total Posts : 244
    • Reward points : 0
    • Joined: 2014/05/10 13:54:42
    • Location: Queensland
    • Status: offline
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/16 00:53:20 (permalink)
    -1 (1)
    OK I found something and I tried it but it fails.
    I can't upload files for some reason so I'll try to embed ...
     
    The problems lines in build_defs.h are those where the original author suggests using 'if' statements.  I have placed a comment in the code.
     

    // this is version_num.c
    /**
      Section: Included Files
    */

    #include <xc.h>

    #include "mcc.h"
    #include "version_num.h"
    #include "build_defs.h"

    /**
      Section: Macro Declarations
    */

    /*

    const unsigned char completeVersion[] =
    {
        VERSION_MAJOR_INIT,
        '.',
        VERSION_MINOR_INIT,
        '-', 'V', '-',
        BUILD_YEAR_CH0, BUILD_YEAR_CH1, BUILD_YEAR_CH2, BUILD_YEAR_CH3,
        '-',
        BUILD_MONTH_CH0, BUILD_MONTH_CH1,
        '-',
        BUILD_DAY_CH0, BUILD_DAY_CH1,
        'T',
        BUILD_HOUR_CH0, BUILD_HOUR_CH1,
        ':',
        BUILD_MIN_CH0, BUILD_MIN_CH1,
        ':',
        BUILD_SEC_CH0, BUILD_SEC_CH1,
        '\0'
    };

    */



    /**
      Section: Global Variables
    */



    /**
      Section: Version Num APIs
    */



    /**
     End of File
    */




    // this is version_num.h

    /*
     * File:   
     * Author:
     * Comments:
     * Revision history:
     */
     
    #ifndef VERSION_NUM_H

    #define VERSION_NUM_H

    #include <xc.h>

    #include <stdint.h>
    #include <stdbool.h>
    #include <stddef.h>
    #include <float.h>
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <time.h>

    #ifdef    __cplusplus

        extern "C" {
        
    #endif /* __cplusplus */

        
    #define VERSION_MAJOR 1
        
    #define VERSION_MINOR 4


    #ifdef    __cplusplus
        
        }

    #endif /* __cplusplus */

    #endif // VERSION_NUM_H

     

    // this is build_defs.h
    /*
     * File:   
     * Author:
     * Comments:
     * Revision history:
     */
     
    #ifndef BUILD_DEFS_H
    #define    BUILD_DEFS_H

    #include <xc.h>

    #include <stdint.h>
    #include <stdbool.h>
    #include <stddef.h>
    #include <float.h>
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <time.h>


    #ifdef    __cplusplus
    extern "C" {
    #endif /* __cplusplus */




    // Example of __DATE__ string: "Jul 27 2012"
    //                              01234567890

    #define BUILD_YEAR_CH0 (__DATE__[ 7])
    #define BUILD_YEAR_CH1 (__DATE__[ 8])
    #define BUILD_YEAR_CH2 (__DATE__[ 9])
    #define BUILD_YEAR_CH3 (__DATE__[10])


    #define BUILD_MONTH_IS_JAN (__DATE__[0] == 'J' && __DATE__[1] == 'a' && __DATE__[2] == 'n')
    #define BUILD_MONTH_IS_FEB (__DATE__[0] == 'F')
    #define BUILD_MONTH_IS_MAR (__DATE__[0] == 'M' && __DATE__[1] == 'a' && __DATE__[2] == 'r')
    #define BUILD_MONTH_IS_APR (__DATE__[0] == 'A' && __DATE__[1] == 'p')
    #define BUILD_MONTH_IS_MAY (__DATE__[0] == 'M' && __DATE__[1] == 'a' && __DATE__[2] == 'y')
    #define BUILD_MONTH_IS_JUN (__DATE__[0] == 'J' && __DATE__[1] == 'u' && __DATE__[2] == 'n')
    #define BUILD_MONTH_IS_JUL (__DATE__[0] == 'J' && __DATE__[1] == 'u' && __DATE__[2] == 'l')
    #define BUILD_MONTH_IS_AUG (__DATE__[0] == 'A' && __DATE__[1] == 'u')
    #define BUILD_MONTH_IS_SEP (__DATE__[0] == 'S')
    #define BUILD_MONTH_IS_OCT (__DATE__[0] == 'O')
    #define BUILD_MONTH_IS_NOV (__DATE__[0] == 'N')
    #define BUILD_MONTH_IS_DEC (__DATE__[0] == 'D')


    #define BUILD_MONTH_CH0 \
        ((BUILD_MONTH_IS_OCT || BUILD_MONTH_IS_NOV || BUILD_MONTH_IS_DEC) ? '1' : '0')

    #define BUILD_MONTH_CH1 \
        ( \
            (BUILD_MONTH_IS_JAN) ? '1' : \
            (BUILD_MONTH_IS_FEB) ? '2' : \
            (BUILD_MONTH_IS_MAR) ? '3' : \
            (BUILD_MONTH_IS_APR) ? '4' : \
            (BUILD_MONTH_IS_MAY) ? '5' : \
            (BUILD_MONTH_IS_JUN) ? '6' : \
            (BUILD_MONTH_IS_JUL) ? '7' : \
            (BUILD_MONTH_IS_AUG) ? '8' : \
            (BUILD_MONTH_IS_SEP) ? '9' : \
            (BUILD_MONTH_IS_OCT) ? '0' : \
            (BUILD_MONTH_IS_NOV) ? '1' : \
            (BUILD_MONTH_IS_DEC) ? '2' : \
            /* error default */    '?' \
        )

    #define BUILD_DAY_CH0 ((__DATE__[4] >= '0') ? (__DATE__[4]) : '0')
    #define BUILD_DAY_CH1 (__DATE__[ 5])



    // Example of __TIME__ string: "21:06:19"
    //                              01234567

    #define BUILD_HOUR_CH0 (__TIME__[0])
    #define BUILD_HOUR_CH1 (__TIME__[1])

    #define BUILD_MIN_CH0 (__TIME__[3])
    #define BUILD_MIN_CH1 (__TIME__[4])

    #define BUILD_SEC_CH0 (__TIME__[6])
    #define BUILD_SEC_CH1 (__TIME__[7])


    // problem lines follow from here

    #if VERSION_MAJOR > 100
        
    #define VERSION_MAJOR_INIT \
        ((VERSION_MAJOR / 100) + '0'), \
        (((VERSION_MAJOR % 100) / 10) + '0'), \
        ((VERSION_MAJOR % 10) + '0')
        
    #elif VERSION_MAJOR > 10

    #define VERSION_MAJOR_INIT \
        ((VERSION_MAJOR / 10) + '0'), \
        ((VERSION_MAJOR % 10) + '0')

    #else

    #define VERSION_MAJOR_INIT \
        (VERSION_MAJOR + '0')

    #endif

    #if VERSION_MINOR > 100

    #define VERSION_MINOR_INIT \
        ((VERSION_MINOR / 100) + '0'), \
        (((VERSION_MINOR % 100) / 10) + '0'), \
        ((VERSION_MINOR % 10) + '0')

    #elif VERSION_MINOR > 10

    #define VERSION_MINOR_INIT \
        ((VERSION_MINOR / 10) + '0'), \
        ((VERSION_MINOR % 10) + '0')

    #else

    #define VERSION_MINOR_INIT \
        (VERSION_MINOR + '0')

    #endif

    */
        
        
        
    #ifdef    __cplusplus
    }
    #endif /* __cplusplus */



    #endif    /* BUILD_DEFS_H */


     
    #8
    rodims
    Super Member
    • Total Posts : 1466
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/16 03:54:33 (permalink)
    +1 (1)
    OK, you tried "something" and "it fails".
    Difficult to guess what your problem is. And to search for some unknown comment in all that code.
    Does this relate to any of the previous advices ?
    #9
    pcbbc
    Super Member
    • Total Posts : 786
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/16 04:25:09 (permalink)
    +2 (2)
    You are still trying to auto increment a version number?  What you have posted won't do that.
    You'll still need to manually edit version_num.h and up VERSION_MAJOR/MINOR, unless you have some script to do that for you.
    In which case, all the macros in build_defs.h seem complete overkill.
    #10
    pcbbc
    Super Member
    • Total Posts : 786
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/16 05:00:20 (permalink)
    0
    Under Windows you can do the following...
     
    Save the following in your project folder as updatever.cmd:
    @ECHO OFF
    SET V = 0
    IF EXIST ".\curvers.cmd" (
        CALL ".\curvers.cmd"
    )
    SET /A V=%V%+1
    ECHO Current Version is %V%
    ECHO #define VERSION %V% > ".\version.h"
    ECHO SET V=%V% > ".\curvers.cmd"

     
    Then under project properties in MPLAB X, select "Building" and:
    a) Check "Execute this line before the build"
    b) Enter "updatever.cmd" as the command to run
     
    Now, every time you run a build the text in version.h will be incremented:
    #define VERSION 3

     
    Justin #include version.h in your source and use as required.
    make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
    make[1]: Entering directory 'C:/Users/SMcConnachie/MPLABXProjects/Timer1.X'
    "--------------------------------------"
    "User defined pre-build step: [UpdateVer.cmd]"
    Current Version is 4
    "--------------------------------------"
    make  -f nbproject/Makefile-default.mk dist/default/production/Timer1.X.production.hex
    make[2]: Entering directory 'C:/Users/SMcConnachie/MPLABXProjects/Timer1.X'
    make[2]: 'dist/default/production/Timer1.X.production.hex' is up to date.
    make[2]: Leaving directory 'C:/Users/SMcConnachie/MPLABXProjects/Timer1.X'
    make[1]: Leaving directory 'C:/Users/SMcConnachie/MPLABXProjects/Timer1.X'

    BUILD SUCCESSFUL (total time: 562ms)
    Loading code from C:/Users/SMcConnachie/MPLABXProjects/Timer1.X/dist/default/production/Timer1.X.production.hex...
    Loading completed

     
    If you're using LINUX or Mac, you're on your own - sorry.
    #11
    swmcl
    Super Member
    • Total Posts : 244
    • Reward points : 0
    • Joined: 2014/05/10 13:54:42
    • Location: Queensland
    • Status: offline
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/16 17:09:58 (permalink)
    0
    Thanks pcbbc, your help is appreciated.
     
    - After post #5, I went away trying to find something using the  __DATE__  and  __TIME__   macros as suggested.
    - Yes I am on Linux.
    - The code I posted was what I found.  I realize it may well be complete overkill but if I could get it to work then I can chop it up as required.  At this stage, it is about the journey to some extent.
    - Yes I did run a file called version.c as seen in post #10 of your previous link.  My problem there is that I don't know how to interpret the results.  (I got a strange feedback but it didn't blow anything up).
     
    The problem in the code I posted is centred around the syntax of using If statements alongside the Define statements.  Just that little bit of code is what is making it fail.  I've tried different things but nothing works as yet.
     
    #12
    pcbbc
    Super Member
    • Total Posts : 786
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: Automatically incrementing a build or software version into my compiled code 2019/04/16 21:51:29 (permalink)
    +2 (2)
    If you get the code to work, it’s not going to give you an autoincrementing build number. Because the build version is hard coded with:
    #define VERSION_MAJOR 1
    #define VERSION_MINOR 4

    Do you want an autoincrementing build number, or are you now just settling for including the build data and time?

    I am sure half an hour learning some shell scripting you could do exactly what I showed with the Windows command file.

    Try this suggestion here:
    https://stackoverflow.com...c-file-from-batch-file
    awk "$2 ~ /VERSION/ {$3++};1" version.h
    #13
    Jump to:
    © 2019 APG vNext Commercial Version 4.5