Browse Source

Reduce dependency on external programs

This commit implements a core_find and core_ls function that
can be used to list files recursively or not.

A few other minute changes are included and a couple hacks
removed.
Loïc Hoguin 9 years ago
parent
commit
6a3bf18655
10 changed files with 59 additions and 57 deletions
  1. 8 3
      core/core.mk
  2. 12 13
      core/erlc.mk
  3. 2 2
      core/test.mk
  4. 1 1
      plugins/c_src.mk
  5. 1 1
      plugins/ct.mk
  6. 1 1
      plugins/erlydtl.mk
  7. 27 31
      plugins/eunit.mk
  8. 1 1
      plugins/protobuffs.mk
  9. 3 4
      plugins/triq.mk
  10. 3 0
      test/Makefile

+ 8 - 3
core/core.mk

@@ -126,13 +126,13 @@ define newline
 
 endef
 
-define erlang_list
-[$(subst $(space),$(comma),$(strip $(1)))]
+define comma_list
+$(subst $(space),$(comma),$(strip $(1)))
 endef
 
 # Adding erlang.mk to make Erlang scripts who call init:get_plain_arguments() happy.
 define erlang
-$(ERL) -pa $(ERLANG_MK_TMP)/rebar/ebin -eval "$(subst $(newline),,$(subst ",\",$(1)))" -- erlang.mk
+$(ERL) -pz $(ERLANG_MK_TMP)/rebar/ebin -eval "$(subst $(newline),,$(subst ",\",$(1)))" -- erlang.mk
 endef
 
 ifeq ($(shell which wget 2>/dev/null | wc -l), 1)
@@ -162,8 +162,13 @@ endif
 
 core_eq = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))
 
+core_find = $(foreach d,$(call core_ls,$1*),$(call core_find,$d/,$2) $(filter $(subst *,%,$2),$d))
+
 core_lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$(1)))))))))))))))))))))))))))
 
+# @todo On Windows: $(shell dir /B $(1)); make sure to handle when no file exists.
+core_ls = $(filter-out $(1),$(shell echo $(1)))
+
 # Automated update.
 
 ERLANG_MK_BUILD_CONFIG ?= build.config

+ 12 - 13
core/erlc.mk

@@ -51,9 +51,9 @@ define app_file
 	{description, "$(PROJECT_DESCRIPTION)"},
 	{vsn, "$(PROJECT_VERSION)"},
 	{id, "$(1)"},
-	{modules, [$(2)]},
+	{modules, [$(call comma_list,$(2))]},
 	{registered, []},
-	{applications, $(call erlang_list,kernel stdlib $(OTP_DEPS) $(DEPS))}
+	{applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(DEPS))]}
 ]}.
 endef
 else
@@ -62,9 +62,9 @@ define app_file
 	{description, "$(PROJECT_DESCRIPTION)"},
 	{vsn, "$(PROJECT_VERSION)"},
 	{id, "$(1)"},
-	{modules, [$(2)]},
-	{registered, $(call erlang_list,$(PROJECT)_sup $(PROJECT_REGISTERED))},
-	{applications, $(call erlang_list,kernel stdlib $(OTP_DEPS) $(DEPS))},
+	{modules, [$(call comma_list,$(2))]},
+	{registered, [$(call comma_list,$(PROJECT)_sup $(PROJECT_REGISTERED))]},
+	{applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(DEPS))]},
 	{mod, {$(PROJECT)_app, []}}
 ]}.
 endef
@@ -72,8 +72,7 @@ endif
 
 app-build: erlc-include ebin/$(PROJECT).app
 	$(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
-	$(eval MODULES := $(shell find ebin -type f -name \*.beam \
-		| sed "s/ebin\//'/;s/\.beam/',/" | sed '$$s/.$$//'))
+	$(eval MODULES := $(patsubst %,'%',$(sort $(notdir $(basename $(wildcard ebin/*.beam))))))
 ifeq ($(wildcard src/$(PROJECT).app.src),)
 	$(app_verbose) echo $(subst $(newline),,$(subst ",\",$(call app_file,$(GITDESCRIBE),$(MODULES)))) \
 		> ebin/$(PROJECT).app
@@ -83,7 +82,7 @@ else
 		exit 1; \
 	fi
 	$(appsrc_verbose) cat src/$(PROJECT).app.src \
-		| sed "s/{[[:space:]]*modules[[:space:]]*,[[:space:]]*\[\]}/{modules, \[$(MODULES)\]}/" \
+		| sed "s/{[[:space:]]*modules[[:space:]]*,[[:space:]]*\[\]}/{modules, \[$(call comma_list,$(MODULES))\]}/" \
 		| sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(GITDESCRIBE)\"}/" \
 		> ebin/$(PROJECT).app
 endif
@@ -123,21 +122,21 @@ ebin/$(PROJECT).app::
 	@mkdir -p ebin/
 
 ifneq ($(wildcard asn1/),)
-ebin/$(PROJECT).app:: $(shell find asn1 -type f -name \*.asn1)
+ebin/$(PROJECT).app:: $(sort $(call core_find,asn1/,*.asn1))
 	@mkdir -p include
 	$(if $(strip $?),$(call compile_asn1,$?))
 endif
 
 ifneq ($(wildcard mibs/),)
-ebin/$(PROJECT).app:: $(shell find mibs -type f -name \*.mib)
+ebin/$(PROJECT).app:: $(sort $(call core_find,mibs/,*.mib))
 	@mkdir -p priv/mibs/ include
 	$(if $(strip $?),$(call compile_mib,$?))
 endif
 
-ebin/$(PROJECT).app:: $(shell find src -type f -name \*.erl -o -name \*.core)
+ebin/$(PROJECT).app:: $(sort $(call core_find,src/,*.erl *.core))
 	$(if $(strip $?),$(call compile_erl,$?))
 
-ebin/$(PROJECT).app:: $(shell find src -type f -name \*.xrl -o -name \*.yrl)
+ebin/$(PROJECT).app:: $(sort $(call core_find,src/,*.xrl *.yrl))
 	$(if $(strip $?),$(call compile_xyrl,$?))
 endif
 
@@ -145,4 +144,4 @@ clean:: clean-app
 
 clean-app:
 	$(gen_verbose) rm -rf ebin/ priv/mibs/ \
-		$(addprefix include/,$(addsuffix .hrl,$(notdir $(basename $(wildcard mibs/*.mib)))))
+		$(addprefix include/,$(addsuffix .hrl,$(notdir $(basename $(call core_find,mibs/,*.mib)))))

+ 2 - 2
core/test.mk

@@ -23,10 +23,10 @@ test-deps: $(ALL_TEST_DEPS_DIRS)
 	@for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
 endif
 
-ifneq ($(strip $(TEST_DIR)),)
+ifneq ($(wildcard $(TEST_DIR)),)
 test-dir:
 	$(gen_verbose) erlc -v $(TEST_ERLC_OPTS) -I include/ -o $(TEST_DIR) \
-		$(wildcard $(TEST_DIR)/*.erl $(TEST_DIR)/*/*.erl) -pa ebin/
+		$(call core_find,$(TEST_DIR)/,*.erl) -pa ebin/
 endif
 
 ifeq ($(wildcard ebin/test),)

+ 1 - 1
plugins/c_src.mk

@@ -64,7 +64,7 @@ clean::
 else
 
 ifeq ($(SOURCES),)
-SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \))
+SOURCES := $(sort $(call core_find,$(C_SRC_DIR)/,*.c *.C *.cc *.cpp))
 endif
 OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))
 

+ 1 - 1
plugins/ct.mk

@@ -7,7 +7,7 @@
 
 CT_OPTS ?=
 ifneq ($(wildcard $(TEST_DIR)),)
-	CT_SUITES ?= $(sort $(subst _SUITE.erl,,$(shell find $(TEST_DIR) -type f -name \*_SUITE.erl -exec basename {} \;)))
+	CT_SUITES ?= $(sort $(subst _SUITE.erl,,$(notdir $(call core_find,$(TEST_DIR)/,*_SUITE.erl))))
 else
 	CT_SUITES ?=
 endif

+ 1 - 1
plugins/erlydtl.mk

@@ -26,6 +26,6 @@ define compile_erlydtl
 endef
 
 ifneq ($(wildcard src/),)
-ebin/$(PROJECT).app:: $(shell find templates -type f -name \*.dtl 2>/dev/null)
+ebin/$(PROJECT).app:: $(sort $(call core_find,templates/,*.dtl))
 	$(if $(strip $?),$(call compile_erlydtl,$?))
 endif

+ 27 - 31
plugins/eunit.mk

@@ -6,28 +6,8 @@
 
 # Configuration
 
-# All modules in TEST_DIR
-ifeq ($(strip $(TEST_DIR)),)
-TEST_DIR_MODS = 
-else
-TEST_DIR_MODS = $(notdir $(basename $(shell find $(TEST_DIR) -type f -name *.beam)))
-endif
-
-# All modules in 'ebin'
-EUNIT_EBIN_MODS = $(notdir $(basename $(shell find ebin -type f -name *.beam)))
-# Only those modules in TEST_DIR with no matching module in 'ebin'.
-# This is done to avoid some tests being executed twice.
-EUNIT_MODS = $(filter-out $(patsubst %,%_tests,$(EUNIT_EBIN_MODS)),$(TEST_DIR_MODS))
-TAGGED_EUNIT_TESTS = $(foreach mod,$(EUNIT_EBIN_MODS) $(EUNIT_MODS),{module,$(mod)})
-
 EUNIT_OPTS ?=
 
-# Utility functions
-
-define str-join
-	$(shell echo '$(strip $(1))' | sed -e "s/ /,/g")
-endef
-
 # Core targets.
 
 tests:: eunit
@@ -39,16 +19,32 @@ help::
 
 # Plugin-specific targets.
 
-EUNIT_RUN_BEFORE ?=
-EUNIT_RUN_AFTER ?=
-EUNIT_RUN = $(ERL) \
-	-pa $(TEST_DIR) $(DEPS_DIR)/*/ebin \
-	-pz ebin \
-	$(EUNIT_RUN_BEFORE) \
-	-eval 'case eunit:test([$(call str-join,$(TAGGED_EUNIT_TESTS))],\
-		[$(EUNIT_OPTS)]) of ok -> ok; error -> halt(1) end.' \
-	$(EUNIT_RUN_AFTER) \
-	-eval 'halt(0).'
+define eunit.erl
+	case "$(COVER)" of
+		"" -> ok;
+		_ ->
+			case cover:compile_beam_directory("ebin") of
+				{error, _} -> halt(1);
+				_ -> ok
+			end
+	end,
+	case eunit:test([$(call comma_list,$(1))], [$(EUNIT_OPTS)]) of
+		ok -> ok;
+		error -> halt(2)
+	end,
+	case "$(COVER)" of
+		"" -> ok;
+		_ ->
+			cover:export("eunit.coverdata")
+	end,
+	halt()
+endef
+
+EUNIT_EBIN_MODS = $(notdir $(basename $(call core_find,ebin/,*.beam)))
+EUNIT_TEST_MODS = $(notdir $(basename $(call core_find,$(TEST_DIR)/,*.beam)))
+EUNIT_MODS = $(foreach mod,$(EUNIT_EBIN_MODS) $(filter-out \
+	$(patsubst %,%_tests,$(EUNIT_EBIN_MODS)),$(EUNIT_TEST_MODS)),{module,'$(mod)'})
 
 eunit: test-build
-	$(gen_verbose) $(EUNIT_RUN)
+	$(gen_verbose) $(ERL) -pa $(TEST_DIR) $(DEPS_DIR)/*/ebin ebin \
+		-eval "$(subst $(newline),,$(subst ",\",$(call eunit.erl,$(EUNIT_MODS))))"

+ 1 - 1
plugins/protobuffs.mk

@@ -26,6 +26,6 @@ define compile_proto.erl
 endef
 
 ifneq ($(wildcard src/),)
-ebin/$(PROJECT).app:: $(shell find src -type f -name \*.proto 2>/dev/null)
+ebin/$(PROJECT).app:: $(sort $(call core_find,src/,*.proto))
 	$(if $(strip $?),$(call compile_proto,$?))
 endif

+ 3 - 4
plugins/triq.mk

@@ -12,7 +12,7 @@ define triq_check.erl
 	code:add_pathsa(["$(CURDIR)/ebin", "$(DEPS_DIR)/*/ebin"]),
 	try
 		case $(1) of
-			all -> [true] =:= lists:usort([triq:check(M) || M <- [$(MODULES)]]);
+			all -> [true] =:= lists:usort([triq:check(M) || M <- [$(call comma_list,$(3))]]);
 			module -> triq:check($(2));
 			function -> triq:check($(2))
 		end
@@ -36,8 +36,7 @@ triq: test-build
 endif
 else
 triq: test-build
-	$(eval MODULES := $(shell find ebin -type f -name \*.beam \
-		| sed "s/ebin\//'/;s/\.beam/',/" | sed '$$s/.$$//'))
-	$(gen_verbose) $(call erlang,$(call triq_check.erl,all,undefined))
+	$(eval MODULES := $(patsubst %,'%',$(sort $(notdir $(basename $(wildcard ebin/*.beam))))))
+	$(gen_verbose) $(call erlang,$(call triq_check.erl,all,undefined,$(MODULES)))
 endif
 endif

+ 3 - 0
test/Makefile

@@ -108,6 +108,7 @@ eunit: app1
 	$i "eunit: Testing the 'eunit' target."
 	$i "Running eunit test case inside module src/t.erl"
 	$t $(call create-module-t)
+	$t $(MAKE) -C app1 distclean $v
 	$t $(MAKE) -C app1 eunit $v
 	$i "Checking that the eunit test in module t."
 	$t echo t | cmp app1/test-eunit.log -
@@ -128,6 +129,7 @@ eunit: app1
 		'	?assertEqual(2, t:succ(1)),' \
 		'	os:cmd("echo x_tests >> test-eunit.log").' \
 		> app1/eunit/x_tests.erl
+	$t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
 	$t $(MAKE) -C app1 eunit TEST_DIR=eunit $v
 	$i "Checking that '$(MAKE) eunit' didn't run the tests in t_tests twice, etc."
 	$t printf "%s\n" t t_tests x_tests | cmp app1/test-eunit.log -
@@ -140,6 +142,7 @@ eunit: app1
 		"succ_test() ->" \
 		"	?assertEqual(42, t:succ(1))." \
 		> app1/eunit/t_tests.erl
+	$t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
 	$t if $(MAKE) -C app1 eunit TEST_DIR=eunit $v ; then false ; fi
 	$t rm -rf app1/eunit app1/src/t.erl app1/test-eunit.log
 	$i "Test 'eunit' passed."