4
%% Copyright Ericsson AB 1996-2009. All Rights Reserved.
4
%% Copyright Ericsson AB 1996-2010. All Rights Reserved.
6
6
%% The contents of this file are subject to the Erlang Public License,
7
7
%% Version 1.1, (the "License"); you may not use this file except in
8
8
%% compliance with the License. You should have received a copy of the
9
9
%% Erlang Public License along with this software. If not, it can be
10
10
%% retrieved online at http://www.erlang.org/.
12
12
%% Software distributed under the License is distributed on an "AS IS"
13
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
14
%% the License for the specific language governing rights and limitations
15
15
%% under the License.
19
19
%% Purpose: Run the Erlang compiler.
187
187
format_error({native, E}) ->
188
188
io_lib:fwrite("native-code compilation failed with reason: ~P.",
190
format_error({native_crash, E}) ->
191
io_lib:fwrite("native-code compilation crashed with reason: ~P.",
190
format_error({native_crash,E,Stk}) ->
191
io_lib:fwrite("native-code compilation crashed with reason: ~P.\n~P\n",
193
193
format_error({open,E}) ->
194
194
io_lib:format("open error '~s'", [file:format_error(E)]);
195
195
format_error({epp,E}) ->
320
320
comp_ret_ok(#compile{code=Code,warnings=Warn0,module=Mod,options=Opts}=St) ->
321
Warn = messages_per_file(Warn0),
322
report_warnings(St#compile{warnings = Warn}),
323
Ret1 = case member(binary, Opts) andalso not member(no_code_generation, Opts) of
327
Ret2 = case member(return_warnings, Opts) of
328
true -> Ret1 ++ [Warn];
331
list_to_tuple([ok,Mod|Ret2]).
321
case member(warnings_as_errors, Opts) andalso length(Warn0) > 0 of
323
case member(report_warnings, Opts) of
325
io:format("~p: warnings being treated as errors\n",
332
Warn = messages_per_file(Warn0),
333
report_warnings(St#compile{warnings = Warn}),
334
Ret1 = case member(binary, Opts) andalso
335
not member(no_code_generation, Opts) of
339
Ret2 = case member(return_warnings, Opts) of
340
true -> Ret1 ++ [Warn];
343
list_to_tuple([ok,Mod|Ret2])
333
346
comp_ret_err(#compile{warnings=Warn0,errors=Err0,options=Opts}=St) ->
334
347
Warn = messages_per_file(Warn0),
344
357
messages_per_file(Ms) ->
345
358
T = lists:sort([{File,M} || {File,Messages} <- Ms, M <- Messages]),
346
359
PrioMs = [erl_scan, epp, erl_parse],
348
361
lists:mapfoldl(fun(M, A) ->
349
362
lists:partition(fun({_,{_,Mod,_}}) -> Mod =:= M;
353
Prio = lists:sort(fun({_,{L1,_,_}}, {_,{L2,_,_}}) -> L1 =< L2 end,
366
Prio = lists:sort(fun({_,{L1,_,_}}, {_,{L2,_,_}}) -> L1 =< L2 end,
354
367
lists:append(Prio0)),
355
368
flatmap(fun mpf/1, [Prio, Rest]).
358
[{File,[M || {F,M} <- Ms, F =:= File]} ||
371
[{File,[M || {F,M} <- Ms, F =:= File]} ||
359
372
File <- lists:usort([F || {F,_} <- Ms])].
361
374
%% passes(form|file, [Option]) -> [{Name,PassFun}]
496
509
select_cond(Flag, ShouldBe, Pass, Ps, Opts) ->
497
510
ShouldNotBe = not ShouldBe,
498
case member(Flag, Opts) of
511
case member(Flag, Opts) of
499
512
ShouldBe -> select_passes([Pass|Ps], Opts);
500
513
ShouldNotBe -> select_passes(Ps, Opts)
503
516
%% select_list_passes([Pass], Opts) -> {done,[Pass]} | {not_done,[Pass]}
504
517
%% Evaluate all conditions having to do with listings in the list of
507
520
select_list_passes(Ps, Opts) ->
508
521
select_list_passes_1(Ps, Opts, []).
704
717
case file:read_file(St#compile.ifile) of
706
719
Infile = St#compile.ifile,
707
case is_too_old(Infile) of
720
case no_native_compilation(Infile, St) of
709
722
{ok,St#compile{module=none,code=none}};
717
730
{error,St#compile{errors=St#compile.errors ++ Es}}
720
is_too_old(BeamFile) ->
733
no_native_compilation(BeamFile, #compile{options=Opts0}) ->
721
734
case beam_lib:chunks(BeamFile, ["CInf"]) of
722
735
{ok,{_,[{"CInf",Term0}]}} ->
723
736
Term = binary_to_term(Term0),
724
Opts = proplists:get_value(options, Term, []),
725
lists:member(no_new_funs, Opts);
738
%% Compiler options in the beam file will override
739
%% options passed to the compiler.
740
Opts = proplists:get_value(options, Term, []) ++ Opts0,
741
member(no_new_funs, Opts) orelse not is_native_enabled(Opts);
782
798
clean_parse_transforms_1(Fs, [F|Acc]);
783
799
clean_parse_transforms_1([], Acc) -> reverse(Acc).
785
transforms(Os) -> [ M || {parse_transform,M} <- Os ].
801
transforms(Os) -> [ M || {parse_transform,M} <- Os ].
787
803
transform_module(#compile{options=Opt,code=Code0}=St0) ->
788
804
%% Extract compile options from code into options field.
816
832
foldl_transform(St, []) -> {ok,St}.
818
get_core_transforms(Opts) -> [M || {core_transform,M} <- Opts].
834
get_core_transforms(Opts) -> [M || {core_transform,M} <- Opts].
820
836
core_transforms(St) ->
821
837
%% The options field holds the complete list of options at this
1034
1050
test_native(#compile{options=Opts}) ->
1035
1051
%% This test is done late, in case some other option has turned off native.
1036
member(native, Opts).
1052
%% 'native' given on the command line can be overridden by
1053
%% 'no_native' in the module itself.
1054
is_native_enabled(Opts).
1056
is_native_enabled([native|_]) -> true;
1057
is_native_enabled([no_native|_]) -> false;
1058
is_native_enabled([_|Opts]) -> is_native_enabled(Opts);
1059
is_native_enabled([]) -> false.
1038
1061
native_compile(#compile{code=none}=St) -> {ok,St};
1039
1062
native_compile(St) ->
1057
1080
St#compile.core_code,
1058
1081
St#compile.code,
1060
{ok, {_Type,Bin} = T} when is_binary(Bin) ->
1061
{ok, embed_native_code(St, T)};
1083
{ok,{_Type,Bin}=T} when is_binary(Bin) ->
1084
{ok,embed_native_code(St, T)};
1063
1086
case IgnoreErrors of
1065
Ws = [{St#compile.ifile,[{none,?MODULE,{native,R}}]}],
1066
{ok, St#compile{warnings=St#compile.warnings ++ Ws}};
1088
Ws = [{St#compile.ifile,[{?MODULE,{native,R}}]}],
1089
{ok,St#compile{warnings=St#compile.warnings ++ Ws}};
1068
Es = [{St#compile.ifile,[{none,?MODULE,{native,R}}]}],
1069
{error, St#compile{errors=St#compile.errors ++ Es}}
1091
Es = [{St#compile.ifile,[{?MODULE,{native,R}}]}],
1092
{error,St#compile{errors=St#compile.errors ++ Es}}
1096
Stk = erlang:get_stacktrace(),
1073
1097
case IgnoreErrors of
1075
Ws = [{St#compile.ifile,[{none,?MODULE,{native_crash,R}}]}],
1076
{ok, St#compile{warnings=St#compile.warnings ++ Ws}};
1099
Ws = [{St#compile.ifile,
1100
[{?MODULE,{native_crash,R,Stk}}]}],
1101
{ok,St#compile{warnings=St#compile.warnings ++ Ws}};
1103
erlang:raise(Class, R, Stk)
1264
1289
listing(LFun, Ext, St) ->
1265
1290
Lfile = outfile(St#compile.base, Ext, St#compile.options),
1266
1291
case file:open(Lfile, [write,delayed_write]) of
1268
1293
Code = restore_expanded_types(Ext, St#compile.code),
1269
1294
LFun(Lf, Code),
1270
1295
ok = file:close(Lf),