Merge remote-tracking branch 'canonical/next'
[sinan.git] / src / sin_compile_erl.erl
blob288a08c2582a1c99ac81c22671106eab09a97616
1 %%%-------------------------------------------------------------------
2 %%% @author Eric Merritt <>
3 %%% @copyright (C) 2011, Eric Merritt
4 %%% @doc
5 %%%
6 %%% @end
7 %%% Created : 15 Apr 2011 by Eric Merritt <>
8 %%%-------------------------------------------------------------------
9 -module(sin_compile_erl).
11 %% API
12 -export([build_file/5,
13 get_target/3,
14 format_exception/1]).
16 -include_lib("sinan/include/sinan.hrl").
18 %%%===================================================================
19 %%% API
20 %%%===================================================================
21 get_target(BuildDir, File, ".erl") ->
22 sin_task_build:get_target(File, ".erl", BuildDir, ".beam").
24 %% @doc Do the actual compilation on the file.
25 -spec build_file(sin_config:matcher(), sin_state:state(),
26 sin_file_info:mod(), [term()], string()) ->
27 {module(), sin_config:config()}.
28 build_file(Config, State, Module=#module{path=File}, Options, _Target) ->
29 sin_log:verbose(Config, "Building ~s", [File]),
30 case compile:file(File, Options) of
31 {ok, ModuleName} ->
32 reload_module(ModuleName),
33 {State, [Module]};
34 {ok, ModuleName, []} ->
35 reload_module(ModuleName),
36 {State, [Module]};
37 {ok, ModuleName, Warnings0} ->
38 reload_module(ModuleName),
39 Warnings1 = sin_task_build:gather_fail_info(Warnings0, "warning"),
40 {?WARN(State,
41 {build_warnings, Warnings1}),
42 [Module]};
43 {error, Errors, Warnings} ->
44 ErrorStrings =
45 lists:flatten([sin_task_build:gather_fail_info(Errors,
46 "error"),
47 sin_task_build:gather_fail_info(Warnings,
48 "warning")]),
49 sin_log:normal(Config, lists:flatten(ErrorStrings)),
50 ?SIN_RAISE(State, {build_error, error_building_erl_file, File});
51 error ->
52 sin_log:normal(Config, "Unknown error occured during build"),
53 ?SIN_RAISE(State, {build_error, error_building_erl_file, File})
54 end.
56 %% @doc Format an exception thrown by this module
57 -spec format_exception(sin_exceptions:exception()) ->
58 string().
59 format_exception(Exception) ->
60 sin_exceptions:format_exception(Exception).
61 %%%===================================================================
62 %%% Internal functions
63 %%%===================================================================
64 %% We don't want to do this on ourselves
65 -spec reload_module(atom()) -> {ok, atom()}.
66 reload_module(Module)
67 when not Module == ?MODULE ->
68 code:purge(Module),
69 code:load_file(Module),
70 code:purge(Module),
71 code:load_file(Module);
72 reload_module(_) ->
73 ok.