Browse Source

Support optional applications

Loïc: Added more info to guide.
jdamanalo 2 years ago
parent
commit
7f7db5d1e6
3 changed files with 40 additions and 2 deletions
  1. 4 2
      core/erlc.mk
  2. 12 0
      doc/src/guide/deps.asciidoc
  3. 24 0
      test/core_deps.mk

+ 4 - 2
core/erlc.mk

@@ -65,7 +65,8 @@ define app_file
 	{id$(comma)$(space)"$(1)"}$(comma))
 	{modules, [$(call comma_list,$(2))]},
 	{registered, []},
-	{applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(foreach dep,$(DEPS),$(call dep_name,$(dep))))]},
+	{applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(OPTIONAL_DEPS) $(foreach dep,$(DEPS),$(call dep_name,$(dep))))]},
+	{optional_applications, [$(call comma_list,$(OPTIONAL_DEPS))]},
 	{env, $(subst \,\\,$(PROJECT_ENV))}$(if $(findstring {,$(PROJECT_APP_EXTRA_KEYS)),$(comma)$(newline)$(tab)$(subst \,\\,$(PROJECT_APP_EXTRA_KEYS)),)
 ]}.
 endef
@@ -77,7 +78,8 @@ define app_file
 	{id$(comma)$(space)"$(1)"}$(comma))
 	{modules, [$(call comma_list,$(2))]},
 	{registered, [$(call comma_list,$(PROJECT)_sup $(PROJECT_REGISTERED))]},
-	{applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(foreach dep,$(DEPS),$(call dep_name,$(dep))))]},
+	{applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(OPTIONAL_DEPS) $(foreach dep,$(DEPS),$(call dep_name,$(dep))))]},
+	{optional_applications, [$(call comma_list,$(OPTIONAL_DEPS))]},
 	{mod, {$(PROJECT_MOD), []}},
 	{env, $(subst \,\\,$(PROJECT_ENV))}$(if $(findstring {,$(PROJECT_APP_EXTRA_KEYS)),$(comma)$(newline)$(tab)$(subst \,\\,$(PROJECT_APP_EXTRA_KEYS)),)
 ]}.

+ 12 - 0
doc/src/guide/deps.asciidoc

@@ -132,6 +132,18 @@ target `deps::` before including 'erlang.mk', for example:
 [source,make]
 deps:: $(CURDIR)/deps/triq
 
+Sometimes dependencies are allowed to be missing. However, your application
+may depend on an optional application being started. To ensure that an
+optional dependency is started before your application, the variable
+`OPTIONAL_DEPS` may be used:
+
+[source,make]
+OPTIONAL_DEPS = quicer
+
+The top-level project can then decide whether to include this
+application by adding it to its `BUILD_DEPS` and including
+it in the release dependencies.
+
 ==== Modifying the dependency source or version
 
 By default, Erlang.mk will look into its package index to

+ 24 - 0
test/core_deps.mk

@@ -1124,6 +1124,30 @@ core-deps-mv-rebar: init
 	$i "Build the application"
 	$t $(MAKE) -C $(APP)-moved $v
 
+core-deps-optional: init
+
+	$i "Bootstrap a new OTP application named $(APP)"
+	$t mkdir $(APP)/
+	$t cp ../erlang.mk $(APP)/
+	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
+
+	$i "Add quicer to the list of optional dependencies"
+	$t perl -ni.bak -e 'print;if ($$.==1) {print "OPTIONAL_DEPS = quicer\n"}' $(APP)/Makefile
+
+	$i "Build the application"
+	$t $(MAKE) -C $(APP) $v
+
+	$i "Check that no dependencies were fetched"
+	$t test ! -e $(APP)/deps
+
+	$i "Check that the application was compiled correctly"
+	$t $(ERL) -pa $(APP)/ebin/ -eval " \
+		ok = application:start($(APP)), \
+		{ok, Deps} = application:get_key($(APP), applications), \
+		true = lists:member(quicer, Deps), \
+		{ok, [quicer]} = application:get_key($(APP), optional_applications), \
+		halt()"
+
 # A lower-level dependency of the first dependency always
 # wins over a lower-level dependency of the second dependency.
 core-deps-order-first: init