Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

782 linhas
33 KiB

15 anos atrás
15 anos atrás
15 anos atrás
15 anos atrás
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 anos atrás
15 anos atrás
15 anos atrás
15 anos atrás
15 anos atrás
15 anos atrás
15 anos atrás
  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/1, compile/2, compile/3,
  29. compile_dir/3, compile_dir/4,
  30. compile_dirs/5,
  31. clean/1]).
  32. -include("rebar.hrl").
  33. -include_lib("stdlib/include/erl_compile.hrl").
  34. -define(ERLCINFO_VSN, 2).
  35. -define(ERLCINFO_FILE, "erlcinfo").
  36. -type erlc_info_v() :: {digraph:vertex(), term()} | 'false'.
  37. -type erlc_info_e() :: {digraph:vertex(), digraph:vertex()}.
  38. -type erlc_info() :: {list(erlc_info_v()), list(erlc_info_e()), list(string())}.
  39. -record(erlcinfo, {
  40. vsn = ?ERLCINFO_VSN :: pos_integer(),
  41. info = {[], [], []} :: erlc_info()
  42. }).
  43. -type compile_opts() :: [compile_opt()].
  44. -type compile_opt() :: {recursive, boolean()}.
  45. -define(DEFAULT_OUTDIR, "ebin").
  46. -define(RE_PREFIX, "^(?!\\._)").
  47. %% ===================================================================
  48. %% Public API
  49. %% ===================================================================
  50. %% Supported configuration variables:
  51. %%
  52. %% * erl_opts - Erlang list of options passed to compile:file/2
  53. %% It is also possible to specify platform specific
  54. %% options by specifying a pair or a triplet where the
  55. %% first string is a regex that is checked against the
  56. %% string
  57. %%
  58. %% OtpRelease ++ "-" ++ SysArch ++ "-" ++ Words.
  59. %%
  60. %% where
  61. %%
  62. %% OtpRelease = erlang:system_info(otp_release).
  63. %% SysArch = erlang:system_info(system_architecture).
  64. %% Words = integer_to_list(8 *
  65. %% erlang:system_info({wordsize, external})).
  66. %%
  67. %% E.g. to define HAVE_SENDFILE only on systems with
  68. %% sendfile(), to define BACKLOG on Linux/FreeBSD as 128,
  69. %% and to define 'old_inets' for R13 OTP release do:
  70. %%
  71. %% {erl_opts, [{platform_define,
  72. %% "(linux|solaris|freebsd|darwin)",
  73. %% 'HAVE_SENDFILE'},
  74. %% {platform_define, "(linux|freebsd)",
  75. %% 'BACKLOG', 128},
  76. %% {platform_define, "R13",
  77. %% 'old_inets'}]}.
  78. %%
  79. %% @equiv compile(AppInfo, [])
  80. -spec compile(rebar_app_info:t()) -> ok.
  81. compile(AppInfo) when element(1, AppInfo) == app_info_t ->
  82. compile(AppInfo, []).
  83. %% @doc compile an individual application.
  84. -spec compile(rebar_app_info:t(), compile_opts()) -> ok.
  85. compile(AppInfo, CompileOpts) when element(1, AppInfo) == app_info_t ->
  86. Dir = ec_cnv:to_list(rebar_app_info:out_dir(AppInfo)),
  87. RebarOpts = rebar_app_info:opts(AppInfo),
  88. SrcOpts = [check_last_mod,
  89. {recursive, dir_recursive(RebarOpts, "src", CompileOpts)}],
  90. MibsOpts = [check_last_mod,
  91. {recursive, dir_recursive(RebarOpts, "mibs", CompileOpts)}],
  92. rebar_base_compiler:run(RebarOpts,
  93. check_files([filename:join(Dir, File)
  94. || File <- rebar_opts:get(RebarOpts, xrl_first_files, [])]),
  95. filename:join(Dir, "src"), ".xrl", filename:join(Dir, "src"), ".erl",
  96. fun compile_xrl/3, SrcOpts),
  97. rebar_base_compiler:run(RebarOpts,
  98. check_files([filename:join(Dir, File)
  99. || File <- rebar_opts:get(RebarOpts, yrl_first_files, [])]),
  100. filename:join(Dir, "src"), ".yrl", filename:join(Dir, "src"), ".erl",
  101. fun compile_yrl/3, SrcOpts),
  102. rebar_base_compiler:run(RebarOpts,
  103. check_files([filename:join(Dir, File)
  104. || File <- rebar_opts:get(RebarOpts, mib_first_files, [])]),
  105. filename:join(Dir, "mibs"), ".mib", filename:join([Dir, "priv", "mibs"]), ".bin",
  106. compile_mib(AppInfo), MibsOpts),
  107. SrcDirs = lists:map(fun(SrcDir) -> filename:join(Dir, SrcDir) end,
  108. rebar_dir:src_dirs(RebarOpts, ["src"])),
  109. OutDir = filename:join(Dir, outdir(RebarOpts)),
  110. compile_dirs(RebarOpts, Dir, SrcDirs, OutDir, CompileOpts),
  111. ExtraDirs = rebar_dir:extra_src_dirs(RebarOpts),
  112. F = fun(D) ->
  113. case ec_file:is_dir(filename:join([Dir, D])) of
  114. true -> compile_dirs(RebarOpts, Dir, [D], D, CompileOpts);
  115. false -> ok
  116. end
  117. end,
  118. lists:foreach(F, lists:map(fun(SrcDir) -> filename:join(Dir, SrcDir) end, ExtraDirs)).
  119. %% @hidden
  120. %% these are kept for backwards compatibility but they're bad functions with
  121. %% bad interfaces you probably shouldn't use
  122. %% State/RebarOpts have to have src_dirs set and BaseDir must be the parent
  123. %% directory of those src_dirs
  124. -spec compile(rebar_dict() | rebar_state:t(), file:name(), file:name()) -> ok.
  125. compile(State, BaseDir, OutDir) when element(1, State) == state_t ->
  126. compile(rebar_state:opts(State), BaseDir, OutDir, [{recursive, false}]);
  127. compile(RebarOpts, BaseDir, OutDir) ->
  128. compile(RebarOpts, BaseDir, OutDir, [{recursive, false}]).
  129. %% @hidden
  130. -spec compile(rebar_dict() | rebar_state:t(), file:name(), file:name(), compile_opts()) -> ok.
  131. compile(State, BaseDir, OutDir, CompileOpts) when element(1, State) == state_t ->
  132. compile(rebar_state:opts(State), BaseDir, OutDir, CompileOpts);
  133. compile(RebarOpts, BaseDir, OutDir, CompileOpts) ->
  134. SrcDirs = lists:map(fun(SrcDir) -> filename:join(BaseDir, SrcDir) end,
  135. rebar_dir:src_dirs(RebarOpts, ["src"])),
  136. compile_dirs(RebarOpts, BaseDir, SrcDirs, OutDir, CompileOpts),
  137. ExtraDirs = rebar_dir:extra_src_dirs(RebarOpts),
  138. F = fun(D) ->
  139. case ec_file:is_dir(filename:join([BaseDir, D])) of
  140. true -> compile_dirs(RebarOpts, BaseDir, [D], D, CompileOpts);
  141. false -> ok
  142. end
  143. end,
  144. lists:foreach(F, lists:map(fun(SrcDir) -> filename:join(BaseDir, SrcDir) end, ExtraDirs)).
  145. %% @equiv compile_dirs(Context, BaseDir, [Dir], Dir, [{recursive, false}])
  146. -spec compile_dir(rebar_dict() | rebar_state:t(), file:name(), file:name()) -> ok.
  147. compile_dir(State, BaseDir, Dir) when element(1, State) == state_t ->
  148. compile_dir(rebar_state:opts(State), BaseDir, Dir, [{recursive, false}]);
  149. compile_dir(RebarOpts, BaseDir, Dir) ->
  150. compile_dir(RebarOpts, BaseDir, Dir, [{recursive, false}]).
  151. %% @equiv compile_dirs(Context, BaseDir, [Dir], Dir, Opts)
  152. -spec compile_dir(rebar_dict() | rebar_state:t(), file:name(), file:name(), compile_opts()) -> ok.
  153. compile_dir(State, BaseDir, Dir, Opts) when element(1, State) == state_t ->
  154. compile_dirs(rebar_state:opts(State), BaseDir, [Dir], Dir, Opts);
  155. compile_dir(RebarOpts, BaseDir, Dir, Opts) ->
  156. compile_dirs(RebarOpts, BaseDir, [Dir], Dir, Opts).
  157. %% @doc compile a list of directories with the given opts.
  158. -spec compile_dirs(rebar_dict() | rebar_state:t(),
  159. file:filename(),
  160. [file:filename()],
  161. file:filename(),
  162. compile_opts()) -> ok.
  163. compile_dirs(State, BaseDir, Dirs, OutDir, CompileOpts) when element(1, State) == state_t ->
  164. compile_dirs(rebar_state:opts(State), BaseDir, Dirs, OutDir, CompileOpts);
  165. compile_dirs(RebarOpts, BaseDir, SrcDirs, OutDir, CompileOpts) ->
  166. ErlOpts = rebar_opts:erl_opts(RebarOpts),
  167. ?DEBUG("erlopts ~p", [ErlOpts]),
  168. AllErlFiles = gather_src(RebarOpts, BaseDir, SrcDirs, CompileOpts),
  169. ?DEBUG("files to compile ~p", [AllErlFiles]),
  170. %% Make sure that outdir is on the path
  171. ok = filelib:ensure_dir(filename:join(OutDir, "dummy.beam")),
  172. true = code:add_patha(filename:absname(OutDir)),
  173. G = init_erlcinfo(include_abs_dirs(ErlOpts, BaseDir), AllErlFiles, BaseDir, OutDir),
  174. {ParseTransforms, Rest} = split_source_files(AllErlFiles, ErlOpts),
  175. NeededErlFiles = case needed_files(G, ErlOpts, RebarOpts, BaseDir, OutDir, ParseTransforms) of
  176. [] -> needed_files(G, ErlOpts, RebarOpts, BaseDir, OutDir, Rest);
  177. %% at least one parse transform in the opts needs updating, so recompile all
  178. _ -> AllErlFiles
  179. end,
  180. {ErlFirstFiles, ErlOptsFirst} = erl_first_files(RebarOpts, ErlOpts, BaseDir, NeededErlFiles),
  181. {DepErls, OtherErls} = lists:partition(
  182. fun(Source) -> digraph:in_degree(G, Source) > 0 end,
  183. [File || File <- NeededErlFiles, not lists:member(File, ErlFirstFiles)]),
  184. SubGraph = digraph_utils:subgraph(G, DepErls),
  185. DepErlsOrdered = digraph_utils:topsort(SubGraph),
  186. FirstErls = ErlFirstFiles ++ lists:reverse(DepErlsOrdered),
  187. try
  188. rebar_base_compiler:run(
  189. RebarOpts, FirstErls, OtherErls,
  190. fun(S, C) ->
  191. ErlOpts1 = case lists:member(S, ErlFirstFiles) of
  192. true -> ErlOptsFirst;
  193. false -> ErlOpts
  194. end,
  195. internal_erl_compile(C, BaseDir, S, OutDir, ErlOpts1, RebarOpts)
  196. end)
  197. after
  198. true = digraph:delete(SubGraph),
  199. true = digraph:delete(G)
  200. end,
  201. ok.
  202. %% @doc remove compiled artifacts from an AppDir.
  203. -spec clean(rebar_app_info:t()) -> 'ok'.
  204. clean(AppInfo) ->
  205. AppDir = rebar_app_info:out_dir(AppInfo),
  206. MibFiles = rebar_utils:find_files(filename:join([AppDir, "mibs"]), ?RE_PREFIX".*\\.mib\$"),
  207. MIBs = [filename:rootname(filename:basename(MIB)) || MIB <- MibFiles],
  208. rebar_file_utils:delete_each(
  209. [filename:join([AppDir, "include",MIB++".hrl"]) || MIB <- MIBs]),
  210. ok = rebar_file_utils:rm_rf(filename:join([AppDir, "priv/mibs/*.bin"])),
  211. YrlFiles = rebar_utils:find_files(filename:join([AppDir, "src"]), ?RE_PREFIX".*\\.[x|y]rl\$"),
  212. rebar_file_utils:delete_each(
  213. [ binary_to_list(iolist_to_binary(re:replace(F, "\\.[x|y]rl$", ".erl")))
  214. || F <- YrlFiles ]),
  215. BinDirs = ["ebin"|rebar_dir:extra_src_dirs(rebar_app_info:opts(AppInfo))],
  216. ok = clean_dirs(AppDir, BinDirs),
  217. %% Delete the build graph, if any
  218. rebar_file_utils:rm_rf(erlcinfo_file(AppDir)).
  219. clean_dirs(_AppDir, []) -> ok;
  220. clean_dirs(AppDir, [Dir|Rest]) ->
  221. ok = rebar_file_utils:rm_rf(filename:join([AppDir, Dir, "*.beam"])),
  222. %% Erlang compilation is recursive, so it's possible that we have a nested
  223. %% directory structure in ebin with .beam files within. As such, we want
  224. %% to scan whatever is left in the app's out_dir directory for sub-dirs which
  225. %% satisfy our criteria.
  226. BeamFiles = rebar_utils:find_files(filename:join([AppDir, Dir]), ?RE_PREFIX".*\\.beam\$"),
  227. rebar_file_utils:delete_each(BeamFiles),
  228. lists:foreach(fun(D) -> delete_dir(D, dirs(D)) end, dirs(filename:join([AppDir, Dir]))),
  229. clean_dirs(AppDir, Rest).
  230. %% ===================================================================
  231. %% Internal functions
  232. %% ===================================================================
  233. gather_src(Opts, BaseDir, Dirs, CompileOpts) ->
  234. gather_src(Opts, filename:split(BaseDir), Dirs, [], CompileOpts).
  235. gather_src(_Opts, _BaseDirParts, [], Srcs, _CompileOpts) -> Srcs;
  236. gather_src(Opts, BaseDirParts, [Dir|Rest], Srcs, CompileOpts) ->
  237. DirParts = filename:split(Dir),
  238. RelDir = case lists:prefix(BaseDirParts,DirParts) of
  239. true ->
  240. case lists:nthtail(length(BaseDirParts),DirParts) of
  241. [] -> ".";
  242. RestParts -> filename:join(RestParts)
  243. end;
  244. false -> Dir
  245. end,
  246. DirRecursive = dir_recursive(Opts, RelDir, CompileOpts),
  247. gather_src(Opts, BaseDirParts, Rest, Srcs ++ rebar_utils:find_files(Dir, ?RE_PREFIX".*\\.erl\$", DirRecursive), CompileOpts).
  248. %% Get files which need to be compiled first, i.e. those specified in erl_first_files
  249. %% and parse_transform options. Also produce specific erl_opts for these first
  250. %% files, so that yet to be compiled parse transformations are excluded from it.
  251. erl_first_files(Opts, ErlOpts, Dir, NeededErlFiles) ->
  252. ErlFirstFilesConf = rebar_opts:get(Opts, erl_first_files, []),
  253. NeededSrcDirs = lists:usort(lists:map(fun filename:dirname/1, NeededErlFiles)),
  254. %% NOTE: order of files here is important!
  255. ErlFirstFiles =
  256. [filename:join(Dir, File) || File <- ErlFirstFilesConf,
  257. lists:member(filename:join(Dir, File), NeededErlFiles)],
  258. {ParseTransforms, ParseTransformsErls} =
  259. lists:unzip(lists:flatmap(
  260. fun(PT) ->
  261. PTerls = [filename:join(D, module_to_erl(PT)) || D <- NeededSrcDirs],
  262. [{PT, PTerl} || PTerl <- PTerls, lists:member(PTerl, NeededErlFiles)]
  263. end, proplists:get_all_values(parse_transform, ErlOpts))),
  264. ErlOptsFirst = lists:filter(fun({parse_transform, PT}) ->
  265. not lists:member(PT, ParseTransforms);
  266. (_) ->
  267. true
  268. end, ErlOpts),
  269. {ErlFirstFiles ++ ParseTransformsErls, ErlOptsFirst}.
  270. split_source_files(SourceFiles, ErlOpts) ->
  271. ParseTransforms = proplists:get_all_values(parse_transform, ErlOpts),
  272. lists:partition(fun(Source) ->
  273. lists:member(filename_to_atom(Source), ParseTransforms)
  274. end, SourceFiles).
  275. filename_to_atom(F) -> list_to_atom(filename:rootname(filename:basename(F))).
  276. %% Get subset of SourceFiles which need to be recompiled, respecting
  277. %% dependencies induced by given graph G.
  278. needed_files(G, ErlOpts, RebarOpts, Dir, OutDir, SourceFiles) ->
  279. lists:filter(fun(Source) ->
  280. TargetBase = target_base(OutDir, Source),
  281. Target = TargetBase ++ ".beam",
  282. PrivIncludes = [{i, filename:join(Dir, Src)}
  283. || Src <- rebar_dir:all_src_dirs(RebarOpts, ["src"], [])],
  284. AllOpts = [{outdir, filename:dirname(Target)}
  285. ,{i, filename:join(Dir, "include")}
  286. ,{i, Dir}] ++ PrivIncludes ++ ErlOpts,
  287. %% necessary for erlang:function_exported/3 to work as expected
  288. %% called here for clarity as it's required by both opts_changed/2
  289. %% and erl_compiler_opts_set/0
  290. _ = code:ensure_loaded(compile),
  291. digraph:vertex(G, Source) > {Source, filelib:last_modified(Target)}
  292. orelse opts_changed(AllOpts, TargetBase)
  293. orelse erl_compiler_opts_set()
  294. end, SourceFiles).
  295. maybe_rm_beam_and_edge(G, OutDir, Source) ->
  296. %% This is NOT a double check it is the only check that the source file is actually gone
  297. case filelib:is_regular(Source) of
  298. true ->
  299. %% Actually exists, don't delete
  300. false;
  301. false ->
  302. Target = target_base(OutDir, Source) ++ ".beam",
  303. ?DEBUG("Source ~s is gone, deleting previous beam file if it exists ~s", [Source, Target]),
  304. file:delete(Target),
  305. digraph:del_vertex(G, Source),
  306. true
  307. end.
  308. opts_changed(NewOpts, Target) ->
  309. TotalOpts = case erlang:function_exported(compile, env_compiler_options, 0) of
  310. true -> NewOpts ++ compile:env_compiler_options();
  311. false -> NewOpts
  312. end,
  313. case compile_info(Target) of
  314. {ok, Opts} -> lists:sort(Opts) =/= lists:sort(TotalOpts);
  315. _ -> true
  316. end.
  317. compile_info(Target) ->
  318. case beam_lib:chunks(Target, [compile_info]) of
  319. {ok, {_mod, Chunks}} ->
  320. CompileInfo = proplists:get_value(compile_info, Chunks, []),
  321. {ok, proplists:get_value(options, CompileInfo, [])};
  322. {error, beam_lib, Reason} ->
  323. ?WARN("Couldn't read debug info from ~p for reason: ~p", [Target, Reason]),
  324. {error, Reason}
  325. end.
  326. erl_compiler_opts_set() ->
  327. EnvSet = case os:getenv("ERL_COMPILER_OPTIONS") of
  328. false -> false;
  329. _ -> true
  330. end,
  331. %% return false if changed env opts would have been caught in opts_changed/2
  332. EnvSet andalso not erlang:function_exported(compile, env_compiler_options, 0).
  333. erlcinfo_file(Dir) ->
  334. filename:join(rebar_dir:local_cache_dir(Dir), ?ERLCINFO_FILE).
  335. %% Get dependency graph of given Erls files and their dependencies (header files,
  336. %% parse transforms, behaviours etc.) located in their directories or given
  337. %% InclDirs. Note that last modification times stored in vertices already respect
  338. %% dependencies induced by given graph G.
  339. init_erlcinfo(InclDirs, Erls, Dir, OutDir) ->
  340. G = digraph:new([acyclic]),
  341. try restore_erlcinfo(G, InclDirs, Dir)
  342. catch
  343. _:_ ->
  344. ?WARN("Failed to restore ~s file. Discarding it.~n", [erlcinfo_file(Dir)]),
  345. file:delete(erlcinfo_file(Dir))
  346. end,
  347. Dirs = source_and_include_dirs(InclDirs, Erls),
  348. %% A source file may have been renamed or deleted. Remove it from the graph
  349. %% and remove any beam file for that source if it exists.
  350. Modified = maybe_rm_beams_and_edges(G, OutDir, Erls),
  351. Modified1 = lists:foldl(update_erlcinfo_fun(G, Dirs), Modified, Erls),
  352. if Modified1 -> store_erlcinfo(G, InclDirs, Dir); not Modified1 -> ok end,
  353. G.
  354. maybe_rm_beams_and_edges(G, Dir, Files) ->
  355. Vertices = digraph:vertices(G),
  356. case lists:filter(fun(File) ->
  357. case filename:extension(File) =:= ".erl" of
  358. true ->
  359. maybe_rm_beam_and_edge(G, Dir, File);
  360. false ->
  361. false
  362. end
  363. end, lists:sort(Vertices) -- lists:sort(Files)) of
  364. [] ->
  365. false;
  366. _ ->
  367. true
  368. end.
  369. source_and_include_dirs(InclDirs, Erls) ->
  370. SourceDirs = lists:map(fun filename:dirname/1, Erls),
  371. lists:usort(InclDirs ++ SourceDirs).
  372. update_erlcinfo(G, Dirs, Source) ->
  373. case digraph:vertex(G, Source) of
  374. {_, LastUpdated} ->
  375. case filelib:last_modified(Source) of
  376. 0 ->
  377. %% The file doesn't exist anymore,
  378. %% erase it from the graph.
  379. %% All the edges will be erased automatically.
  380. digraph:del_vertex(G, Source),
  381. modified;
  382. LastModified when LastUpdated < LastModified ->
  383. modify_erlcinfo(G, Source, LastModified, filename:dirname(Source), Dirs);
  384. _ ->
  385. Modified = lists:foldl(
  386. update_erlcinfo_fun(G, Dirs),
  387. false, digraph:out_neighbours(G, Source)),
  388. MaxModified = update_max_modified_deps(G, Source),
  389. case Modified orelse MaxModified > LastUpdated of
  390. true -> modified;
  391. false -> unmodified
  392. end
  393. end;
  394. false ->
  395. modify_erlcinfo(G, Source, filelib:last_modified(Source), filename:dirname(Source), Dirs)
  396. end.
  397. update_erlcinfo_fun(G, Dirs) ->
  398. fun(Erl, Modified) ->
  399. case update_erlcinfo(G, Dirs, Erl) of
  400. modified -> true;
  401. unmodified -> Modified
  402. end
  403. end.
  404. update_max_modified_deps(G, Source) ->
  405. MaxModified = lists:max(lists:map(
  406. fun(File) -> {_, MaxModified} = digraph:vertex(G, File), MaxModified end,
  407. [Source|digraph:out_neighbours(G, Source)])),
  408. digraph:add_vertex(G, Source, MaxModified),
  409. MaxModified.
  410. modify_erlcinfo(G, Source, LastModified, Dir, Dirs) ->
  411. {ok, Fd} = file:open(Source, [read]),
  412. Incls = parse_attrs(Fd, [], Dir),
  413. AbsIncls = expand_file_names(Incls, Dirs),
  414. ok = file:close(Fd),
  415. digraph:add_vertex(G, Source, LastModified),
  416. digraph:del_edges(G, digraph:out_edges(G, Source)),
  417. lists:foreach(
  418. fun(Incl) ->
  419. update_erlcinfo(G, Dirs, Incl),
  420. digraph:add_edge(G, Source, Incl)
  421. end, AbsIncls),
  422. modified.
  423. restore_erlcinfo(G, InclDirs, Dir) ->
  424. case file:read_file(erlcinfo_file(Dir)) of
  425. {ok, Data} ->
  426. % Since externally passed InclDirs can influence erlcinfo graph (see
  427. % modify_erlcinfo), we have to check here that they didn't change.
  428. #erlcinfo{vsn=?ERLCINFO_VSN, info={Vs, Es, InclDirs}} =
  429. binary_to_term(Data),
  430. lists:foreach(
  431. fun({V, LastUpdated}) ->
  432. digraph:add_vertex(G, V, LastUpdated)
  433. end, Vs),
  434. lists:foreach(
  435. fun({_, V1, V2, _}) ->
  436. digraph:add_edge(G, V1, V2)
  437. end, Es);
  438. {error, _} ->
  439. ok
  440. end.
  441. store_erlcinfo(G, InclDirs, Dir) ->
  442. Vs = lists:map(fun(V) -> digraph:vertex(G, V) end, digraph:vertices(G)),
  443. Es = lists:map(fun(E) -> digraph:edge(G, E) end, digraph:edges(G)),
  444. File = erlcinfo_file(Dir),
  445. ok = filelib:ensure_dir(File),
  446. Data = term_to_binary(#erlcinfo{info={Vs, Es, InclDirs}}, [{compressed, 2}]),
  447. file:write_file(File, Data).
  448. %% NOTE: If, for example, one of the entries in Files, refers to
  449. %% gen_server.erl, that entry will be dropped. It is dropped because
  450. %% such an entry usually refers to the beam file, and we don't pass a
  451. %% list of OTP src dirs for finding gen_server.erl's full path. Also,
  452. %% if gen_server.erl was modified, it's not rebar's task to compile a
  453. %% new version of the beam file. Therefore, it's reasonable to drop
  454. %% such entries. Also see process_attr(behaviour, Form, Includes).
  455. -spec expand_file_names([file:filename()],
  456. [file:filename()]) -> [file:filename()].
  457. expand_file_names(Files, Dirs) ->
  458. %% We check if Files exist by itself or within the directories
  459. %% listed in Dirs.
  460. %% Return the list of files matched.
  461. lists:flatmap(
  462. fun(Incl) ->
  463. case filelib:is_regular(Incl) of
  464. true ->
  465. [Incl];
  466. false ->
  467. lists:flatmap(
  468. fun(Dir) ->
  469. FullPath = filename:join(Dir, Incl),
  470. case filelib:is_regular(FullPath) of
  471. true ->
  472. [FullPath];
  473. false ->
  474. []
  475. end
  476. end, Dirs)
  477. end
  478. end, Files).
  479. -spec internal_erl_compile(rebar_dict(), file:filename(), file:filename(),
  480. file:filename(), list(), rebar_dict()) ->
  481. ok | {ok, any()} | {error, any(), any()}.
  482. internal_erl_compile(Opts, Dir, Module, OutDir, ErlOpts, RebarOpts) ->
  483. Target = target_base(OutDir, Module) ++ ".beam",
  484. ok = filelib:ensure_dir(Target),
  485. PrivIncludes = [{i, filename:join(Dir, Src)}
  486. || Src <- rebar_dir:all_src_dirs(RebarOpts, ["src"], [])],
  487. AllOpts = [{outdir, filename:dirname(Target)}] ++ ErlOpts ++ PrivIncludes ++
  488. [{i, filename:join(Dir, "include")}, {i, Dir}, return],
  489. case compile:file(Module, AllOpts) of
  490. {ok, _Mod} ->
  491. ok;
  492. {ok, _Mod, Ws} ->
  493. FormattedWs = format_error_sources(Ws, Opts),
  494. rebar_base_compiler:ok_tuple(Module, FormattedWs);
  495. {error, Es, Ws} ->
  496. error_tuple(Module, Es, Ws, AllOpts, Opts)
  497. end.
  498. error_tuple(Module, Es, Ws, AllOpts, Opts) ->
  499. FormattedEs = format_error_sources(Es, Opts),
  500. FormattedWs = format_error_sources(Ws, Opts),
  501. rebar_base_compiler:error_tuple(Module, FormattedEs, FormattedWs, AllOpts).
  502. format_error_sources(Es, Opts) ->
  503. [{rebar_base_compiler:format_error_source(Src, Opts), Desc}
  504. || {Src, Desc} <- Es].
  505. target_base(OutDir, Source) ->
  506. filename:join(OutDir, filename:basename(Source, ".erl")).
  507. -spec compile_mib(rebar_app_info:t()) ->
  508. fun((file:filename(), file:filename(), rebar_dict()) -> 'ok').
  509. compile_mib(AppInfo) ->
  510. fun(Source, Target, Opts) ->
  511. Dir = filename:dirname(Target),
  512. Mib = filename:rootname(Target),
  513. HrlFilename = Mib ++ ".hrl",
  514. AppInclude = filename:join([rebar_app_info:dir(AppInfo), "include"]),
  515. ok = filelib:ensure_dir(Target),
  516. ok = filelib:ensure_dir(filename:join([AppInclude, "dummy.hrl"])),
  517. AllOpts = [{outdir, Dir}
  518. ,{i, [Dir]}] ++
  519. rebar_opts:get(Opts, mib_opts, []),
  520. case snmpc:compile(Source, AllOpts) of
  521. {ok, _} ->
  522. MibToHrlOpts =
  523. case proplists:get_value(verbosity, AllOpts, undefined) of
  524. undefined ->
  525. #options{specific = [],
  526. cwd = rebar_dir:get_cwd()};
  527. Verbosity ->
  528. #options{specific = [{verbosity, Verbosity}],
  529. cwd = rebar_dir:get_cwd()}
  530. end,
  531. ok = snmpc:mib_to_hrl(Mib, Mib, MibToHrlOpts),
  532. rebar_file_utils:mv(HrlFilename, AppInclude),
  533. ok;
  534. {error, compilation_failed} ->
  535. ?FAIL
  536. end
  537. end.
  538. -spec compile_xrl(file:filename(), file:filename(),
  539. rebar_dict()) -> 'ok'.
  540. compile_xrl(Source, Target, Opts) ->
  541. AllOpts = [{scannerfile, Target} | rebar_opts:get(Opts, xrl_opts, [])],
  542. compile_xrl_yrl(Opts, Source, Target, AllOpts, leex).
  543. -spec compile_yrl(file:filename(), file:filename(),
  544. rebar_dict()) -> 'ok'.
  545. compile_yrl(Source, Target, Opts) ->
  546. AllOpts = [{parserfile, Target} | rebar_opts:get(Opts, yrl_opts, [])],
  547. compile_xrl_yrl(Opts, Source, Target, AllOpts, yecc).
  548. -spec compile_xrl_yrl(rebar_dict(), file:filename(),
  549. file:filename(), list(), module()) -> 'ok'.
  550. compile_xrl_yrl(_Opts, Source, Target, AllOpts, Mod) ->
  551. %% FIX ME: should be the outdir or something
  552. Dir = filename:dirname(filename:dirname(Target)),
  553. AllOpts1 = [{includefile, filename:join(Dir, I)} || {includefile, I} <- AllOpts,
  554. filename:pathtype(I) =:= relative],
  555. case needs_compile(Source, Target) of
  556. true ->
  557. case Mod:file(Source, AllOpts1 ++ [{return, true}]) of
  558. {ok, _} ->
  559. ok;
  560. {ok, _Mod, Ws} ->
  561. rebar_base_compiler:ok_tuple(Source, Ws);
  562. {error, Es, Ws} ->
  563. rebar_base_compiler:error_tuple(Source,
  564. Es, Ws, AllOpts1)
  565. end;
  566. false ->
  567. skipped
  568. end.
  569. needs_compile(Source, Target) ->
  570. filelib:last_modified(Source) > filelib:last_modified(Target).
  571. -spec dirs(file:filename()) -> [file:filename()].
  572. dirs(Dir) ->
  573. [F || F <- filelib:wildcard(filename:join([Dir, "*"])), filelib:is_dir(F)].
  574. -spec delete_dir(file:filename(), [string()]) -> 'ok' | {'error', atom()}.
  575. delete_dir(Dir, []) ->
  576. file:del_dir(Dir);
  577. delete_dir(Dir, Subdirs) ->
  578. lists:foreach(fun(D) -> delete_dir(D, dirs(D)) end, Subdirs),
  579. file:del_dir(Dir).
  580. parse_attrs(Fd, Includes, Dir) ->
  581. case io:parse_erl_form(Fd, "") of
  582. {ok, Form, _Line} ->
  583. case erl_syntax:type(Form) of
  584. attribute ->
  585. NewIncludes = process_attr(Form, Includes, Dir),
  586. parse_attrs(Fd, NewIncludes, Dir);
  587. _ ->
  588. parse_attrs(Fd, Includes, Dir)
  589. end;
  590. {eof, _} ->
  591. Includes;
  592. _Err ->
  593. parse_attrs(Fd, Includes, Dir)
  594. end.
  595. process_attr(Form, Includes, Dir) ->
  596. AttrName = erl_syntax:atom_value(erl_syntax:attribute_name(Form)),
  597. process_attr(AttrName, Form, Includes, Dir).
  598. process_attr(import, Form, Includes, _Dir) ->
  599. case erl_syntax_lib:analyze_import_attribute(Form) of
  600. {Mod, _Funs} ->
  601. [module_to_erl(Mod)|Includes];
  602. Mod ->
  603. [module_to_erl(Mod)|Includes]
  604. end;
  605. process_attr(file, Form, Includes, _Dir) ->
  606. {File, _} = erl_syntax_lib:analyze_file_attribute(Form),
  607. [File|Includes];
  608. process_attr(include, Form, Includes, _Dir) ->
  609. [FileNode] = erl_syntax:attribute_arguments(Form),
  610. File = erl_syntax:string_value(FileNode),
  611. [File|Includes];
  612. process_attr(include_lib, Form, Includes, Dir) ->
  613. [FileNode] = erl_syntax:attribute_arguments(Form),
  614. RawFile = erl_syntax:string_value(FileNode),
  615. maybe_expand_include_lib_path(RawFile, Dir) ++ Includes;
  616. process_attr(behavior, Form, Includes, _Dir) ->
  617. process_attr(behaviour, Form, Includes, _Dir);
  618. process_attr(behaviour, Form, Includes, _Dir) ->
  619. [FileNode] = erl_syntax:attribute_arguments(Form),
  620. File = module_to_erl(erl_syntax:atom_value(FileNode)),
  621. [File|Includes];
  622. process_attr(compile, Form, Includes, _Dir) ->
  623. [Arg] = erl_syntax:attribute_arguments(Form),
  624. case erl_syntax:concrete(Arg) of
  625. {parse_transform, Mod} ->
  626. [module_to_erl(Mod)|Includes];
  627. {core_transform, Mod} ->
  628. [module_to_erl(Mod)|Includes];
  629. L when is_list(L) ->
  630. lists:foldl(
  631. fun({parse_transform, Mod}, Acc) ->
  632. [module_to_erl(Mod)|Acc];
  633. ({core_transform, Mod}, Acc) ->
  634. [module_to_erl(Mod)|Acc];
  635. (_, Acc) ->
  636. Acc
  637. end, Includes, L);
  638. _ ->
  639. Includes
  640. end;
  641. process_attr(_, _Form, Includes, _Dir) ->
  642. Includes.
  643. module_to_erl(Mod) ->
  644. atom_to_list(Mod) ++ ".erl".
  645. %% Given a path like "stdlib/include/erl_compile.hrl", return
  646. %% "OTP_INSTALL_DIR/lib/erlang/lib/stdlib-x.y.z/include/erl_compile.hrl".
  647. %% Usually a simple [Lib, SubDir, File1] = filename:split(File) should
  648. %% work, but to not crash when an unusual include_lib path is used,
  649. %% utilize more elaborate logic.
  650. maybe_expand_include_lib_path(File, Dir) ->
  651. File1 = filename:basename(File),
  652. case filename:split(filename:dirname(File)) of
  653. [_] ->
  654. warn_and_find_path(File, Dir);
  655. [Lib | SubDir] ->
  656. case code:lib_dir(list_to_atom(Lib), list_to_atom(filename:join(SubDir))) of
  657. {error, bad_name} ->
  658. warn_and_find_path(File, Dir);
  659. AppDir ->
  660. [filename:join(AppDir, File1)]
  661. end
  662. end.
  663. %% The use of -include_lib was probably incorrect by the user but lets try to make it work.
  664. %% We search in the outdir and outdir/../include to see if the header exists.
  665. warn_and_find_path(File, Dir) ->
  666. SrcHeader = filename:join(Dir, File),
  667. case filelib:is_regular(SrcHeader) of
  668. true ->
  669. [SrcHeader];
  670. false ->
  671. IncludeDir = filename:join(rebar_utils:droplast(filename:split(Dir))++["include"]),
  672. IncludeHeader = filename:join(IncludeDir, File),
  673. case filelib:is_regular(IncludeHeader) of
  674. true ->
  675. [filename:join(IncludeDir, File)];
  676. false ->
  677. []
  678. end
  679. end.
  680. %%
  681. %% Ensure all files in a list are present and abort if one is missing
  682. %%
  683. -spec check_files([file:filename()]) -> [file:filename()].
  684. check_files(FileList) ->
  685. [check_file(F) || F <- FileList].
  686. check_file(File) ->
  687. case filelib:is_regular(File) of
  688. false -> ?ABORT("File ~p is missing, aborting\n", [File]);
  689. true -> File
  690. end.
  691. outdir(RebarOpts) ->
  692. ErlOpts = rebar_opts:erl_opts(RebarOpts),
  693. proplists:get_value(outdir, ErlOpts, ?DEFAULT_OUTDIR).
  694. include_abs_dirs(ErlOpts, BaseDir) ->
  695. InclDirs = ["include"|proplists:get_all_values(i, ErlOpts)],
  696. lists:map(fun(Incl) -> filename:join([BaseDir, Incl]) end, InclDirs).
  697. dir_recursive(Opts, Dir, CompileOpts) when is_list(CompileOpts) ->
  698. case proplists:get_value(recursive,CompileOpts) of
  699. undefined -> rebar_dir:recursive(Opts, Dir);
  700. Recursive -> Recursive
  701. end.