Browse Source

Import rebar_utils for improved compatibility

This allows us to skip some of the worst hacks and improve
compatibility since we are using the original code.
Loïc Hoguin 10 years ago
parent
commit
0cdac548f4
3 changed files with 58 additions and 28 deletions
  1. 6 1
      core/core.mk
  2. 46 26
      core/deps.mk
  3. 6 1
      test/Makefile

+ 6 - 1
core/core.mk

@@ -28,6 +28,11 @@ V ?= 0
 gen_verbose_0 = @echo " GEN   " $@;
 gen_verbose_0 = @echo " GEN   " $@;
 gen_verbose = $(gen_verbose_$(V))
 gen_verbose = $(gen_verbose_$(V))
 
 
+# Temporary files directory.
+
+ERLANG_MK_TMP ?= $(CURDIR)/.erlang.mk
+export ERLANG_MK_TMP
+
 # "erl" command.
 # "erl" command.
 
 
 ERL = erl +A0 -noinput -boot start_clean
 ERL = erl +A0 -noinput -boot start_clean
@@ -119,7 +124,7 @@ endef
 
 
 # Adding erlang.mk to make Erlang scripts who call init:get_plain_arguments() happy.
 # Adding erlang.mk to make Erlang scripts who call init:get_plain_arguments() happy.
 define erlang
 define erlang
-$(ERL) -eval "$(subst $(newline),,$(subst ",\",$(1)))" -- erlang.mk
+$(ERL) -pa $(ERLANG_MK_TMP)/ebin -eval "$(subst $(newline),,$(subst ",\",$(1)))" -- erlang.mk
 endef
 endef
 
 
 ifeq ($(shell which wget 2>/dev/null | wc -l), 1)
 ifeq ($(shell which wget 2>/dev/null | wc -l), 1)

+ 46 - 26
core/deps.mk

@@ -78,10 +78,8 @@ define dep_autopatch
 endef
 endef
 
 
 define dep_autopatch2
 define dep_autopatch2
-	if [ -f $(DEPS_DIR)/$(1)/rebar.config.script ]; then \
-		$(call dep_autopatch_rebar_script,$(1)); \
-		$(call dep_autopatch_rebar,$(1)); \
-	elif [ -f $(DEPS_DIR)/$(1)/rebar.config ]; then \
+	if [ -f $(DEPS_DIR)/$(1)/rebar.config -o -f $(DEPS_DIR)/$(1)/rebar.config.script ]; then \
+		$(call dep_autopatch_rebar_utils); \
 		$(call dep_autopatch_rebar,$(1)); \
 		$(call dep_autopatch_rebar,$(1)); \
 	else \
 	else \
 		$(call dep_autopatch_gen,$(1)); \
 		$(call dep_autopatch_gen,$(1)); \
@@ -112,10 +110,17 @@ define dep_autopatch_gen
 	$(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
 	$(call erlang,$(call dep_autopatch_appsrc.erl,$(1)))
 endef
 endef
 
 
-define dep_autopatch_rebar_script
-	mv $(DEPS_DIR)/$(1)/rebar.config.script $(DEPS_DIR)/$(1)/rebar.config.script.orig; \
-	sed -r 's/rebar_utils:is_arch\((.*)\)/\1 =:= "$(PLATFORM)"/g' $(DEPS_DIR)/$(1)/rebar.config.script.orig \
-		> $(DEPS_DIR)/$(1)/rebar.config.script
+define dep_autopatch_rebar_utils
+	mkdir -p $(ERLANG_MK_TMP)/ebin; \
+	if [ ! -f $(ERLANG_MK_TMP)/rebar.hrl ]; then \
+		$(call core_http_get,$(ERLANG_MK_TMP)/rebar.hrl,https://raw.githubusercontent.com/rebar/rebar/791db716b5a3a7671e0b351f95ddf24b848ee173/include/rebar.hrl); \
+	fi; \
+	if [ ! -f $(ERLANG_MK_TMP)/rebar_utils.erl ]; then \
+		$(call core_http_get,$(ERLANG_MK_TMP)/rebar_utils.erl,https://raw.githubusercontent.com/rebar/rebar/791db716b5a3a7671e0b351f95ddf24b848ee173/src/rebar_utils.erl); \
+	fi; \
+	if [ ! -f $(ERLANG_MK_TMP)/ebin/rebar_utils.beam ]; then \
+		erlc -o $(ERLANG_MK_TMP)/ebin $(ERLANG_MK_TMP)/rebar_utils.erl; \
+	fi
 endef
 endef
 
 
 define dep_autopatch_rebar
 define dep_autopatch_rebar
@@ -157,10 +162,9 @@ define dep_autopatch_rebar.erl
 					({d, D}) ->
 					({d, D}) ->
 						Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
 						Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
 					({platform_define, Regex, D}) ->
 					({platform_define, Regex, D}) ->
-						case re:run("$(PLATFORM)", Regex, [{capture, none}]) of
-							nomatch -> ok;
-							match ->
-								Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n")
+						case rebar_utils:is_arch(Regex) of
+							true -> Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
+							false -> ok
 						end;
 						end;
 					({parse_transform, PT}) ->
 					({parse_transform, PT}) ->
 						Write("ERLC_OPTS += +'{parse_transform, " ++ atom_to_list(PT) ++ "}'\n");
 						Write("ERLC_OPTS += +'{parse_transform, " ++ atom_to_list(PT) ++ "}'\n");
@@ -233,7 +237,7 @@ define dep_autopatch_rebar.erl
 		Write(["COMPILE_FIRST +=", [[" ", atom_to_list(M)] || M <- First,
 		Write(["COMPILE_FIRST +=", [[" ", atom_to_list(M)] || M <- First,
 			lists:member("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", ErlFiles)], "\n"])
 			lists:member("$(DEPS_DIR)/$(1)/src/" ++ atom_to_list(M) ++ ".erl", ErlFiles)], "\n"])
 	end(),
 	end(),
-	PortSpec = fun(Name, {_, Output, Input, [{env, Env}]}) ->
+	PortSpecWrite = fun(Name, Output, Input, Env) ->
 		filelib:ensure_dir("$(DEPS_DIR)/$(1)/" ++ Output),
 		filelib:ensure_dir("$(DEPS_DIR)/$(1)/" ++ Output),
 		file:write_file("$(DEPS_DIR)/$(1)/c_src/Makefile." ++ Name, [
 		file:write_file("$(DEPS_DIR)/$(1)/c_src/Makefile." ++ Name, [
 			[["override ", K, " = $$$$\(shell echo ", Escape(V), "\)\n"]
 			[["override ", K, " = $$$$\(shell echo ", Escape(V), "\)\n"]
@@ -243,6 +247,21 @@ define dep_autopatch_rebar.erl
 			[[" ../", F] || F <- Input]
 			[[" ../", F] || F <- Input]
 		])
 		])
 	end,
 	end,
+	PortSpecNoop = fun(Name) -> file:write_file("$(DEPS_DIR)/$(1)/c_src/Makefile." ++ Name, "noop:\n") end,
+	PortSpec = fun
+		(Name, {Output, Input}) ->
+			PortSpecWrite(Name, Output, Input, []);
+		(Name, {Regex, Output, Input}) ->
+			case rebar_utils:is_arch(Regex) of
+				true -> PortSpecWrite(Name, Output, Input, []);
+				false -> PortSpecNoop(Name)
+			end;
+		(Name, {Regex, Output, Input, [{env, Env}]}) ->
+			case rebar_utils:is_arch(Regex) of
+				true -> PortSpecWrite(Name, Output, Input, Env);
+				false -> PortSpecNoop(Name)
+			end
+	end,
 	fun() ->
 	fun() ->
 		case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
 		case filelib:is_dir("$(DEPS_DIR)/$(1)/c_src") of
 			false -> ok;
 			false -> ok;
@@ -266,6 +285,9 @@ define dep_autopatch_rebar.erl
 			_ -> ok
 			_ -> ok
 		end
 		end
 	end(),
 	end(),
+	EnvValue = fun(V) ->
+		Escape(re:replace(V, "\\\\$$$$\ERLANG_ARCH", rebar_utils:wordsize(), [{return, list}]))
+	end,
 	fun() ->
 	fun() ->
 		case lists:keyfind(port_env, 1, Conf) of
 		case lists:keyfind(port_env, 1, Conf) of
 			{_, Vars} ->
 			{_, Vars} ->
@@ -274,18 +296,19 @@ define dep_autopatch_rebar.erl
 						case lists:member(K, Acc) of
 						case lists:member(K, Acc) of
 							true -> Acc;
 							true -> Acc;
 							false ->
 							false ->
-								Write(K ++ " = $$$$\(shell echo " ++ Escape(V) ++ "\)\n"),
+								Write(K ++ " = $$$$\(shell echo " ++ EnvValue(V) ++ "\)\n"),
 								[K|Acc]
 								[K|Acc]
 						end;
 						end;
 					({Regex, K, V}, Acc) ->
 					({Regex, K, V}, Acc) ->
 						case lists:member(K, Acc) of
 						case lists:member(K, Acc) of
 							true -> Acc;
 							true -> Acc;
 							false ->
 							false ->
-								case re:run("$(PLATFORM)", Regex, [{capture, none}]) of
-									nomatch -> Acc;
-									match ->
-										Write(K ++ " = $$$$\(shell echo " ++ Escape(V) ++ "\)\n"),
-										[K|Acc]
+								case rebar_utils:is_arch(Regex) of
+									true ->
+										Write(K ++ " = $$$$\(shell echo " ++ EnvValue(V) ++ "\)\n"),
+										[K|Acc];
+									false ->
+										Acc
 								end
 								end
 						end
 						end
 				end, [], Vars),
 				end, [], Vars),
@@ -308,8 +331,8 @@ define dep_autopatch_rebar.erl
 					{compile, Command} ->
 					{compile, Command} ->
 						Write("\npre-app::\n\t" ++ Escape(Command) ++ "\n");
 						Write("\npre-app::\n\t" ++ Escape(Command) ++ "\n");
 					{Regex, compile, Command0} ->
 					{Regex, compile, Command0} ->
-						case re:run("$(PLATFORM)", Regex, [{capture, none}]) of
-							match ->
+						case rebar_utils:is_arch(Regex) of
+							true ->
 								Command = case Command0 of
 								Command = case Command0 of
 									"make -C" ++ _ -> Escape(Command0);
 									"make -C" ++ _ -> Escape(Command0);
 									"gmake -C" ++ _ -> Escape(Command0);
 									"gmake -C" ++ _ -> Escape(Command0);
@@ -318,7 +341,7 @@ define dep_autopatch_rebar.erl
 									_ -> Command0
 									_ -> Command0
 								end,
 								end,
 								Write("\npre-app::\n\t" ++ Command ++ "\n");
 								Write("\npre-app::\n\t" ++ Command ++ "\n");
-							nomatch ->
+							false ->
 								ok
 								ok
 						end;
 						end;
 					_ -> ok
 					_ -> ok
@@ -339,10 +362,7 @@ define dep_autopatch_rebar.erl
 				ErlFile = "$(DEPS_DIR)/$(1)/plugins/" ++ atom_to_list(Plugin) ++ ".erl",
 				ErlFile = "$(DEPS_DIR)/$(1)/plugins/" ++ atom_to_list(Plugin) ++ ".erl",
 				try
 				try
 					{ok, PF} = file:read_file(ErlFile),
 					{ok, PF} = file:read_file(ErlFile),
-					PF2 = re:replace(PF, "rebar_utils:find_files", "find_files", [global, {return, list}]),
-					PF3 = PF2 ++ "find_files(Dir, Regex) ->
-						filelib:fold_files(Dir, Regex, true, fun(F, Acc) -> [F|Acc] end, []).",
-					ok = file:write_file(ErlFile, PF3),
+					ok = file:write_file(ErlFile, PF),
 					{ok, Mod, Bin} = compile:file(ErlFile, [binary]),
 					{ok, Mod, Bin} = compile:file(ErlFile, [binary]),
 					{module, Mod} = code:load_binary(Mod, ErlFile, Bin),
 					{module, Mod} = code:load_binary(Mod, ErlFile, Bin),
 					c:cd("$(DEPS_DIR)/$(1)/"),
 					c:cd("$(DEPS_DIR)/$(1)/"),

+ 6 - 1
test/Makefile

@@ -8,6 +8,11 @@
 
 
 V ?= 0
 V ?= 0
 
 
+# Temporary files directory.
+
+ERLANG_MK_TMP=$(CURDIR)/tmp
+export ERLANG_MK_TMP
+
 # t = Verbosity control for tests
 # t = Verbosity control for tests
 # v = Verbosity control for erlang.mk
 # v = Verbosity control for erlang.mk
 # i = Command to display (or suppress) info messages
 # i = Command to display (or suppress) info messages
@@ -43,7 +48,7 @@ all: clean app ct eunit tests-cover docs pkgs
 	$i '+---------------------+'
 	$i '+---------------------+'
 
 
 clean:
 clean:
-	$t rm -rf app1
+	$t rm -rf app1 $(ERLANG_MK_TMP)
 
 
 app: app1
 app: app1
 	$i "app: Testing the 'app' target."
 	$i "app: Testing the 'app' target."