2011-01-31 16:43:31 +00:00
|
|
|
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
|
2009-12-31 18:42:53 +00:00
|
|
|
%% ex: ts=4 sw=4 et
|
2009-11-30 23:03:45 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% 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,
|
2011-04-05 14:04:03 +00:00
|
|
|
clean/2,
|
|
|
|
setup_env/1]).
|
2009-11-30 23:03:45 +00:00
|
|
|
|
|
|
|
-include("rebar.hrl").
|
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Public API
|
|
|
|
%% ===================================================================
|
|
|
|
|
2009-12-01 17:38:30 +00:00
|
|
|
%% Supported configuration variables:
|
|
|
|
%%
|
2012-01-03 17:59:36 +00:00
|
|
|
%% * port_specs - Erlang list of tuples of the forms
|
2012-02-07 19:15:58 +00:00
|
|
|
%% {ArchRegex, TargetFile, Sources, Options}
|
|
|
|
%% {ArchRegex, TargetFile, Sources}
|
|
|
|
%% {TargetFile, Sources}
|
2010-04-28 23:27:54 +00:00
|
|
|
%%
|
2012-03-09 19:00:15 +00:00
|
|
|
%% * port_env - Erlang list of key/value pairs which will control
|
|
|
|
%% the environment when running the compiler and linker.
|
2011-01-28 15:08:27 +00:00
|
|
|
%%
|
2012-03-09 19:00:15 +00:00
|
|
|
%% 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)
|
2009-12-01 17:38:30 +00:00
|
|
|
%%
|
2012-03-09 19:00:15 +00:00
|
|
|
%% 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:
|
2009-12-01 17:38:30 +00:00
|
|
|
%%
|
2012-03-09 19:00:15 +00:00
|
|
|
%% {port_env, [{"CFLAGS", "$CFLAGS -MyOtherOptions"}]}
|
2009-12-01 17:38:30 +00:00
|
|
|
%%
|
2012-03-09 19:00:15 +00:00
|
|
|
%% 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:
|
2009-12-01 17:38:30 +00:00
|
|
|
%%
|
2012-03-09 19:00:15 +00:00
|
|
|
%% {port_env, [{"x86_64.*-linux", "CFLAGS",
|
|
|
|
%% "$CFLAGS -X86Options"}]}
|
2009-12-01 17:38:30 +00:00
|
|
|
%%
|
2009-11-30 23:03:45 +00:00
|
|
|
|
2012-02-07 19:15:58 +00:00
|
|
|
-record(spec, {type::'drv' | 'exe',
|
|
|
|
target::file:filename(),
|
2012-07-23 18:28:57 +00:00
|
|
|
sources = [] :: [file:filename(), ...],
|
|
|
|
objects = [] :: [file:filename(), ...],
|
2012-02-07 19:15:58 +00:00
|
|
|
opts = [] ::list() | []}).
|
|
|
|
|
2012-11-11 20:03:24 +00:00
|
|
|
compile(Config, AppFile) ->
|
|
|
|
case get_specs(Config, AppFile) of
|
2012-07-23 18:28:57 +00:00
|
|
|
[] ->
|
|
|
|
ok;
|
|
|
|
Specs ->
|
2012-02-07 19:15:58 +00:00
|
|
|
SharedEnv = rebar_config:get_env(Config, ?MODULE),
|
2009-12-30 12:13:39 +00:00
|
|
|
|
2009-11-30 23:03:45 +00:00
|
|
|
%% Compile each of the sources
|
2012-08-05 17:56:27 +00:00
|
|
|
NewBins = compile_sources(Config, Specs, SharedEnv),
|
2009-12-01 17:38:30 +00:00
|
|
|
|
2012-02-07 19:15:58 +00:00
|
|
|
%% Make sure that the target directories exist
|
2012-01-03 17:59:36 +00:00
|
|
|
?INFO("Using specs ~p\n", [Specs]),
|
2012-02-07 19:15:58 +00:00
|
|
|
lists:foreach(fun(#spec{target=Target}) ->
|
2012-01-03 17:59:36 +00:00
|
|
|
ok = filelib:ensure_dir(Target)
|
|
|
|
end, Specs),
|
|
|
|
|
|
|
|
%% Only relink if necessary, given the Target
|
2011-01-28 15:08:27 +00:00
|
|
|
%% and list of new binaries
|
|
|
|
lists:foreach(
|
2012-02-07 19:15:58 +00:00
|
|
|
fun(#spec{target=Target, objects=Bins, opts=Opts}) ->
|
2011-05-23 14:26:24 +00:00
|
|
|
AllBins = [sets:from_list(Bins),
|
|
|
|
sets:from_list(NewBins)],
|
2011-05-23 10:24:55 +00:00
|
|
|
Intersection = sets:intersection(AllBins),
|
2012-01-03 17:59:36 +00:00
|
|
|
case needs_link(Target, sets:to_list(Intersection)) of
|
2011-05-23 10:24:55 +00:00
|
|
|
true ->
|
2012-01-03 17:59:36 +00:00
|
|
|
LinkTemplate = select_link_template(Target),
|
2012-02-07 19:15:58 +00:00
|
|
|
Env = proplists:get_value(env, Opts, SharedEnv),
|
2012-01-03 17:59:36 +00:00
|
|
|
Cmd = expand_command(LinkTemplate, Env,
|
2011-04-30 20:47:15 +00:00
|
|
|
string:join(Bins, " "),
|
2012-01-03 17:59:36 +00:00
|
|
|
Target),
|
2011-04-30 20:47:15 +00:00
|
|
|
rebar_utils:sh(Cmd, [{env, Env}]);
|
2011-05-23 10:24:55 +00:00
|
|
|
false ->
|
2012-01-03 17:59:36 +00:00
|
|
|
?INFO("Skipping relink of ~s\n", [Target]),
|
2011-05-23 10:24:55 +00:00
|
|
|
ok
|
|
|
|
end
|
2012-01-03 17:59:36 +00:00
|
|
|
end, Specs)
|
2009-11-30 23:03:45 +00:00
|
|
|
end.
|
|
|
|
|
2012-11-11 20:03:24 +00:00
|
|
|
clean(Config, AppFile) ->
|
|
|
|
case get_specs(Config, AppFile) of
|
2012-07-23 18:28:57 +00:00
|
|
|
[] ->
|
|
|
|
ok;
|
|
|
|
Specs ->
|
2012-02-07 19:15:58 +00:00
|
|
|
lists:foreach(fun(#spec{target=Target, objects=Objects}) ->
|
|
|
|
rebar_file_utils:delete_each([Target]),
|
|
|
|
rebar_file_utils:delete_each(Objects)
|
|
|
|
end, Specs)
|
2012-04-22 19:53:32 +00:00
|
|
|
end,
|
2012-07-23 18:28:57 +00:00
|
|
|
ok.
|
2009-12-01 17:38:30 +00:00
|
|
|
|
2011-04-05 14:04:03 +00:00
|
|
|
setup_env(Config) ->
|
2012-02-07 19:15:58 +00:00
|
|
|
setup_env(Config, []).
|
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
|
|
|
|
setup_env(Config, ExtraEnv) ->
|
2011-04-05 14:04:03 +00:00
|
|
|
%% Extract environment values from the config (if specified) and
|
|
|
|
%% merge with the default for this operating system. This enables
|
|
|
|
%% max flexibility for users.
|
2012-03-09 19:00:15 +00:00
|
|
|
DefaultEnv = filter_env(default_env(), []),
|
2012-11-11 20:03:24 +00:00
|
|
|
|
|
|
|
%% 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, [])),
|
|
|
|
|
2012-07-23 18:28:57 +00:00
|
|
|
PortEnv = filter_env(RawPortEnv, []),
|
2012-07-28 14:04:51 +00:00
|
|
|
Defines = get_defines(Config),
|
|
|
|
OverrideEnv = Defines ++ PortEnv ++ filter_env(ExtraEnv, []),
|
2012-03-09 19:00:15 +00:00
|
|
|
RawEnv = apply_defaults(os_env(), DefaultEnv) ++ OverrideEnv,
|
2011-04-05 14:04:03 +00:00
|
|
|
expand_vars_loop(merge_each_var(RawEnv, [])).
|
2009-11-30 23:03:45 +00:00
|
|
|
|
2012-07-28 14:04:51 +00:00
|
|
|
get_defines(Config) ->
|
|
|
|
RawDefines = rebar_config:get_xconf(Config, defines, []),
|
|
|
|
Defines = string:join(["-D" ++ D || D <- RawDefines], " "),
|
|
|
|
[{"ERL_CFLAGS", "$ERL_CFLAGS " ++ Defines}].
|
2011-10-22 20:25:40 +00:00
|
|
|
|
2012-02-07 19:15:58 +00:00
|
|
|
replace_extension(File, NewExt) ->
|
|
|
|
OldExt = filename:extension(File),
|
|
|
|
replace_extension(File, OldExt, NewExt).
|
2012-01-03 17:59:36 +00:00
|
|
|
|
2012-02-07 19:15:58 +00:00
|
|
|
replace_extension(File, OldExt, NewExt) ->
|
|
|
|
filename:rootname(File, OldExt) ++ NewExt.
|
2012-01-03 17:59:36 +00:00
|
|
|
|
2012-02-07 19:15:58 +00:00
|
|
|
%%
|
|
|
|
%% == compile and link ==
|
|
|
|
%%
|
2012-01-03 17:59:36 +00:00
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
compile_sources(Config, Specs, SharedEnv) ->
|
2012-02-07 19:15:58 +00:00
|
|
|
lists:foldl(
|
|
|
|
fun(#spec{sources=Sources, type=Type, opts=Opts}, NewBins) ->
|
|
|
|
Env = proplists:get_value(env, Opts, SharedEnv),
|
2012-08-05 17:56:27 +00:00
|
|
|
compile_each(Config, Sources, Type, Env, NewBins)
|
2012-07-23 18:28:57 +00:00
|
|
|
end, [], Specs).
|
2012-02-07 19:15:58 +00:00
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
compile_each(_Config, [], _Type, _Env, NewBins) ->
|
2012-02-07 19:15:58 +00:00
|
|
|
lists:reverse(NewBins);
|
2012-08-05 17:56:27 +00:00
|
|
|
compile_each(Config, [Source | Rest], Type, Env, NewBins) ->
|
2009-11-30 23:03:45 +00:00
|
|
|
Ext = filename:extension(Source),
|
2012-02-07 19:15:58 +00:00
|
|
|
Bin = replace_extension(Source, Ext, ".o"),
|
2009-12-01 17:38:30 +00:00
|
|
|
case needs_compile(Source, Bin) of
|
|
|
|
true ->
|
2012-01-03 17:59:36 +00:00
|
|
|
Template = select_compile_template(Type, compiler(Ext)),
|
2012-05-22 18:03:46 +00:00
|
|
|
Cmd = expand_command(Template, Env, Source, Bin),
|
|
|
|
ShOpts = [{env, Env}, return_on_error, {use_stdout, false}],
|
2012-08-05 17:56:27 +00:00
|
|
|
exec_compiler(Config, Source, Cmd, ShOpts),
|
|
|
|
compile_each(Config, Rest, Type, Env, [Bin | NewBins]);
|
2009-12-01 17:38:30 +00:00
|
|
|
false ->
|
|
|
|
?INFO("Skipping ~s\n", [Source]),
|
2012-08-05 17:56:27 +00:00
|
|
|
compile_each(Config, Rest, Type, Env, NewBins)
|
2009-12-01 17:38:30 +00:00
|
|
|
end.
|
2009-11-30 23:03:45 +00:00
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
exec_compiler(Config, Source, Cmd, ShOpts) ->
|
2012-05-22 18:03:46 +00:00
|
|
|
case rebar_utils:sh(Cmd, ShOpts) of
|
|
|
|
{error, {_RC, RawError}} ->
|
2012-08-05 17:56:27 +00:00
|
|
|
AbsSource = case rebar_utils:processing_base_dir(Config) of
|
|
|
|
true ->
|
|
|
|
Source;
|
|
|
|
false ->
|
|
|
|
filename:absname(Source)
|
|
|
|
end,
|
2012-05-22 18:03:46 +00:00
|
|
|
?CONSOLE("Compiling ~s\n", [AbsSource]),
|
|
|
|
Error = re:replace(RawError, Source, AbsSource,
|
|
|
|
[{return, list}, global]),
|
|
|
|
?CONSOLE("~s", [Error]),
|
2012-07-28 17:51:57 +00:00
|
|
|
?FAIL;
|
2012-05-22 18:03:46 +00:00
|
|
|
{ok, Output} ->
|
|
|
|
?CONSOLE("Compiling ~s\n", [Source]),
|
|
|
|
?CONSOLE("~s", [Output])
|
|
|
|
end.
|
|
|
|
|
2009-11-30 23:03:45 +00:00
|
|
|
needs_compile(Source, Bin) ->
|
2011-01-28 15:08:27 +00:00
|
|
|
%% TODO: Generate depends using gcc -MM so we can also
|
|
|
|
%% check for include changes
|
2009-11-30 23:03:45 +00:00
|
|
|
filelib:last_modified(Bin) < filelib:last_modified(Source).
|
|
|
|
|
2009-12-01 17:38:30 +00:00
|
|
|
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 ->
|
2009-12-16 15:27:14 +00:00
|
|
|
?DEBUG("Last mod is 0 on ~s\n", [SoName]),
|
2009-12-01 17:38:30 +00:00
|
|
|
true;
|
|
|
|
Other ->
|
2011-04-28 16:39:46 +00:00
|
|
|
?DEBUG("Checking ~p >= ~p\n", [MaxLastMod, Other]),
|
2009-12-16 15:27:14 +00:00
|
|
|
MaxLastMod >= Other
|
2009-12-01 17:38:30 +00:00
|
|
|
end.
|
2009-12-30 12:13:39 +00:00
|
|
|
|
2012-02-07 19:15:58 +00:00
|
|
|
%%
|
|
|
|
%% == port_specs ==
|
|
|
|
%%
|
|
|
|
|
2012-11-11 20:03:24 +00:00
|
|
|
get_specs(Config, AppFile) ->
|
2012-11-13 03:18:00 +00:00
|
|
|
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 /= []].
|
2012-11-11 20:03:24 +00:00
|
|
|
|
|
|
|
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) }.
|
2012-02-07 19:15:58 +00:00
|
|
|
|
|
|
|
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
|
2012-05-14 20:14:17 +00:00
|
|
|
".so" -> filename:rootname(Target, ".so") ++ ".dll";
|
|
|
|
[] -> Target ++ ".exe";
|
|
|
|
_Other -> Target
|
2012-02-07 19:15:58 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% == port_env ==
|
|
|
|
%%
|
2009-11-30 23:03:45 +00:00
|
|
|
|
2009-12-01 17:38:30 +00:00
|
|
|
%%
|
|
|
|
%% Choose a compiler variable, based on a provided extension
|
|
|
|
%%
|
2009-11-30 23:03:45 +00:00
|
|
|
compiler(".cc") -> "$CXX";
|
|
|
|
compiler(".cp") -> "$CXX";
|
|
|
|
compiler(".cxx") -> "$CXX";
|
|
|
|
compiler(".cpp") -> "$CXX";
|
|
|
|
compiler(".CPP") -> "$CXX";
|
|
|
|
compiler(".c++") -> "$CXX";
|
|
|
|
compiler(".C") -> "$CXX";
|
|
|
|
compiler(_) -> "$CC".
|
2009-12-01 17:38:30 +00:00
|
|
|
|
2011-05-12 21:14:24 +00:00
|
|
|
%%
|
|
|
|
%% 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(
|
2011-05-23 14:26:24 +00:00
|
|
|
dict:merge(fun(Key, VarValue, DefaultValue) ->
|
|
|
|
case is_expandable(DefaultValue) of
|
2011-05-23 10:24:55 +00:00
|
|
|
true ->
|
2011-08-08 18:12:04 +00:00
|
|
|
rebar_utils:expand_env_variable(DefaultValue,
|
2011-08-20 16:20:07 +00:00
|
|
|
Key,
|
|
|
|
VarValue);
|
2011-05-23 10:24:55 +00:00
|
|
|
false -> VarValue
|
2011-05-23 14:26:24 +00:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
dict:from_list(Vars),
|
|
|
|
dict:from_list(Defaults))).
|
|
|
|
|
2010-02-16 14:05:29 +00:00
|
|
|
%%
|
|
|
|
%% 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) ->
|
2011-05-11 22:14:25 +00:00
|
|
|
Evalue = case orddict:find(Key, Vars) of
|
|
|
|
error ->
|
|
|
|
%% Nothing yet defined for this key/value.
|
|
|
|
%% Expand any self-references as blank.
|
2011-08-08 18:12:04 +00:00
|
|
|
rebar_utils:expand_env_variable(Value, Key, "");
|
2011-05-11 22:14:25 +00:00
|
|
|
{ok, Value0} ->
|
|
|
|
%% Use previous definition in expansion
|
2011-08-08 18:12:04 +00:00
|
|
|
rebar_utils:expand_env_variable(Value, Key, Value0)
|
2011-05-11 22:14:25 +00:00
|
|
|
end,
|
2010-02-16 14:05:29 +00:00
|
|
|
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) ->
|
2011-10-26 05:59:50 +00:00
|
|
|
expand_vars_loop(Vars, [], dict:from_list(Vars), 10).
|
2010-02-16 14:05:29 +00:00
|
|
|
|
2011-10-26 05:59:50 +00:00
|
|
|
expand_vars_loop(_Pending, _Recurse, _Vars, 0) ->
|
2010-02-16 14:05:29 +00:00
|
|
|
?ABORT("Max. expansion reached for ENV vars!\n", []);
|
2011-10-26 05:59:50 +00:00
|
|
|
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
|
2012-11-20 09:26:31 +00:00
|
|
|
ReOpts = [global, {capture, all_but_first, list}, unicode],
|
2011-10-26 18:08:35 +00:00
|
|
|
case re:run(V, "\\\${?(\\w+)}?", ReOpts) of
|
2011-10-26 05:59:50 +00:00
|
|
|
{match, Matches} ->
|
|
|
|
%% Identify the unique variables that need to be expanded
|
|
|
|
UniqueMatches = lists:usort([M || [M] <- Matches]),
|
|
|
|
|
2011-10-26 18:08:35 +00:00
|
|
|
%% 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
|
2011-10-26 05:59:50 +00:00
|
|
|
case expand_keys_in_value(UniqueMatches, V, Vars) of
|
|
|
|
V ->
|
|
|
|
%% No change after expansion; move along
|
|
|
|
expand_vars_loop(Rest, Recurse, Vars, Count);
|
|
|
|
Expanded ->
|
2011-10-26 18:08:35 +00:00
|
|
|
%% Some expansion occurred; move to next k/v but
|
|
|
|
%% revisit this value in the next loop to check
|
|
|
|
%% for further expansion
|
2011-10-26 05:59:50 +00:00
|
|
|
NewVars = dict:store(K, Expanded, Vars),
|
2011-10-26 18:08:35 +00:00
|
|
|
expand_vars_loop(Rest, [{K, Expanded} | Recurse],
|
|
|
|
NewVars, Count)
|
2011-10-26 05:59:50 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
nomatch ->
|
|
|
|
%% No values in this variable need expansion; move along
|
|
|
|
expand_vars_loop(Rest, Recurse, Vars, Count)
|
2010-02-16 14:05:29 +00:00
|
|
|
end.
|
|
|
|
|
2011-10-26 05:59:50 +00:00
|
|
|
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).
|
|
|
|
|
2011-04-30 20:47:15 +00:00
|
|
|
expand_command(TmplName, Env, InFiles, OutFile) ->
|
|
|
|
Cmd0 = proplists:get_value(TmplName, Env),
|
2011-08-08 18:12:04 +00:00
|
|
|
Cmd1 = rebar_utils:expand_env_variable(Cmd0, "PORT_IN_FILES", InFiles),
|
2012-06-17 14:11:59 +00:00
|
|
|
rebar_utils:expand_env_variable(Cmd1, "PORT_OUT_FILE", OutFile).
|
2011-04-30 20:47:15 +00:00
|
|
|
|
2011-05-12 21:14:24 +00:00
|
|
|
%%
|
|
|
|
%% Given a string, determine if it is expandable
|
|
|
|
%%
|
|
|
|
is_expandable(InStr) ->
|
|
|
|
case re:run(InStr,"\\\$",[{capture,none}]) of
|
|
|
|
match -> true;
|
|
|
|
nomatch -> false
|
|
|
|
end.
|
|
|
|
|
2009-12-01 17:38:30 +00:00
|
|
|
%%
|
|
|
|
%% Filter a list of env vars such that only those which match the provided
|
|
|
|
%% architecture regex (or do not have a regex) are returned.
|
|
|
|
%%
|
2012-03-09 19:00:15 +00:00
|
|
|
filter_env([], Acc) ->
|
2009-12-01 17:38:30 +00:00
|
|
|
lists:reverse(Acc);
|
2012-03-09 19:00:15 +00:00
|
|
|
filter_env([{ArchRegex, Key, Value} | Rest], Acc) ->
|
2009-12-01 17:38:30 +00:00
|
|
|
case rebar_utils:is_arch(ArchRegex) of
|
|
|
|
true ->
|
2012-03-09 19:00:15 +00:00
|
|
|
filter_env(Rest, [{Key, Value} | Acc]);
|
2009-12-01 17:38:30 +00:00
|
|
|
false ->
|
2012-03-09 19:00:15 +00:00
|
|
|
filter_env(Rest, Acc)
|
2009-12-01 17:38:30 +00:00
|
|
|
end;
|
2012-03-09 19:00:15 +00:00
|
|
|
filter_env([{Key, Value} | Rest], Acc) ->
|
|
|
|
filter_env(Rest, [{Key, Value} | Acc]).
|
2009-11-30 23:03:45 +00:00
|
|
|
|
|
|
|
erts_dir() ->
|
|
|
|
lists:concat([code:root_dir(), "/erts-", erlang:system_info(version)]).
|
|
|
|
|
2010-02-16 14:05:29 +00:00
|
|
|
os_env() ->
|
2012-11-20 09:26:31 +00:00
|
|
|
ReOpts = [{return, list}, {parts, 2}, unicode],
|
|
|
|
Os = [list_to_tuple(re:split(S, "=", ReOpts)) || S <- os:getenv()],
|
2011-04-30 20:47:15 +00:00
|
|
|
%% Drop variables without a name (win32)
|
2011-06-02 19:03:09 +00:00
|
|
|
[T1 || {K, _V} = T1 <- Os, K =/= []].
|
2010-02-16 14:05:29 +00:00
|
|
|
|
2012-01-03 17:59:36 +00:00
|
|
|
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.
|
|
|
|
|
2011-12-08 19:03:18 +00:00
|
|
|
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.
|
|
|
|
|
2009-12-01 17:38:30 +00:00
|
|
|
default_env() ->
|
2010-04-28 15:31:38 +00:00
|
|
|
[
|
2012-01-03 17:59:36 +00:00
|
|
|
{"CC" , "cc"},
|
|
|
|
{"CXX", "c++"},
|
|
|
|
{"DRV_CXX_TEMPLATE",
|
2011-04-30 20:47:15 +00:00
|
|
|
"$CXX -c $CXXFLAGS $DRV_CFLAGS $PORT_IN_FILES -o $PORT_OUT_FILE"},
|
2012-01-03 17:59:36 +00:00
|
|
|
{"DRV_CC_TEMPLATE",
|
2011-04-30 20:47:15 +00:00
|
|
|
"$CC -c $CFLAGS $DRV_CFLAGS $PORT_IN_FILES -o $PORT_OUT_FILE"},
|
2012-01-03 17:59:36 +00:00
|
|
|
{"DRV_LINK_TEMPLATE",
|
2011-04-30 20:47:15 +00:00
|
|
|
"$CC $PORT_IN_FILES $LDFLAGS $DRV_LDFLAGS -o $PORT_OUT_FILE"},
|
2012-01-03 17:59:36 +00:00
|
|
|
{"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"},
|
|
|
|
|
2011-12-08 19:03:18 +00:00
|
|
|
{"ERL_CFLAGS", lists:concat([" -I", erl_interface_dir(include),
|
2010-09-29 19:28:20 +00:00
|
|
|
" -I", filename:join(erts_dir(), "include"),
|
2010-02-16 14:05:29 +00:00
|
|
|
" "])},
|
2011-12-08 19:03:18 +00:00
|
|
|
{"ERL_EI_LIBDIR", erl_interface_dir(lib)},
|
2012-01-03 17:59:36 +00:00
|
|
|
{"ERL_LDFLAGS" , " -L$ERL_EI_LIBDIR -lerl_interface -lei"},
|
|
|
|
{"ERLANG_ARCH" , rebar_utils:wordsize()},
|
|
|
|
{"ERLANG_TARGET", rebar_utils:get_arch()},
|
|
|
|
|
2011-01-28 15:08:27 +00:00
|
|
|
{"darwin", "DRV_LDFLAGS",
|
|
|
|
"-bundle -flat_namespace -undefined suppress $ERL_LDFLAGS"},
|
2010-04-28 15:31:38 +00:00
|
|
|
|
2011-05-12 21:14:24 +00:00
|
|
|
%% Solaris specific flags
|
|
|
|
{"solaris.*-64$", "CFLAGS", "-D_REENTRANT -m64 $CFLAGS"},
|
|
|
|
{"solaris.*-64$", "CXXFLAGS", "-D_REENTRANT -m64 $CXXFLAGS"},
|
|
|
|
{"solaris.*-64$", "LDFLAGS", "-m64 $LDFLAGS"},
|
2010-04-28 15:31:38 +00:00
|
|
|
|
2011-05-12 21:14:24 +00:00
|
|
|
%% OS X Leopard flags for 64-bit
|
|
|
|
{"darwin9.*-64$", "CFLAGS", "-m64 $CFLAGS"},
|
|
|
|
{"darwin9.*-64$", "CXXFLAGS", "-m64 $CXXFLAGS"},
|
|
|
|
{"darwin9.*-64$", "LDFLAGS", "-arch x86_64 $LDFLAGS"},
|
2010-04-28 15:31:38 +00:00
|
|
|
|
2012-09-01 09:13:34 +00:00
|
|
|
%% 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"},
|
2012-05-25 21:53:18 +00:00
|
|
|
|
|
|
|
%% 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",
|
2012-07-23 18:31:29 +00:00
|
|
|
%% DRV_* and EXE_* Templates are identical
|
2012-05-25 21:53:18 +00:00
|
|
|
"$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"}
|
2010-04-28 15:31:38 +00:00
|
|
|
].
|