Browse Source

Allow hooking before/after autopatch

Loïc Hoguin 6 years ago
parent
commit
672f9310b5
3 changed files with 74 additions and 7 deletions
  1. 7 2
      core/deps.mk
  2. 28 5
      doc/src/guide/deps.asciidoc
  3. 39 0
      test/core_autopatch.mk

+ 7 - 2
core/deps.mk

@@ -659,6 +659,12 @@ $(DEPS_DIR)/$(call dep_name,$1):
 		cd $(DEPS_DIR)/$(DEP_NAME) && ./configure; \
 	fi
 ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
+	$(verbose) $$(MAKE) --no-print-directory autopatch-$(DEP_NAME)
+endif
+
+.PHONY: autopatch-$(call dep_name,$1)
+
+autopatch-$(call dep_name,$1)::
 	$(verbose) if [ "$(1)" = "amqp_client" -a "$(RABBITMQ_CLIENT_PATCH)" ]; then \
 		if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \
 			echo " PATCH  Downloading rabbitmq-codegen"; \
@@ -677,9 +683,8 @@ ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
 	elif [ "$1" = "elixir" -a "$(ELIXIR_PATCH)" ]; then \
 		ln -s lib/elixir/ebin $(DEPS_DIR)/elixir/; \
 	else \
-		$$(call dep_autopatch,$(DEP_NAME)) \
+		$$(call dep_autopatch,$(call dep_name,$1)) \
 	fi
-endif
 endef
 
 $(foreach dep,$(BUILD_DEPS) $(DEPS),$(eval $(call dep_target,$(dep))))

+ 28 - 5
doc/src/guide/deps.asciidoc

@@ -502,11 +502,10 @@ performed:
 * Run autopatch on the project
 
 Autopatch first checks if there is any project-specific patch
-enabled. There are currently two: `RABBITMQ_CLIENT_PATCH` for
-the `amqp_client` dependency, and `RABBITMQ_SERVER_PATCH` for
-the `rabbit` dependency. These are needed only for RabbitMQ
-versions before 3.6.0 (assuming you are using upstream RabbitMQ,
-and not a fork).
+enabled. There are currently three: `RABBITMQ_CLIENT_PATCH` for
+the `amqp_client` dependency (before 3.6.0), `RABBITMQ_SERVER_PATCH`
+for the `rabbit` dependency (before 3.6.0) and `ELIXIR_PATCH`
+for the `elixir` dependency.
 
 Otherwise, autopatch performs different operations depending
 on the kind of project it finds the dependency to be.
@@ -529,6 +528,30 @@ empty Makefile generated, for compatibility purposes.
 
 * Other projects with no Makefile are left untouched.
 
+You can add additional commands to be run immediately before
+or after autopatch is done by extending the target
+`autopatch-$(dep)::`, for example this would remove
+a module:
+
+[source,make]
+----
+autopatch-ranch::
+	rm -f $(DEPS_DIR)/ranch/src/ranch_proxy_header.erl
+----
+
+A common use case for this feature is to apply a PATCH
+file on the dependency immediately after fetching it.
+It can also be used to add compiler options, for example:
+
+[source,make]
+----
+autopatch-couchbeam::
+	printf "\nERLC_OPTS += -DWITH_JIFFY\n" >> $(DEPS_DIR)/couchbeam/Makefile
+----
+
+The commands will run before autopatch when the target is
+defined before including 'erlang.mk', and after otherwise.
+
 You can disable the replacing of the 'erlang.mk' file by
 defining the `NO_AUTOPATCH_ERLANG_MK` variable:
 

+ 39 - 0
test/core_autopatch.mk

@@ -6,6 +6,45 @@ CORE_AUTOPATCH_TARGETS = $(call list_targets,core-autopatch)
 
 core-autopatch: $(CORE_AUTOPATCH_TARGETS)
 
+core-autopatch-extended: build clean
+
+	$i "Bootstrap a new OTP library named $(APP)"
+	$t mkdir $(APP)/
+	$t cp ../erlang.mk $(APP)/
+	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
+
+	$i "Add Ranch to the list of dependencies"
+	$t perl -ni.bak -e 'print;if ($$.==1) {print "DEPS = ranch\n"}' $(APP)/Makefile
+
+	$i "Extend autopatch-ranch to create an additional module"
+	$t echo "autopatch-ranch:: ; rm -f \$$(DEPS_DIR)/ranch/src/ranch_protocol.erl" >> $(APP)/Makefile
+
+	$i "Build the application"
+	$t $(MAKE) -C $(APP) $v
+
+	$i "Check that the module was removed"
+	$t ! test -e $(APP)/deps/ranch/src/ranch_protocol.erl
+	$t ! test -e $(APP)/deps/ranch/ebin/ranch_protocol.beam
+
+core-autopatch-extended-erlc-opts: build clean
+
+	$i "Bootstrap a new OTP library named $(APP)"
+	$t mkdir $(APP)/
+	$t cp ../erlang.mk $(APP)/
+	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
+
+	$i "Add couchbeam to the list of dependencies"
+	$t perl -ni.bak -e 'print;if ($$.==1) {print "DEPS = couchbeam\n"}' $(APP)/Makefile
+
+	$i "Extend autopatch-couchbeam to add options to its ERLC_OPTS"
+	$t echo "autopatch-couchbeam:: ; printf '\nERLC_OPTS += -DWITH_JIFFY\n' >> \$$(DEPS_DIR)/couchbeam/Makefile" >> $(APP)/Makefile
+
+	$i "Build the application"
+	$t $(MAKE) -C $(APP) $v
+
+	$i "Check that couchbeam_ejson was compiled with the added option"
+	$t $(ERL) -pa $(APP)/deps/couchbeam/ebin -eval 'c:m(couchbeam_ejson), halt()' | grep -c "WITH_JIFFY" | grep -q 1
+
 core-autopatch-no-autopatch: build clean
 
 	$i "Bootstrap a new OTP library named $(APP)"