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.

86 lines
3.7 KiB

7 years ago
  1. -module(rebar_env).
  2. -export([create_env/1,
  3. create_env/2]).
  4. -include("rebar.hrl").
  5. %% @doc The following environment variables are exported when running
  6. %% a hook (absolute paths):
  7. %%
  8. %% REBAR_DEPS_DIR = rebar_dir:deps_dir/1
  9. %% REBAR_BUILD_DIR = rebar_dir:base_dir/1
  10. %% REBAR_ROOT_DIR = rebar_dir:root_dir/1
  11. %% REBAR_CHECKOUTS_DIR = rebar_dir:checkouts_dir/1
  12. %% REBAR_PLUGINS_DIR = rebar_dir:plugins_dir/1
  13. %% REBAR_GLOBAL_CONFIG_DIR = rebar_dir:global_config_dir/1
  14. %% REBAR_GLOBAL_CACHE_DIR = rebar_dir:global_cache_dir/1
  15. %% REBAR_TEMPLATE_DIR = rebar_dir:template_dir/1
  16. %% REBAR_APP_DIRS = rebar_dir:lib_dirs/1
  17. %% REBAR_SRC_DIRS = rebar_dir:src_dirs/1
  18. %%
  19. %% autoconf compatible variables
  20. %% (see: http://www.gnu.org/software/autoconf/manual/autoconf.html#Erlang-Libraries):
  21. %% ERLANG_ERTS_VER = erlang:system_info(version)
  22. %% ERLANG_ROOT_DIR = code:root_dir/0
  23. %% ERLANG_LIB_DIR_erl_interface = code:lib_dir(erl_interface)
  24. %% ERLANG_LIB_VER_erl_interface = version part of path returned by code:lib_dir(erl_interface)
  25. %% ERL = ERLANG_ROOT_DIR/bin/erl
  26. %% ERLC = ERLANG_ROOT_DIR/bin/erl
  27. %%
  28. -spec create_env(rebar_state:t()) -> proplists:proplist().
  29. create_env(State) ->
  30. Opts = rebar_state:opts(State),
  31. create_env(State, Opts).
  32. -spec create_env(rebar_state:t(), rebar_dict()) -> proplists:proplist().
  33. create_env(State, Opts) ->
  34. BaseDir = rebar_dir:base_dir(State),
  35. EnvVars = [
  36. {"REBAR_DEPS_DIR", filename:absname(rebar_dir:deps_dir(State))},
  37. {"REBAR_BUILD_DIR", filename:absname(rebar_dir:base_dir(State))},
  38. {"REBAR_ROOT_DIR", filename:absname(rebar_dir:root_dir(State))},
  39. {"REBAR_CHECKOUTS_DIR", filename:absname(rebar_dir:checkouts_dir(State))},
  40. {"REBAR_PLUGINS_DIR", filename:absname(rebar_dir:plugins_dir(State))},
  41. {"REBAR_GLOBAL_CONFIG_DIR", filename:absname(rebar_dir:global_config_dir(State))},
  42. {"REBAR_GLOBAL_CACHE_DIR", filename:absname(rebar_dir:global_cache_dir(Opts))},
  43. {"REBAR_TEMPLATE_DIR", filename:absname(rebar_dir:template_dir(State))},
  44. {"REBAR_APP_DIRS", join_dirs(BaseDir, rebar_dir:lib_dirs(State))},
  45. {"REBAR_SRC_DIRS", join_dirs(BaseDir, rebar_dir:all_src_dirs(Opts))},
  46. {"ERLANG_ERTS_VER", erlang:system_info(version)},
  47. {"ERLANG_ROOT_DIR", code:root_dir()},
  48. {"ERL", filename:join([code:root_dir(), "bin", "erl"])},
  49. {"ERLC", filename:join([code:root_dir(), "bin", "erlc"])},
  50. {"ERLANG_ARCH" , rebar_api:wordsize()},
  51. {"ERLANG_TARGET", rebar_api:get_arch()}
  52. ],
  53. EInterfaceVars = create_erl_interface_env(),
  54. lists:append([EnvVars, EInterfaceVars]).
  55. -spec create_erl_interface_env() -> list().
  56. create_erl_interface_env() ->
  57. case code:lib_dir(erl_interface) of
  58. {error, bad_name} ->
  59. ?WARN("erl_interface is missing. ERLANG_LIB_DIR_erl_interface and "
  60. "ERLANG_LIB_VER_erl_interface will not be added to the environment.", []),
  61. [];
  62. Dir ->
  63. [
  64. {"ERLANG_LIB_DIR_erl_interface", Dir},
  65. {"ERLANG_LIB_VER_erl_interface", re_version(Dir)}
  66. ]
  67. end.
  68. %% ====================================================================
  69. %% Internal functions
  70. %% ====================================================================
  71. join_dirs(BaseDir, Dirs) ->
  72. rebar_string:join([filename:join(BaseDir, Dir) || Dir <- Dirs], ":").
  73. re_version(Path) ->
  74. case re:run(Path, "^.*-(?<VER>[^/-]*)$", [{capture,[1],list}, unicode]) of
  75. nomatch -> "";
  76. {match, [Ver]} -> Ver
  77. end.