compile(3erl) | Erlang Module Definition | compile(3erl) |
compile - Erlang Compiler
This module provides an interface to the standard Erlang compiler. It can generate either a new file, which contains the object code, or return a binary, which can be loaded directly.
option() = term()
See file/2 for detailed description
forms() = term()
List of Erlang abstract or Core Erlang format representations, as used by forms/2
env_compiler_options()
Return compiler options given via the environment variable ERL_COMPILER_OPTIONS. If the value is a list, it is returned as is. If it is not a list, it is put into a list.
file(File)
Is the same as file(File, [verbose,report_errors,report_warnings]).
file(File, Options) -> CompRet
Types:
Compiles the code in the file File, which is an Erlang source code file without the .erl extension. Options determine the behavior of the compiler.
Returns {ok,ModuleName} if successful, or error if there are errors. An object code file is created if the compilation succeeds without errors. It is considered to be an error if the module name in the source code is not the same as the basename of the output file.
Available options:
Use option strong_validation to generate all warnings that the compiler would generate.
It is advised for compilers to remove all non-deterministic information if the deterministic option is supported and it was supplied by the user.
Includes debug information in the form of Erlang Abstract Format in the debug_info chunk of the compiled beam module. Tools such as Debugger, Xref, and Cover require the debug information to be included.
Warning: Source code can be reconstructed from the debug information. Use encrypted debug information (encrypt_debug_info) to prevent this.
For details, see beam_lib(3erl).
Includes custom debug information in the form of a Backend module with custom Data in the compiled beam module. The given module must implement a debug_info/4 function and is responsible for generating different code representations, as described in the debug_info under beam_lib(3erl).
Warning: Source code can be reconstructed from the debug information. Use encrypted debug information (encrypt_debug_info) to prevent this.
Includes debug information, but encrypts it so that it cannot be accessed without supplying the key. (To give option debug_info as well is allowed, but not necessary.) Using this option is a good way to always have the debug information available during testing, yet protecting the source code.
Mode is the type of crypto algorithm to be used for encrypting the debug information. The default (and currently the only) type is des3_cbc.
For details, see beam_lib(3erl).
Similar to the debug_info_key option, but the key is read from an .erlang.crypt file.
For details, see beam_lib(3erl).
A feature can also be enabled (disabled) using the -feature(Feature, enable | disable). module directive. Note that this directive can only be present in a prefix of the file, before exports and function definitions. This is the preferred method of enabling and disabling features, since it is a local property of a module.
By default, this rule is written to <File>.Pbeam. However, if option binary is set, nothing is written and the rule is returned in Binary.
The output will be encoded in UTF-8.
For example, if you have the following module:
-module(module). -include_lib("eunit/include/eunit.hrl"). -include("header.hrl").
The Makefile rule generated by this option looks as follows:
module.beam: module.erl \ /usr/local/lib/erlang/lib/eunit/include/eunit.hrl \ header.hrl
If the value of this flag is column, debug information includes column information.
See also the no_lint option.
By default, the generated code for operation Record#record_tag.field verifies that the tuple Record has the correct size for the record, and that the first element is the tag record_tag. Use this option to omit the verification code.
If this option is written in the source code, as a -compile directive, the syntax F/A can be used instead of {F,A}, for example:
-compile({no_auto_import,[error/1]}).
Caveat: When this option is used, there are no guarantees that the code output by the compiler is correct and safe to use. The responsibility for correctness lies on the code or person generating the abstract format. If the code contains errors, the compiler may crash or produce unsafe code.
If warnings are turned on (option report_warnings described earlier), the following options control what type of warnings that are generated. Except from {warn_format,Verbosity}, the following options have two forms:
In the descriptions that follow, the form that is used to change the default value are listed.
Verbosity selects the number of warnings:
The default verbosity is 1. Verbosity 0 can also be selected by option nowarn_format.
The use of this option has always been discouraged. As from R14A, it is an error to use it.
To resolve BIF clashes, use explicit module names or the {no_auto_import,[F/A]} compiler directive.
To resolve BIF clashes, use explicit module names or the {no_auto_import,[F/A]} compiler directive.
Other kinds of warnings are opportunistic warnings. They are generated when the compiler happens to notice potential issues during optimization and code generation.
The absence of warnings does not mean that there are no remaining errors in the code.
Opportunistic warnings can be disabled using the following options:
For debugging of the compiler, or for pure curiosity, the intermediate code generated by each compiler pass can be inspected. To print a complete list of the options to produce list files, type compile:options() at the Erlang shell prompt. The options are printed in the order that the passes are executed. If more than one listing option is used, the one representing the earliest pass takes effect.
Unrecognized options are ignored.
Both WarningList and ErrorList have the following format:
[{FileName,[ErrorInfo]}].
ErrorInfo is described later in this section. The filename is included here, as the compiler uses the Erlang pre-processor epp, which allows the code to be included in other files. It is therefore important to know to which file the location of an error or a warning refers.
forms(Forms)
Is the same as forms(Forms, [verbose,report_errors,report_warnings]).
forms(Forms, Options) -> CompRet
Types:
Analogous to file/1, but takes a list of forms (in either Erlang abstract or Core Erlang format representation) as first argument. Option binary is implicit, that is, no object code file is produced. For options that normally produce a listing file, such as 'E', the internal format for that compiler pass (an Erlang term, usually not a binary) is returned instead of a binary.
format_error(ErrorDescriptor) -> chars()
Types:
Uses an ErrorDescriptor and returns a deep list of characters that describes the error. This function is usually called implicitly when an ErrorInfo structure (described in section Error Information) is processed.
output_generated(Options) -> true | false
Types:
Determines whether the compiler generates a beam file with the given options. true means that a beam file is generated. false means that the compiler generates some listing file, returns a binary, or merely checks the syntax of the source code.
noenv_file(File, Options) -> CompRet
Works like file/2, except that the environment variable ERL_COMPILER_OPTIONS is not consulted.
noenv_forms(Forms, Options) -> CompRet
Works like forms/2, except that the environment variable ERL_COMPILER_OPTIONS is not consulted.
noenv_output_generated(Options) -> true | false
Types:
Works like output_generated/1, except that the environment variable ERL_COMPILER_OPTIONS is not consulted.
The (host operating system) environment variable ERL_COMPILER_OPTIONS can be used to give default compiler options. Its value must be a valid Erlang term. If the value is a list, it is used as is. If it is not a list, it is put into a list.
The list is appended to any options given to file/2, forms/2, and output_generated/2. Use the alternative functions noenv_file/2, noenv_forms/2, or noenv_output_generated/2 if you do not want the environment variable to be consulted, for example, if you are calling the compiler recursively from inside a parse transform.
The list can be retrieved with env_compiler_options/0.
The compiler can do function inlining within an Erlang module. Inlining means that a call to a function is replaced with the function body with the arguments replaced with the actual values. The semantics are preserved, except if exceptions are generated in the inlined code. Exceptions are reported as occurring in the function the body was inlined into. Also, function_clause exceptions are converted to similar case_clause exceptions.
When a function is inlined, the original function is kept if it is exported (either by an explicit export or if the option export_all was given) or if not all calls to the function are inlined.
Inlining does not necessarily improve running time. For example, inlining can increase Beam stack use, which probably is detrimental to performance for recursive functions.
Inlining is never default. It must be explicitly enabled with a compiler option or a -compile() attribute in the source module.
To enable inlining, either use the option inline to let the compiler decide which functions to inline, or {inline,[{Name,Arity},...]} to have the compiler inline all calls to the given functions. If the option is given inside a compile directive in an Erlang module, {Name,Arity} can be written as Name/Arity.
Example of explicit inlining:
-compile({inline,[pi/0]}). pi() -> 3.1416.
Example of implicit inlining:
-compile(inline).
The option {inline_size,Size} controls how large functions that are allowed to be inlined. Default is 24, which keeps the size of the inlined code roughly the same as the un-inlined version (only relatively small functions are inlined).
Example:
%% Aggressive inlining - will increase code size. -compile(inline). -compile({inline_size,100}).
The compiler can also inline various list manipulation functions from the module list in STDLIB.
This feature must be explicitly enabled with a compiler option or a -compile() attribute in the source module.
To enable inlining of list functions, use option inline_list_funcs.
The following functions are inlined:
Parse transformations are used when a programmer wants to use Erlang syntax but with different semantics. The original Erlang code is then transformed into other Erlang code.
See erl_id_trans(3erl) for an example and an explanation of the function parse_transform_info/0.
The ErrorInfo mentioned earlier is the standard ErrorInfo structure, which is returned from all I/O modules. It has the following format:
{ErrorLocation, Module, ErrorDescriptor}
ErrorLocation is the atom none if the error does not correspond to a specific location, for example, if the source file does not exist.
A string describing the error is obtained with the following call:
Module:format_error(ErrorDescriptor)
epp(3erl), erl_id_trans(3erl), erl_lint(3erl), beam_lib(3erl)
compiler 8.2.6.3 | Ericsson AB |