summaryrefslogtreecommitdiff
path: root/src/rebar_erlc_compiler.erl
diff options
context:
space:
mode:
authorTristan Sloughter <t@crashfast.com>2015-04-11 16:06:15 -0500
committerTristan Sloughter <t@crashfast.com>2015-04-11 16:09:34 -0500
commit4e663aa7990540eee1400a37177a83ae0263d6aa (patch)
treecd0032cc56a9c4b44a8a4b26140b689677dc4cfc /src/rebar_erlc_compiler.erl
parentc3026cc8ced2bf44f3b816089c4d28b34c4356eb (diff)
Warn on bad use of include_lib but try to find the header anyway
Diffstat (limited to 'src/rebar_erlc_compiler.erl')
-rw-r--r--src/rebar_erlc_compiler.erl52
1 files changed, 33 insertions, 19 deletions
diff --git a/src/rebar_erlc_compiler.erl b/src/rebar_erlc_compiler.erl
index 50d180c..792d358 100644
--- a/src/rebar_erlc_compiler.erl
+++ b/src/rebar_erlc_compiler.erl
@@ -519,31 +519,45 @@ process_attr(_, _Form, Includes, _Dir) ->
module_to_erl(Mod) ->
atom_to_list(Mod) ++ ".erl".
-
-%% Given the filename from an include_lib attribute, if the path
-%% exists, return unmodified, or else get the absolute ERL_LIBS
-%% path.
-maybe_expand_include_lib_path(File, Dir) ->
- case filelib:is_regular(filename:join(Dir, File)) of
- true ->
- [filename:join(Dir, File)];
- false ->
- expand_include_lib_path(File)
- end.
-
%% Given a path like "stdlib/include/erl_compile.hrl", return
%% "OTP_INSTALL_DIR/lib/erlang/lib/stdlib-x.y.z/include/erl_compile.hrl".
%% Usually a simple [Lib, SubDir, File1] = filename:split(File) should
%% work, but to not crash when an unusual include_lib path is used,
%% utilize more elaborate logic.
-expand_include_lib_path(File) ->
+maybe_expand_include_lib_path(File, Dir) ->
File1 = filename:basename(File),
- Split = filename:split(filename:dirname(File)),
- Lib = hd(Split),
- SubDir = filename:join(tl(Split)),
- case code:lib_dir(list_to_atom(Lib), list_to_atom(SubDir)) of
- {error, bad_name} -> [];
- Dir -> [filename:join(Dir, File1)]
+ case filename:split(filename:dirname(File)) of
+ [_] ->
+ warn_and_find_path(File, Dir);
+ [Lib | SubDir] ->
+ case code:lib_dir(list_to_atom(Lib), list_to_atom(filename:join(SubDir))) of
+ {error, bad_name} ->
+ warn_and_find_path(File, Dir);
+ AppDir ->
+ [filename:join(AppDir, File1)]
+ end
+ end.
+
+%% The use of -include_lib was probably incorrect by the user but lets try to make it work.
+%% We search in the outdir and outdir/../include to see if the header exists.
+warn_and_find_path(File, Dir) ->
+ ?WARN("Bad use of -include_lib(\"~s\")."
+ " First path component should be the name of an application."
+ " You probably meant -include(\"~s\").", [File, File]),
+ SrcHeader = filename:join(Dir, File),
+ case filelib:is_regular(SrcHeader) of
+ true ->
+ [SrcHeader];
+ false ->
+ IncludeDir = filename:join(filename:join(lists:droplast(filename:split(Dir))), "include"),
+ IncludeHeader = filename:join(IncludeDir, File),
+ case filelib:is_regular(IncludeHeader) of
+ true ->
+ [filename:join(IncludeDir, File)];
+ false ->
+ ?WARN("Could not find header for -include_lib(\"~s\").", [File]),
+ []
+ end
end.
%%