66
set_primary_archive/2,
66
set_primary_archive/3,
69
-export_type([load_error_rsn/0, load_ret/0]).
69
71
-include_lib("kernel/include/file.hrl").
73
%% objfile_extension() -> ".beam"
74
%% set_path(Dir*) -> true
76
%% add_path(Dir) -> true | {error, What}
77
%% add_patha(Dir) -> true | {error, What}
78
%% add_pathz(Dir) -> true | {error, What}
79
%% add_paths(DirList) -> true | {error, What}
80
%% add_pathsa(DirList) -> true | {error, What}
81
%% add_pathsz(DirList) -> true | {error, What}
82
%% del_path(Dir) -> true | {error, What}
83
%% replace_path(Name,Dir) -> true | {error, What}
84
%% load_file(File) -> {error,What} | {module, Mod}
85
%% load_abs(File) -> {error,What} | {module, Mod}
86
%% load_abs(File,Mod) -> {error,What} | {module, Mod}
87
%% load_binary(Mod,File,Bin) -> {error,What} | {module,Mod}
88
%% ensure_loaded(Module) -> {error,What} | {module, Mod}
90
%% purge(Module) kills all procs running old code
91
%% soft_purge(Module) -> true | false
92
%% is_loaded(Module) -> {file, File} | false
93
%% all_loaded() -> {Module, File}*
94
%% get_object_code(Mod) -> error | {Mod, Bin, Filename}
100
%% stick_dir(Dir) -> ok | error
101
%% unstick_dir(Dir) -> ok | error
102
%% is_sticky(Module) -> true | false
103
%% which(Module) -> Filename
104
%% set_primary_archive((FileName, Bin) -> ok | {error, Reason}
105
%% clash() -> -> print out
75
%% objfile_extension() -> ".beam"
76
%% get_path() -> [Dir]
77
%% set_path([Dir]) -> true | {error, bad_directory | bad_path}
78
%% add_path(Dir) -> true | {error, bad_directory}
79
%% add_patha(Dir) -> true | {error, bad_directory}
80
%% add_pathz(Dir) -> true | {error, bad_directory}
81
%% add_paths([Dir]) -> ok
82
%% add_pathsa([Dir]) -> ok
83
%% add_pathsz([Dir]) -> ok
84
%% del_path(Dir) -> boolean() | {error, bad_name}
85
%% replace_path(Name, Dir) -> true | replace_path_error()
86
%% load_file(Module) -> {module, Module} | {error, What :: atom()}
87
%% load_abs(File) -> {module, Module} | {error, What :: atom()}
88
%% load_abs(File, Module) -> {module, Module} | {error, What :: atom()}
89
%% load_binary(Module, File, Bin)-> {module, Module} | {error, What :: atom()}
90
%% ensure_loaded(Module) -> {module, Module} | {error, What :: atom()}
91
%% delete(Module) -> boolean()
92
%% purge(Module) -> boolean() kills all procs running old code
93
%% soft_purge(Module) -> boolean()
94
%% is_loaded(Module) -> {file, loaded_filename()} | false
95
%% all_loaded() -> [{Module, loaded_filename()}]
96
%% get_object_code(Module) -> {Module, Bin, Filename} | error
97
%% stop() -> no_return()
99
%% compiler_dir() -> Dir
101
%% lib_dir(Application) -> Dir | {error, bad_name}
102
%% priv_dir(Application) -> Dir | {error, bad_name}
103
%% stick_dir(Dir) -> ok | error
104
%% unstick_dir(Dir) -> ok | error
105
%% stick_mod(Module) -> true
106
%% unstick_mod(Module) -> true
107
%% is_sticky(Module) -> boolean()
108
%% which(Module) -> Filename | loaded_ret_atoms() | non_existing
109
%% set_primary_archive((FileName, Bin, FileInfo) -> ok | {error, Reason}
110
%% clash() -> ok prints out number of clashes
107
112
%%----------------------------------------------------------------------------
108
113
%% Some types for basic exported functions of this module
137
142
%% XXX Filename is also an atom(), e.g. 'cover_compiled'
138
143
-spec load_abs(Filename :: loaded_filename(), Module :: atom()) -> load_ret().
139
load_abs(File,M) when (is_list(File) orelse is_atom(File)), is_atom(M) ->
144
load_abs(File, M) when (is_list(File) orelse is_atom(File)), is_atom(M) ->
140
145
call({load_abs,File,M}).
142
147
%% XXX Filename is also an atom(), e.g. 'cover_compiled'
143
148
-spec load_binary(Module :: atom(), Filename :: loaded_filename(), Binary :: binary()) -> load_ret().
144
load_binary(Mod,File,Bin)
149
load_binary(Mod, File, Bin)
145
150
when is_atom(Mod), (is_list(File) orelse is_atom(File)), is_binary(Bin) ->
146
151
call({load_binary,Mod,File,Bin}).
148
153
-spec load_native_partial(Module :: atom(), Binary :: binary()) -> load_ret().
149
load_native_partial(Mod,Bin) when is_atom(Mod), is_binary(Bin) ->
154
load_native_partial(Mod, Bin) when is_atom(Mod), is_binary(Bin) ->
150
155
call({load_native_partial,Mod,Bin}).
152
157
-spec load_native_sticky(Module :: atom(), Binary :: binary(), WholeModule :: 'false' | binary()) -> load_ret().
153
load_native_sticky(Mod,Bin,WholeModule)
158
load_native_sticky(Mod, Bin, WholeModule)
154
159
when is_atom(Mod), is_binary(Bin),
155
160
(is_binary(WholeModule) orelse WholeModule =:= false) ->
156
161
call({load_native_sticky,Mod,Bin,WholeModule}).
211
216
-spec is_sticky(Module :: atom()) -> boolean().
212
217
is_sticky(Mod) when is_atom(Mod) -> call({is_sticky,Mod}).
214
-spec set_path(Directories :: [file:filename()]) -> 'true' | {'error', term()}.
219
-spec set_path(Directories :: [file:filename()]) ->
220
'true' | {'error', 'bad_directory' | 'bad_path'}.
215
221
set_path(PathList) when is_list(PathList) -> call({set_path,PathList}).
217
223
-spec get_path() -> [file:filename()].
218
224
get_path() -> call(get_path).
220
-spec add_path(Directory :: file:filename()) -> 'true' | {'error', term()}.
226
-type add_path_ret() :: 'true' | {'error', 'bad_directory'}.
227
-spec add_path(Directory :: file:filename()) -> add_path_ret().
221
228
add_path(Dir) when is_list(Dir) -> call({add_path,last,Dir}).
223
-spec add_pathz(Directory :: file:filename()) -> 'true' | {'error', term()}.
230
-spec add_pathz(Directory :: file:filename()) -> add_path_ret().
224
231
add_pathz(Dir) when is_list(Dir) -> call({add_path,last,Dir}).
226
-spec add_patha(Directory :: file:filename()) -> 'true' | {'error', term()}.
233
-spec add_patha(Directory :: file:filename()) -> add_path_ret().
227
234
add_patha(Dir) when is_list(Dir) -> call({add_path,first,Dir}).
229
236
-spec add_paths(Directories :: [file:filename()]) -> 'ok'.
235
242
-spec add_pathsa(Directories :: [file:filename()]) -> 'ok'.
236
243
add_pathsa(Dirs) when is_list(Dirs) -> call({add_paths,first,Dirs}).
238
%% XXX Contract's input argument differs from add_path/1 -- why?
239
245
-spec del_path(Name :: file:filename() | atom()) -> boolean() | {'error', 'bad_name'}.
240
246
del_path(Name) when is_list(Name) ; is_atom(Name) -> call({del_path,Name}).
242
248
-type replace_path_error() :: {'error', 'bad_directory' | 'bad_name' | {'badarg',_}}.
243
249
-spec replace_path(Name:: atom(), Dir :: file:filename()) -> 'true' | replace_path_error().
244
replace_path(Name, Dir) when (is_atom(Name) or is_list(Name)) and
245
(is_atom(Dir) or is_list(Dir)) ->
250
replace_path(Name, Dir) when (is_atom(Name) orelse is_list(Name)),
251
(is_atom(Dir) orelse is_list(Dir)) ->
246
252
call({replace_path,Name,Dir}).
248
254
-spec rehash() -> 'ok'.
274
280
do_start(Flags) ->
275
281
%% The following module_info/1 calls are here to ensure
276
%% that the modules are loaded prior to their use elsewhere in
282
%% that these modules are loaded prior to their use elsewhere in
277
283
%% the code_server.
278
284
%% Otherwise a deadlock may occur when the code_server is starting.
279
code_server:module_info(module),
280
packages:module_info(module),
285
code_server = code_server:module_info(module),
286
packages = packages:module_info(module),
281
287
catch hipe_unified_loader:load_hipe_modules(),
282
gb_sets:module_info(module),
283
gb_trees:module_info(module),
285
ets:module_info(module),
286
os:module_info(module),
287
filename:module_info(module),
288
lists:module_info(module),
288
Modules2 = [gb_sets, gb_trees, ets, os, binary, unicode, filename, lists],
289
lists:foreach(fun (M) -> M = M:module_info(module) end, Modules2),
290
291
Mode = get_mode(Flags),
291
292
case init:get_argument(root) of
420
423
which(File, ".", Path)
423
-spec set_primary_archive(ArchiveFile :: file:filename(), ArchiveBin :: binary()) -> 'ok' | {'error', atom()}.
426
-spec set_primary_archive(ArchiveFile :: file:filename(),
427
ArchiveBin :: binary(),
428
FileInfo :: file:file_info())
429
-> 'ok' | {'error', atom()}.
425
set_primary_archive(ArchiveFile0, ArchiveBin) when is_list(ArchiveFile0), is_binary(ArchiveBin) ->
431
set_primary_archive(ArchiveFile0, ArchiveBin, #file_info{} = FileInfo)
432
when is_list(ArchiveFile0), is_binary(ArchiveBin) ->
426
433
ArchiveFile = filename:absname(ArchiveFile0),
427
case call({set_primary_archive, ArchiveFile, ArchiveBin}) of
434
case call({set_primary_archive, ArchiveFile, ArchiveBin, FileInfo}) of
430
437
{ok, _Mode, Ebins} ->
463
470
build([Dir|Tail]) ->
464
Files = filter(objfile_extension(), Dir, file:list_dir(Dir)),
471
Files = filter(objfile_extension(), Dir,
472
erl_prim_loader:list_dir(Dir)),
465
473
[decorate(Files, Dir) | build(Tail)].
467
475
decorate([], _) -> [];
468
476
decorate([File|Tail], Dir) ->
469
477
[{Dir, File} | decorate(Tail, Dir)].
471
filter(_Ext, Dir, {error,_}) ->
479
filter(_Ext, Dir, error) ->
472
480
io:format("** Bad path can't read ~s~n", [Dir]), [];
473
481
filter(Ext, _, {ok,Files}) ->
474
482
filter2(Ext, length(Ext), Files).
476
484
filter2(_Ext, _Extlen, []) -> [];
477
filter2(Ext, Extlen,[File|Tail]) ->
478
case has_ext(Ext,Extlen, File) of
485
filter2(Ext, Extlen, [File|Tail]) ->
486
case has_ext(Ext, Extlen, File) of
479
487
true -> [File | filter2(Ext, Extlen, Tail)];
480
488
false -> filter2(Ext, Extlen, Tail)
483
has_ext(Ext, Extlen,File) ->
491
has_ext(Ext, Extlen, File) ->
484
492
L = length(File),
485
493
case catch lists:nthtail(L - Extlen, File) of
491
499
filename:join(packages:split(X)).
501
-spec load_native_code_for_all_loaded() -> ok.
502
load_native_code_for_all_loaded() ->
503
Architecture = erlang:system_info(hipe_architecture),
504
ChunkName = hipe_unified_loader:chunk_name(Architecture),
505
lists:foreach(fun({Module, BeamFilename}) ->
506
case code:is_module_native(Module) of
508
case beam_lib:chunks(BeamFilename, [ChunkName]) of
509
{ok,{_,[{_,Bin}]}} when is_binary(Bin) ->
510
load_native_partial(Module, Bin);
511
{error, beam_lib, _} -> ok