about summary refs log tree commit diff
path: root/third_party/nix/mk
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-05-17T14·52+0100
committerVincent Ambo <tazjin@google.com>2020-05-17T14·52+0100
commit7994fd1d545cc5c876d6f21db7ddf9185d23dad6 (patch)
tree32dd695785378c5b9c8be97fc583e9dfc62cb105 /third_party/nix/mk
parentcf8cd640c1adf74a3706efbcb0ea4625da106fb2 (diff)
parent90b3b31dc27f31e9b11653a636025d29ddb087a3 (diff)
Add 'third_party/nix/' from commit 'be66c7a6b24e3c3c6157fd37b86c7203d14acf10' r/724
git-subtree-dir: third_party/nix
git-subtree-mainline: cf8cd640c1adf74a3706efbcb0ea4625da106fb2
git-subtree-split: be66c7a6b24e3c3c6157fd37b86c7203d14acf10
Diffstat (limited to 'third_party/nix/mk')
-rw-r--r--third_party/nix/mk/clean.mk11
-rw-r--r--third_party/nix/mk/dist.mk17
-rw-r--r--third_party/nix/mk/functions.mk14
-rw-r--r--third_party/nix/mk/install.mk62
-rw-r--r--third_party/nix/mk/jars.mk36
-rw-r--r--third_party/nix/mk/lib.mk160
-rw-r--r--third_party/nix/mk/libraries.mk162
-rw-r--r--third_party/nix/mk/patterns.mk11
-rw-r--r--third_party/nix/mk/programs.mk79
-rw-r--r--third_party/nix/mk/templates.mk19
-rw-r--r--third_party/nix/mk/tests.mk45
-rw-r--r--third_party/nix/mk/tracing.mk17
12 files changed, 633 insertions, 0 deletions
diff --git a/third_party/nix/mk/clean.mk b/third_party/nix/mk/clean.mk
new file mode 100644
index 000000000000..ce9afb3b0db7
--- /dev/null
+++ b/third_party/nix/mk/clean.mk
@@ -0,0 +1,11 @@
+clean-files :=
+
+clean:
+	$(suppress) rm -fv -- $(clean-files)
+
+dryclean:
+	@for i in $(clean-files); do if [ -e $$i ]; then echo $$i; fi; done | sort
+
+print-top-help += \
+  echo "  clean: Delete generated files"; \
+  echo "  dryclean: Show what files would be deleted by 'make clean'";
diff --git a/third_party/nix/mk/dist.mk b/third_party/nix/mk/dist.mk
new file mode 100644
index 000000000000..794b277713d4
--- /dev/null
+++ b/third_party/nix/mk/dist.mk
@@ -0,0 +1,17 @@
+ifdef PACKAGE_NAME
+
+dist-name = $(PACKAGE_NAME)-$(PACKAGE_VERSION)
+
+dist: $(dist-name).tar.bz2 $(dist-name).tar.xz
+
+$(dist-name).tar.bz2: $(dist-files)
+	$(trace-gen) tar cfj $@ $(sort $(dist-files)) --transform 's,^,$(dist-name)/,'
+
+$(dist-name).tar.xz: $(dist-files)
+	$(trace-gen) tar cfJ $@ $(sort $(dist-files)) --transform 's,^,$(dist-name)/,'
+
+clean-files += $(dist-name).tar.bz2 $(dist-name).tar.xz
+
+print-top-help += echo "  dist: Generate a source distribution";
+
+endif
diff --git a/third_party/nix/mk/functions.mk b/third_party/nix/mk/functions.mk
new file mode 100644
index 000000000000..c48775db8c3b
--- /dev/null
+++ b/third_party/nix/mk/functions.mk
@@ -0,0 +1,14 @@
+# Utility function for recursively finding files, e.g.
+# ‘$(call rwildcard, path/to/dir, *.c *.h)’.
+rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
+
+# Given a file name, produce the corresponding dependency file
+# (e.g. ‘foo/bar.o’ becomes ‘foo/.bar.o.dep’).
+filename-to-dep = $(dir $1).$(notdir $1).dep
+
+# Return the full path to a program by looking it up in $PATH, or the
+# empty string if not found.
+find-program = $(shell for i in $$(IFS=: ; echo $$PATH); do p=$$i/$(strip $1); if [ -e $$p ]; then echo $$p; break; fi; done)
+
+# Ensure that the given string ends in a single slash.
+add-trailing-slash = $(patsubst %/,%,$(1))/
diff --git a/third_party/nix/mk/install.mk b/third_party/nix/mk/install.mk
new file mode 100644
index 000000000000..dad0fd8533ab
--- /dev/null
+++ b/third_party/nix/mk/install.mk
@@ -0,0 +1,62 @@
+# Add a rule for creating $(1) as a directory.  This template may be
+# called multiple times for the same directory.
+define create-dir
+   _i := $$(call add-trailing-slash, $(DESTDIR)$$(strip $(1)))
+  ifndef $$(_i)_SEEN
+    $$(_i)_SEEN = 1
+    $$(_i):
+	$$(trace-mkdir) install -d "$$@"
+  endif
+endef
+
+
+# Add a rule for installing file $(1) as file $(2) with mode $(3).
+# The directory containing $(2) will be created automatically.
+define install-file-as
+
+  _i := $(DESTDIR)$$(strip $(2))
+
+  install: $$(_i)
+
+  $$(_i): $(1) | $$(dir $$(_i))
+	$$(trace-install) install -m $(3) $(1) "$$@"
+
+  $$(eval $$(call create-dir, $$(dir $(2))))
+
+endef
+
+
+# Add a rule for installing file $(1) in directory $(2) with mode
+# $(3).  The directory will be created automatically.
+define install-file-in
+  $$(eval $$(call install-file-as,$(1),$(2)/$$(notdir $(1)),$(3)))
+endef
+
+
+define install-program-in
+  $$(eval $$(call install-file-in,$(1),$(2),0755))
+endef
+
+
+define install-data-in
+  $$(eval $$(call install-file-in,$(1),$(2),0644))
+endef
+
+
+# Install a symlink from $(2) to $(1).  Note that $(1) need not exist.
+define install-symlink
+
+  _i := $(DESTDIR)$$(strip $(2))
+
+  install: $$(_i)
+
+  $$(_i): | $$(dir $$(_i))
+	$$(trace-install) ln -sfn $(1) "$$@"
+
+  $$(eval $$(call create-dir, $$(dir $(2))))
+
+endef
+
+
+print-top-help += \
+  echo "  install: Install into \$$(prefix) (currently set to '$(prefix)')";
diff --git a/third_party/nix/mk/jars.mk b/third_party/nix/mk/jars.mk
new file mode 100644
index 000000000000..c8513e664ed5
--- /dev/null
+++ b/third_party/nix/mk/jars.mk
@@ -0,0 +1,36 @@
+define build-jar
+
+  $(1)_NAME ?= $(1)
+
+  _d := $$(strip $$($(1)_DIR))
+
+  $(1)_PATH := $$(_d)/$$($(1)_NAME).jar
+
+  $(1)_TMPDIR := $$(_d)/.$$($(1)_NAME).jar.tmp
+
+  _jars := $$(foreach jar, $$($(1)_JARS), $$($$(jar)_PATH))
+
+  $$($(1)_PATH): $$($(1)_SOURCES) $$(_jars) $$($(1)_EXTRA_DEPS)| $$($(1)_ORDER_AFTER)
+	@rm -rf $$($(1)_TMPDIR)
+	@mkdir -p $$($(1)_TMPDIR)
+	$$(trace-javac) javac $(GLOBAL_JAVACFLAGS) $$($(1)_JAVACFLAGS) -d $$($(1)_TMPDIR) \
+	  $$(foreach fn, $$($(1)_SOURCES), '$$(fn)') \
+	  -cp "$$(subst $$(space),,$$(foreach jar,$$($(1)_JARS),$$($$(jar)_PATH):))$$$$CLASSPATH"
+	@echo -e '$$(subst $$(newline),\n,$$($(1)_MANIFEST))' > $$($(1)_PATH).manifest
+	$$(trace-jar) jar cfm $$($(1)_PATH) $$($(1)_PATH).manifest -C $$($(1)_TMPDIR) .
+	@rm $$($(1)_PATH).manifest
+	@rm -rf $$($(1)_TMPDIR)
+
+  $(1)_INSTALL_DIR ?= $$(jardir)
+
+  $(1)_INSTALL_PATH := $$($(1)_INSTALL_DIR)/$$($(1)_NAME).jar
+
+  $$(eval $$(call install-file-as, $$($(1)_PATH), $$($(1)_INSTALL_PATH), 0644))
+
+  install: $$($(1)_INSTALL_PATH)
+
+  jars-list += $$($(1)_PATH)
+
+  clean-files += $$($(1)_PATH)
+
+endef
diff --git a/third_party/nix/mk/lib.mk b/third_party/nix/mk/lib.mk
new file mode 100644
index 000000000000..1da51d879734
--- /dev/null
+++ b/third_party/nix/mk/lib.mk
@@ -0,0 +1,160 @@
+default: all
+
+
+# Get rid of default suffixes. FIXME: is this a good idea?
+.SUFFIXES:
+
+
+# Initialise some variables.
+bin-scripts :=
+noinst-scripts :=
+man-pages :=
+install-tests :=
+dist-files :=
+OS = $(shell uname -s)
+
+
+# Hack to define a literal space.
+space :=
+space +=
+
+
+# Hack to define a literal newline.
+define newline
+
+
+endef
+
+
+# Default installation paths.
+prefix ?= /usr/local
+libdir ?= $(prefix)/lib
+bindir ?= $(prefix)/bin
+libexecdir ?= $(prefix)/libexec
+datadir ?= $(prefix)/share
+jardir ?= $(datadir)/java
+localstatedir ?= $(prefix)/var
+sysconfdir ?= $(prefix)/etc
+mandir ?= $(prefix)/share/man
+
+
+# Initialise support for build directories.
+builddir ?=
+
+ifdef builddir
+  buildprefix = $(builddir)/
+else
+  buildprefix =
+endif
+
+
+# Pass -fPIC if we're building dynamic libraries.
+BUILD_SHARED_LIBS ?= 1
+
+ifeq ($(BUILD_SHARED_LIBS), 1)
+  ifeq (CYGWIN,$(findstring CYGWIN,$(OS)))
+    GLOBAL_CFLAGS += -U__STRICT_ANSI__ -D_GNU_SOURCE
+    GLOBAL_CXXFLAGS += -U__STRICT_ANSI__ -D_GNU_SOURCE
+  else
+    GLOBAL_CFLAGS += -fPIC
+    GLOBAL_CXXFLAGS += -fPIC
+  endif
+  ifneq ($(OS), Darwin)
+   ifneq ($(OS), SunOS)
+    ifneq ($(OS), FreeBSD)
+     GLOBAL_LDFLAGS += -Wl,--no-copy-dt-needed-entries
+    endif
+   endif
+  endif
+  SET_RPATH_TO_LIBS ?= 1
+endif
+
+# Pass -g if we want debug info.
+BUILD_DEBUG ?= 1
+
+ifeq ($(BUILD_DEBUG), 1)
+  GLOBAL_CFLAGS += -g
+  GLOBAL_CXXFLAGS += -g
+  GLOBAL_JAVACFLAGS += -g
+endif
+
+
+include mk/functions.mk
+include mk/tracing.mk
+include mk/clean.mk
+include mk/install.mk
+include mk/libraries.mk
+include mk/programs.mk
+include mk/jars.mk
+include mk/patterns.mk
+include mk/templates.mk
+include mk/tests.mk
+
+
+# Include all sub-Makefiles.
+define include-sub-makefile
+  d := $$(patsubst %/,%,$$(dir $(1)))
+  include $(1)
+endef
+
+$(foreach mf, $(makefiles), $(eval $(call include-sub-makefile, $(mf))))
+
+
+# Instantiate stuff.
+$(foreach lib, $(libraries), $(eval $(call build-library,$(lib))))
+$(foreach prog, $(programs), $(eval $(call build-program,$(prog))))
+$(foreach jar, $(jars), $(eval $(call build-jar,$(jar))))
+$(foreach script, $(bin-scripts), $(eval $(call install-program-in,$(script),$(bindir))))
+$(foreach script, $(bin-scripts), $(eval programs-list += $(script)))
+$(foreach script, $(noinst-scripts), $(eval programs-list += $(script)))
+$(foreach template, $(template-files), $(eval $(call instantiate-template,$(template))))
+$(foreach test, $(install-tests), $(eval $(call run-install-test,$(test))))
+$(foreach file, $(man-pages), $(eval $(call install-data-in, $(file), $(mandir)/man$(patsubst .%,%,$(suffix $(file))))))
+
+
+include mk/dist.mk
+
+
+.PHONY: default all man help
+
+all: $(programs-list) $(libs-list) $(jars-list) $(man-pages)
+
+man: $(man-pages)
+
+
+help:
+	@echo "The following targets are available:"
+	@echo ""
+	@echo "  default: Build default targets"
+ifdef man-pages
+	@echo "  man: Generate manual pages"
+endif
+	@$(print-top-help)
+ifdef programs-list
+	@echo ""
+	@echo "The following programs can be built:"
+	@echo ""
+	@for i in $(programs-list); do echo "  $$i"; done
+endif
+ifdef libs-list
+	@echo ""
+	@echo "The following libraries can be built:"
+	@echo ""
+	@for i in $(libs-list); do echo "  $$i"; done
+endif
+ifdef jars-list
+	@echo ""
+	@echo "The following JARs can be built:"
+	@echo ""
+	@for i in $(jars-list); do echo "  $$i"; done
+endif
+	@echo ""
+	@echo "The following variables control the build:"
+	@echo ""
+	@echo "  BUILD_SHARED_LIBS ($(BUILD_SHARED_LIBS)): Whether to build shared libraries"
+	@echo "  BUILD_DEBUG ($(BUILD_DEBUG)): Whether to include debug symbols"
+	@echo "  CC ($(CC)): C compiler to be used"
+	@echo "  CFLAGS: Flags for the C compiler"
+	@echo "  CXX ($(CXX)): C++ compiler to be used"
+	@echo "  CXXFLAGS: Flags for the C++ compiler"
+	@$(print-var-help)
diff --git a/third_party/nix/mk/libraries.mk b/third_party/nix/mk/libraries.mk
new file mode 100644
index 000000000000..307e29b9d05b
--- /dev/null
+++ b/third_party/nix/mk/libraries.mk
@@ -0,0 +1,162 @@
+libs-list :=
+
+ifeq ($(OS), Darwin)
+  SO_EXT = dylib
+else
+  ifeq (CYGWIN,$(findstring CYGWIN,$(OS)))
+    SO_EXT = dll
+  else
+    SO_EXT = so
+  endif
+endif
+
+# Build a library with symbolic name $(1).  The library is defined by
+# various variables prefixed by ‘$(1)_’:
+#
+# - $(1)_NAME: the name of the library (e.g. ‘libfoo’); defaults to
+#   $(1).
+#
+# - $(1)_DIR: the directory where the (non-installed) library will be
+#   placed.
+#
+# - $(1)_SOURCES: the source files of the library.
+#
+# - $(1)_CFLAGS: additional C compiler flags.
+#
+# - $(1)_CXXFLAGS: additional C++ compiler flags.
+#
+# - $(1)_ORDER_AFTER: a set of targets on which the object files of
+#   this libraries will have an order-only dependency.
+#
+# - $(1)_LIBS: the symbolic names of other libraries on which this
+#   library depends.
+#
+# - $(1)_ALLOW_UNDEFINED: if set, the library is allowed to have
+#   undefined symbols.  Has no effect for static libraries.
+#
+# - $(1)_LDFLAGS: additional linker flags.
+#
+# - $(1)_LDFLAGS_PROPAGATED: additional linker flags, also propagated
+#   to the linking of programs/libraries that use this library.
+#
+# - $(1)_FORCE_INSTALL: if defined, the library will be installed even
+#   if it's not needed (i.e. dynamically linked) by a program.
+#
+# - $(1)_INSTALL_DIR: the directory where the library will be
+#   installed.  Defaults to $(libdir).
+#
+# - $(1)_EXCLUDE_FROM_LIBRARY_LIST: if defined, the library will not
+#   be automatically marked as a dependency of the top-level all
+#   target andwill not be listed in the make help output. This is
+#   useful for libraries built solely for testing, for example.
+#
+# - BUILD_SHARED_LIBS: if equal to ‘1’, a dynamic library will be
+#   built, otherwise a static library.
+define build-library
+  $(1)_NAME ?= $(1)
+  _d := $(buildprefix)$$(strip $$($(1)_DIR))
+  _srcs := $$(sort $$(foreach src, $$($(1)_SOURCES), $$(src)))
+  $(1)_OBJS := $$(addprefix $(buildprefix), $$(addsuffix .o, $$(basename $$(_srcs))))
+  _libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_PATH))
+
+  ifeq (CYGWIN,$(findstring CYGWIN,$(OS)))
+    $(1)_INSTALL_DIR ?= $$(bindir)
+  else
+    $(1)_INSTALL_DIR ?= $$(libdir)
+  endif
+
+  $(1)_LDFLAGS_USE :=
+  $(1)_LDFLAGS_USE_INSTALLED :=
+
+  $$(eval $$(call create-dir, $$(_d)))
+
+  ifeq ($(BUILD_SHARED_LIBS), 1)
+
+    ifdef $(1)_ALLOW_UNDEFINED
+      ifeq ($(OS), Darwin)
+        $(1)_LDFLAGS += -undefined suppress -flat_namespace
+      endif
+    else
+      ifneq ($(OS), Darwin)
+        ifneq (CYGWIN,$(findstring CYGWIN,$(OS)))
+          $(1)_LDFLAGS += -Wl,-z,defs
+        endif
+      endif
+    endif
+
+    ifneq ($(OS), Darwin)
+      $(1)_LDFLAGS += -Wl,-soname=$$($(1)_NAME).$(SO_EXT)
+    endif
+
+    $(1)_PATH := $$(_d)/$$($(1)_NAME).$(SO_EXT)
+
+    $$($(1)_PATH): $$($(1)_OBJS) $$(_libs) | $$(_d)/
+	$$(trace-ld) $(CXX) -o $$(abspath $$@) -shared $$(LDFLAGS) $$(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$($(1)_LDFLAGS_PROPAGATED) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE)) $$($(1)_LDFLAGS_UNINSTALLED)
+
+    ifneq ($(OS), Darwin)
+      $(1)_LDFLAGS_USE += -Wl,-rpath,$$(abspath $$(_d))
+    endif
+    $(1)_LDFLAGS_USE += -L$$(_d) -l$$(patsubst lib%,%,$$(strip $$($(1)_NAME)))
+
+    $(1)_INSTALL_PATH := $(DESTDIR)$$($(1)_INSTALL_DIR)/$$($(1)_NAME).$(SO_EXT)
+
+    _libs_final := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_INSTALL_PATH))
+
+    $$(eval $$(call create-dir, $$($(1)_INSTALL_DIR)))
+
+    $$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final) | $(DESTDIR)$$($(1)_INSTALL_DIR)/
+	$$(trace-ld) $(CXX) -o $$@ -shared $$(LDFLAGS) $$(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$($(1)_LDFLAGS_PROPAGATED) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE_INSTALLED))
+
+    $(1)_LDFLAGS_USE_INSTALLED += -L$$(DESTDIR)$$($(1)_INSTALL_DIR) -l$$(patsubst lib%,%,$$(strip $$($(1)_NAME)))
+    ifneq ($(OS), Darwin)
+      ifeq ($(SET_RPATH_TO_LIBS), 1)
+        $(1)_LDFLAGS_USE_INSTALLED += -Wl,-rpath,$$($(1)_INSTALL_DIR)
+      else
+        $(1)_LDFLAGS_USE_INSTALLED += -Wl,-rpath-link,$$($(1)_INSTALL_DIR)
+      endif
+    endif
+
+    ifdef $(1)_FORCE_INSTALL
+      install: $$($(1)_INSTALL_PATH)
+    endif
+
+  else
+
+    $(1)_PATH := $$(_d)/$$($(1)_NAME).a
+
+    $$($(1)_PATH): $$($(1)_OBJS) | $$(_d)/
+	$(trace-ar) $(AR) crs $$@ $$?
+
+    $(1)_LDFLAGS_USE += $$($(1)_PATH) $$($(1)_LDFLAGS)
+
+    $(1)_INSTALL_PATH := $$(libdir)/$$($(1)_NAME).a
+
+  endif
+
+  $(1)_LDFLAGS_USE += $$($(1)_LDFLAGS_PROPAGATED)
+  $(1)_LDFLAGS_USE_INSTALLED += $$($(1)_LDFLAGS_PROPAGATED)
+
+  # Propagate CFLAGS and CXXFLAGS to the individual object files.
+  $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj)_CFLAGS=$$($(1)_CFLAGS)))
+  $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj)_CXXFLAGS=$$($(1)_CXXFLAGS)))
+
+  # Make each object file depend on the common dependencies.
+  $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj): $$($(1)_COMMON_DEPS) $$(GLOBAL_COMMON_DEPS)))
+
+  # Make each object file have order-only dependencies on the common
+  # order-only dependencies. This includes the order-only dependencies
+  # of libraries we're depending on.
+  $(1)_ORDER_AFTER_CLOSED = $$($(1)_ORDER_AFTER) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_ORDER_AFTER_CLOSED))
+
+  $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj): | $$($(1)_ORDER_AFTER_CLOSED) $$(GLOBAL_ORDER_AFTER)))
+
+  # Include .dep files, if they exist.
+  $(1)_DEPS := $$(foreach fn, $$($(1)_OBJS), $$(call filename-to-dep, $$(fn)))
+  -include $$($(1)_DEPS)
+
+  ifndef $(1)_EXCLUDE_FROM_LIBRARY_LIST
+  libs-list += $$($(1)_PATH)
+  endif
+  clean-files += $$(_d)/*.a $$(_d)/*.$(SO_EXT) $$(_d)/*.o $$(_d)/.*.dep $$($(1)_DEPS) $$($(1)_OBJS)
+  dist-files += $$(_srcs)
+endef
diff --git a/third_party/nix/mk/patterns.mk b/third_party/nix/mk/patterns.mk
new file mode 100644
index 000000000000..3219d9629fbf
--- /dev/null
+++ b/third_party/nix/mk/patterns.mk
@@ -0,0 +1,11 @@
+$(buildprefix)%.o: %.cc
+	@mkdir -p "$(dir $@)"
+	$(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(GLOBAL_CXXFLAGS_PCH) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP
+
+$(buildprefix)%.o: %.cpp
+	@mkdir -p "$(dir $@)"
+	$(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(GLOBAL_CXXFLAGS_PCH) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP
+
+$(buildprefix)%.o: %.c
+	@mkdir -p "$(dir $@)"
+	$(trace-cc) $(CC) -o $@ -c $< $(GLOBAL_CFLAGS) $(CFLAGS) $($@_CFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP
diff --git a/third_party/nix/mk/programs.mk b/third_party/nix/mk/programs.mk
new file mode 100644
index 000000000000..d93df4468086
--- /dev/null
+++ b/third_party/nix/mk/programs.mk
@@ -0,0 +1,79 @@
+programs-list :=
+
+# Build a program with symbolic name $(1).  The program is defined by
+# various variables prefixed by ‘$(1)_’:
+#
+# - $(1)_DIR: the directory where the (non-installed) program will be
+#   placed.
+#
+# - $(1)_SOURCES: the source files of the program.
+#
+# - $(1)_CFLAGS: additional C compiler flags.
+#
+# - $(1)_CXXFLAGS: additional C++ compiler flags.
+#
+# - $(1)_ORDER_AFTER: a set of targets on which the object files of
+#   this program will have an order-only dependency.
+#
+# - $(1)_LIBS: the symbolic names of libraries on which this program
+#   depends.
+#
+# - $(1)_LDFLAGS: additional linker flags.
+#
+# - $(1)_INSTALL_DIR: the directory where the program will be
+#   installed; defaults to $(bindir).
+define build-program
+  _d := $(buildprefix)$$($(1)_DIR)
+  _srcs := $$(sort $$(foreach src, $$($(1)_SOURCES), $$(src)))
+  $(1)_OBJS := $$(addprefix $(buildprefix), $$(addsuffix .o, $$(basename $$(_srcs))))
+  _libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_PATH))
+  $(1)_PATH := $$(_d)/$(1)
+
+  $$(eval $$(call create-dir, $$(_d)))
+
+  $$($(1)_PATH): $$($(1)_OBJS) $$(_libs) | $$(_d)/
+	$$(trace-ld) $(CXX) -o $$@ $$(LDFLAGS) $$(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE))
+
+  $(1)_INSTALL_DIR ?= $$(bindir)
+  $(1)_INSTALL_PATH := $$($(1)_INSTALL_DIR)/$(1)
+
+  $$(eval $$(call create-dir, $$($(1)_INSTALL_DIR)))
+
+  install: $(DESTDIR)$$($(1)_INSTALL_PATH)
+
+  ifeq ($(BUILD_SHARED_LIBS), 1)
+
+    _libs_final := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_INSTALL_PATH))
+
+    $(DESTDIR)$$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final) | $(DESTDIR)$$($(1)_INSTALL_DIR)/
+	$$(trace-ld) $(CXX) -o $$@ $$(LDFLAGS) $$(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE_INSTALLED))
+
+  else
+
+    $(DESTDIR)$$($(1)_INSTALL_PATH): $$($(1)_PATH) | $(DESTDIR)$$($(1)_INSTALL_DIR)/
+	install -t $(DESTDIR)$$($(1)_INSTALL_DIR) $$<
+
+  endif
+
+  # Propagate CFLAGS and CXXFLAGS to the individual object files.
+  $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj)_CFLAGS=$$($(1)_CFLAGS)))
+  $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj)_CXXFLAGS=$$($(1)_CXXFLAGS)))
+
+  # Make each object file depend on the common dependencies.
+  $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj): $$($(1)_COMMON_DEPS) $$(GLOBAL_COMMON_DEPS)))
+
+  # Make each object file have order-only dependencies on the common
+  # order-only dependencies. This includes the order-only dependencies
+  # of libraries we're depending on.
+  $(1)_ORDER_AFTER_CLOSED = $$($(1)_ORDER_AFTER) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_ORDER_AFTER_CLOSED))
+
+  $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj): | $$($(1)_ORDER_AFTER_CLOSED) $$(GLOBAL_ORDER_AFTER)))
+
+  # Include .dep files, if they exist.
+  $(1)_DEPS := $$(foreach fn, $$($(1)_OBJS), $$(call filename-to-dep, $$(fn)))
+  -include $$($(1)_DEPS)
+
+  programs-list += $$($(1)_PATH)
+  clean-files += $$($(1)_PATH) $$(_d)/*.o $$(_d)/.*.dep $$($(1)_DEPS) $$($(1)_OBJS)
+  dist-files += $$(_srcs)
+endef
diff --git a/third_party/nix/mk/templates.mk b/third_party/nix/mk/templates.mk
new file mode 100644
index 000000000000..c7ac7afbff27
--- /dev/null
+++ b/third_party/nix/mk/templates.mk
@@ -0,0 +1,19 @@
+template-files :=
+
+# Create the file $(1) from $(1).in by running config.status (which
+# substitutes all ‘@var@’ variables set by the configure script).
+define instantiate-template
+
+  clean-files += $(1)
+
+endef
+
+ifneq ($(MAKECMDGOALS), clean)
+
+%.h: %.h.in
+	$(trace-gen) rm -f $@ && ./config.status --quiet --header=$@
+
+%: %.in
+	$(trace-gen) rm -f $@ && ./config.status --quiet --file=$@
+
+endif
diff --git a/third_party/nix/mk/tests.mk b/third_party/nix/mk/tests.mk
new file mode 100644
index 000000000000..70c30661b95f
--- /dev/null
+++ b/third_party/nix/mk/tests.mk
@@ -0,0 +1,45 @@
+# Run program $1 as part of ‘make installcheck’.
+define run-install-test
+
+  installcheck: $1
+
+  _installcheck-list += $1
+
+endef
+
+# Color code from https://unix.stackexchange.com/a/10065
+installcheck:
+	@total=0; failed=0; \
+	red=""; \
+	green=""; \
+	yellow=""; \
+	normal=""; \
+	if [ -t 1 ]; then \
+		red=""; \
+		green=""; \
+		yellow=""; \
+		normal=""; \
+	fi; \
+	for i in $(_installcheck-list); do \
+	  total=$$((total + 1)); \
+	  printf "running test $$i..."; \
+	  log="$$(cd $$(dirname $$i) && $(tests-environment) $$(basename $$i) 2>&1)"; \
+	  status=$$?; \
+	  if [ $$status -eq 0 ]; then \
+	    echo " [$${green}PASS$$normal]"; \
+	  elif [ $$status -eq 99 ]; then \
+	    echo " [$${yellow}SKIP$$normal]"; \
+	  else \
+	    echo " [$${red}FAIL$$normal]"; \
+	    echo "$$log" | sed 's/^/    /'; \
+	    failed=$$((failed + 1)); \
+	  fi; \
+	done; \
+	if [ "$$failed" != 0 ]; then \
+	  echo "$${red}$$failed out of $$total tests failed $$normal"; \
+	  exit 1; \
+	else \
+		echo "$${green}All tests succeeded$$normal"; \
+	fi
+
+.PHONY: check installcheck
diff --git a/third_party/nix/mk/tracing.mk b/third_party/nix/mk/tracing.mk
new file mode 100644
index 000000000000..13912d3c7821
--- /dev/null
+++ b/third_party/nix/mk/tracing.mk
@@ -0,0 +1,17 @@
+V ?= 0
+
+ifeq ($(V), 0)
+
+  trace-gen     = @echo "  GEN   " $@;
+  trace-cc      = @echo "  CC    " $@;
+  trace-cxx     = @echo "  CXX   " $@;
+  trace-ld      = @echo "  LD    " $@;
+  trace-ar      = @echo "  AR    " $@;
+  trace-install = @echo "  INST  " $@;
+  trace-javac   = @echo "  JAVAC " $@;
+  trace-jar     = @echo "  JAR   " $@;
+  trace-mkdir   = @echo "  MKDIR " $@;
+
+  suppress  = @
+
+endif