Просмотр исходного кода

Fix error messages appearing when using parse transforms

The errors were happening in the rebar.config generation
code. In addition to the fix, three tests were added:
a project that uses a pt from its own code with -compile
directives, a project that uses a pt from a dep with the
pt set as ERLC_OPTS, and the inclusion of parse transform
options in the generated rebar.config when compatibility
is desired.
Loïc Hoguin 9 лет назад
Родитель
Сommit
c1131f4f74
3 измененных файлов с 146 добавлено и 4 удалено
  1. 5 2
      core/compat.mk
  2. 76 1
      test/core_app.mk
  3. 65 1
      test/core_compat.mk

+ 5 - 2
core/compat.mk

@@ -6,7 +6,9 @@
 # We strip out -Werror because we don't want to fail due to
 # warnings when used as a dependency.
 
-define compat_convert_erlc_opt
+compat_prepare_erlc_opts = $(shell echo "$1" | sed 's/, */,/')
+
+define compat_convert_erlc_opts
 $(if $(filter-out -Werror,$1),\
 	$(if $(findstring +,$1),\
 		$(shell echo $1 | cut -b 2-)))
@@ -15,7 +17,8 @@ endef
 define compat_rebar_config
 {deps, [$(call comma_list,$(foreach d,$(DEPS),\
 	{$(call dep_name,$d),".*",{git,"$(call dep_repo,$d)","$(call dep_commit,$d)"}}))]}.
-{erl_opts, [$(call comma_list,$(foreach o,$(ERLC_OPTS),$(call compat_convert_erlc_opt,$o)))]}.
+{erl_opts, [$(call comma_list,$(foreach o,$(call compat_prepare_erlc_opts,$(ERLC_OPTS)),\
+	$(call compat_convert_erlc_opts,$o)))]}.
 endef
 
 $(eval _compat_rebar_config = $$(compat_rebar_config))

+ 76 - 1
test/core_app.mk

@@ -1,6 +1,6 @@
 # Core: Building applications.
 
-CORE_APP_CASES = asn1 auto-git-id erlc-exclude erlc-opts erlc-opts-filter error generate-erl generate-erl-include generate-erl-prepend hrl hrl-recursive mib no-app no-makedep xrl xrl-include yrl yrl-include
+CORE_APP_CASES = asn1 auto-git-id erlc-exclude erlc-opts erlc-opts-filter error generate-erl generate-erl-include generate-erl-prepend hrl hrl-recursive mib no-app no-makedep pt pt-erlc-opts xrl xrl-include yrl yrl-include
 CORE_APP_TARGETS = $(addprefix core-app-,$(CORE_APP_CASES))
 CORE_APP_CLEAN_TARGETS = $(addprefix clean-,$(CORE_APP_TARGETS))
 
@@ -954,6 +954,81 @@ endif
 		[{module, M} = code:load_file(M) || M <- Mods], \
 		halt()"
 
+core-app-pt: build clean-core-app-pt
+
+	$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 "Generate a parse_transform module"
+	$t printf "%s\n" \
+		"-module(my_pt)." \
+		"-export([parse_transform/2])." \
+		"parse_transform(Forms, _) ->" \
+		"	io:format(\"*** Running my_pt parse_transform.~n\")," \
+		"	Forms." > $(APP)/src/my_pt.erl
+
+	$i "Generate a .erl file that uses the my_pt parse_transform"
+	$t printf "%s\n" \
+		"-module(my_user)." \
+		"-compile({parse_transform, my_pt})." > $(APP)/src/my_user.erl
+
+	$i "Compile my_pt first"
+	$t echo "COMPILE_FIRST += my_pt" >> $(APP)/Makefile
+
+	$i "Build the application; confirm the parse_transform is used"
+	$t $(MAKE) -C $(APP) | grep "*** Running my_pt parse_transform."
+
+	$i "Check that the application was compiled correctly"
+	$t $(ERL) -pa $(APP)/ebin/ -eval " \
+		ok = application:start($(APP)), \
+		{ok, Mods = [my_pt, my_user]} = application:get_key($(APP), modules), \
+		[{module, M} = code:load_file(M) || M <- Mods], \
+		halt()"
+
+core-app-pt-erlc-opts: build clean-core-app-pt-erlc-opts
+
+	$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 "Bootstrap a new OTP library in $(APP)/deps/my_pt_dep"
+	$t mkdir -p $(APP)/deps/my_pt_dep/
+	$t cp ../erlang.mk $(APP)/deps/my_pt_dep/
+	$t $(MAKE) -C $(APP)/deps/my_pt_dep/ -f erlang.mk bootstrap-lib $v
+
+	$i "Generate a parse_transform module in my_pt_dep"
+	$t printf "%s\n" \
+		"-module(my_pt)." \
+		"-export([parse_transform/2])." \
+		"parse_transform(Forms, _) ->" \
+		"	io:format(\"*** Running my_pt parse_transform.~n\")," \
+		"	Forms." > $(APP)/deps/my_pt_dep/src/my_pt.erl
+
+	$i "Add my_pt_dep to the list of dependencies"
+	$t sed -i.bak '2i\
+BUILD_DEPS = my_pt_dep\
+' $(APP)/Makefile
+
+	$i "Generate .erl files"
+	$t echo "-module(boy)." > $(APP)/src/boy.erl
+	$t echo "-module(girl)." > $(APP)/src/girl.erl
+
+	$i "Add the my_pt parse_transform to ERLC_OPTS"
+	$t echo "ERLC_OPTS += +'{parse_transform, my_pt}'" >> $(APP)/Makefile
+
+	$i "Build the application; confirm the parse_transform is used"
+	$t $(MAKE) -C $(APP) | grep "*** Running my_pt parse_transform."
+
+	$i "Check that the application was compiled correctly"
+	$t $(ERL) -pa $(APP)/ebin/ -eval " \
+		ok = application:start($(APP)), \
+		{ok, Mods = [boy, girl]} = application:get_key($(APP), modules), \
+		[{module, M} = code:load_file(M) || M <- Mods], \
+		halt()"
+
 core-app-xrl: build clean-core-app-xrl
 
 	$i "Bootstrap a new OTP library named $(APP)"

+ 65 - 1
test/core_compat.mk

@@ -2,7 +2,7 @@
 #
 # Note: autopatch functionality is covered separately.
 
-CORE_COMPAT_CASES = auto-rebar rebar rebar-deps rebar-deps-pkg rebar-erlc-opts
+CORE_COMPAT_CASES = auto-rebar rebar rebar-deps rebar-deps-pkg rebar-erlc-opts rebar-pt
 CORE_COMPAT_TARGETS = $(addprefix core-compat-,$(CORE_COMPAT_CASES))
 CORE_COMPAT_CLEAN_TARGETS = $(addprefix clean-,$(CORE_COMPAT_TARGETS))
 
@@ -219,3 +219,67 @@ core-compat-rebar-erlc-opts: build clean-core-compat-rebar-erlc-opts
 
 	$i "Use rebar to build the application"
 	$t cd $(APP) && ./rebar compile $v
+
+core-compat-rebar-pt: build clean-core-compat-rebar-pt
+
+	$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 "Generate .erl files"
+	$t echo "-module(boy)." > $(APP)/src/boy.erl
+	$t echo "-module(girl)." > $(APP)/src/girl.erl
+
+	$i "Add lager to the list of dependencies"
+	$t sed -i.bak '2i\
+DEPS = lager\
+' $(APP)/Makefile
+
+	$i "Add the lager_transform parse_transform to ERLC_OPTS"
+	$t echo "ERLC_OPTS += +'{parse_transform, lager_transform}'" >> $(APP)/Makefile
+
+	$i "Build the application"
+	$t $(MAKE) -C $(APP) $v
+
+	$i "Run 'make rebar.config'"
+	$t $(MAKE) -C $(APP) rebar.config $v
+
+	$i "Check that rebar.config was created"
+	$t test -f $(APP)/rebar.config
+
+	$i "Check that the parse_transform option is listed in rebar.config"
+	$t $(ERL) -eval " \
+		{ok, C} = file:consult(\"$(APP)/rebar.config\"), \
+		{_, Opts} = lists:keyfind(erl_opts, 1, C), \
+		true = lists:member({parse_transform, lager_transform}, Opts), \
+		halt()"
+
+# For the new build method, we have to simulate keeping the .app file
+# inside the repository, by leaving it in the ebin/ directory before
+# calling Rebar.
+ifndef LEGACY
+	$i "Move the .app file outside ebin/"
+	$t mv $(APP)/ebin/$(APP).app $(APP)/
+endif
+
+	$i "Distclean the application"
+	$t $(MAKE) -C $(APP) distclean $v
+
+ifndef LEGACY
+	$i "Put the .app file back into ebin/"
+	$t mkdir $(APP)/ebin/
+	$t mv $(APP)/$(APP).app $(APP)/ebin/
+endif
+
+	$i "Download rebar"
+	$t curl -s -L -o $(APP)/rebar $(REBAR_BINARY)
+	$t chmod +x $(APP)/rebar
+
+	$i "Use rebar to build the application"
+	$t cd $(APP) && ./rebar get-deps compile $v
+
+	$i "Check that all compiled files exist"
+	$t test -f $(APP)/ebin/$(APP).app
+	$t test -f $(APP)/ebin/boy.beam
+	$t test -f $(APP)/ebin/girl.beam