phord / GenericMakefile

A generic makefile with version information support for use with C++ projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Auto Versioning Makefile

This repository contains a generic makefile for use with small/medium C and C++ projects. This makefile project is forked from https://github.com/mbcrawfo/GenericMakefile but majorly addapted in the meantime. It allows for easy project setup without the need to create tedious build rules or dependency lists. In the test directory is a makefile configured to create an example executable which references the static library generated by the makefile in the root directory.

Features:

  • Automatically finds and compiles all source files within the source directory.
  • Compiles most recently modified files first, to hopefully find problems earlier.
  • Automatically generates dependecies as files are compiled, ensuring that files are correctly recompiled when dependecies have updated.
  • Includes configurations for normal (release) build and debug build suitable for GDB debugging (the debug is the standard build that is called with a 'make' command.)
  • Times the compilation of each file and the entire build.
  • Generates version numbers based on git or subversion tags (see below), which are passed the compiler as preprocessor macros.
  • Implements and includes a header file with a static PrintVersion function (see example in sources).
  • By default, builds in a "verbose" mode which allows you to see the full compiler commands being issued. By passing V=false to make, you can compile with only an output for the actions being performed.
  • Automatically added Build Date and Build Number Symbols which can be extracted from the generated lib or executable with the 'getVersion.sh' script included in this repository. Examples follow below.
  • Added stacked compilation support to enable a simple project build on multiple cores by make -f all [option]. There are multiple options available like clean, newd (= new debug) and newr (= new release).
  • Makefile now supports out of the box executables, static (.a) and shared (.so) libraries. All you need to change is the TYPE flag to one of the specified NAMES (TARGET_EXECUTABLE, TARGET_STATIC_LIB or TARGET_SHARED_LIB).
  • Added IGNORE_SOURCE flag. With IGNORE_SOURCE="old" each file, that contains old will be ignored.
  • SYMBOLIC_LINK_DIR is now optional. Just specify a directory for a symlink to be build in. If nothing is specified, no symlink will be created. Make sure to also specify the LIB_SYMLINK_DELTA flag, which has to contain the path from the SYMBOLIC_LINK_DIR to the project root (otherwise the created links won't work).
  • The DIST_INFO Variable can now be used e.g. for the SYMBOLIC_LINK_DIR. It contains information about the distribution and the current OS-Type (32 or 64 bit).
  • Supported make flags are:
    • clean - removes all compiled or generated data
    • release - builds an optimized release build of the project
    • debug - builds an gdb debuggable build of this project
    • all - builds both, debug and release
    • reset - resets build number
    • help - shows this overview

Versioning:

Tags should be made in the format "vMAJOR.MINOR.PATCH[-description]", where MAJOR, MINOR and PATCH are numeric. The following macros will be generated and passed to the preprocessor:

  • VERSION_MAJOR (int) - The major version number from the most recent tag.
  • VERSION_MINOR (int) - The minor version number from the most recent tag.
  • VERSION_PATCH (int) - The patch version number from the most recent tag.
  • VERSION_REVISION (int) - The number of commits since the most recent tag.
  • BUILD_NUMBER (int) - The build number, which is automatically increasing every time the project is linked.
  • BUILD_YEAR (int) - The year of the build date.
  • BUILD_MONTH (int) - The month of the build date.
  • BUILD_DAY (int) - The day of the build date.
  • BUILD_HOUR (int) - The hour of the build date.
  • BUILD_MIN (int) - The minute of the build date.
  • BUILD_SEC (int) - The second of the build date.

Examples:

How to add a versioned tag:

git tag -a v1.0.1 -m "Your tag message"

How to use the getVersion.sh

# to list all version information of all linked libraries included in libtest.a.
./getVersion.sh libtest.a
# lists only the version information of libtest.a itself.
./getVersion.sh libtest.a libtest.a

How to use the linked references in code

# Find all references:
# The file extension .a of static libraries is removed in the reference symbols.
objdump -t libtest.a | grep REF

# For usage of the additional linked REF symboles see test/src/main.cpp
extern char libtest_BUILD_NUMBER_REF;
std::cout << "Version: v" << (unsigned long) &libtest_BUILD_NUMBER_REF;

How to use the generated jversion.hpp header

# first of all you need to activate the option PRINTVERSION_HEADER := true in makefile.
____________
// Source code
#include "jversion.hpp"

void yourFunction()
{
    // Use the $(NAME) flag you defined in the makefile.
    JVersion::Name::PrintVersion();
}

How to use the different make flags

make clean
make clean debug
make clean reset debug
make clean reset release
make clean reset all
make install
make uninstall
...

If the makefile is not used in a git repository, or is in a repository with no tags, the version macros are not created.

Limitations:

  • Assumes GNU make.
  • Doesn't really support multiple types of source files in the same project.
  • Currently no full support for svn.
  • Only tested and adapted for Linux system and g++ and ar toolchain.

Thanks to mbcrawfo and his contributors:

  • My friend Jay and people on Stack Overflow for help with regex to parse the version info.
  • The residents of /r/programming for suggesting numerous tweaks and improvements.
  • For the support and help from Sebastian with this project and a lot of good improvement ideas.

About

A generic makefile with version information support for use with C++ projects

License:MIT License


Languages

Language:Makefile 89.6%Language:Shell 9.0%Language:C++ 1.4%