Skip to content
anemitzPublic

About

A small, top-down build system for C and C++ projects. Keep component Makefiles declarative while one shared Makefile.inc handles compilation, testing, and packaging.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

15 Commits

Folders and files

Repository files navigation

LazyMake

Declare what your C or C++ project builds. Keep how it builds in one shared file.

BINS := hello
hello_SOURCES := Hello.cc

include ../Makefile.inc
component Makefile ──include──▶ Makefile.inc ──▶ build/… + package/…
     project data                  mechanics           artifacts

Run make. You get:

  • Incremental builds that track headers and command changes
  • Executables, libraries, tests, and packaged resources
  • Predictable per-platform artifact directories
  • Standard compiler and linker overrides

No configure step. No generator. Just GNU Make and a compiler.

See what disappears

A two-source executable with header tracking:

- CXXFLAGS += -g -O3 -std=c++17 -pedantic -Wall
- SOURCES := Main.cc Greeter.cc
- OBJECTS := $(SOURCES:%.cc=build/%.o)
- DEPENDS := $(OBJECTS:.o=.d)
-
- app: $(OBJECTS)
-	$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
-
- build/%.o: %.cc
-	@mkdir -p $(@D)
-	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -MMD -MP -c $< -o $@
-
- -include $(DEPENDS)
+ BINS := app
+ app_SOURCES := Main.cc Greeter.cc
+
+ include ../Makefile.inc

The removed mechanics live once in Makefile.inc, not in every component.

Grow by adding declarations

BINS := server
DYNLIBS := libmetrics
STATICLIBS := libprotocol
TESTS := ParserTest SocketTest

server_SOURCES := Main.cc Server.cc
libmetrics_SOURCES := Metrics.cc
libprotocol_SOURCES := Frame.cc Parser.cc

SUBDIRS := tools
CHECKDIRS := functionaltests
RESOURCES := conf

include Makefile.inc
make          production package + local tests
make check    production package + every test

Pick the right tool

Need Use
Tiny or unusual build Hand-written Make
Small, repeatable C/C++ build LazyMake
Source packages that probe varied Unix hosts or features Autoconf and Automake
Package discovery, exports, or IDE generation CMake or Meson
Hermetic builds, remote execution, or shared caching Bazel or similar

Reference

  • Include Makefile.inc last. Its location defines the project root.
  • Targets: BINS, DYNLIBS, STATICLIBS, and TESTS.
  • Composition: SUBDIRS, CHECKDIRS, OPTIONALDIRS, and RESOURCES. OPTIONALDIRS names components that build only on request, such as benchmarks; make and make check skip them.
  • Sources: <target>_SOURCES; defaults to <target>.cc. A source outside the component, such as ../shared/Probe.cc, still gets one object per target.
  • Extensions: .c, .cc, .cpp, and .cxx.
  • Global settings: CC, CXX, CPPFLAGS, CFLAGS, CXXFLAGS, LDFLAGS, and LDLIBS.
  • Per-target settings: <target>_CPPFLAGS, <target>_CFLAGS, <target>_CXXFLAGS, <target>_LDFLAGS, <target>_LDLIBS, and <target>_STATICLIBS.
  • Rebuilds: an output rebuilds when a prerequisite is newer or its compile, archive, or link command changed. Each output records its last command under build/. A variable an output inherits from another target's target-specific assignment is not compared.
  • Compile prerequisites: every object waits for COMPILE_PREREQUISITES, such as the stamp of a dependency build that installs headers, and rebuilds when one is newer. Dependency files omit headers found through -isystem.
  • Groups: TARGET_GROUPS, <group>_TARGETS, and <group>_STATICLIBS. A group applies archive paths to named binaries, shared libraries, or tests as both link inputs and prerequisites. Per-target _STATICLIBS come first, then matching groups in TARGET_GROUPS order. Use = when an archive path contains $(PKGDIR).
TARGET_GROUPS := core
core_TARGETS := app SmokeTest
core_STATICLIBS = $(PKGDIR)/lib/libcore.a
  • Checks: make check finishes all, then runs the component's TESTS in order and each CHECKDIRS entry, and reports every failure. -j overlaps those suites, so run without it when they cannot share a host. Tests within a component stay serial. CHECK_ENV prefixes each test command. check: <goal> runs a goal before the tests. A component with no TESTS or CHECKDIRS may write its own check: recipe.
TESTS := ParserTest
CHECK_ENV = FIXTURES="$(PROJECT_ROOT)/fixtures"
check: fixtures
  • External archives: EXTERNAL_STATICLIBS, <lib>_PATH, and <lib>_DIR. Names are the owning component's STATICLIBS targets. A path that appears in an effective _STATICLIBS list is refreshed by recursing into _DIR for that name, unless this Makefile itself lists the name in STATICLIBS. Use = when _PATH contains $(PKGDIR) or _DIR contains $(PROJECT_ROOT). Parent SUBDIRS still need an explicit producer-before-consumer order for make -j. Concurrent standalone consumer builds that share a stale archive can both recurse into the owner and race on ar; that is out of contract.
EXTERNAL_STATICLIBS := libcore
libcore_PATH = $(PKGDIR)/lib/libcore.a
libcore_DIR = $(PROJECT_ROOT)/lib
  • Compilation database: make compile-db writes compile_commands.json in the component's build directory without compiling. Each object's entry holds the command that builds it. The database merges in every component that SUBDIRS, CHECKDIRS, and OPTIONALDIRS reach and waits for COMPILE_PREREQUISITES. Clang tools reject extra keys, so compile_targets.json gives each entry's output its target and type: BINS, DYNLIBS, STATICLIBS, or TESTS. A listed directory whose Makefile does not include Makefile.inc must define a compile-db rule, which may be empty.
OPTIONALDIRS := benchmarks
COMPILE_PREREQUISITES = $(BUILD_ROOT)/dependencies.stamp
Command Result
make Package plus local tests
make package Production targets, resources, and SUBDIRS
make check Build, then run all tests; -j overlaps suites
make <name> One declared target
make compile-db Compilation database, without compiling
make clean Current component's build state
make distclean Every build and package variant

Separate incompatible artifacts with BUILD_VARIANT:

make BUILD_VARIANT=debug CXXFLAGS=-O0
build/<os>-<architecture>/<variant>/<component>/<target>/
package/<os>-<architecture>/<variant>/{bin,lib}/

Try it

make -C examples/main_deliverable
make -C examples/shared_lib
make -C examples/mixed_sources
make -C examples/test_suite check
make -C examples/target_groups check
make -C examples/external_staticlibs
make -C examples/main_deliverable compile-db

Requires GNU Make 3.81+, a C/C++ toolchain, and basic host utilities. Validate LazyMake with make -C tests host; use make -C tests docker for Ubuntu 24.04 and 26.04 on ARM64 and AMD64.

About

A small, top-down build system for C and C++ projects. Keep component Makefiles declarative while one shared Makefile.inc handles compilation, testing, and packaging.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages