Get your updates right, but don’t leave old files behind

During development you often update the deliverables with the latest version of your code. But don’t forget what you removed. Here is how you can do it with Make.

What is Make?

GNU Make is a tool which controls the generation of executables and other non-source files of a program from the program’s source files.

Make gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files. When you write a program, you should write a makefile for it, so that it is possible to use Make to build and install the program. (Source)

With awareness of both how to generate a file (the target) and what that file needs (its dependencies), make can help save valuable time and prevent you from having to rebuild a target whose dependencies did not change since the last build.

Although make is great at saving time, it takes some expertise to manage efficient work during code cleanup phases.

The make clean target

Most projects come with a make clean target providing a developer with the ability to remove any non-source file the make program generated. Even the reference itself instructs how to clean build output:

The whole Makefile might be close to:

It is then possible to run:

It also works when adding targets:

However, when targets are removed (e.g. when you are moving outside of a feature branch adding a program), make won’t be able to clean your working directory properly:

Improving the make clean target

As we noticed, our current implementation of the build is not reproducible, hence some files in the bin are still available but not rebuildable after a git clone.

To solve this issue, the first approach would be to clean the whole repository after each checkout with git clean -dxf . However this can be very inefficient if your build time is high.

Another option is to add a simple script in charge of cleaning previously buildable targets (./cleanup.sh):

integrate it in the makefile:

and test the changes:

Conclusion

Whenever you’re considering performing an update either with make as described here, or with puppet, ansible or other configuration management tools, don’t forget to remove anything redundant to prevent the famous it works for me pattern.

You can find the illustrations of this article hosted on GitHub: https://github.com/leboncoin/bytes-clean-examples

Read more from the Software engineering category
SUBSCRIBE TO OUR UPDATES
Menu