From a1a05e80ecf3d6a6775e17494a426317b4a19813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 1 Oct 2024 14:59:45 +0200 Subject: Initial beam-cache implementation When switching between normal build and running tests it takes a while for modules to be rebuilt. With the beam-cache the files are saved and can be restored when switching between contexts. This greatly speeds up the context switching. --- CHANGELOG.asciidoc | 4 ++++ build.config | 3 +++ core/beam-cache.mk | 42 ++++++++++++++++++++++++++++++++++++++++++ core/erlc.mk | 2 +- core/test.mk | 7 +++++-- 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 core/beam-cache.mk diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 6a38a21..c6b759b 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -49,3 +49,7 @@ `CACHE_DEPS=1` to enable. 2023/05/16: Remove support for HiPE and ErlLLVM. + +2024/10/01: Initial beam-cache implementation. This is used + to cache beam files when switching from/to test + builds. diff --git a/build.config b/build.config index b363ef0..5ef60f8 100644 --- a/build.config +++ b/build.config @@ -6,11 +6,14 @@ # Core modules. core/core core/kerl + +# Index and dependencies. index/* core/index core/deps # Core modules, continued. +core/beam-cache core/erlc core/docs core/rel diff --git a/core/beam-cache.mk b/core/beam-cache.mk new file mode 100644 index 0000000..d0f1d3c --- /dev/null +++ b/core/beam-cache.mk @@ -0,0 +1,42 @@ +# Copyright (c) 2024, Loïc Hoguin +# This file is part of erlang.mk and subject to the terms of the ISC License. + +.PHONY: beam-cache-restore-app beam-cache-restore-test clean-beam-cache distclean-beam-cache + +BEAM_CACHE_DIR ?= $(ERLANG_MK_TMP)/beam-cache +PROJECT_BEAM_CACHE_DIR = $(BEAM_CACHE_DIR)/$(PROJECT) + +clean:: clean-beam-cache + +clean-beam-cache: + $(verbose) rm -rf $(PROJECT_BEAM_CACHE_DIR) + +distclean:: distclean-beam-cache + +$(PROJECT_BEAM_CACHE_DIR): + $(verbose) mkdir -p $(PROJECT_BEAM_CACHE_DIR) + +distclean-beam-cache: + $(gen_verbose) rm -rf $(BEAM_CACHE_DIR) + +beam-cache-restore-app: | $(PROJECT_BEAM_CACHE_DIR) + $(verbose) rm -rf $(PROJECT_BEAM_CACHE_DIR)/ebin-test +ifneq ($(wildcard ebin/),) + $(verbose) mv ebin/ $(PROJECT_BEAM_CACHE_DIR)/ebin-test +endif +ifneq ($(wildcard $(PROJECT_BEAM_CACHE_DIR)/ebin-app),) + $(gen_verbose) mv $(PROJECT_BEAM_CACHE_DIR)/ebin-app ebin/ +else + $(verbose) $(MAKE) --no-print-directory clean-app +endif + +beam-cache-restore-test: | $(PROJECT_BEAM_CACHE_DIR) + $(verbose) rm -rf $(PROJECT_BEAM_CACHE_DIR)/ebin-app +ifneq ($(wildcard ebin/),) + $(verbose) mv ebin/ $(PROJECT_BEAM_CACHE_DIR)/ebin-app +endif +ifneq ($(wildcard $(PROJECT_BEAM_CACHE_DIR)/ebin-test),) + $(gen_verbose) mv $(PROJECT_BEAM_CACHE_DIR)/ebin-test ebin/ +else + $(verbose) $(MAKE) --no-print-directory clean-app +endif diff --git a/core/erlc.mk b/core/erlc.mk index b8555b7..8f9abc2 100644 --- a/core/erlc.mk +++ b/core/erlc.mk @@ -53,7 +53,7 @@ ifneq ($(wildcard src/),) # Targets. -app:: $(if $(wildcard ebin/test),clean) deps +app:: $(if $(wildcard ebin/test),beam-cache-restore-app) deps $(verbose) $(MAKE) --no-print-directory $(PROJECT).d $(verbose) $(MAKE) --no-print-directory app-build diff --git a/core/test.mk b/core/test.mk index 8054abc..8967fd1 100644 --- a/core/test.mk +++ b/core/test.mk @@ -45,14 +45,17 @@ define compile_test_erl endef ERL_TEST_FILES = $(call core_find,$(TEST_DIR)/,*.erl) + $(ERLANG_MK_TMP)/$(PROJECT).last-testdir-build: $(ERL_TEST_FILES) $(MAKEFILE_LIST) - $(eval FILES_TO_COMPILE := $(if $(filter $(MAKEFILE_LIST),$?),$(filter $(ERL_TEST_FILES),$^),$?)) +# When we have to recompile files in src/ the .d file always gets rebuilt. +# Therefore we want to ignore it when rebuilding test files. + $(eval FILES_TO_COMPILE := $(if $(filter $(filter-out $(PROJECT).d,$(MAKEFILE_LIST)),$?),$(filter $(ERL_TEST_FILES),$^),$(filter $(ERL_TEST_FILES),$?))) $(if $(strip $(FILES_TO_COMPILE)),$(call compile_test_erl,$(FILES_TO_COMPILE)) && touch $@) endif test-build:: IS_TEST=1 test-build:: ERLC_OPTS=$(TEST_ERLC_OPTS) -test-build:: $(if $(wildcard src),$(if $(wildcard ebin/test),,clean)) $(if $(IS_APP),,deps test-deps) +test-build:: $(if $(wildcard src),$(if $(wildcard ebin/test),,beam-cache-restore-test)) $(if $(IS_APP),,deps test-deps) # We already compiled everything when IS_APP=1. ifndef IS_APP ifneq ($(wildcard src),) -- cgit v1.2.3