Merge remote-tracking branch 'canonical/next'
[sinan.git] / src / sin_compile_yrl.erl
blob407d07beb23e94e71de1d8a618cca33ff6b81bc0
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_yrl).
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, ".yrl") ->
22 sin_task_build:get_target(File, ".yrl", BuildDir, ".beam").
24 build_file(Config, State0, Module=#module{path=File}, Options, Target) ->
25 ErlFile = filename:basename(File, ".yrl"),
26 AppDir = filename:dirname(Target),
27 ErlTarget = filename:join([AppDir,"src"]),
28 ErlName = filename:join([ErlTarget,
29 lists:flatten([ErlFile, ".erl"])]),
30 sin_log:verbose(Config, "Building ~s", [File]),
31 {State2, ErlModule1} =
32 case yecc:file(File, [{parserfile, ErlName} |
33 sin_task_build:strip_options(Options)]) of
34 {ok, _ModuleName} ->
35 {State1, ErlModule0} = sin_file_info:process_file(State0, File, []),
36 sin_compile_erl:build_file(Config, State1,
37 rename_path(ErlModule0, ErlName),
38 Options, Target);
39 {ok, _ModuleName, []} ->
40 {State1, ErlModule0} = sin_file_info:process_file(State0, File, []),
41 sin_compile_erl:build_file(Config, State1, rename_path(ErlModule0, ErlName),
42 Options, Target);
43 {ok, _ModuleName, Warnings0} ->
44 Warnings1 = sin_task_build:gather_fail_info(Warnings0, "warning"),
45 ?SIN_RAISE(State0, {build_error, error_building_yecc, File, Warnings1});
46 {error, Errors, Warnings} ->
47 Errors = lists:flatten([sin_task_build:gather_fail_info(Errors,
48 "error"),
49 sin_task_build:gather_fail_info(Warnings,
50 "warning")]),
51 ?SIN_RAISE(State0, {build_error, error_building_yecc, File})
52 end,
53 {State2, [ErlModule1, Module]}.
55 rename_path(Mod, NewPath) ->
56 Mod#module{path=NewPath}.
58 %% @doc Format an exception thrown by this module
59 -spec format_exception(sin_exceptions:exception()) ->
60 string().
61 format_exception(Exception) ->
62 sin_exceptions:format_exception(Exception).
63 %%%===================================================================
64 %%% Internal functions
65 %%%===================================================================