You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2013/12/16 17:49:01 UTC
[04/10] Import rebar
http://git-wip-us.apache.org/repos/asf/couchdb/blob/626455a4/src/rebar/src/rebar_eunit.erl
----------------------------------------------------------------------
diff --git a/src/rebar/src/rebar_eunit.erl b/src/rebar/src/rebar_eunit.erl
new file mode 100644
index 0000000..d39b1a2
--- /dev/null
+++ b/src/rebar/src/rebar_eunit.erl
@@ -0,0 +1,812 @@
+%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ts=4 sw=4 et
+%% -------------------------------------------------------------------
+%%
+%% rebar: Erlang Build Tools
+%%
+%% Copyright (c) 2009, 2010 Dave Smith (dizzyd@dizzyd.com)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -------------------------------------------------------------------
+%% @author Dave Smith <di...@dizzyd.com>
+%% @doc rebar_eunit supports the following commands:
+%% <ul>
+%% <li>eunit - runs eunit tests</li>
+%% <li>clean - remove ?EUNIT_DIR directory</li>
+%% <li>reset_after_eunit::boolean() - default = true.
+%% If true, try to "reset" VM state to approximate state prior to
+%% running the EUnit tests:
+%% <ul>
+%% <li>Stop net_kernel if it was started</li>
+%% <li>Stop OTP applications not running before EUnit tests were run</li>
+%% <li>Kill processes not running before EUnit tests were run</li>
+%% <li>Reset OTP application environment variables</li>
+%% </ul>
+%% </li>
+%% </ul>
+%% The following Global options are supported:
+%% <ul>
+%% <li>verbose=1 - show extra output from the eunit test</li>
+%% <li>
+%% suites="foo,bar" - runs tests in foo.erl, test/foo_tests.erl and
+%% tests in bar.erl, test/bar_tests.erl
+%% </li>
+%% <li>
+%% suites="foo,bar" tests="baz"- runs first test with name starting
+%% with 'baz' in foo.erl, test/foo_tests.erl and tests in bar.erl,
+%% test/bar_tests.erl
+%% </li>
+%% <li>
+%% tests="baz"- For every existing suite, run the first test whose
+%% name starts with bar and, if no such test exists, run the test
+%% whose name starts with bar in the suite's _tests module
+%% </li>
+%% </ul>
+%% Additionally, for projects that have separate folders for the core
+%% implementation, and for the unit tests, then the following
+%% <code>rebar.config</code> option can be provided:
+%% <code>{eunit_compile_opts, [{src_dirs, ["src", "dir"]}]}.</code>.
+%% @copyright 2009, 2010 Dave Smith
+%% -------------------------------------------------------------------
+-module(rebar_eunit).
+
+-export([eunit/2,
+ clean/2]).
+
+%% for internal use only
+-export([info/2]).
+
+-include("rebar.hrl").
+
+-define(EUNIT_DIR, ".eunit").
+
+%% ===================================================================
+%% Public API
+%% ===================================================================
+
+eunit(Config, _AppFile) ->
+ ok = ensure_dirs(),
+ %% Save code path
+ CodePath = setup_code_path(),
+ CompileOnly = rebar_utils:get_experimental_global(Config, compile_only,
+ false),
+ {ok, SrcErls} = rebar_erlc_compiler:test_compile(Config, "eunit",
+ ?EUNIT_DIR),
+ case CompileOnly of
+ "true" ->
+ true = code:set_path(CodePath),
+ ?CONSOLE("Compiled modules for eunit~n", []);
+ false ->
+ run_eunit(Config, CodePath, SrcErls)
+ end.
+
+clean(_Config, _File) ->
+ rebar_file_utils:rm_rf(?EUNIT_DIR).
+
+%% ===================================================================
+%% Internal functions
+%% ===================================================================
+
+info(help, eunit) ->
+ info_help("Run eunit tests");
+info(help, clean) ->
+ Description = ?FMT("Delete eunit test dir (~s)", [?EUNIT_DIR]),
+ info_help(Description).
+
+info_help(Description) ->
+ ?CONSOLE(
+ "~s.~n"
+ "~n"
+ "Valid rebar.config options:~n"
+ " ~p~n"
+ " ~p~n"
+ " ~p~n"
+ " ~p~n"
+ " ~p~n"
+ " ~p~n"
+ "Valid command line options:~n"
+ " suites=\"foo,bar\" (Run tests in foo.erl, test/foo_tests.erl and~n"
+ " tests in bar.erl, test/bar_tests.erl)~n"
+ " tests=\"baz\" (For every existing suite, run the first test whose~n"
+ " name starts with bar and, if no such test exists,~n"
+ " run the test whose name starts with bar in the~n"
+ " suite's _tests module)~n",
+ [
+ Description,
+ {eunit_opts, []},
+ {eunit_compile_opts, []},
+ {eunit_first_files, []},
+ {cover_enabled, false},
+ {cover_print_enabled, false},
+ {cover_export_enabled, false}
+ ]).
+
+run_eunit(Config, CodePath, SrcErls) ->
+ %% Build a list of all the .beams in ?EUNIT_DIR -- use this for
+ %% cover and eunit testing. Normally you can just tell cover
+ %% and/or eunit to scan the directory for you, but eunit does a
+ %% code:purge in conjunction with that scan and causes any cover
+ %% compilation info to be lost.
+
+ AllBeamFiles = rebar_utils:beams(?EUNIT_DIR),
+ {BeamFiles, TestBeamFiles} =
+ lists:partition(fun(N) -> string:str(N, "_tests.beam") =:= 0 end,
+ AllBeamFiles),
+ OtherBeamFiles = TestBeamFiles --
+ [filename:rootname(N) ++ "_tests.beam" || N <- AllBeamFiles],
+ ModuleBeamFiles = BeamFiles ++ OtherBeamFiles,
+
+ %% Get modules to be run in eunit
+ AllModules = [rebar_utils:beam_to_mod(?EUNIT_DIR, N) || N <- AllBeamFiles],
+ {SuitesProvided, FilteredModules} = filter_suites(Config, AllModules),
+
+ %% Get matching tests
+ Tests = get_tests(Config, SuitesProvided, ModuleBeamFiles, FilteredModules),
+
+ SrcModules = [rebar_utils:erl_to_mod(M) || M <- SrcErls],
+
+ {ok, CoverLog} = cover_init(Config, ModuleBeamFiles),
+
+ StatusBefore = status_before_eunit(),
+ EunitResult = perform_eunit(Config, Tests),
+
+ perform_cover(Config, FilteredModules, SrcModules),
+ cover_close(CoverLog),
+
+ case proplists:get_value(reset_after_eunit, get_eunit_opts(Config),
+ true) of
+ true ->
+ reset_after_eunit(StatusBefore);
+ false ->
+ ok
+ end,
+
+ %% Stop cover to clean the cover_server state. This is important if we want
+ %% eunit+cover to not slow down when analyzing many Erlang modules.
+ ok = cover:stop(),
+
+ case EunitResult of
+ ok ->
+ ok;
+ _ ->
+ ?ABORT("One or more eunit tests failed.~n", [])
+ end,
+
+ %% Restore code path
+ true = code:set_path(CodePath),
+ ok.
+
+ensure_dirs() ->
+ %% Make sure ?EUNIT_DIR/ and ebin/ directory exists (append dummy module)
+ ok = filelib:ensure_dir(filename:join(eunit_dir(), "dummy")),
+ ok = filelib:ensure_dir(filename:join(rebar_utils:ebin_dir(), "dummy")).
+
+eunit_dir() ->
+ filename:join(rebar_utils:get_cwd(), ?EUNIT_DIR).
+
+setup_code_path() ->
+ %% Setup code path prior to compilation so that parse_transforms
+ %% and the like work properly. Also, be sure to add ebin_dir()
+ %% to the END of the code path so that we don't have to jump
+ %% through hoops to access the .app file
+ CodePath = code:get_path(),
+ true = code:add_patha(eunit_dir()),
+ true = code:add_pathz(rebar_utils:ebin_dir()),
+ CodePath.
+
+%%
+%% == filter suites ==
+%%
+
+filter_suites(Config, Modules) ->
+ RawSuites = rebar_config:get_global(Config, suites, ""),
+ SuitesProvided = RawSuites =/= "",
+ Suites = [list_to_atom(Suite) || Suite <- string:tokens(RawSuites, ",")],
+ {SuitesProvided, filter_suites1(Modules, Suites)}.
+
+filter_suites1(Modules, []) ->
+ Modules;
+filter_suites1(Modules, Suites) ->
+ [M || M <- Suites, lists:member(M, Modules)].
+
+%%
+%% == get matching tests ==
+%%
+get_tests(Config, SuitesProvided, ModuleBeamFiles, FilteredModules) ->
+ Modules = case SuitesProvided of
+ false ->
+ %% No specific suites have been provided, use
+ %% ModuleBeamFiles which filters out "*_tests" modules
+ %% so eunit won't doubly run them and cover only
+ %% calculates coverage on production code. However,
+ %% keep "*_tests" modules that are not automatically
+ %% included by eunit.
+ %%
+ %% From 'Primitives' in the EUnit User's Guide
+ %% http://www.erlang.org/doc/apps/eunit/chapter.html
+ %% "In addition, EUnit will also look for another
+ %% module whose name is ModuleName plus the suffix
+ %% _tests, and if it exists, all the tests from that
+ %% module will also be added. (If ModuleName already
+ %% contains the suffix _tests, this is not done.) E.g.,
+ %% the specification {module, mymodule} will run all
+ %% tests in the modules mymodule and mymodule_tests.
+ %% Typically, the _tests module should only contain
+ %% test cases that use the public interface of the main
+ %% module (and no other code)."
+ [rebar_utils:beam_to_mod(?EUNIT_DIR, N) ||
+ N <- ModuleBeamFiles];
+ true ->
+ %% Specific suites have been provided, return the
+ %% filtered modules
+ FilteredModules
+ end,
+ get_matching_tests(Config, Modules).
+
+get_matching_tests(Config, Modules) ->
+ RawFunctions = rebar_utils:get_experimental_global(Config, tests, ""),
+ Tests = [list_to_atom(F1) || F1 <- string:tokens(RawFunctions, ",")],
+ case Tests of
+ [] ->
+ Modules;
+ Functions ->
+ case get_matching_tests1(Modules, Functions, []) of
+ [] ->
+ [];
+ RawTests ->
+ make_test_primitives(RawTests)
+ end
+ end.
+
+get_matching_tests1([], _Functions, TestFunctions) ->
+ TestFunctions;
+
+get_matching_tests1([Module|TModules], Functions, TestFunctions) ->
+ %% Get module exports
+ ModuleStr = atom_to_list(Module),
+ ModuleExports = get_beam_test_exports(ModuleStr),
+ %% Get module _tests exports
+ TestModuleStr = string:concat(ModuleStr, "_tests"),
+ TestModuleExports = get_beam_test_exports(TestModuleStr),
+ %% Build tests {M, F} list
+ Tests = get_matching_tests2(Functions, {Module, ModuleExports},
+ {list_to_atom(TestModuleStr),
+ TestModuleExports}),
+ get_matching_tests1(TModules, Functions,
+ lists:merge([TestFunctions, Tests])).
+
+get_matching_tests2(Functions, {Mod, ModExports}, {TestMod, TestModExports}) ->
+ %% Look for matching functions into ModExports
+ ModExportsStr = [atom_to_list(E1) || E1 <- ModExports],
+ TestModExportsStr = [atom_to_list(E2) || E2 <- TestModExports],
+ get_matching_exports(Functions, {Mod, ModExportsStr},
+ {TestMod, TestModExportsStr}, []).
+
+get_matching_exports([], _, _, Matched) ->
+ Matched;
+get_matching_exports([Function|TFunctions], {Mod, ModExportsStr},
+ {TestMod, TestModExportsStr}, Matched) ->
+
+ FunctionStr = atom_to_list(Function),
+ %% Get matching Function in module, otherwise look in _tests module
+ NewMatch = case get_matching_export(FunctionStr, ModExportsStr) of
+ [] ->
+ {TestMod, get_matching_export(FunctionStr,
+ TestModExportsStr)};
+ MatchingExport ->
+ {Mod, MatchingExport}
+ end,
+ case NewMatch of
+ {_, []} ->
+ get_matching_exports(TFunctions, {Mod, ModExportsStr},
+ {TestMod, TestModExportsStr}, Matched);
+ _ ->
+ get_matching_exports(TFunctions, {Mod, ModExportsStr},
+ {TestMod, TestModExportsStr},
+ [NewMatch|Matched])
+ end.
+
+get_matching_export(_FunctionStr, []) ->
+ [];
+get_matching_export(FunctionStr, [ExportStr|TExportsStr]) ->
+ case string:str(ExportStr, FunctionStr) of
+ 1 ->
+ list_to_atom(ExportStr);
+ _ ->
+ get_matching_export(FunctionStr, TExportsStr)
+ end.
+
+get_beam_test_exports(ModuleStr) ->
+ FilePath = filename:join(eunit_dir(),
+ string:concat(ModuleStr, ".beam")),
+ case filelib:is_regular(FilePath) of
+ true ->
+ {beam_file, _, Exports0, _, _, _} = beam_disasm:file(FilePath),
+ Exports1 = [FunName || {FunName, FunArity, _} <- Exports0,
+ FunArity =:= 0],
+ F = fun(FName) ->
+ FNameStr = atom_to_list(FName),
+ re:run(FNameStr, "_test(_)?") =/= nomatch
+ end,
+ lists:filter(F, Exports1);
+ _ ->
+ []
+ end.
+
+make_test_primitives(RawTests) ->
+ %% Use {test,M,F} and {generator,M,F} if at least R15B02. Otherwise,
+ %% use eunit_test:function_wrapper/2 fallback.
+ %% eunit_test:function_wrapper/2 was renamed to eunit_test:mf_wrapper/2
+ %% in R15B02; use that as >= R15B02 check.
+ %% TODO: remove fallback and use only {test,M,F} and {generator,M,F}
+ %% primitives once at least R15B02 is required.
+ {module, eunit_test} = code:ensure_loaded(eunit_test),
+ MakePrimitive = case erlang:function_exported(eunit_test, mf_wrapper, 2) of
+ true -> fun eunit_primitive/3;
+ false -> fun pre15b02_eunit_primitive/3
+ end,
+
+ ?CONSOLE(" Running test function(s):~n", []),
+ F = fun({M, F2}, Acc) ->
+ ?CONSOLE(" ~p:~p/0~n", [M, F2]),
+ FNameStr = atom_to_list(F2),
+ NewFunction =
+ case re:run(FNameStr, "_test_") of
+ nomatch ->
+ %% Normal test
+ MakePrimitive(test, M, F2);
+ _ ->
+ %% Generator
+ MakePrimitive(generator, M, F2)
+ end,
+ [NewFunction|Acc]
+ end,
+ lists:foldl(F, [], RawTests).
+
+eunit_primitive(Type, M, F) ->
+ {Type, M, F}.
+
+pre15b02_eunit_primitive(test, M, F) ->
+ eunit_test:function_wrapper(M, F);
+pre15b02_eunit_primitive(generator, M, F) ->
+ {generator, eunit_test:function_wrapper(M, F)}.
+
+%%
+%% == run tests ==
+%%
+
+perform_eunit(Config, Tests) ->
+ EunitOpts = get_eunit_opts(Config),
+
+ %% Move down into ?EUNIT_DIR while we run tests so any generated files
+ %% are created there (versus in the source dir)
+ Cwd = rebar_utils:get_cwd(),
+ ok = file:set_cwd(?EUNIT_DIR),
+
+ EunitResult = (catch eunit:test(Tests, EunitOpts)),
+
+ %% Return to original working dir
+ ok = file:set_cwd(Cwd),
+
+ EunitResult.
+
+get_eunit_opts(Config) ->
+ %% Enable verbose in eunit if so requested..
+ BaseOpts = case rebar_config:is_verbose(Config) of
+ true ->
+ [verbose];
+ false ->
+ []
+ end,
+
+ BaseOpts ++ rebar_config:get_list(Config, eunit_opts, []).
+
+%%
+%% == code coverage ==
+%%
+
+perform_cover(Config, BeamFiles, SrcModules) ->
+ perform_cover(rebar_config:get(Config, cover_enabled, false),
+ Config, BeamFiles, SrcModules).
+
+perform_cover(false, _Config, _BeamFiles, _SrcModules) ->
+ ok;
+perform_cover(true, Config, BeamFiles, SrcModules) ->
+ cover_analyze(Config, BeamFiles, SrcModules).
+
+cover_analyze(_Config, [], _SrcModules) ->
+ ok;
+cover_analyze(Config, FilteredModules, SrcModules) ->
+ %% Generate coverage info for all the cover-compiled modules
+ Coverage = lists:flatten([cover_analyze_mod(M)
+ || M <- FilteredModules,
+ cover:is_compiled(M) =/= false]),
+
+ %% Write index of coverage info
+ cover_write_index(lists:sort(Coverage), SrcModules),
+
+ %% Write coverage details for each file
+ lists:foreach(fun({M, _, _}) ->
+ {ok, _} = cover:analyze_to_file(M, cover_file(M),
+ [html])
+ end, Coverage),
+
+ Index = filename:join([rebar_utils:get_cwd(), ?EUNIT_DIR, "index.html"]),
+ ?CONSOLE("Cover analysis: ~s\n", [Index]),
+
+ %% Export coverage data, if configured
+ case rebar_config:get(Config, cover_export_enabled, false) of
+ true ->
+ cover_export_coverdata();
+ false ->
+ ok
+ end,
+
+ %% Print coverage report, if configured
+ case rebar_config:get(Config, cover_print_enabled, false) of
+ true ->
+ cover_print_coverage(lists:sort(Coverage));
+ false ->
+ ok
+ end.
+
+cover_close(not_enabled) ->
+ ok;
+cover_close(F) ->
+ ok = file:close(F).
+
+cover_init(false, _BeamFiles) ->
+ {ok, not_enabled};
+cover_init(true, BeamFiles) ->
+ %% Attempt to start the cover server, then set its group leader to
+ %% .eunit/cover.log, so all cover log messages will go there instead of
+ %% to stdout. If the cover server is already started, we'll kill that
+ %% server and start a new one in order not to inherit a polluted
+ %% cover_server state.
+ {ok, CoverPid} = case whereis(cover_server) of
+ undefined ->
+ cover:start();
+ _ ->
+ cover:stop(),
+ cover:start()
+ end,
+
+ {ok, F} = OkOpen = file:open(
+ filename:join([?EUNIT_DIR, "cover.log"]),
+ [write]),
+
+ group_leader(F, CoverPid),
+
+ ?INFO("Cover compiling ~s\n", [rebar_utils:get_cwd()]),
+
+ Compiled = [{Beam, cover:compile_beam(Beam)} || Beam <- BeamFiles],
+ case [Module || {_, {ok, Module}} <- Compiled] of
+ [] ->
+ %% No modules compiled successfully...fail
+ ?ERROR("Cover failed to compile any modules; aborting.~n", []),
+ ?FAIL;
+ _ ->
+ %% At least one module compiled successfully
+
+ %% It's not an error for cover compilation to fail partially,
+ %% but we do want to warn about them
+ PrintWarning =
+ fun(Beam, Desc) ->
+ ?CONSOLE("Cover compilation warning for ~p: ~p",
+ [Beam, Desc])
+ end,
+ _ = [PrintWarning(Beam, Desc) || {Beam, {error, Desc}} <- Compiled],
+ OkOpen
+ end;
+cover_init(Config, BeamFiles) ->
+ cover_init(rebar_config:get(Config, cover_enabled, false), BeamFiles).
+
+cover_analyze_mod(Module) ->
+ case cover:analyze(Module, coverage, module) of
+ {ok, {Module, {Covered, NotCovered}}} ->
+ %% Modules that include the eunit header get an implicit
+ %% test/0 fun, which cover considers a runnable line, but
+ %% eunit:test(TestRepresentation) never calls. Decrement
+ %% NotCovered in this case.
+ [align_notcovered_count(Module, Covered, NotCovered,
+ is_eunitized(Module))];
+ {error, Reason} ->
+ ?ERROR("Cover analyze failed for ~p: ~p ~p\n",
+ [Module, Reason, code:which(Module)]),
+ []
+ end.
+
+is_eunitized(Mod) ->
+ has_eunit_test_fun(Mod) andalso
+ has_header(Mod, "include/eunit.hrl").
+
+has_eunit_test_fun(Mod) ->
+ [F || {exports, Funs} <- Mod:module_info(),
+ {F, 0} <- Funs, F =:= test] =/= [].
+
+has_header(Mod, Header) ->
+ Mod1 = case code:which(Mod) of
+ cover_compiled ->
+ {file, File} = cover:is_compiled(Mod),
+ File;
+ non_existing -> Mod;
+ preloaded -> Mod;
+ L -> L
+ end,
+ {ok, {_, [{abstract_code, {_, AC}}]}} = beam_lib:chunks(Mod1,
+ [abstract_code]),
+ [F || {attribute, 1, file, {F, 1}} <- AC,
+ string:str(F, Header) =/= 0] =/= [].
+
+align_notcovered_count(Module, Covered, NotCovered, false) ->
+ {Module, Covered, NotCovered};
+align_notcovered_count(Module, Covered, NotCovered, true) ->
+ {Module, Covered, NotCovered - 1}.
+
+cover_write_index(Coverage, SrcModules) ->
+ {ok, F} = file:open(filename:join([?EUNIT_DIR, "index.html"]), [write]),
+ ok = file:write(F, "<!DOCTYPE HTML><html>\n"
+ "<head><meta charset=\"utf-8\">"
+ "<title>Coverage Summary</title></head>\n"
+ "<body>\n"),
+ IsSrcCoverage = fun({Mod,_C,_N}) -> lists:member(Mod, SrcModules) end,
+ {SrcCoverage, TestCoverage} = lists:partition(IsSrcCoverage, Coverage),
+ cover_write_index_section(F, "Source", SrcCoverage),
+ cover_write_index_section(F, "Test", TestCoverage),
+ ok = file:write(F, "</body></html>"),
+ ok = file:close(F).
+
+cover_write_index_section(_F, _SectionName, []) ->
+ ok;
+cover_write_index_section(F, SectionName, Coverage) ->
+ %% Calculate total coverage
+ {Covered, NotCovered} = lists:foldl(fun({_Mod, C, N}, {CAcc, NAcc}) ->
+ {CAcc + C, NAcc + N}
+ end, {0, 0}, Coverage),
+ TotalCoverage = percentage(Covered, NotCovered),
+
+ %% Write the report
+ ok = file:write(F, ?FMT("<h1>~s Summary</h1>\n", [SectionName])),
+ ok = file:write(F, ?FMT("<h3>Total: ~s</h3>\n", [TotalCoverage])),
+ ok = file:write(F, "<table><tr><th>Module</th><th>Coverage %</th></tr>\n"),
+
+ FmtLink =
+ fun(Module, Cov, NotCov) ->
+ ?FMT("<tr><td><a href='~s.COVER.html'>~s</a></td><td>~s</td>\n",
+ [Module, Module, percentage(Cov, NotCov)])
+ end,
+ lists:foreach(fun({Module, Cov, NotCov}) ->
+ ok = file:write(F, FmtLink(Module, Cov, NotCov))
+ end, Coverage),
+ ok = file:write(F, "</table>\n").
+
+cover_print_coverage(Coverage) ->
+ {Covered, NotCovered} = lists:foldl(fun({_Mod, C, N}, {CAcc, NAcc}) ->
+ {CAcc + C, NAcc + N}
+ end, {0, 0}, Coverage),
+ TotalCoverage = percentage(Covered, NotCovered),
+
+ %% Determine the longest module name for right-padding
+ Width = lists:foldl(fun({Mod, _, _}, Acc) ->
+ case length(atom_to_list(Mod)) of
+ N when N > Acc ->
+ N;
+ _ ->
+ Acc
+ end
+ end, 0, Coverage) * -1,
+
+ %% Print the output the console
+ ?CONSOLE("~nCode Coverage:~n", []),
+ lists:foreach(fun({Mod, C, N}) ->
+ ?CONSOLE("~*s : ~3s~n",
+ [Width, Mod, percentage(C, N)])
+ end, Coverage),
+ ?CONSOLE("~n~*s : ~s~n", [Width, "Total", TotalCoverage]).
+
+cover_file(Module) ->
+ filename:join([?EUNIT_DIR, atom_to_list(Module) ++ ".COVER.html"]).
+
+cover_export_coverdata() ->
+ ExportFile = filename:join(eunit_dir(), "eunit.coverdata"),
+ case cover:export(ExportFile) of
+ ok ->
+ ?CONSOLE("Coverdata export: ~s~n", [ExportFile]);
+ {error, Reason} ->
+ ?ERROR("Coverdata export failed: ~p~n", [Reason])
+ end.
+
+percentage(0, 0) ->
+ "not executed";
+percentage(Cov, NotCov) ->
+ integer_to_list(trunc((Cov / (Cov + NotCov)) * 100)) ++ "%".
+
+%%
+%% == reset_after_eunit ==
+%%
+
+status_before_eunit() ->
+ Apps = get_app_names(),
+ AppEnvs = [{App, application:get_all_env(App)} || App <- Apps],
+ {erlang:processes(), erlang:is_alive(), AppEnvs, ets:tab2list(ac_tab)}.
+
+get_app_names() ->
+ [AppName || {AppName, _, _} <- application:loaded_applications()].
+
+reset_after_eunit({OldProcesses, WasAlive, OldAppEnvs, _OldACs}) ->
+ IsAlive = erlang:is_alive(),
+ if not WasAlive andalso IsAlive ->
+ ?DEBUG("Stopping net kernel....\n", []),
+ erl_epmd:stop(),
+ _ = net_kernel:stop(),
+ pause_until_net_kernel_stopped();
+ true ->
+ ok
+ end,
+
+ OldApps = [App || {App, _} <- OldAppEnvs],
+ Apps = get_app_names(),
+ _ = [begin
+ _ = case lists:member(App, OldApps) of
+ true -> ok;
+ false -> application:stop(App)
+ end,
+ ok = application:unset_env(App, K)
+ end || App <- Apps, App /= rebar,
+ {K, _V} <- application:get_all_env(App),
+ K =/= included_applications],
+
+ reconstruct_app_env_vars(Apps),
+
+ Processes = erlang:processes(),
+ _ = kill_extras(Processes -- OldProcesses),
+
+ ok.
+
+kill_extras(Pids) ->
+ %% Killing any of the procs below will either:
+ %% 1. Interfere with stuff that we don't want interfered with, or
+ %% 2. May/will force the 'kernel' app to shutdown, which *will*
+ %% interfere with rebar's ability To Do Useful Stuff(tm).
+ %% This list may require changes as OTP versions and/or
+ %% rebar use cases change.
+ KeepProcs = [cover_server, eunit_server,
+ eqc, eqc_license, eqc_locked,
+ %% inet_gethost_native is started on demand, when
+ %% doing name lookups. It is under kernel_sup, under
+ %% a supervisor_bridge.
+ inet_gethost_native],
+ Killed = [begin
+ Info = case erlang:process_info(Pid) of
+ undefined -> [];
+ Else -> Else
+ end,
+ Keep1 = case proplists:get_value(registered_name, Info) of
+ undefined ->
+ false;
+ Name ->
+ lists:member(Name, KeepProcs)
+ end,
+ Keep2 = case proplists:get_value(dictionary, Info) of
+ undefined ->
+ false;
+ Ds ->
+ case proplists:get_value('$ancestors', Ds) of
+ undefined ->
+ false;
+ As ->
+ lists:member(kernel_sup, As)
+ end
+ end,
+ if Keep1 orelse Keep2 ->
+ ok;
+ true ->
+ ?DEBUG("Kill ~p ~p\n", [Pid, Info]),
+ exit(Pid, kill),
+ Pid
+ end
+ end || Pid <- Pids],
+ case lists:usort(Killed) -- [ok] of
+ [] ->
+ ?DEBUG("No processes to kill\n", []),
+ [];
+ Else ->
+ lists:foreach(fun(Pid) -> wait_until_dead(Pid) end, Else),
+ Else
+ end.
+
+reconstruct_app_env_vars([App|Apps]) ->
+ CmdLine0 = proplists:get_value(App, init:get_arguments(), []),
+ CmdVars = [{list_to_atom(K), list_to_atom(V)} || {K, V} <- CmdLine0],
+ AppFile = (catch filename:join([code:lib_dir(App),
+ "ebin",
+ atom_to_list(App) ++ ".app"])),
+ AppVars = case file:consult(AppFile) of
+ {ok, [{application, App, Ps}]} ->
+ proplists:get_value(env, Ps, []);
+ _ ->
+ []
+ end,
+
+ %% App vars specified in config files override those in the .app file.
+ %% Config files later in the args list override earlier ones.
+ AppVars1 = case init:get_argument(config) of
+ {ok, ConfigFiles} ->
+ {App, MergedAppVars} = lists:foldl(fun merge_app_vars/2,
+ {App, AppVars},
+ ConfigFiles),
+ MergedAppVars;
+ error ->
+ AppVars
+ end,
+ AllVars = CmdVars ++ AppVars1,
+ ?DEBUG("Reconstruct ~p ~p\n", [App, AllVars]),
+ lists:foreach(fun({K, V}) -> application:set_env(App, K, V) end, AllVars),
+ reconstruct_app_env_vars(Apps);
+reconstruct_app_env_vars([]) ->
+ ok.
+
+merge_app_vars(ConfigFile, {App, AppVars}) ->
+ File = ensure_config_extension(ConfigFile),
+ FileAppVars = app_vars_from_config_file(File, App),
+ Dict1 = dict:from_list(AppVars),
+ Dict2 = dict:from_list(FileAppVars),
+ Dict3 = dict:merge(fun(_Key, _Value1, Value2) -> Value2 end, Dict1, Dict2),
+ {App, dict:to_list(Dict3)}.
+
+ensure_config_extension(File) ->
+ %% config files must end with .config on disk but when specifying them
+ %% via the -config option the extension is optional
+ BaseFileName = filename:basename(File, ".config"),
+ DirName = filename:dirname(File),
+ filename:join(DirName, BaseFileName ++ ".config").
+
+app_vars_from_config_file(File, App) ->
+ case file:consult(File) of
+ {ok, [Env]} ->
+ proplists:get_value(App, Env, []);
+ _ ->
+ []
+ end.
+
+wait_until_dead(Pid) when is_pid(Pid) ->
+ Ref = erlang:monitor(process, Pid),
+ receive
+ {'DOWN', Ref, process, _Obj, Info} ->
+ Info
+ after 10*1000 ->
+ exit({timeout_waiting_for, Pid})
+ end;
+wait_until_dead(_) ->
+ ok.
+
+pause_until_net_kernel_stopped() ->
+ pause_until_net_kernel_stopped(10).
+
+pause_until_net_kernel_stopped(0) ->
+ exit(net_kernel_stop_failed);
+pause_until_net_kernel_stopped(N) ->
+ case node() of
+ 'nonode@nohost' ->
+ ?DEBUG("Stopped net kernel.\n", []),
+ ok;
+ _ ->
+ timer:sleep(100),
+ pause_until_net_kernel_stopped(N - 1)
+ end.
http://git-wip-us.apache.org/repos/asf/couchdb/blob/626455a4/src/rebar/src/rebar_file_utils.erl
----------------------------------------------------------------------
diff --git a/src/rebar/src/rebar_file_utils.erl b/src/rebar/src/rebar_file_utils.erl
new file mode 100644
index 0000000..fcd9c5e
--- /dev/null
+++ b/src/rebar/src/rebar_file_utils.erl
@@ -0,0 +1,194 @@
+%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ts=4 sw=4 et
+%% -------------------------------------------------------------------
+%%
+%% rebar: Erlang Build Tools
+%%
+%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -------------------------------------------------------------------
+-module(rebar_file_utils).
+
+-export([rm_rf/1,
+ cp_r/2,
+ mv/2,
+ delete_each/1,
+ write_file_if_contents_differ/2]).
+
+-include("rebar.hrl").
+
+%% ===================================================================
+%% Public API
+%% ===================================================================
+
+%% @doc Remove files and directories.
+%% Target is a single filename, directoryname or wildcard expression.
+-spec rm_rf(string()) -> 'ok'.
+rm_rf(Target) ->
+ case os:type() of
+ {unix, _} ->
+ EscTarget = escape_spaces(Target),
+ {ok, []} = rebar_utils:sh(?FMT("rm -rf ~s", [EscTarget]),
+ [{use_stdout, false}, return_on_error]),
+ ok;
+ {win32, _} ->
+ Filelist = filelib:wildcard(Target),
+ Dirs = [F || F <- Filelist, filelib:is_dir(F)],
+ Files = Filelist -- Dirs,
+ ok = delete_each(Files),
+ ok = delete_each_dir_win32(Dirs),
+ ok
+ end.
+
+-spec cp_r(list(string()), file:filename()) -> 'ok'.
+cp_r([], _Dest) ->
+ ok;
+cp_r(Sources, Dest) ->
+ case os:type() of
+ {unix, _} ->
+ EscSources = [escape_spaces(Src) || Src <- Sources],
+ SourceStr = string:join(EscSources, " "),
+ {ok, []} = rebar_utils:sh(?FMT("cp -R ~s \"~s\"",
+ [SourceStr, Dest]),
+ [{use_stdout, false}, return_on_error]),
+ ok;
+ {win32, _} ->
+ lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources),
+ ok
+ end.
+
+-spec mv(string(), file:filename()) -> 'ok'.
+mv(Source, Dest) ->
+ case os:type() of
+ {unix, _} ->
+ EscSource = escape_spaces(Source),
+ EscDest = escape_spaces(Dest),
+ {ok, []} = rebar_utils:sh(?FMT("mv ~s ~s", [EscSource, EscDest]),
+ [{use_stdout, false}, return_on_error]),
+ ok;
+ {win32, _} ->
+ {ok, R} = rebar_utils:sh(
+ ?FMT("move /y \"~s\" \"~s\" 1> nul",
+ [filename:nativename(Source),
+ filename:nativename(Dest)]),
+ [{use_stdout, false}, return_on_error]),
+ case R of
+ [] ->
+ ok;
+ _ ->
+ {error, lists:flatten(
+ io_lib:format("Failed to move ~s to ~s~n",
+ [Source, Dest]))}
+ end
+ end.
+
+delete_each([]) ->
+ ok;
+delete_each([File | Rest]) ->
+ case file:delete(File) of
+ ok ->
+ delete_each(Rest);
+ {error, enoent} ->
+ delete_each(Rest);
+ {error, Reason} ->
+ ?ERROR("Failed to delete file ~s: ~p\n", [File, Reason]),
+ ?FAIL
+ end.
+
+write_file_if_contents_differ(Filename, Bytes) ->
+ ToWrite = iolist_to_binary(Bytes),
+ case file:read_file(Filename) of
+ {ok, ToWrite} ->
+ ok;
+ {ok, _} ->
+ file:write_file(Filename, ToWrite);
+ {error, _} ->
+ file:write_file(Filename, ToWrite)
+ end.
+
+%% ===================================================================
+%% Internal functions
+%% ===================================================================
+
+delete_each_dir_win32([]) -> ok;
+delete_each_dir_win32([Dir | Rest]) ->
+ {ok, []} = rebar_utils:sh(?FMT("rd /q /s \"~s\"",
+ [filename:nativename(Dir)]),
+ [{use_stdout, false}, return_on_error]),
+ delete_each_dir_win32(Rest).
+
+xcopy_win32(Source,Dest)->
+ {ok, R} = rebar_utils:sh(
+ ?FMT("xcopy \"~s\" \"~s\" /q /y /e 2> nul",
+ [filename:nativename(Source), filename:nativename(Dest)]),
+ [{use_stdout, false}, return_on_error]),
+ case length(R) > 0 of
+ %% when xcopy fails, stdout is empty and and error message is printed
+ %% to stderr (which is redirected to nul)
+ true -> ok;
+ false ->
+ {error, lists:flatten(
+ io_lib:format("Failed to xcopy from ~s to ~s~n",
+ [Source, Dest]))}
+ end.
+
+cp_r_win32({true, SourceDir}, {true, DestDir}) ->
+ %% from directory to directory
+ SourceBase = filename:basename(SourceDir),
+ ok = case file:make_dir(filename:join(DestDir, SourceBase)) of
+ {error, eexist} -> ok;
+ Other -> Other
+ end,
+ ok = xcopy_win32(SourceDir, filename:join(DestDir, SourceBase));
+cp_r_win32({false, Source} = S,{true, DestDir}) ->
+ %% from file to directory
+ cp_r_win32(S, {false, filename:join(DestDir, filename:basename(Source))});
+cp_r_win32({false, Source},{false, Dest}) ->
+ %% from file to file
+ {ok,_} = file:copy(Source, Dest),
+ ok;
+cp_r_win32({true, SourceDir}, {false, DestDir}) ->
+ case filelib:is_regular(DestDir) of
+ true ->
+ %% From directory to file? This shouldn't happen
+ {error, lists:flatten(
+ io_lib:format("Cannot copy dir (~p) to file (~p)\n",
+ [SourceDir, DestDir]))};
+ false ->
+ %% Specifying a target directory that doesn't currently exist.
+ %% So let's attempt to create this directory
+ case filelib:ensure_dir(filename:join(DestDir, "dummy")) of
+ ok ->
+ ok = xcopy_win32(SourceDir, DestDir);
+ {error, Reason} ->
+ {error, lists:flatten(
+ io_lib:format("Unable to create dir ~p: ~p\n",
+ [DestDir, Reason]))}
+ end
+ end;
+cp_r_win32(Source,Dest) ->
+ Dst = {filelib:is_dir(Dest), Dest},
+ lists:foreach(fun(Src) ->
+ ok = cp_r_win32({filelib:is_dir(Src), Src}, Dst)
+ end, filelib:wildcard(Source)),
+ ok.
+
+escape_spaces(Str) ->
+ re:replace(Str, " ", "\\\\ ", [global, {return, list}]).
http://git-wip-us.apache.org/repos/asf/couchdb/blob/626455a4/src/rebar/src/rebar_lfe_compiler.erl
----------------------------------------------------------------------
diff --git a/src/rebar/src/rebar_lfe_compiler.erl b/src/rebar/src/rebar_lfe_compiler.erl
new file mode 100644
index 0000000..8488b0f
--- /dev/null
+++ b/src/rebar/src/rebar_lfe_compiler.erl
@@ -0,0 +1,84 @@
+%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ts=4 sw=4 et
+%% -------------------------------------------------------------------
+%%
+%% rebar: Erlang Build Tools
+%%
+%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com),
+%% Tim Dysinger (tim@dysinger.net)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -------------------------------------------------------------------
+
+-module(rebar_lfe_compiler).
+
+-export([compile/2]).
+
+%% for internal use only
+-export([info/2]).
+
+-include("rebar.hrl").
+
+%% ===================================================================
+%% Public API
+%% ===================================================================
+
+compile(Config, _AppFile) ->
+ FirstFiles = rebar_config:get_list(Config, lfe_first_files, []),
+ rebar_base_compiler:run(Config, FirstFiles, "src", ".lfe", "ebin", ".beam",
+ fun compile_lfe/3).
+
+%% ===================================================================
+%% Internal functions
+%% ===================================================================
+
+info(help, compile) ->
+ ?CONSOLE(
+ "Build Lisp Flavoured Erlang (*.lfe) sources.~n"
+ "~n"
+ "Valid rebar.config options:~n"
+ " erl_opts is reused.'~n",
+ []).
+
+compile_lfe(Source, _Target, Config) ->
+ case code:which(lfe_comp) of
+ non_existing ->
+ ?ERROR("~n"
+ "*** MISSING LFE COMPILER ***~n"
+ " You must do one of the following:~n"
+ " a) Install LFE globally in your erl libs~n"
+ " b) Add LFE as a dep for your project, eg:~n"
+ " {lfe, \"0.6.1\",~n"
+ " {git, \"git://github.com/rvirding/lfe\",~n"
+ " {tag, \"v0.6.1\"}}}~n"
+ "~n", []),
+ ?FAIL;
+ _ ->
+ ErlOpts = rebar_utils:erl_opts(Config),
+ Opts = [{i, "include"}, {outdir, "ebin"}, return] ++ ErlOpts,
+ case lfe_comp:file(Source, Opts) of
+ {ok, _Mod, Ws} ->
+ rebar_base_compiler:ok_tuple(Config, Source, Ws);
+ {error, Es, Ws} ->
+ rebar_base_compiler:error_tuple(Config, Source,
+ Es, Ws, Opts);
+ _ ->
+ ?FAIL
+ end
+ end.
http://git-wip-us.apache.org/repos/asf/couchdb/blob/626455a4/src/rebar/src/rebar_log.erl
----------------------------------------------------------------------
diff --git a/src/rebar/src/rebar_log.erl b/src/rebar/src/rebar_log.erl
new file mode 100644
index 0000000..4108c9c
--- /dev/null
+++ b/src/rebar/src/rebar_log.erl
@@ -0,0 +1,83 @@
+%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ts=4 sw=4 et
+%% -------------------------------------------------------------------
+%%
+%% rebar: Erlang Build Tools
+%%
+%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -------------------------------------------------------------------
+-module(rebar_log).
+
+-export([init/1,
+ set_level/1, default_level/0,
+ log/3]).
+
+%% ===================================================================
+%% Public API
+%% ===================================================================
+
+init(Config) ->
+ Verbosity = rebar_config:get_global(Config, verbose, default_level()),
+ case valid_level(Verbosity) of
+ 0 -> set_level(error);
+ 1 -> set_level(warn);
+ 2 -> set_level(info);
+ 3 -> set_level(debug)
+ end.
+
+set_level(Level) ->
+ ok = application:set_env(rebar, log_level, Level).
+
+log(Level, Str, Args) ->
+ {ok, LogLevel} = application:get_env(rebar, log_level),
+ case should_log(LogLevel, Level) of
+ true ->
+ io:format(log_prefix(Level) ++ Str, Args);
+ false ->
+ ok
+ end.
+
+default_level() -> error_level().
+
+%% ===================================================================
+%% Internal functions
+%% ===================================================================
+
+valid_level(Level) ->
+ erlang:max(error_level(), erlang:min(Level, debug_level())).
+
+error_level() -> 0.
+debug_level() -> 3.
+
+should_log(debug, _) -> true;
+should_log(info, debug) -> false;
+should_log(info, _) -> true;
+should_log(warn, debug) -> false;
+should_log(warn, info) -> false;
+should_log(warn, _) -> true;
+should_log(error, error) -> true;
+should_log(error, _) -> false;
+should_log(_, _) -> false.
+
+log_prefix(debug) -> "DEBUG: ";
+log_prefix(info) -> "INFO: ";
+log_prefix(warn) -> "WARN: ";
+log_prefix(error) -> "ERROR: ".
http://git-wip-us.apache.org/repos/asf/couchdb/blob/626455a4/src/rebar/src/rebar_neotoma_compiler.erl
----------------------------------------------------------------------
diff --git a/src/rebar/src/rebar_neotoma_compiler.erl b/src/rebar/src/rebar_neotoma_compiler.erl
new file mode 100644
index 0000000..5549dc4
--- /dev/null
+++ b/src/rebar/src/rebar_neotoma_compiler.erl
@@ -0,0 +1,163 @@
+%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ts=4 sw=4 et
+%% -------------------------------------------------------------------
+%%
+%% rebar: Erlang Build Tools
+%%
+%% Copyright (c) 2010 Cliff Moon (cliff@moonpolysoft.com)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -------------------------------------------------------------------
+
+%% The rebar_neotoma module is a plugin for rebar that compiles
+%% neotoma peg files. By default, it compiles all src/*.peg to src/*.erl
+%%
+%% Configuration options should be placed in rebar.config under
+%% neotoma_opts. Available options include:
+%%
+%% doc_root: where to find the peg files to compile.
+%% "src" by default
+%% out_dir: where to put the generated erl files.
+%% "src" by defualt
+%% module_ext: characters to append to the module's name.
+%% "" by default
+%% source_ext: extension of peg source files
+-module(rebar_neotoma_compiler).
+
+-export([compile/2]).
+
+%% for internal use only
+-export([info/2]).
+
+-include("rebar.hrl").
+
+%% ============================================================================
+%% Public API
+%% ============================================================================
+
+compile(Config, _AppFile) ->
+ NeoOpts = neotoma_opts(Config),
+ rebar_base_compiler:run(Config, [],
+ option(doc_root, NeoOpts), ".peg",
+ option(out_dir, NeoOpts),
+ option(module_ext, NeoOpts) ++ ".erl",
+ fun compile_neo/3, [{check_last_mod, true}]).
+
+%% ============================================================================
+%% Internal functions
+%% ============================================================================
+
+info(help, compile) ->
+ ?CONSOLE(
+ "Build Neotoma (*.peg) sources.~n"
+ "~n"
+ "Valid rebar.config options:~n"
+ " ~p~n",
+ [
+ {neotoma_opts, [{doc_root, "src"},
+ {out_dir, "src"},
+ {source_ext, ".peg"},
+ {module_ext, ""}]}
+ ]).
+
+neotoma_opts(Config) ->
+ rebar_config:get(Config, neotoma_opts, []).
+
+option(Opt, Options) ->
+ proplists:get_value(Opt, Options, default(Opt)).
+
+default(doc_root) -> "src";
+default(out_dir) -> "src";
+default(module_ext) -> "";
+default(source_ext) -> ".peg".
+
+compile_neo(Source, Target, Config) ->
+ case code:which(neotoma) of
+ non_existing ->
+ ?ERROR("~n===============================================~n"
+ " You need to install neotoma to compile PEG grammars~n"
+ " Download the latest tarball release from github~n"
+ " https://github.com/seancribbs/neotoma~n"
+ " and install it into your erlang library dir~n"
+ "===============================================~n~n", []),
+ ?FAIL;
+ _ ->
+ case needs_compile(Source, Target, Config) of
+ true ->
+ do_compile(Source, Target, Config);
+ false ->
+ skipped
+ end
+ end.
+
+do_compile(Source, _Target, Config) ->
+ %% TODO: Check last mod on target and referenced DTLs here..
+ NeoOpts = neotoma_opts(Config),
+ %% ensure that doc_root and out_dir are defined,
+ %% using defaults if necessary
+ Opts = [{output, option(out_dir, NeoOpts)},
+ {module, list_to_atom(filename:basename(Source, ".peg")
+ ++ option(module_ext, NeoOpts))}],
+ case neotoma:file(Source, Opts ++ NeoOpts) of
+ ok ->
+ ok;
+ Reason ->
+ ?ERROR("Compiling peg ~s failed:~n ~p~n",
+ [Source, Reason]),
+ ?FAIL
+ end.
+
+needs_compile(Source, Target, Config) ->
+ LM = filelib:last_modified(Target),
+ LM < filelib:last_modified(Source) orelse
+ lists:any(fun(D) -> LM < filelib:last_modified(D) end,
+ referenced_pegs(Source, Config)).
+
+referenced_pegs(Source, Config) ->
+ Set = referenced_pegs1([Source], Config,
+ sets:add_element(Source, sets:new())),
+ sets:to_list(sets:del_element(Source, Set)).
+
+referenced_pegs1(Step, Config, Seen) ->
+ NeoOpts = neotoma_opts(Config),
+ ExtMatch = re:replace(option(source_ext, NeoOpts), "\.", "\\\\\\\\.",
+ [{return, list}]),
+
+ ShOpts = [{use_stdout, false}, return_on_error],
+ AllRefs =
+ lists:append(
+ [begin
+ Cmd = lists:flatten(["grep -o [^\\\"]*",
+ ExtMatch, " ", F]),
+ case rebar_utils:sh(Cmd, ShOpts) of
+ {ok, Res} ->
+ string:tokens(Res, "\n");
+ {error, _} ->
+ ""
+ end
+ end || F <- Step]),
+ DocRoot = option(doc_root, NeoOpts),
+ WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ],
+ Existing = [F || F <- WithPaths, filelib:is_regular(F)],
+ New = sets:subtract(sets:from_list(Existing), Seen),
+ case sets:size(New) of
+ 0 -> Seen;
+ _ -> referenced_pegs1(sets:to_list(New), Config,
+ sets:union(New, Seen))
+ end.
http://git-wip-us.apache.org/repos/asf/couchdb/blob/626455a4/src/rebar/src/rebar_otp_app.erl
----------------------------------------------------------------------
diff --git a/src/rebar/src/rebar_otp_app.erl b/src/rebar/src/rebar_otp_app.erl
new file mode 100644
index 0000000..b3566c8
--- /dev/null
+++ b/src/rebar/src/rebar_otp_app.erl
@@ -0,0 +1,220 @@
+%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ts=4 sw=4 et
+%% -------------------------------------------------------------------
+%%
+%% rebar: Erlang Build Tools
+%%
+%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -------------------------------------------------------------------
+-module(rebar_otp_app).
+
+-export([compile/2,
+ clean/2]).
+
+%% for internal use only
+-export([info/2]).
+
+-include("rebar.hrl").
+
+%% ===================================================================
+%% Public API
+%% ===================================================================
+
+compile(Config, File) ->
+ %% If we get an .app.src file, it needs to be pre-processed and
+ %% written out as a ebin/*.app file. That resulting file will then
+ %% be validated as usual.
+ {Config1, AppFile} = case rebar_app_utils:is_app_src(File) of
+ true ->
+ preprocess(Config, File);
+ false ->
+ {Config, File}
+ end,
+
+ %% Load the app file and validate it.
+ case rebar_app_utils:load_app_file(Config1, AppFile) of
+ {ok, Config2, AppName, AppData} ->
+ validate_name(AppName, AppFile),
+
+ %% In general, the list of modules is an important thing to validate
+ %% for compliance with OTP guidelines and upgrade procedures.
+ %% However, some people prefer not to validate this list.
+ case rebar_config:get_local(Config1, validate_app_modules, true) of
+ true ->
+ Modules = proplists:get_value(modules, AppData),
+ {validate_modules(AppName, Modules), Config2};
+ false ->
+ {ok, Config2}
+ end;
+ {error, Reason} ->
+ ?ABORT("Failed to load app file ~s: ~p\n", [AppFile, Reason])
+ end.
+
+clean(_Config, File) ->
+ %% If the app file is a .app.src, delete the generated .app file
+ case rebar_app_utils:is_app_src(File) of
+ true ->
+ case file:delete(rebar_app_utils:app_src_to_app(File)) of
+ ok ->
+ ok;
+ {error, enoent} ->
+ %% The file not existing is OK, we can ignore the error.
+ ok;
+ Other ->
+ Other
+ end;
+ false ->
+ ok
+ end.
+
+%% ===================================================================
+%% Internal functions
+%% ===================================================================
+
+info(help, compile) ->
+ info_help("Validate .app file");
+info(help, clean) ->
+ info_help("Delete .app file if generated from .app.src").
+
+info_help(Description) ->
+ ?CONSOLE(
+ "~s.~n"
+ "~n"
+ "Valid rebar.config options:~n"
+ " ~p~n",
+ [
+ Description,
+ {validate_app_modules, true}
+ ]).
+
+preprocess(Config, AppSrcFile) ->
+ case rebar_app_utils:load_app_file(Config, AppSrcFile) of
+ {ok, Config1, AppName, AppData} ->
+ %% Look for a configuration file with vars we want to
+ %% substitute. Note that we include the list of modules available in
+ %% ebin/ and update the app data accordingly.
+ AppVars = load_app_vars(Config1) ++ [{modules, ebin_modules()}],
+ A1 = apply_app_vars(AppVars, AppData),
+
+
+ %% AppSrcFile may contain instructions for generating a vsn number
+ {Config2, Vsn} = rebar_app_utils:app_vsn(Config1, AppSrcFile),
+ A2 = lists:keystore(vsn, 1, A1, {vsn, Vsn}),
+
+ %% systools:make_relup/4 fails with {missing_param, registered}
+ %% without a 'registered' value.
+ A3 = ensure_registered(A2),
+
+ %% Build the final spec as a string
+ Spec = io_lib:format("~p.\n", [{application, AppName, A3}]),
+
+ %% Setup file .app filename and write new contents
+ AppFile = rebar_app_utils:app_src_to_app(AppSrcFile),
+ ok = rebar_file_utils:write_file_if_contents_differ(AppFile, Spec),
+
+ %% Make certain that the ebin/ directory is available
+ %% on the code path
+ true = code:add_path(filename:absname(filename:dirname(AppFile))),
+
+ {Config2, AppFile};
+
+ {error, Reason} ->
+ ?ABORT("Failed to read ~s for preprocessing: ~p\n",
+ [AppSrcFile, Reason])
+ end.
+
+load_app_vars(Config) ->
+ case rebar_config:get_local(Config, app_vars_file, undefined) of
+ undefined ->
+ ?INFO("No app_vars_file defined.\n", []),
+ [];
+ Filename ->
+ ?INFO("Loading app vars from ~p\n", [Filename]),
+ {ok, Vars} = file:consult(Filename),
+ Vars
+ end.
+
+apply_app_vars([], AppData) ->
+ AppData;
+apply_app_vars([{Key, Value} | Rest], AppData) ->
+ AppData2 = lists:keystore(Key, 1, AppData, {Key, Value}),
+ apply_app_vars(Rest, AppData2).
+
+validate_name(AppName, File) ->
+ %% Convert the .app file name to an atom -- check it against the
+ %% identifier within the file
+ ExpApp = list_to_atom(filename:basename(File, ".app")),
+ case ExpApp == AppName of
+ true ->
+ ok;
+ false ->
+ ?ERROR("Invalid ~s: name of application (~p) "
+ "must match filename.\n", [File, AppName]),
+ ?FAIL
+ end.
+
+validate_modules(AppName, undefined) ->
+ ?ERROR("Missing modules declaration in ~p.app~n", [AppName]),
+ ?FAIL;
+
+validate_modules(AppName, Mods) ->
+ %% Construct two sets -- one for the actual .beam files in ebin/
+ %% and one for the modules
+ %% listed in the .app file
+ EbinSet = ordsets:from_list(ebin_modules()),
+ ModSet = ordsets:from_list(Mods),
+
+ %% Identify .beam files listed in the .app, but not present in ebin/
+ case ordsets:subtract(ModSet, EbinSet) of
+ [] ->
+ ok;
+ MissingBeams ->
+ Msg1 = lists:flatten([io_lib:format("\t* ~p\n", [M]) ||
+ M <- MissingBeams]),
+ ?ERROR("One or more modules listed in ~p.app are not "
+ "present in ebin/*.beam:\n~s", [AppName, Msg1]),
+ ?FAIL
+ end,
+
+ %% Identify .beam files NOT list in the .app, but present in ebin/
+ case ordsets:subtract(EbinSet, ModSet) of
+ [] ->
+ ok;
+ MissingMods ->
+ Msg2 = lists:flatten([io_lib:format("\t* ~p\n", [M]) ||
+ M <- MissingMods]),
+ ?ERROR("One or more .beam files exist that are not "
+ "listed in ~p.app:\n~s", [AppName, Msg2]),
+ ?FAIL
+ end.
+
+ebin_modules() ->
+ lists:sort([rebar_utils:beam_to_mod("ebin", N) ||
+ N <- rebar_utils:beams("ebin")]).
+
+ensure_registered(AppData) ->
+ case lists:keyfind(registered, 1, AppData) of
+ false ->
+ [{registered, []} | AppData];
+ {registered, _} ->
+ %% We could further check whether the value is a list of atoms.
+ AppData
+ end.
http://git-wip-us.apache.org/repos/asf/couchdb/blob/626455a4/src/rebar/src/rebar_port_compiler.erl
----------------------------------------------------------------------
diff --git a/src/rebar/src/rebar_port_compiler.erl b/src/rebar/src/rebar_port_compiler.erl
new file mode 100644
index 0000000..0abb044
--- /dev/null
+++ b/src/rebar/src/rebar_port_compiler.erl
@@ -0,0 +1,607 @@
+%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ts=4 sw=4 et
+%% -------------------------------------------------------------------
+%%
+%% rebar: Erlang Build Tools
+%%
+%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -------------------------------------------------------------------
+-module(rebar_port_compiler).
+
+-export([compile/2,
+ clean/2]).
+
+%% for internal use only
+-export([setup_env/1,
+ info/2]).
+
+-include("rebar.hrl").
+
+%% ===================================================================
+%% Public API
+%% ===================================================================
+
+%% Supported configuration variables:
+%%
+%% * port_specs - Erlang list of tuples of the forms
+%% {ArchRegex, TargetFile, Sources, Options}
+%% {ArchRegex, TargetFile, Sources}
+%% {TargetFile, Sources}
+%%
+%% * port_env - Erlang list of key/value pairs which will control
+%% the environment when running the compiler and linker.
+%%
+%% By default, the following variables are defined:
+%% CC - C compiler
+%% CXX - C++ compiler
+%% CFLAGS - C compiler
+%% CXXFLAGS - C++ compiler
+%% LDFLAGS - Link flags
+%% ERL_CFLAGS - default -I paths for erts and ei
+%% ERL_LDFLAGS - default -L and -lerl_interface -lei
+%% DRV_CFLAGS - flags that will be used for compiling
+%% DRV_LDFLAGS - flags that will be used for linking
+%% EXE_CFLAGS - flags that will be used for compiling
+%% EXE_LDFLAGS - flags that will be used for linking
+%% ERL_EI_LIBDIR - ei library directory
+%% DRV_CXX_TEMPLATE - C++ command template
+%% DRV_CC_TEMPLATE - C command template
+%% DRV_LINK_TEMPLATE - Linker command template
+%% EXE_CXX_TEMPLATE - C++ command template
+%% EXE_CC_TEMPLATE - C command template
+%% EXE_LINK_TEMPLATE - Linker command template
+%% PORT_IN_FILES - contains a space separated list of input
+%% file(s), (used in command template)
+%% PORT_OUT_FILE - contains the output filename (used in
+%% command template)
+%%
+%% Note that if you wish to extend (vs. replace) these variables,
+%% you MUST include a shell-style reference in your definition.
+%% e.g. to extend CFLAGS, do something like:
+%%
+%% {port_env, [{"CFLAGS", "$CFLAGS -MyOtherOptions"}]}
+%%
+%% It is also possible to specify platform specific options
+%% by specifying a triplet where the first string is a regex
+%% that is checked against Erlang's system architecture string.
+%% e.g. to specify a CFLAG that only applies to x86_64 on linux
+%% do:
+%%
+%% {port_env, [{"x86_64.*-linux", "CFLAGS",
+%% "$CFLAGS -X86Options"}]}
+%%
+
+-record(spec, {type::'drv' | 'exe',
+ target::file:filename(),
+ sources = [] :: [file:filename(), ...],
+ objects = [] :: [file:filename(), ...],
+ opts = [] ::list() | []}).
+
+compile(Config, AppFile) ->
+ case get_specs(Config, AppFile) of
+ [] ->
+ ok;
+ Specs ->
+ SharedEnv = rebar_config:get_env(Config, rebar_deps) ++
+ rebar_config:get_env(Config, ?MODULE),
+
+ %% Compile each of the sources
+ NewBins = compile_sources(Config, Specs, SharedEnv),
+
+ %% Make sure that the target directories exist
+ ?INFO("Using specs ~p\n", [Specs]),
+ lists:foreach(fun(#spec{target=Target}) ->
+ ok = filelib:ensure_dir(Target)
+ end, Specs),
+
+ %% Only relink if necessary, given the Target
+ %% and list of new binaries
+ lists:foreach(
+ fun(#spec{target=Target, objects=Bins, opts=Opts}) ->
+ AllBins = [sets:from_list(Bins),
+ sets:from_list(NewBins)],
+ Intersection = sets:intersection(AllBins),
+ case needs_link(Target, sets:to_list(Intersection)) of
+ true ->
+ LinkTemplate = select_link_template(Target),
+ Env = proplists:get_value(env, Opts, SharedEnv),
+ Cmd = expand_command(LinkTemplate, Env,
+ string:join(Bins, " "),
+ Target),
+ rebar_utils:sh(Cmd, [{env, Env}]);
+ false ->
+ ?INFO("Skipping relink of ~s\n", [Target]),
+ ok
+ end
+ end, Specs)
+ end.
+
+clean(Config, AppFile) ->
+ case get_specs(Config, AppFile) of
+ [] ->
+ ok;
+ Specs ->
+ lists:foreach(fun(#spec{target=Target, objects=Objects}) ->
+ rebar_file_utils:delete_each([Target]),
+ rebar_file_utils:delete_each(Objects)
+ end, Specs)
+ end,
+ ok.
+
+setup_env(Config) ->
+ setup_env(Config, []).
+
+%% ===================================================================
+%% Internal functions
+%% ===================================================================
+
+info(help, compile) ->
+ info_help("Build port sources");
+info(help, clean) ->
+ info_help("Delete port build results").
+
+info_help(Description) ->
+ ?CONSOLE(
+ "~s.~n"
+ "~n"
+ "Valid rebar.config options:~n"
+ " ~p~n"
+ " ~p~n",
+ [
+ Description,
+ {port_env, [{"CFLAGS", "$CFLAGS -Ifoo"},
+ {"freebsd", "LDFLAGS", "$LDFLAGS -lfoo"}]},
+ {port_specs, [{"priv/so_name.so", ["c_src/*.c"]},
+ {"linux", "priv/hello_linux", ["c_src/hello_linux.c"]},
+ {"linux", "priv/hello_linux", ["c_src/*.c"], [{env, []}]}]}
+ ]).
+
+setup_env(Config, ExtraEnv) ->
+ %% Extract environment values from the config (if specified) and
+ %% merge with the default for this operating system. This enables
+ %% max flexibility for users.
+ DefaultEnv = filter_env(default_env(), []),
+
+ %% Get any port-specific envs; use port_env first and then fallback
+ %% to port_envs for compatibility
+ RawPortEnv = rebar_config:get_list(Config, port_env,
+ rebar_config:get_list(Config, port_envs, [])),
+
+ PortEnv = filter_env(RawPortEnv, []),
+ Defines = get_defines(Config),
+ OverrideEnv = Defines ++ PortEnv ++ filter_env(ExtraEnv, []),
+ RawEnv = apply_defaults(os_env(), DefaultEnv) ++ OverrideEnv,
+ expand_vars_loop(merge_each_var(RawEnv, [])).
+
+get_defines(Config) ->
+ RawDefines = rebar_config:get_xconf(Config, defines, []),
+ Defines = string:join(["-D" ++ D || D <- RawDefines], " "),
+ [{"ERL_CFLAGS", "$ERL_CFLAGS " ++ Defines}].
+
+replace_extension(File, NewExt) ->
+ OldExt = filename:extension(File),
+ replace_extension(File, OldExt, NewExt).
+
+replace_extension(File, OldExt, NewExt) ->
+ filename:rootname(File, OldExt) ++ NewExt.
+
+%%
+%% == compile and link ==
+%%
+
+compile_sources(Config, Specs, SharedEnv) ->
+ lists:foldl(
+ fun(#spec{sources=Sources, type=Type, opts=Opts}, NewBins) ->
+ Env = proplists:get_value(env, Opts, SharedEnv),
+ compile_each(Config, Sources, Type, Env, NewBins)
+ end, [], Specs).
+
+compile_each(_Config, [], _Type, _Env, NewBins) ->
+ lists:reverse(NewBins);
+compile_each(Config, [Source | Rest], Type, Env, NewBins) ->
+ Ext = filename:extension(Source),
+ Bin = replace_extension(Source, Ext, ".o"),
+ case needs_compile(Source, Bin) of
+ true ->
+ Template = select_compile_template(Type, compiler(Ext)),
+ Cmd = expand_command(Template, Env, Source, Bin),
+ ShOpts = [{env, Env}, return_on_error, {use_stdout, false}],
+ exec_compiler(Config, Source, Cmd, ShOpts),
+ compile_each(Config, Rest, Type, Env, [Bin | NewBins]);
+ false ->
+ ?INFO("Skipping ~s\n", [Source]),
+ compile_each(Config, Rest, Type, Env, NewBins)
+ end.
+
+exec_compiler(Config, Source, Cmd, ShOpts) ->
+ case rebar_utils:sh(Cmd, ShOpts) of
+ {error, {_RC, RawError}} ->
+ AbsSource = case rebar_utils:processing_base_dir(Config) of
+ true ->
+ Source;
+ false ->
+ filename:absname(Source)
+ end,
+ ?CONSOLE("Compiling ~s\n", [AbsSource]),
+ Error = re:replace(RawError, Source, AbsSource,
+ [{return, list}, global]),
+ ?CONSOLE("~s", [Error]),
+ ?FAIL;
+ {ok, Output} ->
+ ?CONSOLE("Compiling ~s\n", [Source]),
+ ?CONSOLE("~s", [Output])
+ end.
+
+needs_compile(Source, Bin) ->
+ %% TODO: Generate depends using gcc -MM so we can also
+ %% check for include changes
+ filelib:last_modified(Bin) < filelib:last_modified(Source).
+
+needs_link(SoName, []) ->
+ filelib:last_modified(SoName) == 0;
+needs_link(SoName, NewBins) ->
+ MaxLastMod = lists:max([filelib:last_modified(B) || B <- NewBins]),
+ case filelib:last_modified(SoName) of
+ 0 ->
+ ?DEBUG("Last mod is 0 on ~s\n", [SoName]),
+ true;
+ Other ->
+ ?DEBUG("Checking ~p >= ~p\n", [MaxLastMod, Other]),
+ MaxLastMod >= Other
+ end.
+
+%%
+%% == port_specs ==
+%%
+
+get_specs(Config, AppFile) ->
+ Specs = case rebar_config:get_local(Config, port_specs, []) of
+ [] ->
+ %% No spec provided. Construct a spec
+ %% from old-school so_name and sources
+ [port_spec_from_legacy(Config, AppFile)];
+ PortSpecs ->
+ Filtered = filter_port_specs(PortSpecs),
+ OsType = os:type(),
+ [get_port_spec(Config, OsType, Spec) || Spec <- Filtered]
+ end,
+ [S || S <- Specs, S#spec.sources /= []].
+
+port_spec_from_legacy(Config, AppFile) ->
+ %% Get the target from the so_name variable
+ Target = case rebar_config:get(Config, so_name, undefined) of
+ undefined ->
+ %% Generate a sensible default from app file
+ {_, AppName} = rebar_app_utils:app_name(Config, AppFile),
+ filename:join("priv",
+ lists:concat([AppName, "_drv.so"]));
+ AName ->
+ %% Old form is available -- use it
+ filename:join("priv", AName)
+ end,
+ %% Get the list of source files from port_sources
+ Sources = port_sources(rebar_config:get_list(Config, port_sources,
+ ["c_src/*.c"])),
+ #spec { type = target_type(Target),
+ target = maybe_switch_extension(os:type(), Target),
+ sources = Sources,
+ objects = port_objects(Sources) }.
+
+filter_port_specs(Specs) ->
+ [S || S <- Specs, filter_port_spec(S)].
+
+filter_port_spec({ArchRegex, _, _, _}) ->
+ rebar_utils:is_arch(ArchRegex);
+filter_port_spec({ArchRegex, _, _}) ->
+ rebar_utils:is_arch(ArchRegex);
+filter_port_spec({_, _}) ->
+ true.
+
+get_port_spec(Config, OsType, {Target, Sources}) ->
+ get_port_spec(Config, OsType, {undefined, Target, Sources, []});
+get_port_spec(Config, OsType, {Arch, Target, Sources}) ->
+ get_port_spec(Config, OsType, {Arch, Target, Sources, []});
+get_port_spec(Config, OsType, {_Arch, Target, Sources, Opts}) ->
+ SourceFiles = port_sources(Sources),
+ ObjectFiles = port_objects(SourceFiles),
+ #spec{type=target_type(Target),
+ target=maybe_switch_extension(OsType, Target),
+ sources=SourceFiles,
+ objects=ObjectFiles,
+ opts=port_opts(Config, Opts)}.
+
+port_sources(Sources) ->
+ lists:flatmap(fun filelib:wildcard/1, Sources).
+
+port_objects(SourceFiles) ->
+ [replace_extension(O, ".o") || O <- SourceFiles].
+
+port_opts(Config, Opts) ->
+ [port_opt(Config, O) || O <- Opts].
+
+port_opt(Config, {env, Env}) ->
+ {env, setup_env(Config, Env)};
+port_opt(_Config, Opt) ->
+ Opt.
+
+maybe_switch_extension({win32, nt}, Target) ->
+ switch_to_dll_or_exe(Target);
+maybe_switch_extension(_OsType, Target) ->
+ Target.
+
+switch_to_dll_or_exe(Target) ->
+ case filename:extension(Target) of
+ ".so" -> filename:rootname(Target, ".so") ++ ".dll";
+ [] -> Target ++ ".exe";
+ _Other -> Target
+ end.
+
+%%
+%% == port_env ==
+%%
+
+%%
+%% Choose a compiler variable, based on a provided extension
+%%
+compiler(".cc") -> "$CXX";
+compiler(".cp") -> "$CXX";
+compiler(".cxx") -> "$CXX";
+compiler(".cpp") -> "$CXX";
+compiler(".CPP") -> "$CXX";
+compiler(".c++") -> "$CXX";
+compiler(".C") -> "$CXX";
+compiler(_) -> "$CC".
+
+%%
+%% Given a list of {Key, Value} variables, and another list of default
+%% {Key, Value} variables, return a merged list where the rule is if the
+%% default is expandable expand it with the value of the variable list,
+%% otherwise just return the value of the variable.
+%%
+apply_defaults(Vars, Defaults) ->
+ dict:to_list(
+ dict:merge(fun(Key, VarValue, DefaultValue) ->
+ case is_expandable(DefaultValue) of
+ true ->
+ rebar_utils:expand_env_variable(DefaultValue,
+ Key,
+ VarValue);
+ false -> VarValue
+ end
+ end,
+ dict:from_list(Vars),
+ dict:from_list(Defaults))).
+
+%%
+%% Given a list of {Key, Value} environment variables, where Key may be defined
+%% multiple times, walk the list and expand each self-reference so that we
+%% end with a list of each variable singly-defined.
+%%
+merge_each_var([], Vars) ->
+ Vars;
+merge_each_var([{Key, Value} | Rest], Vars) ->
+ Evalue = case orddict:find(Key, Vars) of
+ error ->
+ %% Nothing yet defined for this key/value.
+ %% Expand any self-references as blank.
+ rebar_utils:expand_env_variable(Value, Key, "");
+ {ok, Value0} ->
+ %% Use previous definition in expansion
+ rebar_utils:expand_env_variable(Value, Key, Value0)
+ end,
+ merge_each_var(Rest, orddict:store(Key, Evalue, Vars)).
+
+%%
+%% Give a unique list of {Key, Value} environment variables, expand each one
+%% for every other key until no further expansions are possible.
+%%
+expand_vars_loop(Vars) ->
+ expand_vars_loop(Vars, [], dict:from_list(Vars), 10).
+
+expand_vars_loop(_Pending, _Recurse, _Vars, 0) ->
+ ?ABORT("Max. expansion reached for ENV vars!\n", []);
+expand_vars_loop([], [], Vars, _Count) ->
+ lists:keysort(1, dict:to_list(Vars));
+expand_vars_loop([], Recurse, Vars, Count) ->
+ expand_vars_loop(Recurse, [], Vars, Count-1);
+expand_vars_loop([{K, V} | Rest], Recurse, Vars, Count) ->
+ %% Identify the variables that need expansion in this value
+ ReOpts = [global, {capture, all_but_first, list}, unicode],
+ case re:run(V, "\\\${?(\\w+)}?", ReOpts) of
+ {match, Matches} ->
+ %% Identify the unique variables that need to be expanded
+ UniqueMatches = lists:usort([M || [M] <- Matches]),
+
+ %% For each variable, expand it and return the final
+ %% value. Note that if we have a bunch of unresolvable
+ %% variables, nothing happens and we don't bother
+ %% attempting further expansion
+ case expand_keys_in_value(UniqueMatches, V, Vars) of
+ V ->
+ %% No change after expansion; move along
+ expand_vars_loop(Rest, Recurse, Vars, Count);
+ Expanded ->
+ %% Some expansion occurred; move to next k/v but
+ %% revisit this value in the next loop to check
+ %% for further expansion
+ NewVars = dict:store(K, Expanded, Vars),
+ expand_vars_loop(Rest, [{K, Expanded} | Recurse],
+ NewVars, Count)
+ end;
+
+ nomatch ->
+ %% No values in this variable need expansion; move along
+ expand_vars_loop(Rest, Recurse, Vars, Count)
+ end.
+
+expand_keys_in_value([], Value, _Vars) ->
+ Value;
+expand_keys_in_value([Key | Rest], Value, Vars) ->
+ NewValue = case dict:find(Key, Vars) of
+ {ok, KValue} ->
+ rebar_utils:expand_env_variable(Value, Key, KValue);
+ error ->
+ Value
+ end,
+ expand_keys_in_value(Rest, NewValue, Vars).
+
+expand_command(TmplName, Env, InFiles, OutFile) ->
+ Cmd0 = proplists:get_value(TmplName, Env),
+ Cmd1 = rebar_utils:expand_env_variable(Cmd0, "PORT_IN_FILES", InFiles),
+ rebar_utils:expand_env_variable(Cmd1, "PORT_OUT_FILE", OutFile).
+
+%%
+%% Given a string, determine if it is expandable
+%%
+is_expandable(InStr) ->
+ case re:run(InStr,"\\\$",[{capture,none}]) of
+ match -> true;
+ nomatch -> false
+ end.
+
+%%
+%% Filter a list of env vars such that only those which match the provided
+%% architecture regex (or do not have a regex) are returned.
+%%
+filter_env([], Acc) ->
+ lists:reverse(Acc);
+filter_env([{ArchRegex, Key, Value} | Rest], Acc) ->
+ case rebar_utils:is_arch(ArchRegex) of
+ true ->
+ filter_env(Rest, [{Key, Value} | Acc]);
+ false ->
+ filter_env(Rest, Acc)
+ end;
+filter_env([{Key, Value} | Rest], Acc) ->
+ filter_env(Rest, [{Key, Value} | Acc]).
+
+erts_dir() ->
+ lists:concat([code:root_dir(), "/erts-", erlang:system_info(version)]).
+
+os_env() ->
+ ReOpts = [{return, list}, {parts, 2}, unicode],
+ Os = [list_to_tuple(re:split(S, "=", ReOpts)) || S <- os:getenv()],
+ %% Drop variables without a name (win32)
+ [T1 || {K, _V} = T1 <- Os, K =/= []].
+
+select_compile_template(drv, Compiler) ->
+ select_compile_drv_template(Compiler);
+select_compile_template(exe, Compiler) ->
+ select_compile_exe_template(Compiler).
+
+select_compile_drv_template("$CC") -> "DRV_CC_TEMPLATE";
+select_compile_drv_template("$CXX") -> "DRV_CXX_TEMPLATE".
+
+select_compile_exe_template("$CC") -> "EXE_CC_TEMPLATE";
+select_compile_exe_template("$CXX") -> "EXE_CXX_TEMPLATE".
+
+select_link_template(Target) ->
+ case target_type(Target) of
+ drv -> "DRV_LINK_TEMPLATE";
+ exe -> "EXE_LINK_TEMPLATE"
+ end.
+
+target_type(Target) -> target_type1(filename:extension(Target)).
+
+target_type1(".so") -> drv;
+target_type1(".dll") -> drv;
+target_type1("") -> exe;
+target_type1(".exe") -> exe.
+
+erl_interface_dir(Subdir) ->
+ case code:lib_dir(erl_interface, Subdir) of
+ {error, bad_name} ->
+ throw({error, {erl_interface,Subdir,"code:lib_dir(erl_interface)"
+ "is unable to find the erl_interface library."}});
+ Dir -> Dir
+ end.
+
+default_env() ->
+ [
+ {"CC" , "cc"},
+ {"CXX", "c++"},
+ {"DRV_CXX_TEMPLATE",
+ "$CXX -c $CXXFLAGS $DRV_CFLAGS $PORT_IN_FILES -o $PORT_OUT_FILE"},
+ {"DRV_CC_TEMPLATE",
+ "$CC -c $CFLAGS $DRV_CFLAGS $PORT_IN_FILES -o $PORT_OUT_FILE"},
+ {"DRV_LINK_TEMPLATE",
+ "$CC $PORT_IN_FILES $LDFLAGS $DRV_LDFLAGS -o $PORT_OUT_FILE"},
+ {"EXE_CXX_TEMPLATE",
+ "$CXX -c $CXXFLAGS $EXE_CFLAGS $PORT_IN_FILES -o $PORT_OUT_FILE"},
+ {"EXE_CC_TEMPLATE",
+ "$CC -c $CFLAGS $EXE_CFLAGS $PORT_IN_FILES -o $PORT_OUT_FILE"},
+ {"EXE_LINK_TEMPLATE",
+ "$CC $PORT_IN_FILES $LDFLAGS $EXE_LDFLAGS -o $PORT_OUT_FILE"},
+ {"DRV_CFLAGS" , "-g -Wall -fPIC $ERL_CFLAGS"},
+ {"DRV_LDFLAGS", "-shared $ERL_LDFLAGS"},
+ {"EXE_CFLAGS" , "-g -Wall -fPIC $ERL_CFLAGS"},
+ {"EXE_LDFLAGS", "$ERL_LDFLAGS"},
+
+ {"ERL_CFLAGS", lists:concat([" -I", erl_interface_dir(include),
+ " -I", filename:join(erts_dir(), "include"),
+ " "])},
+ {"ERL_EI_LIBDIR", erl_interface_dir(lib)},
+ {"ERL_LDFLAGS" , " -L$ERL_EI_LIBDIR -lerl_interface -lei"},
+ {"ERLANG_ARCH" , rebar_utils:wordsize()},
+ {"ERLANG_TARGET", rebar_utils:get_arch()},
+
+ {"darwin", "DRV_LDFLAGS",
+ "-bundle -flat_namespace -undefined suppress $ERL_LDFLAGS"},
+
+ %% Solaris specific flags
+ {"solaris.*-64$", "CFLAGS", "-D_REENTRANT -m64 $CFLAGS"},
+ {"solaris.*-64$", "CXXFLAGS", "-D_REENTRANT -m64 $CXXFLAGS"},
+ {"solaris.*-64$", "LDFLAGS", "-m64 $LDFLAGS"},
+
+ %% OS X Leopard flags for 64-bit
+ {"darwin9.*-64$", "CFLAGS", "-m64 $CFLAGS"},
+ {"darwin9.*-64$", "CXXFLAGS", "-m64 $CXXFLAGS"},
+ {"darwin9.*-64$", "LDFLAGS", "-arch x86_64 $LDFLAGS"},
+
+ %% OS X Snow Leopard, Lion, and Mountain Lion flags for 32-bit
+ {"darwin1[0-2].*-32", "CFLAGS", "-m32 $CFLAGS"},
+ {"darwin1[0-2].*-32", "CXXFLAGS", "-m32 $CXXFLAGS"},
+ {"darwin1[0-2].*-32", "LDFLAGS", "-arch i386 $LDFLAGS"},
+
+ %% Windows specific flags
+ %% add MS Visual C++ support to rebar on Windows
+ {"win32", "CC", "cl.exe"},
+ {"win32", "CXX", "cl.exe"},
+ {"win32", "LINKER", "link.exe"},
+ {"win32", "DRV_CXX_TEMPLATE",
+ %% DRV_* and EXE_* Templates are identical
+ "$CXX /c $CXXFLAGS $DRV_CFLAGS $PORT_IN_FILES /Fo$PORT_OUT_FILE"},
+ {"win32", "DRV_CC_TEMPLATE",
+ "$CC /c $CFLAGS $DRV_CFLAGS $PORT_IN_FILES /Fo$PORT_OUT_FILE"},
+ {"win32", "DRV_LINK_TEMPLATE",
+ "$LINKER $PORT_IN_FILES $LDFLAGS $DRV_LDFLAGS /OUT:$PORT_OUT_FILE"},
+ %% DRV_* and EXE_* Templates are identical
+ {"win32", "EXE_CXX_TEMPLATE",
+ "$CXX /c $CXXFLAGS $EXE_CFLAGS $PORT_IN_FILES /Fo$PORT_OUT_FILE"},
+ {"win32", "EXE_CC_TEMPLATE",
+ "$CC /c $CFLAGS $EXE_CFLAGS $PORT_IN_FILES /Fo$PORT_OUT_FILE"},
+ {"win32", "EXE_LINK_TEMPLATE",
+ "$LINKER $PORT_IN_FILES $LDFLAGS $EXE_LDFLAGS /OUT:$PORT_OUT_FILE"},
+ %% ERL_CFLAGS are ok as -I even though strictly it should be /I
+ {"win32", "ERL_LDFLAGS", " /LIBPATH:$ERL_EI_LIBDIR erl_interface.lib ei.lib"},
+ {"win32", "DRV_CFLAGS", "/Zi /Wall $ERL_CFLAGS"},
+ {"win32", "DRV_LDFLAGS", "/DLL $ERL_LDFLAGS"}
+ ].