Praise and Criticism
Having been around for more than 30 years, the Make tool (GNU Make being a modern version) has had plenty of opportunity to gather praise and criticism. Clearly, Make offers many benefits; otherwise, it would no longer be the most popular tool for C/C++ development. On the other hand, plenty of empirical experience has shown that Make has a number of flaws.
Let's now review what users of Make have been saying. The following comments were either found on Internet web sites, published in books, or gathered via personal experience.
Wide support: Make (particularly GNU Make) is widely supported on a large number of operating systems. Most software engineers have at least a passing knowledge of makefile construction, and quite a few people consider themselves to be makefile experts. Part of this widespread knowledge results from universities teaching Make as a standard build tool. The significant number of legacy build systems using Make is also a contributing factor.
If you were starting a new software project, the fact that so many developers are already familiar with Make would likely convince you to use the same tool again. In addition, numerous Make-related tools (such as automatic makefile generators, automatic parallelization tools, and makefile editors) are available either commercially or for free. Make is clearly the best-supported build tool, at least for C/C++ development.
- Very fast tool: Being written in C, GNU Make is fast and highly optimized. Compared to other tools, GNU Make is extremely fast for computing and traversing the dependency graph. As a side note, some people counter this benefit by questioning whether speed is important if accuracy of the tool's dependency information can't be guaranteed.
- Portable syntax: GNU Make has a portable syntax and is available on a wide range of platforms, including Microsoft Windows. Before the introduction of GNU Make, developers were required to write a makefile that was compatible with every operating system's variant of Make. They often were limited to using only the small subset of features that all Make implementations had in common. With GNU Make, the syntax is the same across all platforms, and you can use the entire set of the GNU Make's features.
Fully featured programming language: As a general-purpose dependency engine, Make can be used for any type of dependency analysis. As long as you can write a rule that maps input files to output files, there's no limit on the type of compilation you can perform. Whereas other build tools might focus on C, C++, Java, or C# compilation, Make enables you to compile any type of file (such as creating PDF files from TeX source).
It's worth noting that GNU Make's language is Turing complete. This means that any program written in a general-purpose programming language can also be written as a GNU Make program. It would be incorrect to claim that "GNU Make can't do that" because any feature of any other build tool can be implemented in GNU Make. (Just ask a Make guru how it can be done.)
- The first tool: Being the first build tool ever invented, Make paved the way for automated build systems. Newer tools would never have been able to improve on Make if it hadn't first demonstrated what was possible.
On the flip slide, there are many criticisms of the Make tool to be aware of:
Inconsistent language design: GNU Make's language has clearly grown over time, and the design hasn't always stayed consistent. Some of the language features (such as rules) were part of the original Make design, but many other features were added over time as people found a need. In addition, the syntax for each of the features isn't always consistent with the syntax of other features. This makes the language difficult for new developers to learn.
Some of the common concerns include the following:
- When writing a makefile rule, the shell commands must be indented by a tab character instead of by spaces. This syntactical rule has impacted almost everyone, especially if their editor automatically converts tabs to spaces.
- All makefile variables are global, so it can be challenging to determine where a variable is defined and whether you're conflicting with a different variable that accidentally has the same name.
- Some parts of the makefile syntax ignore whitespace; in other parts, whitespace must be included.
- It can be confusing to determine which parts of a makefile enable you to write shell commands and which parts enable GNU Make functions.
- When invoking shell commands within a Make rule, you need to be familiar with the syntax of each command being invoked. Often a lot of inconsistency arises in the command-line arguments and return values each tool provides.
- For developers familiar with procedural programming (sequencing, loops, conditionals, and function calls), it can be challenging to write GNU Make code. In particular, it's difficult to fit together the necessary shell commands, GNU Make functions, and user-defined macros to achieve the desired effect.
As a result of these syntax and semantic issues, a number of additional tools such as Automake and CMake (see Chapter 9, "CMake") automatically generate a makefile from a higher-level description. This alleviates the need to learn the GNU Make syntax.
No standard framework: Although this chapter discussed using inclusive Make to solve a number of build system problems, no standard framework can be used as a starting point. GNU Make provides a powerful set of language features, but it wasn't designed to work out-of-the-box for large software projects. In particular, the following important features must be implemented by hand:
- Automatic dependency analysis for common languages such as C/C++. Without a good dependency system, the chance of introducing build failures is much higher.
- Multidirectory support with a single dependency graph for the whole build tree.
- C compiler flags that can be set on a per-directory or per-file basis.
- A mechanism for rebuilding object files if the C compiler flags are modified.
- A mechanism for abstracting out values such as SRCS, SUBDIRS, and CFLAGS, as was done with the Files.mk fragments.
Lack of portability: Although GNU Make provides a consistent syntax across all operating systems, it's unlikely that the syntax of the shell commands will be consistent. Each operating system is free to store its standard tools (such as ls, cat, sed, and grep) in whichever directory it likes, and it's free to implement whichever optional tool features it desires. Even with modern versions of UNIX and Linux, some amount of inconsistency always seems to arise between shell commands.
To make things easier, follow a couple good practices:
- Use the standard GNU versions of command-line tools instead of the operating system's own version. This at least guarantees that command options are consistent.
- Use makefile conditionals (such as ifdef SOLARIS) to select an appropriate tool or tool path that works on each operating system, and then use a variable to access the tool instead of hard-coding the name. For example, use $(RM) foo.o instead of rm foo.o.
- Challenging debugging: Many developers find it difficult to follow Make's flow of control when filenames are being pattern-matched against rules. In contrast to most other programming languages, the flow of control isn't sequential. This means that the next rule to be triggered could be defined at any point within the makefile system, including built-in rules and those included in Files.mk fragments. Before the use of the GNU Make debugger (a recent creation), developers were left to interpret confusing errors messages or to scan through complex listings of the dependency graph.
Language completeness versus ease of use: Even though any general-purpose program can be implemented within the GNU Make programming environment, the real question is how much work needs to be done to make that happen. As you've seen already, constructing a complete GNU Make framework isn't trivial and requires the author to have "guru" status. The authors must have a perfect understanding of GNU Make's flow of control, as well as an intimate knowledge of GNU Make's syntax and built-in functions. Finally, they need to have a handful of clever tricks to convince GNU Make to perform certain operations that aren't officially supported.
If you decide to create an inclusive Make framework for yourself, be prepared to devote a large amount of time (months, not weeks). You need to support your development team on an ongoing basis when it requests that new functionality be added. After all this work, you'll end up with a solid build system, but be prepared for average software engineers to not have any understanding of how it works. After all, many people never get beyond the much simpler recursive Make systems, even with all the associated problems.
To summarize the GNU Make tool, let's evaluate it against the build system quality measurements discussed in Chapter 1, "Build System Overview."
- Convenience: Poor. As you've seen, creating a fully functioning build system is difficult. This includes the detection of implicit dependencies and the traversal of a full build tree. Although a simple makefile is quick and easy to construct, the tool is much less convenient for nontrivial build systems.
- Correctness: Poor. Because of the poor level of convenience, GNU Make is notorious for not producing a correct build image. It's possible to guarantee a correct build, although the effort to do so can be enormous.
- Performance: Excellent. GNU Make is written in optimized C code and has an efficient algorithm for dependency analysis. Compared to other build tools discussed in later chapters, GNU Make is extremely fast.
- Scalability: Excellent. As with the performance criteria, GNU Make is highly scalable. The assumption is that you've already created a makefile framework that adequately supports multiple directories.
As a general rule, consider using GNU Make for legacy software that already uses a Make-based build system. However, if you're writing a new build system for C/C++ software, first consider using SCons (Chapter 8) or CMake (Chapter 9). If you're writing a build system for Java, consider using Ant (Chapter 7). For C# code, use MSBuild (discussed briefly in Chapter 7). If none of these tools meets your needs, especially for performance reasons, writing a new build system using GNU Make is a possibility.
Note that these evaluation criteria are subjective in nature, so your value judgment could be quite different.