You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

574 lines
24 KiB

15 years ago
15 years ago
15 years ago
15 years ago
mib_to_hrl compilation verbosity via 'mib_opts' Previously, the configuration setting 'mib_opts' in rebar.config would affect the call to snmpc:compile/2, so that (for example) verbosity could be controlled. However, the subsequent call to snmpc:mib_to_hrl/1 did not include any of these options, so it did not appear to be possible to control the verbosity of the process of converting a MIB to a .hrl file. To make matters worse, the default was to dump a full trace -- including debug output and various logging -- so the act of compiling a large number of MIBs could result in a huge amount of "noisy" output that hid any signal (meaningful warnings, errors, etc.). This commit addresses that issue by replacing the call to snmpc:mib_to_hrl/1 with a call to snmpc:mib_to_hrl/3 instead, which includes an "options" argument that, at present, is only capable of setting verbosity. The verbosity setting is taken from the 'mib_opts' setting in rebar_config, if present, and the approriate kind of argument is passed to snmpc:mib_to_hrl/3. It should be noted that snmpc:mib_to_hrl/3 is not listed in Erlang's documentation, but does appear in the list of "API" exports at the top of snmpc.erl in R15B01 (and remains that way in R16B01), so this appears to be more of a documentation oversight than the use of a deep, dark function call that was not intended to be public. snmpc:mib_to_hrl/3 accepts an #options{} record (defined in lib/srdlib/include/erl_compile.hrl within Erlang's source distribution), though most of the fields in that record are ignored by snmpc:mib_to_hrl/3; only verbosity can be controlled this way.
11 years ago
15 years ago
15 years ago
15 years ago
14 years ago
14 years ago
14 years ago
15 years ago
15 years ago
15 years ago
mib_to_hrl compilation verbosity via 'mib_opts' Previously, the configuration setting 'mib_opts' in rebar.config would affect the call to snmpc:compile/2, so that (for example) verbosity could be controlled. However, the subsequent call to snmpc:mib_to_hrl/1 did not include any of these options, so it did not appear to be possible to control the verbosity of the process of converting a MIB to a .hrl file. To make matters worse, the default was to dump a full trace -- including debug output and various logging -- so the act of compiling a large number of MIBs could result in a huge amount of "noisy" output that hid any signal (meaningful warnings, errors, etc.). This commit addresses that issue by replacing the call to snmpc:mib_to_hrl/1 with a call to snmpc:mib_to_hrl/3 instead, which includes an "options" argument that, at present, is only capable of setting verbosity. The verbosity setting is taken from the 'mib_opts' setting in rebar_config, if present, and the approriate kind of argument is passed to snmpc:mib_to_hrl/3. It should be noted that snmpc:mib_to_hrl/3 is not listed in Erlang's documentation, but does appear in the list of "API" exports at the top of snmpc.erl in R15B01 (and remains that way in R16B01), so this appears to be more of a documentation oversight than the use of a deep, dark function call that was not intended to be public. snmpc:mib_to_hrl/3 accepts an #options{} record (defined in lib/srdlib/include/erl_compile.hrl within Erlang's source distribution), though most of the fields in that record are ignored by snmpc:mib_to_hrl/3; only verbosity can be controlled this way.
11 years ago
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% Copyright (c) 2009, 2010 Dave Smith (dizzyd@dizzyd.com)
  8. %%
  9. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %% of this software and associated documentation files (the "Software"), to deal
  11. %% in the Software without restriction, including without limitation the rights
  12. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %% copies of the Software, and to permit persons to whom the Software is
  14. %% furnished to do so, subject to the following conditions:
  15. %%
  16. %% The above copyright notice and this permission notice shall be included in
  17. %% all copies or substantial portions of the Software.
  18. %%
  19. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %% THE SOFTWARE.
  26. %% -------------------------------------------------------------------
  27. -module(rebar_erlc_compiler).
  28. -export([compile/2,
  29. compile/3,
  30. clean/2]).
  31. -include("rebar.hrl").
  32. -include_lib("stdlib/include/erl_compile.hrl").
  33. -define(ERLCINFO_VSN, 2).
  34. -define(ERLCINFO_FILE, "erlcinfo").
  35. -type erlc_info_v() :: {digraph:vertex(), term()} | 'false'.
  36. -type erlc_info_e() :: {digraph:vertex(), digraph:vertex()}.
  37. -type erlc_info() :: {list(erlc_info_v()), list(erlc_info_e()), list(string())}.
  38. -record(erlcinfo,
  39. {
  40. vsn = ?ERLCINFO_VSN :: pos_integer(),
  41. info = {[], [], []} :: erlc_info()
  42. }).
  43. -define(RE_PREFIX, "^[^._]").
  44. %% ===================================================================
  45. %% Public API
  46. %% ===================================================================
  47. %% Supported configuration variables:
  48. %%
  49. %% * erl_opts - Erlang list of options passed to compile:file/2
  50. %% It is also possible to specify platform specific
  51. %% options by specifying a pair or a triplet where the
  52. %% first string is a regex that is checked against the
  53. %% string
  54. %%
  55. %% OtpRelease ++ "-" ++ SysArch ++ "-" ++ Words.
  56. %%
  57. %% where
  58. %%
  59. %% OtpRelease = erlang:system_info(otp_release).
  60. %% SysArch = erlang:system_info(system_architecture).
  61. %% Words = integer_to_list(8 *
  62. %% erlang:system_info({wordsize, external})).
  63. %%
  64. %% E.g. to define HAVE_SENDFILE only on systems with
  65. %% sendfile(), to define BACKLOG on Linux/FreeBSD as 128,
  66. %% and to define 'old_inets' for R13 OTP release do:
  67. %%
  68. %% {erl_opts, [{platform_define,
  69. %% "(linux|solaris|freebsd|darwin)",
  70. %% 'HAVE_SENDFILE'},
  71. %% {platform_define, "(linux|freebsd)",
  72. %% 'BACKLOG', 128},
  73. %% {platform_define, "R13",
  74. %% 'old_inets'}]}.
  75. %%
  76. -spec compile(rebar_state:t(), file:name()) -> 'ok'.
  77. compile(Config, Dir) ->
  78. compile(Config, Dir, filename:join([Dir, "ebin"])).
  79. -spec compile(rebar_state:t(), file:name(), file:name()) -> 'ok'.
  80. compile(Config, Dir, OutDir) ->
  81. rebar_base_compiler:run(Config,
  82. check_files(rebar_state:get(
  83. Config, xrl_first_files, [])),
  84. filename:join(Dir, "src"), ".xrl", filename:join(Dir, "src"), ".erl",
  85. fun compile_xrl/3),
  86. rebar_base_compiler:run(Config,
  87. check_files(rebar_state:get(
  88. Config, yrl_first_files, [])),
  89. filename:join(Dir, "src"), ".yrl", filename:join(Dir, "src"), ".erl",
  90. fun compile_yrl/3),
  91. rebar_base_compiler:run(Config,
  92. check_files(rebar_state:get(
  93. Config, mib_first_files, [])),
  94. filename:join(Dir, "mibs"), ".mib", filename:join([Dir, "priv", "mibs"]), ".bin",
  95. fun compile_mib/3),
  96. doterl_compile(Config, Dir, OutDir).
  97. -spec clean(rebar_state:t(), file:filename()) -> 'ok'.
  98. clean(_Config, AppDir) ->
  99. MibFiles = rebar_utils:find_files(filename:join(AppDir, "mibs"), ?RE_PREFIX".*\\.mib\$"),
  100. MIBs = [filename:rootname(filename:basename(MIB)) || MIB <- MibFiles],
  101. rebar_file_utils:delete_each(
  102. [filename:join([AppDir, "include",MIB++".hrl"]) || MIB <- MIBs]),
  103. lists:foreach(fun(F) -> ok = rebar_file_utils:rm_rf(F) end,
  104. [filename:join(AppDir, "ebin/*.beam"), filename:join(AppDir, "priv/mibs/*.bin")]),
  105. YrlFiles = rebar_utils:find_files(filename:join(AppDir, "src"), ?RE_PREFIX".*\\.[x|y]rl\$"),
  106. rebar_file_utils:delete_each(
  107. [ binary_to_list(iolist_to_binary(re:replace(F, "\\.[x|y]rl$", ".erl")))
  108. || F <- YrlFiles ]),
  109. %% Delete the build graph, if any
  110. rebar_file_utils:rm_rf(erlcinfo_file(AppDir)),
  111. %% Erlang compilation is recursive, so it's possible that we have a nested
  112. %% directory structure in ebin with .beam files within. As such, we want
  113. %% to scan whatever is left in the ebin/ directory for sub-dirs which
  114. %% satisfy our criteria.
  115. BeamFiles = rebar_utils:find_files(filename:join(AppDir, "ebin"), ?RE_PREFIX".*\\.beam\$"),
  116. rebar_file_utils:delete_each(BeamFiles),
  117. lists:foreach(fun(Dir) -> delete_dir(Dir, dirs(Dir)) end, dirs(filename:join(AppDir, "ebin"))),
  118. ok.
  119. %% ===================================================================
  120. %% Internal functions
  121. %% ===================================================================
  122. -spec doterl_compile(rebar_state:t(), file:filename(), file:filename()) -> ok.
  123. doterl_compile(State, Dir, ODir) ->
  124. ErlOpts = rebar_utils:erl_opts(State),
  125. doterl_compile(State, Dir, ODir, [], ErlOpts).
  126. doterl_compile(Config, Dir, OutDir, MoreSources, ErlOpts) ->
  127. ?DEBUG("erl_opts ~p", [ErlOpts]),
  128. %% Support the src_dirs option allowing multiple directories to
  129. %% contain erlang source. This might be used, for example, should
  130. %% eunit tests be separated from the core application source.
  131. SrcDirs = [filename:join(Dir, X) || X <- proplists:get_value(src_dirs, ErlOpts, ["src"])],
  132. AllErlFiles = gather_src(SrcDirs, []) ++ MoreSources,
  133. %% Make sure that ebin/ exists and is on the path
  134. ok = filelib:ensure_dir(filename:join(OutDir, "dummy.beam")),
  135. CurrPath = code:get_path(),
  136. true = code:add_path(filename:absname(OutDir)),
  137. OutDir1 = proplists:get_value(outdir, ErlOpts, OutDir),
  138. G = init_erlcinfo(proplists:get_all_values(i, ErlOpts), AllErlFiles, Dir),
  139. %% A source file may have been renamed or deleted. Remove it from the graph
  140. %% and remove any beam file for that source if it exists.
  141. Vertices = digraph:vertices(G),
  142. [maybe_rm_beam_and_edge(G, OutDir, File) || File <- lists:sort(Vertices) -- lists:sort(AllErlFiles),
  143. filename:extension(File) =:= ".erl"],
  144. NeededErlFiles = needed_files(G, ErlOpts, Dir, OutDir1, AllErlFiles),
  145. ErlFirstFiles = erl_first_files(Config, NeededErlFiles),
  146. {DepErls, OtherErls} = lists:partition(
  147. fun(Source) -> digraph:in_degree(G, Source) > 0 end,
  148. [File || File <- NeededErlFiles, not lists:member(File, ErlFirstFiles)]),
  149. DepErlsOrdered = digraph_utils:topsort(digraph_utils:subgraph(G, DepErls)),
  150. FirstErls = ErlFirstFiles ++ lists:reverse(DepErlsOrdered),
  151. ?DEBUG("Files to compile first: ~p", [FirstErls]),
  152. rebar_base_compiler:run(
  153. Config, FirstErls, OtherErls,
  154. fun(S, C) ->
  155. internal_erl_compile(C, Dir, S, OutDir1, ErlOpts)
  156. end),
  157. true = code:set_path(CurrPath),
  158. ok.
  159. erl_first_files(Config, NeededErlFiles) ->
  160. ErlFirstFilesConf = rebar_state:get(Config, erl_first_files, []),
  161. %% NOTE: order of files in ErlFirstFiles is important!
  162. [File || File <- ErlFirstFilesConf, lists:member(File, NeededErlFiles)].
  163. %% Get subset of SourceFiles which need to be recompiled, respecting
  164. %% dependencies induced by given graph G.
  165. needed_files(G, ErlOpts, Dir, OutDir, SourceFiles) ->
  166. lists:filter(fun(Source) ->
  167. TargetBase = target_base(OutDir, Source),
  168. Target = TargetBase ++ ".beam",
  169. Opts = [{outdir, filename:dirname(Target)}
  170. ,{i, filename:join(Dir, "include")}] ++ ErlOpts,
  171. digraph:vertex(G, Source) > {Source, filelib:last_modified(Target)}
  172. orelse opts_changed(Opts, TargetBase)
  173. end, SourceFiles).
  174. maybe_rm_beam_and_edge(G, OutDir, Source) ->
  175. %% This is NOT a double check it is the only check that the source file is actually gone
  176. case filelib:is_regular(Source) of
  177. true ->
  178. %% Actually exists, don't delete
  179. ok;
  180. false ->
  181. Target = target_base(OutDir, Source) ++ ".beam",
  182. ?DEBUG("Source ~s is gone, deleting previous beam file if it exists ~s", [Source, Target]),
  183. file:delete(Target),
  184. digraph:del_vertex(G, Source)
  185. end.
  186. opts_changed(Opts, ObjectFile) ->
  187. case code:load_abs(ObjectFile) of
  188. {module, Mod} ->
  189. Compile = Mod:module_info(compile),
  190. lists:sort(Opts) =/= lists:sort(proplists:get_value(options,
  191. Compile,
  192. []));
  193. {error, _} -> true
  194. end.
  195. erlcinfo_file(Dir) ->
  196. filename:join(rebar_dir:local_cache_dir(Dir), ?ERLCINFO_FILE).
  197. %% Get dependency graph of given Erls files and their dependencies (header files,
  198. %% parse transforms, behaviours etc.) located in their directories or given
  199. %% InclDirs. Note that last modification times stored in vertices already respect
  200. %% dependencies induced by given graph G.
  201. init_erlcinfo(InclDirs, Erls, Dir) ->
  202. G = digraph:new([acyclic]),
  203. try restore_erlcinfo(G, InclDirs, Dir)
  204. catch
  205. _:_ ->
  206. ?WARN("Failed to restore ~s file. Discarding it.~n", [erlcinfo_file(Dir)]),
  207. file:delete(erlcinfo_file(Dir))
  208. end,
  209. Dirs = source_and_include_dirs(InclDirs, Erls),
  210. Modified = lists:foldl(update_erlcinfo_fun(G, Dirs), false, Erls),
  211. if Modified -> store_erlcinfo(G, InclDirs, Dir); not Modified -> ok end,
  212. G.
  213. source_and_include_dirs(InclDirs, Erls) ->
  214. SourceDirs = lists:map(fun filename:dirname/1, Erls),
  215. lists:usort(["include" | InclDirs ++ SourceDirs]).
  216. update_erlcinfo(G, Dirs, Source) ->
  217. case digraph:vertex(G, Source) of
  218. {_, LastUpdated} ->
  219. case filelib:last_modified(Source) of
  220. 0 ->
  221. %% The file doesn't exist anymore,
  222. %% erase it from the graph.
  223. %% All the edges will be erased automatically.
  224. digraph:del_vertex(G, Source),
  225. modified;
  226. LastModified when LastUpdated < LastModified ->
  227. modify_erlcinfo(G, Source, LastModified, filename:dirname(Source), Dirs);
  228. _ ->
  229. Modified = lists:foldl(
  230. update_erlcinfo_fun(G, Dirs),
  231. false, digraph:out_neighbours(G, Source)),
  232. MaxModified = update_max_modified_deps(G, Source),
  233. case Modified orelse MaxModified > LastUpdated of
  234. true -> modified;
  235. false -> unmodified
  236. end
  237. end;
  238. false ->
  239. modify_erlcinfo(G, Source, filelib:last_modified(Source), filename:dirname(Source), Dirs)
  240. end.
  241. update_erlcinfo_fun(G, Dirs) ->
  242. fun(Erl, Modified) ->
  243. case update_erlcinfo(G, Dirs, Erl) of
  244. modified -> true;
  245. unmodified -> Modified
  246. end
  247. end.
  248. update_max_modified_deps(G, Source) ->
  249. MaxModified = lists:max(lists:map(
  250. fun(File) -> {_, MaxModified} = digraph:vertex(G, File), MaxModified end,
  251. [Source|digraph:out_neighbours(G, Source)])),
  252. digraph:add_vertex(G, Source, MaxModified),
  253. MaxModified.
  254. modify_erlcinfo(G, Source, LastModified, Dir, Dirs) ->
  255. {ok, Fd} = file:open(Source, [read]),
  256. Incls = parse_attrs(Fd, [], Dir),
  257. AbsIncls = expand_file_names(Incls, Dirs),
  258. ok = file:close(Fd),
  259. digraph:add_vertex(G, Source, LastModified),
  260. digraph:del_edges(G, digraph:out_edges(G, Source)),
  261. lists:foreach(
  262. fun(Incl) ->
  263. update_erlcinfo(G, Dirs, Incl),
  264. digraph:add_edge(G, Source, Incl)
  265. end, AbsIncls),
  266. modified.
  267. restore_erlcinfo(G, InclDirs, Dir) ->
  268. case file:read_file(erlcinfo_file(Dir)) of
  269. {ok, Data} ->
  270. % Since externally passed InclDirs can influence erlcinfo graph (see
  271. % modify_erlcinfo), we have to check here that they didn't change.
  272. #erlcinfo{vsn=?ERLCINFO_VSN, info={Vs, Es, InclDirs}} =
  273. binary_to_term(Data),
  274. lists:foreach(
  275. fun({V, LastUpdated}) ->
  276. digraph:add_vertex(G, V, LastUpdated)
  277. end, Vs),
  278. lists:foreach(
  279. fun({_, V1, V2, _}) ->
  280. digraph:add_edge(G, V1, V2)
  281. end, Es);
  282. {error, _} ->
  283. ok
  284. end.
  285. store_erlcinfo(G, InclDirs, Dir) ->
  286. Vs = lists:map(fun(V) -> digraph:vertex(G, V) end, digraph:vertices(G)),
  287. Es = lists:map(fun(E) -> digraph:edge(G, E) end, digraph:edges(G)),
  288. File = erlcinfo_file(Dir),
  289. ok = filelib:ensure_dir(File),
  290. Data = term_to_binary(#erlcinfo{info={Vs, Es, InclDirs}}, [{compressed, 2}]),
  291. file:write_file(File, Data).
  292. %% NOTE: If, for example, one of the entries in Files, refers to
  293. %% gen_server.erl, that entry will be dropped. It is dropped because
  294. %% such an entry usually refers to the beam file, and we don't pass a
  295. %% list of OTP src dirs for finding gen_server.erl's full path. Also,
  296. %% if gen_server.erl was modified, it's not rebar's task to compile a
  297. %% new version of the beam file. Therefore, it's reasonable to drop
  298. %% such entries. Also see process_attr(behaviour, Form, Includes).
  299. -spec expand_file_names([file:filename()],
  300. [file:filename()]) -> [file:filename()].
  301. expand_file_names(Files, Dirs) ->
  302. %% We check if Files exist by itself or within the directories
  303. %% listed in Dirs.
  304. %% Return the list of files matched.
  305. lists:flatmap(
  306. fun(Incl) ->
  307. case filelib:is_regular(Incl) of
  308. true ->
  309. [Incl];
  310. false ->
  311. lists:flatmap(
  312. fun(Dir) ->
  313. FullPath = filename:join(Dir, Incl),
  314. case filelib:is_regular(FullPath) of
  315. true ->
  316. [FullPath];
  317. false ->
  318. []
  319. end
  320. end, Dirs)
  321. end
  322. end, Files).
  323. -spec internal_erl_compile(rebar_config:config(), file:filename(), file:filename(),
  324. file:filename(), list()) -> ok | {ok, any()} | {error, any(), any()}.
  325. internal_erl_compile(Config, Dir, Module, OutDir, ErlOpts) ->
  326. Target = target_base(OutDir, Module) ++ ".beam",
  327. ok = filelib:ensure_dir(Target),
  328. Opts = [{outdir, filename:dirname(Target)}] ++ ErlOpts ++
  329. [{i, filename:join(Dir, "include")}, return],
  330. case compile:file(Module, Opts) of
  331. {ok, _Mod} ->
  332. ok;
  333. {ok, _Mod, Ws} ->
  334. rebar_base_compiler:ok_tuple(Config, Module, Ws);
  335. {error, Es, Ws} ->
  336. rebar_base_compiler:error_tuple(Config, Module, Es, Ws, Opts)
  337. end.
  338. target_base(OutDir, Source) ->
  339. filename:join(OutDir, filename:basename(Source, ".erl")).
  340. -spec compile_mib(file:filename(), file:filename(),
  341. rebar_state:t()) -> 'ok'.
  342. compile_mib(Source, Target, Config) ->
  343. ok = filelib:ensure_dir(Target),
  344. ok = filelib:ensure_dir(filename:join("include", "dummy.hrl")),
  345. Opts = [{outdir, "priv/mibs"}, {i, ["priv/mibs"]}] ++
  346. rebar_state:get(Config, mib_opts, []),
  347. case snmpc:compile(Source, Opts) of
  348. {ok, _} ->
  349. Mib = filename:rootname(Target),
  350. MibToHrlOpts =
  351. case proplists:get_value(verbosity, Opts, undefined) of
  352. undefined ->
  353. #options{specific = []};
  354. Verbosity ->
  355. #options{specific = [{verbosity, Verbosity}]}
  356. end,
  357. ok = snmpc:mib_to_hrl(Mib, Mib, MibToHrlOpts),
  358. Hrl_filename = Mib ++ ".hrl",
  359. rebar_file_utils:mv(Hrl_filename, "include"),
  360. ok;
  361. {error, compilation_failed} ->
  362. ?FAIL
  363. end.
  364. -spec compile_xrl(file:filename(), file:filename(),
  365. rebar_state:t()) -> 'ok'.
  366. compile_xrl(Source, Target, Config) ->
  367. Opts = [{scannerfile, Target} | rebar_state:get(Config, xrl_opts, [])],
  368. compile_xrl_yrl(Config, Source, Target, Opts, leex).
  369. -spec compile_yrl(file:filename(), file:filename(),
  370. rebar_state:t()) -> 'ok'.
  371. compile_yrl(Source, Target, Config) ->
  372. Opts = [{parserfile, Target} | rebar_state:get(Config, yrl_opts, [])],
  373. compile_xrl_yrl(Config, Source, Target, Opts, yecc).
  374. -spec compile_xrl_yrl(rebar_state:t(), file:filename(),
  375. file:filename(), list(), module()) -> 'ok'.
  376. compile_xrl_yrl(Config, Source, Target, Opts, Mod) ->
  377. Dir = rebar_state:dir(Config),
  378. Opts1 = [{includefile, filename:join(Dir, I)} || {includefile, I} <- Opts,
  379. filename:pathtype(I) =:= relative],
  380. case needs_compile(Source, Target) of
  381. true ->
  382. case Mod:file(Source, Opts1 ++ [{return, true}]) of
  383. {ok, _} ->
  384. ok;
  385. {ok, _Mod, Ws} ->
  386. rebar_base_compiler:ok_tuple(Config, Source, Ws);
  387. {error, Es, Ws} ->
  388. rebar_base_compiler:error_tuple(Config, Source,
  389. Es, Ws, Opts1)
  390. end;
  391. false ->
  392. skipped
  393. end.
  394. needs_compile(Source, Target) ->
  395. filelib:last_modified(Source) > filelib:last_modified(Target).
  396. gather_src([], Srcs) ->
  397. Srcs;
  398. gather_src([Dir|Rest], Srcs) ->
  399. gather_src(
  400. Rest, Srcs ++ rebar_utils:find_files(Dir, ?RE_PREFIX".*\\.erl\$")).
  401. -spec dirs(file:filename()) -> [file:filename()].
  402. dirs(Dir) ->
  403. [F || F <- filelib:wildcard(filename:join([Dir, "*"])), filelib:is_dir(F)].
  404. -spec delete_dir(file:filename(), [string()]) -> 'ok' | {'error', atom()}.
  405. delete_dir(Dir, []) ->
  406. file:del_dir(Dir);
  407. delete_dir(Dir, Subdirs) ->
  408. lists:foreach(fun(D) -> delete_dir(D, dirs(D)) end, Subdirs),
  409. file:del_dir(Dir).
  410. parse_attrs(Fd, Includes, Dir) ->
  411. case io:parse_erl_form(Fd, "") of
  412. {ok, Form, _Line} ->
  413. case erl_syntax:type(Form) of
  414. attribute ->
  415. NewIncludes = process_attr(Form, Includes, Dir),
  416. parse_attrs(Fd, NewIncludes, Dir);
  417. _ ->
  418. parse_attrs(Fd, Includes, Dir)
  419. end;
  420. {eof, _} ->
  421. Includes;
  422. _Err ->
  423. parse_attrs(Fd, Includes, Dir)
  424. end.
  425. process_attr(Form, Includes, Dir) ->
  426. AttrName = erl_syntax:atom_value(erl_syntax:attribute_name(Form)),
  427. process_attr(AttrName, Form, Includes, Dir).
  428. process_attr(import, Form, Includes, _Dir) ->
  429. case erl_syntax_lib:analyze_import_attribute(Form) of
  430. {Mod, _Funs} ->
  431. [module_to_erl(Mod)|Includes];
  432. Mod ->
  433. [module_to_erl(Mod)|Includes]
  434. end;
  435. process_attr(file, Form, Includes, _Dir) ->
  436. {File, _} = erl_syntax_lib:analyze_file_attribute(Form),
  437. [File|Includes];
  438. process_attr(include, Form, Includes, _Dir) ->
  439. [FileNode] = erl_syntax:attribute_arguments(Form),
  440. File = erl_syntax:string_value(FileNode),
  441. [File|Includes];
  442. process_attr(include_lib, Form, Includes, Dir) ->
  443. [FileNode] = erl_syntax:attribute_arguments(Form),
  444. RawFile = erl_syntax:string_value(FileNode),
  445. maybe_expand_include_lib_path(RawFile, Dir) ++ Includes;
  446. process_attr(behaviour, Form, Includes, _Dir) ->
  447. [FileNode] = erl_syntax:attribute_arguments(Form),
  448. File = module_to_erl(erl_syntax:atom_value(FileNode)),
  449. [File|Includes];
  450. process_attr(compile, Form, Includes, _Dir) ->
  451. [Arg] = erl_syntax:attribute_arguments(Form),
  452. case erl_syntax:concrete(Arg) of
  453. {parse_transform, Mod} ->
  454. [module_to_erl(Mod)|Includes];
  455. {core_transform, Mod} ->
  456. [module_to_erl(Mod)|Includes];
  457. L when is_list(L) ->
  458. lists:foldl(
  459. fun({parse_transform, Mod}, Acc) ->
  460. [module_to_erl(Mod)|Acc];
  461. ({core_transform, Mod}, Acc) ->
  462. [module_to_erl(Mod)|Acc];
  463. (_, Acc) ->
  464. Acc
  465. end, Includes, L);
  466. _ ->
  467. Includes
  468. end;
  469. process_attr(_, _Form, Includes, _Dir) ->
  470. Includes.
  471. module_to_erl(Mod) ->
  472. atom_to_list(Mod) ++ ".erl".
  473. %% Given a path like "stdlib/include/erl_compile.hrl", return
  474. %% "OTP_INSTALL_DIR/lib/erlang/lib/stdlib-x.y.z/include/erl_compile.hrl".
  475. %% Usually a simple [Lib, SubDir, File1] = filename:split(File) should
  476. %% work, but to not crash when an unusual include_lib path is used,
  477. %% utilize more elaborate logic.
  478. maybe_expand_include_lib_path(File, Dir) ->
  479. File1 = filename:basename(File),
  480. case filename:split(filename:dirname(File)) of
  481. [_] ->
  482. warn_and_find_path(File, Dir);
  483. [Lib | SubDir] ->
  484. case code:lib_dir(list_to_atom(Lib), list_to_atom(filename:join(SubDir))) of
  485. {error, bad_name} ->
  486. warn_and_find_path(File, Dir);
  487. AppDir ->
  488. [filename:join(AppDir, File1)]
  489. end
  490. end.
  491. %% The use of -include_lib was probably incorrect by the user but lets try to make it work.
  492. %% We search in the outdir and outdir/../include to see if the header exists.
  493. warn_and_find_path(File, Dir) ->
  494. ?WARN("Bad use of -include_lib(\"~s\")."
  495. " First path component should be the name of an application."
  496. " You probably meant -include(\"~s\").", [File, File]),
  497. SrcHeader = filename:join(Dir, File),
  498. case filelib:is_regular(SrcHeader) of
  499. true ->
  500. [SrcHeader];
  501. false ->
  502. IncludeDir = filename:join(filename:join(rebar_utils:droplast(filename:split(Dir))), "include"),
  503. IncludeHeader = filename:join(IncludeDir, File),
  504. case filelib:is_regular(IncludeHeader) of
  505. true ->
  506. [filename:join(IncludeDir, File)];
  507. false ->
  508. ?WARN("Could not find header for -include_lib(\"~s\").", [File]),
  509. []
  510. end
  511. end.
  512. %%
  513. %% Ensure all files in a list are present and abort if one is missing
  514. %%
  515. -spec check_files([file:filename()]) -> [file:filename()].
  516. check_files(FileList) ->
  517. [check_file(F) || F <- FileList].
  518. check_file(File) ->
  519. case filelib:is_regular(File) of
  520. false -> ?ABORT("File ~p is missing, aborting\n", [File]);
  521. true -> File
  522. end.