Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

623 righe
24 KiB

15 anni fa
15 anni fa
15 anni fa
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 anni fa
15 anni fa
15 anni fa
14 anni fa
14 anni fa
14 anni fa
15 anni fa
15 anni fa
15 anni fa
14 anni fa
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 anni fa
  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. clean/2]).
  30. -include("rebar.hrl").
  31. -include_lib("stdlib/include/erl_compile.hrl").
  32. -define(ERLCINFO_VSN, 1).
  33. -define(ERLCINFO_FILE, "erlcinfo").
  34. -type erlc_info_v() :: {digraph:vertex(), term()} | 'false'.
  35. -type erlc_info_e() :: {digraph:vertex(), digraph:vertex()}.
  36. -type erlc_info() :: {list(erlc_info_v()), list(erlc_info_e())}.
  37. -record(erlcinfo,
  38. {
  39. vsn = ?ERLCINFO_VSN :: pos_integer(),
  40. info = {[], []} :: erlc_info()
  41. }).
  42. -define(RE_PREFIX, "^[^._]").
  43. %% ===================================================================
  44. %% Public API
  45. %% ===================================================================
  46. %% Supported configuration variables:
  47. %%
  48. %% * erl_opts - Erlang list of options passed to compile:file/2
  49. %% It is also possible to specify platform specific
  50. %% options by specifying a pair or a triplet where the
  51. %% first string is a regex that is checked against the
  52. %% string
  53. %%
  54. %% OtpRelease ++ "-" ++ SysArch ++ "-" ++ Words.
  55. %%
  56. %% where
  57. %%
  58. %% OtpRelease = erlang:system_info(otp_release).
  59. %% SysArch = erlang:system_info(system_architecture).
  60. %% Words = integer_to_list(8 *
  61. %% erlang:system_info({wordsize, external})).
  62. %%
  63. %% E.g. to define HAVE_SENDFILE only on systems with
  64. %% sendfile(), to define BACKLOG on Linux/FreeBSD as 128,
  65. %% and to define 'old_inets' for R13 OTP release do:
  66. %%
  67. %% {erl_opts, [{platform_define,
  68. %% "(linux|solaris|freebsd|darwin)",
  69. %% 'HAVE_SENDFILE'},
  70. %% {platform_define, "(linux|freebsd)",
  71. %% 'BACKLOG', 128},
  72. %% {platform_define, "R13",
  73. %% 'old_inets'}]}.
  74. %%
  75. -spec compile(rebar_state:t(), file:name()) -> 'ok'.
  76. compile(Config, Dir) ->
  77. rebar_base_compiler:run(Config,
  78. check_files(rebar_state:get(
  79. Config, xrl_first_files, [])),
  80. filename:join(Dir, "src"), ".xrl", filename:join(Dir, "src"), ".erl",
  81. fun compile_xrl/3),
  82. rebar_base_compiler:run(Config,
  83. check_files(rebar_state:get(
  84. Config, yrl_first_files, [])),
  85. filename:join(Dir, "src"), ".yrl", filename:join(Dir, "src"), ".erl",
  86. fun compile_yrl/3),
  87. rebar_base_compiler:run(Config,
  88. check_files(rebar_state:get(
  89. Config, mib_first_files, [])),
  90. filename:join(Dir, "mibs"), ".mib", filename:join([Dir, "priv", "mibs"]), ".bin",
  91. fun compile_mib/3),
  92. doterl_compile(Config, Dir).
  93. -spec clean(rebar_state:t(), file:filename()) -> 'ok'.
  94. clean(Config, AppDir) ->
  95. MibFiles = rebar_utils:find_files(filename:join(AppDir, "mibs"), ?RE_PREFIX".*\\.mib\$"),
  96. MIBs = [filename:rootname(filename:basename(MIB)) || MIB <- MibFiles],
  97. rebar_file_utils:delete_each(
  98. [filename:join([AppDir, "include",MIB++".hrl"]) || MIB <- MIBs]),
  99. lists:foreach(fun(F) -> ok = rebar_file_utils:rm_rf(F) end,
  100. [filename:join(AppDir, "ebin/*.beam"), filename:join(AppDir, "priv/mibs/*.bin")]),
  101. YrlFiles = rebar_utils:find_files(filename:join(AppDir, "src"), ?RE_PREFIX".*\\.[x|y]rl\$"),
  102. rebar_file_utils:delete_each(
  103. [ binary_to_list(iolist_to_binary(re:replace(F, "\\.[x|y]rl$", ".erl")))
  104. || F <- YrlFiles ]),
  105. %% Delete the build graph, if any
  106. rebar_file_utils:rm_rf(erlcinfo_file(Config)),
  107. %% Erlang compilation is recursive, so it's possible that we have a nested
  108. %% directory structure in ebin with .beam files within. As such, we want
  109. %% to scan whatever is left in the ebin/ directory for sub-dirs which
  110. %% satisfy our criteria.
  111. BeamFiles = rebar_utils:find_files(filename:join(AppDir, "ebin"), ?RE_PREFIX".*\\.beam\$"),
  112. rebar_file_utils:delete_each(BeamFiles),
  113. lists:foreach(fun(Dir) -> delete_dir(Dir, dirs(Dir)) end, dirs(filename:join(AppDir, "ebin"))),
  114. ok.
  115. %% ===================================================================
  116. %% Internal functions
  117. %% ===================================================================
  118. -spec doterl_compile(rebar_state:t(), file:filename()) -> ok.
  119. doterl_compile(State, Dir) ->
  120. ErlOpts = rebar_utils:erl_opts(State),
  121. doterl_compile(State, Dir, [], ErlOpts).
  122. doterl_compile(Config, Dir, MoreSources, ErlOpts) ->
  123. OutDir = filename:join(Dir, "ebin"),
  124. ErlFirstFilesConf = rebar_state:get(Config, erl_first_files, []),
  125. ?DEBUG("erl_opts ~p", [ErlOpts]),
  126. %% Support the src_dirs option allowing multiple directories to
  127. %% contain erlang source. This might be used, for example, should
  128. %% eunit tests be separated from the core application source.
  129. SrcDirs = lists:map(fun(X) ->
  130. filename:join(Dir, X)
  131. end, rebar_dir:src_dirs(proplists:append_values(src_dirs, ErlOpts))),
  132. AllErlFiles = gather_src(SrcDirs, []) ++ MoreSources,
  133. %% NOTE: If and when erl_first_files is not inherited anymore
  134. %% (rebar_state:get instead of rebar_state:get_list), consider
  135. %% logging a warning message for any file listed in erl_first_files which
  136. %% wasn't found via gather_src.
  137. {ErlFirstFiles, RestErls} =
  138. lists:partition(
  139. fun(Source) ->
  140. lists:member(list_to_atom(filename:basename(Source, ".erl")), ErlFirstFilesConf)
  141. end, AllErlFiles),
  142. %% Make sure that ebin/ exists and is on the path
  143. ok = filelib:ensure_dir(filename:join([Dir, "ebin", "dummy.beam"])),
  144. CurrPath = code:get_path(),
  145. true = code:add_path(filename:absname(filename:join(Dir, "ebin"))),
  146. OutDir1 = proplists:get_value(outdir, ErlOpts, OutDir),
  147. G = init_erlcinfo(Config, AllErlFiles),
  148. %% Split RestErls so that files which are depended on are treated
  149. %% like erl_first_files.
  150. {OtherFirstErls, OtherErls} =
  151. lists:partition(
  152. fun(F) ->
  153. Children = get_children(G, F),
  154. log_files(?FMT("Files dependent on ~s", [F]), Children),
  155. case erls(Children) of
  156. [] ->
  157. %% There are no files dependent on this file.
  158. false;
  159. _ ->
  160. %% There are some files dependent on the file.
  161. %% Thus the file has higher priority
  162. %% and should be compiled in the first place.
  163. true
  164. end
  165. end, RestErls),
  166. %% Dependencies of OtherFirstErls that must be compiled first.
  167. OtherFirstErlsDeps = lists:flatmap(
  168. fun(Erl) -> erls(get_parents(G, Erl)) end,
  169. OtherFirstErls),
  170. %% NOTE: In case the way we retrieve OtherFirstErlsDeps or merge
  171. %% it with OtherFirstErls does not result in the correct compile
  172. %% priorities, or the method in use proves to be too slow for
  173. %% certain projects, consider using a more elaborate method (maybe
  174. %% digraph_utils) or alternatively getting and compiling the .erl
  175. %% parents of an individual Source in internal_erl_compile. By not
  176. %% handling this in internal_erl_compile, we also avoid extra
  177. %% needs_compile/2 calls.
  178. FirstErls = ErlFirstFiles ++ uo_merge(OtherFirstErlsDeps, OtherFirstErls),
  179. ?DEBUG("Files to compile first: ~p", [FirstErls]),
  180. rebar_base_compiler:run(
  181. Config, FirstErls, OtherErls,
  182. fun(S, C) ->
  183. internal_erl_compile(C, Dir, S, OutDir1, ErlOpts, G)
  184. end),
  185. true = code:set_path(CurrPath),
  186. ok.
  187. %%
  188. %% Return all .erl files from a list of files
  189. %%
  190. erls(Files) ->
  191. [Erl || Erl <- Files, filename:extension(Erl) =:= ".erl"].
  192. %%
  193. %% Return a list without duplicates while preserving order
  194. %%
  195. ulist(L) ->
  196. ulist(L, []).
  197. ulist([H|T], Acc) ->
  198. case lists:member(H, T) of
  199. true ->
  200. ulist(T, Acc);
  201. false ->
  202. ulist(T, [H|Acc])
  203. end;
  204. ulist([], Acc) ->
  205. lists:reverse(Acc).
  206. %%
  207. %% Merge two lists without duplicates while preserving order
  208. %%
  209. uo_merge(L1, L2) ->
  210. lists:foldl(fun(E, Acc) -> u_add_element(E, Acc) end, ulist(L1), L2).
  211. u_add_element(Elem, [Elem|_]=Set) -> Set;
  212. u_add_element(Elem, [E1|Set]) -> [E1|u_add_element(Elem, Set)];
  213. u_add_element(Elem, []) -> [Elem].
  214. -spec include_path(file:filename(),
  215. rebar_state:t()) -> [file:filename(), ...].
  216. include_path(Source, Config) ->
  217. ErlOpts = rebar_state:get(Config, erl_opts, []),
  218. Dir = filename:join(rebar_utils:droplast(filename:split(filename:dirname(Source)))),
  219. lists:usort([filename:join(Dir, "include"), filename:dirname(Source)]
  220. ++ proplists:get_all_values(i, ErlOpts)).
  221. -spec needs_compile(file:filename(), file:filename(),
  222. [string()]) -> boolean().
  223. needs_compile(Source, Target, Parents) ->
  224. TargetLastMod = filelib:last_modified(Target),
  225. lists:any(fun(I) -> TargetLastMod < filelib:last_modified(I) end,
  226. [Source] ++ Parents).
  227. check_erlcinfo(_Config, #erlcinfo{vsn=?ERLCINFO_VSN}) ->
  228. ok;
  229. check_erlcinfo(Config, #erlcinfo{vsn=Vsn}) ->
  230. ?ABORT("~s file version is incompatible. expected: ~b got: ~b",
  231. [erlcinfo_file(Config), ?ERLCINFO_VSN, Vsn]);
  232. check_erlcinfo(Config, _) ->
  233. ?ABORT("~s file is invalid. Please delete before next run.",
  234. [erlcinfo_file(Config)]).
  235. erlcinfo_file(_Config) ->
  236. filename:join([rebar_dir:get_cwd(), ?CONFIG_DIR, ?ERLCINFO_FILE]).
  237. init_erlcinfo(Config, Erls) ->
  238. G = restore_erlcinfo(Config),
  239. %% Get a unique list of dirs based on the source files' locations.
  240. %% This is used for finding files in sub dirs of the configured
  241. %% src_dirs. For example, src/sub_dir/foo.erl.
  242. Dirs = sets:to_list(lists:foldl(
  243. fun(Erl, Acc) ->
  244. Dir = filename:dirname(Erl),
  245. sets:add_element(Dir, Acc)
  246. end, sets:new(), Erls)),
  247. Updates = [update_erlcinfo(G, Erl, include_path(Erl, Config) ++ Dirs)
  248. || Erl <- Erls],
  249. Modified = lists:member(modified, Updates),
  250. ok = store_erlcinfo(G, Config, Modified),
  251. G.
  252. update_erlcinfo(G, Source, Dirs) ->
  253. case digraph:vertex(G, Source) of
  254. {_, LastUpdated} ->
  255. case filelib:last_modified(Source) of
  256. 0 ->
  257. %% The file doesn't exist anymore,
  258. %% erase it from the graph.
  259. %% All the edges will be erased automatically.
  260. digraph:del_vertex(G, Source),
  261. modified;
  262. LastModified when LastUpdated < LastModified ->
  263. modify_erlcinfo(G, Source, Dirs),
  264. modified;
  265. _ ->
  266. unmodified
  267. end;
  268. false ->
  269. modify_erlcinfo(G, Source, Dirs),
  270. modified
  271. end.
  272. modify_erlcinfo(G, Source, Dirs) ->
  273. {ok, Fd} = file:open(Source, [read]),
  274. Incls = parse_attrs(Fd, []),
  275. AbsIncls = expand_file_names(Incls, Dirs),
  276. ok = file:close(Fd),
  277. LastUpdated = {date(), time()},
  278. digraph:add_vertex(G, Source, LastUpdated),
  279. lists:foreach(
  280. fun(Incl) ->
  281. update_erlcinfo(G, Incl, Dirs),
  282. digraph:add_edge(G, Source, Incl)
  283. end, AbsIncls).
  284. restore_erlcinfo(Config) ->
  285. File = erlcinfo_file(Config),
  286. G = digraph:new(),
  287. case file:read_file(File) of
  288. {ok, Data} ->
  289. try binary_to_term(Data) of
  290. Erlcinfo ->
  291. ok = check_erlcinfo(Config, Erlcinfo),
  292. #erlcinfo{info=ErlcInfo} = Erlcinfo,
  293. {Vs, Es} = ErlcInfo,
  294. lists:foreach(
  295. fun({V, LastUpdated}) ->
  296. digraph:add_vertex(G, V, LastUpdated)
  297. end, Vs),
  298. lists:foreach(
  299. fun({V1, V2}) ->
  300. digraph:add_edge(G, V1, V2)
  301. end, Es)
  302. catch
  303. error:badarg ->
  304. ?ERROR(
  305. "Failed (binary_to_term) to restore rebar info file."
  306. " Discard file.", []),
  307. ok
  308. end;
  309. _Err ->
  310. ok
  311. end,
  312. G.
  313. store_erlcinfo(_G, _Config, _Modified = false) ->
  314. ok;
  315. store_erlcinfo(G, Config, _Modified) ->
  316. Vs = lists:map(
  317. fun(V) ->
  318. digraph:vertex(G, V)
  319. end, digraph:vertices(G)),
  320. Es = lists:flatmap(
  321. fun({V, _}) ->
  322. lists:map(
  323. fun(E) ->
  324. {_, V1, V2, _} = digraph:edge(G, E),
  325. {V1, V2}
  326. end, digraph:out_edges(G, V))
  327. end, Vs),
  328. File = erlcinfo_file(Config),
  329. ok = filelib:ensure_dir(File),
  330. Data = term_to_binary(#erlcinfo{info={Vs, Es}}, [{compressed, 9}]),
  331. file:write_file(File, Data).
  332. %% NOTE: If, for example, one of the entries in Files, refers to
  333. %% gen_server.erl, that entry will be dropped. It is dropped because
  334. %% such an entry usually refers to the beam file, and we don't pass a
  335. %% list of OTP src dirs for finding gen_server.erl's full path. Also,
  336. %% if gen_server.erl was modified, it's not rebar's task to compile a
  337. %% new version of the beam file. Therefore, it's reasonable to drop
  338. %% such entries. Also see process_attr(behaviour, Form, Includes).
  339. -spec expand_file_names([file:filename()],
  340. [file:filename()]) -> [file:filename()].
  341. expand_file_names(Files, Dirs) ->
  342. %% We check if Files exist by itself or within the directories
  343. %% listed in Dirs.
  344. %% Return the list of files matched.
  345. lists:flatmap(
  346. fun(Incl) ->
  347. case filelib:is_regular(Incl) of
  348. true ->
  349. [Incl];
  350. false ->
  351. lists:flatmap(
  352. fun(Dir) ->
  353. FullPath = filename:join(Dir, Incl),
  354. case filelib:is_regular(FullPath) of
  355. true ->
  356. [FullPath];
  357. false ->
  358. []
  359. end
  360. end, Dirs)
  361. end
  362. end, Files).
  363. -spec get_parents(rebar_digraph(), file:filename()) -> [file:filename()].
  364. get_parents(G, Source) ->
  365. %% Return all files which the Source depends upon.
  366. digraph_utils:reachable_neighbours([Source], G).
  367. -spec get_children(rebar_digraph(), file:filename()) -> [file:filename()].
  368. get_children(G, Source) ->
  369. %% Return all files dependent on the Source.
  370. digraph_utils:reaching_neighbours([Source], G).
  371. -spec internal_erl_compile(rebar_state:t(), file:filename(), file:filename(),
  372. file:filename(), list(),
  373. rebar_digraph()) -> 'ok' | 'skipped'.
  374. internal_erl_compile(Config, Dir, Source, OutDir, ErlOpts, G) ->
  375. %% Determine the target name and includes list by inspecting the source file
  376. Module = filename:basename(Source, ".erl"),
  377. Parents = get_parents(G, Source),
  378. log_files(?FMT("Dependencies of ~s", [Source]), Parents),
  379. %% Construct the target filename
  380. Target = filename:join([OutDir | string:tokens(Module, ".")]) ++ ".beam",
  381. ok = filelib:ensure_dir(Target),
  382. %% If the file needs compilation, based on last mod date of includes or
  383. %% the target
  384. case needs_compile(Source, Target, Parents) of
  385. true ->
  386. Opts = [{outdir, filename:dirname(Target)}] ++
  387. ErlOpts ++ [{i, filename:join(Dir, "include")}, return],
  388. case compile:file(Source, Opts) of
  389. {ok, _Mod} ->
  390. ok;
  391. {ok, _Mod, Ws} ->
  392. rebar_base_compiler:ok_tuple(Config, Source, Ws);
  393. {error, Es, Ws} ->
  394. rebar_base_compiler:error_tuple(Config, Source,
  395. Es, Ws, Opts)
  396. end;
  397. false ->
  398. skipped
  399. end.
  400. -spec compile_mib(file:filename(), file:filename(),
  401. rebar_state:t()) -> 'ok'.
  402. compile_mib(Source, Target, Config) ->
  403. ok = rebar_dir:ensure_dir(Target),
  404. ok = rebar_dir:ensure_dir(filename:join("include", "dummy.hrl")),
  405. Opts = [{outdir, "priv/mibs"}, {i, ["priv/mibs"]}] ++
  406. rebar_state:get(Config, mib_opts, []),
  407. case snmpc:compile(Source, Opts) of
  408. {ok, _} ->
  409. Mib = filename:rootname(Target),
  410. MibToHrlOpts =
  411. case proplists:get_value(verbosity, Opts, undefined) of
  412. undefined ->
  413. #options{specific = []};
  414. Verbosity ->
  415. #options{specific = [{verbosity, Verbosity}]}
  416. end,
  417. ok = snmpc:mib_to_hrl(Mib, Mib, MibToHrlOpts),
  418. Hrl_filename = Mib ++ ".hrl",
  419. rebar_file_utils:mv(Hrl_filename, "include"),
  420. ok;
  421. {error, compilation_failed} ->
  422. ?FAIL
  423. end.
  424. -spec compile_xrl(file:filename(), file:filename(),
  425. rebar_state:t()) -> 'ok'.
  426. compile_xrl(Source, Target, Config) ->
  427. Opts = [{scannerfile, Target} | rebar_state:get(Config, xrl_opts, [])],
  428. compile_xrl_yrl(Config, Source, Target, Opts, leex).
  429. -spec compile_yrl(file:filename(), file:filename(),
  430. rebar_state:t()) -> 'ok'.
  431. compile_yrl(Source, Target, Config) ->
  432. Opts = [{parserfile, Target} | rebar_state:get(Config, yrl_opts, [])],
  433. compile_xrl_yrl(Config, Source, Target, Opts, yecc).
  434. -spec compile_xrl_yrl(rebar_state:t(), file:filename(),
  435. file:filename(), list(), module()) -> 'ok'.
  436. compile_xrl_yrl(Config, Source, Target, Opts, Mod) ->
  437. Dir = rebar_state:dir(Config),
  438. Opts1 = [{includefile, filename:join(Dir, I)} || {includefile, I} <- Opts,
  439. filename:pathtype(I) =:= relative],
  440. case needs_compile(Source, Target, []) of
  441. true ->
  442. case Mod:file(Source, Opts1 ++ [{return, true}]) of
  443. {ok, _} ->
  444. ok;
  445. {ok, _Mod, Ws} ->
  446. rebar_base_compiler:ok_tuple(Config, Source, Ws);
  447. {error, Es, Ws} ->
  448. rebar_base_compiler:error_tuple(Config, Source,
  449. Es, Ws, Opts1)
  450. end;
  451. false ->
  452. skipped
  453. end.
  454. gather_src([], Srcs) ->
  455. Srcs;
  456. gather_src([Dir|Rest], Srcs) ->
  457. gather_src(
  458. Rest, Srcs ++ rebar_utils:find_files(Dir, ?RE_PREFIX".*\\.erl\$")).
  459. -spec dirs(file:filename()) -> [file:filename()].
  460. dirs(Dir) ->
  461. [F || F <- filelib:wildcard(filename:join([Dir, "*"])), filelib:is_dir(F)].
  462. -spec delete_dir(file:filename(), [string()]) -> 'ok' | {'error', atom()}.
  463. delete_dir(Dir, []) ->
  464. file:del_dir(Dir);
  465. delete_dir(Dir, Subdirs) ->
  466. lists:foreach(fun(D) -> delete_dir(D, dirs(D)) end, Subdirs),
  467. file:del_dir(Dir).
  468. parse_attrs(Fd, Includes) ->
  469. case io:parse_erl_form(Fd, "") of
  470. {ok, Form, _Line} ->
  471. case erl_syntax:type(Form) of
  472. attribute ->
  473. NewIncludes = process_attr(Form, Includes),
  474. parse_attrs(Fd, NewIncludes);
  475. _ ->
  476. parse_attrs(Fd, Includes)
  477. end;
  478. {eof, _} ->
  479. Includes;
  480. _Err ->
  481. parse_attrs(Fd, Includes)
  482. end.
  483. process_attr(Form, Includes) ->
  484. try
  485. AttrName = erl_syntax:atom_value(erl_syntax:attribute_name(Form)),
  486. process_attr(AttrName, Form, Includes)
  487. catch _:_ ->
  488. %% TODO: We should probably try to be more specific here
  489. %% and not suppress all errors.
  490. Includes
  491. end.
  492. process_attr(import, Form, Includes) ->
  493. case erl_syntax_lib:analyze_import_attribute(Form) of
  494. {Mod, _Funs} ->
  495. [atom_to_list(Mod) ++ ".erl"|Includes];
  496. Mod ->
  497. [atom_to_list(Mod) ++ ".erl"|Includes]
  498. end;
  499. process_attr(file, Form, Includes) ->
  500. {File, _} = erl_syntax_lib:analyze_file_attribute(Form),
  501. [File|Includes];
  502. process_attr(include, Form, Includes) ->
  503. [FileNode] = erl_syntax:attribute_arguments(Form),
  504. File = erl_syntax:string_value(FileNode),
  505. [File|Includes];
  506. process_attr(include_lib, Form, Includes) ->
  507. [FileNode] = erl_syntax:attribute_arguments(Form),
  508. RawFile = erl_syntax:string_value(FileNode),
  509. File = maybe_expand_include_lib_path(RawFile),
  510. [File|Includes];
  511. process_attr(behaviour, Form, Includes) ->
  512. [FileNode] = erl_syntax:attribute_arguments(Form),
  513. File = erl_syntax:atom_name(FileNode) ++ ".erl",
  514. [File|Includes];
  515. process_attr(compile, Form, Includes) ->
  516. [Arg] = erl_syntax:attribute_arguments(Form),
  517. case erl_syntax:concrete(Arg) of
  518. {parse_transform, Mod} ->
  519. [atom_to_list(Mod) ++ ".erl"|Includes];
  520. {core_transform, Mod} ->
  521. [atom_to_list(Mod) ++ ".erl"|Includes];
  522. L when is_list(L) ->
  523. lists:foldl(
  524. fun({parse_transform, M}, Acc) ->
  525. [atom_to_list(M) ++ ".erl"|Acc];
  526. ({core_transform, M}, Acc) ->
  527. [atom_to_list(M) ++ ".erl"|Acc];
  528. (_, Acc) ->
  529. Acc
  530. end, Includes, L)
  531. end.
  532. %% Given the filename from an include_lib attribute, if the path
  533. %% exists, return unmodified, or else get the absolute ERL_LIBS
  534. %% path.
  535. maybe_expand_include_lib_path(File) ->
  536. case filelib:is_regular(File) of
  537. true ->
  538. File;
  539. false ->
  540. expand_include_lib_path(File)
  541. end.
  542. %% Given a path like "stdlib/include/erl_compile.hrl", return
  543. %% "OTP_INSTALL_DIR/lib/erlang/lib/stdlib-x.y.z/include/erl_compile.hrl".
  544. %% Usually a simple [Lib, SubDir, File1] = filename:split(File) should
  545. %% work, but to not crash when an unusual include_lib path is used,
  546. %% utilize more elaborate logic.
  547. expand_include_lib_path(File) ->
  548. File1 = filename:basename(File),
  549. Split = filename:split(filename:dirname(File)),
  550. Lib = hd(Split),
  551. SubDir = filename:join(tl(Split)),
  552. Dir = code:lib_dir(list_to_atom(Lib), list_to_atom(SubDir)),
  553. filename:join(Dir, File1).
  554. %%
  555. %% Ensure all files in a list are present and abort if one is missing
  556. %%
  557. -spec check_files([file:filename()]) -> [file:filename()].
  558. check_files(FileList) ->
  559. [check_file(F) || F <- FileList].
  560. check_file(File) ->
  561. case filelib:is_regular(File) of
  562. false -> ?ABORT("File ~p is missing, aborting\n", [File]);
  563. true -> File
  564. end.
  565. %% Print prefix followed by list of files. If the list is empty, print
  566. %% on the same line, otherwise use a separate line.
  567. log_files(Prefix, Files) ->
  568. case Files of
  569. [] ->
  570. ?DEBUG("~s: ~p", [Prefix, Files]);
  571. _ ->
  572. ?DEBUG("~s:~n~p", [Prefix, Files])
  573. end.