Skip to content

Instantly share code, notes, and snippets.

@johnfosborneiii
Created October 16, 2023 11:50
Show Gist options
  • Save johnfosborneiii/b277a14df1894c04b91e581533151fa4 to your computer and use it in GitHub Desktop.
Save johnfosborneiii/b277a14df1894c04b91e581533151fa4 to your computer and use it in GitHub Desktop.
GNU Makefile Convections Cheat Sheet

GNU Makefile Conventions Cheat Sheet (Based on Section 7.2 of GNU Coding Standards)

General Structure

Standard Targets for Users

  • all: Compile the entire program.
  • install: Compile and copy executables, libraries, etc., to their installation directories.
  • install-html, install-dvi, install-pdf, install-ps: Alternative installations for documentation.
  • uninstall: Delete all installed files.
  • clean: Delete files created during compilation.
  • distclean: Delete all files not in the source distribution.
  • mostlyclean: Like clean, but keep files you may want to keep around.
  • maintainer-clean: Delete almost everything that can be rebuilt.
  • TAGS: Update all tags files.
  • check or test: Run test suites.
  • installcheck: Check the installed programs or libraries.
  • dist: Create a source distribution archive.

Targets for Developers

  • stage1, stage2, etc.: Partially build the system, useful for bootstrapping or rebuilding.
  • info, dvi, pdf, ps, html: Create documentation in alternative formats.

Variables

Commonly Used Variables

  • CFLAGS: Compilation flags for C code.
  • CXXFLAGS: Compilation flags for C++ code.
  • LDFLAGS: Flags for linking.
  • CPPFLAGS: Flags for the C preprocessor.

Variables for Installation Directories

  • prefix: Prefix for installation directories, usually /usr/local.
  • exec_prefix: Prefix for executable-specific files, derived from prefix.
  • bindir: Location for executables, typically ${exec_prefix}/bin.
  • sbindir: Location for system executables, typically ${exec_prefix}/sbin.
  • libexecdir: Location for program executables not meant for user interaction, typically ${exec_prefix}/libexec.
  • datarootdir: Location for read-only data, typically ${prefix}/share.
  • datadir: Location for data files that can be shared among different architecture platforms, typically ${datarootdir}.
  • sysconfdir: Location for configuration files, typically ${prefix}/etc.
  • sharedstatedir: Location for data that can be shared by different versions of a program, usually ${prefix}/com.
  • localstatedir: Location for data that changes as the program runs, usually ${prefix}/var.
  • includedir: Header file directory, typically ${prefix}/include.
  • oldincludedir: Directory for storing old header files, typically /usr/include.
  • docdir: Location for documentation files, typically ${datarootdir}/doc/${PACKAGE}.
  • infodir, htmldir, dvidir, pdfdir, psdir: Specialized directories for different types of documentation, typically under ${datarootdir}.

DESTDIR: Support for Staged Installs

  • DESTDIR: Use this variable for staged installs, which allows you to install files into a temporary location (DESTDIR) rather than their final location.
    • Commonly used in package building.
    • Example: make install DESTDIR=/tmp/package-root
    • Note: DESTDIR is prepended to the file paths but should not change the code's behavior.

Automatic Dependency Tracking

  • Use -MMD -MP options with GCC to generate dependency files.
  • Include dependencies in the Makefile: -include $(DEPS).

Conditionals

ifeq ($(CC),gcc)
CFLAGS += -Wall
endif

Phony Targets

Use .PHONY to declare phony targets, ensuring they always execute.

.PHONY: all install uninstall clean distclean mostlyclean maintainer-clean TAGS check test installcheck dist info dvi pdf ps html

Command Echoing and Error Handling

Suppress command echoing with @:

@echo "Building..."

Continue despite errors in certain rules with -:

-rm some-file

Variable Assignment Types

  • =: Recursive expansion. Value is expanded each time the variable is used.

    VAR = value
  • :=: Simple expansion. Value is expanded at the time of assignment.

    VAR := value
  • ?=: Conditional variable assignment. Assigns value only if the variable is not yet defined.

    VAR ?= value
  • +=: Append to the variable value. Adds the given value to the existing variable value.

    VAR += value

Implicit Rules

  • Define implicit rules for transforming file types.
    %.o: %.c
        $(CC) $(CFLAGS) -c $< -o $@
  • Implicit rules can simplify your Makefile by applying general rules for file transformations.

String and File Operations

  • Use functions for string and file manipulations.

    • $(wildcard ...): Gets a list of filenames matching the pattern.

      SOURCES := $(wildcard *.c)
    • $(patsubst ...): Pattern-based string substitution.

      OBJECTS := $(patsubst %.c,%.o,$(SOURCES))
    • $(subst ...): Replace text within a string.

      NEW_TEXT := $(subst old,new,$(TEXT))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment