Makefile Generator, version 2

genmake, the Not so Simple Makefile Generator Bash Script, is now hosted on Google Code (for older version, check this post).

The version 2 now supports:

  • executables, static libraries, and dynamic link libraries, based on the output file name format,
  • automatic increment of build number on each build (the header file including the version and build number is automatically generated; requires Awk),
  • several build targets – debug, debug profile, release, release profile,
  • easily specifying additional flags passed to compiler,
  • passing list of link libraries used for build,
  • selectively excluding files from build, and more…

Happy coding! 🙂

Increase build number in C/C++ header (using Awk)

This trick works with most IDE’s that use pre-build step, but easiest I guess is to use it in a Makefile. Feel free to modify the “script” in any way it suites your purposes.

Suppose you have a build number defined as #define BuildNum 1 in BuildNum.h header.

All you have to do is add the following command in your make all section in Makefile ahead of other dependencies (it’s one line, broken here for visibility):

awk '$2 !~ /BuildNum/ {print} $2 ~ /BuildNum/ {print "#define BuildNum "$3+1}'\
    BuildNum.h > BuildNum.h~; mv BuildNum.h~ BuildNum.h

Yes, it’s a clumsy solution, but it does the work just fine… 😀 Sure, you can catch the lines in array, and afterwards print them one by one back to file, etc. … but it’s just not at all pretty bash script (enhancements and suggestions welcome!).

Notice that the command is pretty much dependent on the format of the line the BuildNum is defined on, and that “BuildNum” should not appear anywhere else as a second word on the line!

Simple Makefile generator

Update: For a much more advanced version of this script, see Makefile Generator, version 2 post.

Presented here is a simple bash script for automatic Makefile generating.
This script simply takes all .c, .cpp, .c++ and .cxx files in the current folder, and generates Makefile that builds an executeable from all these sources.
Dependencies are generated using the gcc‘s -MM functionality.

About the script

This script is provided under BSD-style license.

Of course, any comments, reports of use, and suggestions are very much welcome – just leave me a comment! It takes just a moment, and makes me know I didn’t put all the work in posting and updating this for nothing.

Continue reading