app.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. == Building
  2. Erlang.mk can do a lot of things, but it is, first and
  3. foremost, a build tool. In this chapter we will cover
  4. the basics of building a project with Erlang.mk.
  5. For most of this chapter, we will assume that you are
  6. using a project link:getting_started.asciidoc[generated by Erlang.mk].
  7. === How to build
  8. To build a project, all you have to do is type `make`:
  9. [source,bash]
  10. $ make
  11. It will work regardless of your project: OTP applications,
  12. library applications, NIFs, port drivers or even releases.
  13. Erlang.mk also automatically downloads and compiles the
  14. dependencies for your project.
  15. All this is possible thanks to a combination of configuration
  16. and conventions. Most of the conventions come from Erlang/OTP
  17. itself so any seasoned Erlang developers should feel right at
  18. home.
  19. === What to build
  20. Erlang.mk gives you control over three steps of the build
  21. process, allowing you to do a partial build if needed.
  22. A build has three phases: first any dependency is fetched
  23. and built, then the project itself is built and finally a
  24. release may be generated when applicable. A release is only
  25. generated for projects specifically configured to do so.
  26. Erlang.mk handles those three phases automatically when you
  27. type `make`. But sometimes you just want to repeat one or
  28. two of them.
  29. The commands detailed in this section are most useful after
  30. you have a successful build as they allow you to quickly
  31. redo a step instead of going through everything. This is
  32. especially useful for large projects or projects that end
  33. up generating releases.
  34. ==== Application
  35. You can build your application specifically, without
  36. looking at handling dependencies or generating a release,
  37. by running the following command:
  38. [source,bash]
  39. $ make app
  40. This command is very useful if you have a lot of dependencies
  41. and develop on a machine with slow file access, like the
  42. Raspberry Pi and many other embedded devices.
  43. Note that this command may fail if a required dependency
  44. is missing.
  45. ==== Dependencies
  46. You can build all dependencies, and nothing else, by
  47. running the following command:
  48. [source,bash]
  49. $ make deps
  50. This will fetch and compile all dependencies and their
  51. dependencies, recursively.
  52. link:deps.asciidoc[Packages and dependencies] are covered
  53. in the next chapter.
  54. ==== Release
  55. You can generate the release, skipping the steps for building
  56. the application and dependencies, by running the following
  57. command:
  58. [source,bash]
  59. $ make rel
  60. This command can be useful if nothing changed except the
  61. release configuration files.
  62. Consult the link:relx.asciidoc[Releases] chapter for more
  63. information about what releases are and how they are generated.
  64. Note that this command may fail if a required dependency
  65. is missing.
  66. === Application resource file
  67. When building your application, Erlang.mk will generate the
  68. http://www.erlang.org/doc/man/app.html[application resource file].
  69. This file is mandatory for all Erlang applications and is
  70. found in 'ebin/$(PROJECT).app'.
  71. `PROJECT` is a variable defined in your Makefile and taken
  72. from the name of the directory when Erlang.mk bootstraps
  73. your project.
  74. Erlang.mk can build the 'ebin/$(PROJECT).app' in two different
  75. ways: from the configuration found in the Makefile, or from
  76. the 'src/$(PROJECT).app.src' file.
  77. ==== Application configuration
  78. Erlang.mk automatically fills the `PROJECT` variable when
  79. bootstrapping a new project, but everything else is up to
  80. you. None of the values are required to build your project,
  81. although it is recommended to fill everything relevant to
  82. your situation.
  83. `PROJECT`::
  84. The name of the OTP application or library.
  85. `PROJECT_DESCRIPTION`::
  86. Short description of the project.
  87. `PROJECT_VERSION`::
  88. Current version of the project.
  89. `PROJECT_REGISTERED`::
  90. List of the names of all registered processes.
  91. `OTP_DEPS`::
  92. List of Erlang/OTP applications this project depends on,
  93. excluding `erts`, `kernel` and `stdlib`.
  94. `DEPS`::
  95. List of applications this project depends on that need
  96. to be fetched by Erlang.mk.
  97. There's no need for quotes or anything. The relevant part of
  98. the Cowboy Makefile follows, if you need an example:
  99. [source,make]
  100. ----
  101. PROJECT = cowboy
  102. PROJECT_DESCRIPTION = Small, fast, modular HTTP server.
  103. PROJECT_VERSION = 2.0.0-pre.2
  104. PROJECT_REGISTERED = cowboy_clock
  105. OTP_DEPS = crypto
  106. DEPS = cowlib ranch
  107. ----
  108. Any space before and after the value is dropped.
  109. link:deps.asciidoc[Dependencies] are covered in details in
  110. the next chapter.
  111. ==== Legacy method
  112. The 'src/$(PROJECT).app.src' file is a legacy method of
  113. building Erlang applications. It was introduced by the original
  114. `rebar` build tool, of which Erlang.mk owes a great deal as it
  115. is its main inspiration.
  116. The '.app.src' file serves as a template to generate the '.app'
  117. file. Erlang.mk will take it, fill in the `modules` value
  118. dynamically, and save the result in 'ebin/$(PROJECT).app'.
  119. When using this method, Erlang.mk cannot fill the `applications`
  120. key from dependencies automatically, which means you need to
  121. add them to Erlang.mk and to the '.app.src' at the same time,
  122. duplicating the work.
  123. === File formats
  124. Erlang.mk supports a variety of different source file formats.
  125. The following formats are supported natively:
  126. [cols="<,3*^",options="header"]
  127. |===
  128. | Extension | Location | Description | Output
  129. | .erl | src/ | Erlang source | ebin/*.beam
  130. | .core | src/ | Core Erlang source | ebin/*.beam
  131. | .xrl | src/ | Leex source | src/*.erl
  132. | .yrl | src/ | Yecc source | src/*.erl
  133. | .asn1 | asn1/ | ASN.1 files | include/*.hrl include/*.asn1db src/*.erl
  134. | .mib | mibs/ | SNMP MIB files | include/*.hrl priv/mibs/*.bin
  135. |===
  136. Files are always searched recursively.
  137. The build is ordered, so that files that generate Erlang source
  138. files are run before, and the resulting Erlang source files are
  139. then built normally.
  140. In addition, Erlang.mk keeps track of header files (`.hrl`)
  141. as described at the end of this chapter. It can also compile
  142. C code, as described in the link:ports.asciidoc[NIFs and port drivers]
  143. chapter.
  144. Erlang.mk also comes with plugins for the following formats:
  145. [cols="<,3*^",options="header"]
  146. |===
  147. | Extension | Location | Description | Output
  148. | .dtl | templates/ | Django templates | ebin/*.beam
  149. | .proto | src/ | Protocol buffers | ebin/*.beam
  150. |===
  151. === Compilation options
  152. Erlang.mk provides a few variables that you can use to customize
  153. the build process and the resulting files.
  154. ==== ERLC_OPTS
  155. `ERLC_OPTS` can be used to pass some options to `erlc`, the Erlang
  156. compiler. Erlang.mk does not restrict any option. Please refer to
  157. the http://www.erlang.org/doc/man/erlc.html[erlc Manual] for the
  158. full list.
  159. By default, Erlang.mk will set the following options:
  160. [source,make]
  161. ERLC_OPTS = -Werror +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard
  162. In other words: warnings as errors, debug info (recommended) and
  163. enable warnings for exported variables, shadow variables and
  164. obsolete guard functions.
  165. You can redefine this variable in your Makefile to change it
  166. completely, either before or after including Erlang.mk:
  167. [source,make]
  168. ERLC_OPTS = +debug_info
  169. You can also filter out some options from the defaults Erlang.mk
  170. sets, by defining ERLC_OPTS after including Erlang.mk using the
  171. `:=` operator.
  172. [source,make]
  173. ----
  174. include erlang.mk
  175. ERLC_OPTS := $(filter-out -Werror,$(ERLC_OPTS))
  176. ----
  177. ==== ERLC_EXCLUDE
  178. `ERLC_EXCLUDE` can be used to exclude some modules from the
  179. compilation. It's there for handling special cases, you should
  180. not normally need it.
  181. To exclude a module, simply list it in the variable, either
  182. before or after including Erlang.mk:
  183. [source,make]
  184. ERLC_EXCLUDE = cowboy_http2
  185. === Cold and hot builds
  186. The first time you run `make`, Erlang.mk will build everything.
  187. The second time you run `make`, and all subsequent times, Erlang.mk
  188. will only rebuild what changed. Erlang.mk has been optimized for
  189. this use case, as it is the most common during development.
  190. Erlang.mk figures out what changed by using the dependency tracking
  191. feature of Make. Make automatically rebuilds a target if one of its
  192. dependency has changed (for example if a header file has changed,
  193. all the source files that include it will be rebuilt), and Erlang.mk
  194. leverages this feature to cut down on rebuild times.
  195. Note that this applies only to building; some other features of
  196. Erlang.mk will run every time they are called regardless of files
  197. changed.
  198. === Dependency tracking
  199. NOTE: This section is about the dependency tracking between files
  200. inside your project, not application dependencies.
  201. Erlang.mk keeps track of the dependencies between the different
  202. files in your project. This information is kept in the '$(PROJECT).d'
  203. file in your directory. It is generated if missing, and will be
  204. generated again after every file change, by default.
  205. Dependency tracking is what allows Erlang.mk to know when to
  206. rebuild Erlang files when header files, behaviors or parse
  207. transforms have changed. Erlang.mk also automatically keeps
  208. track of which files should be compiled first, for example
  209. when you have behaviors used by other modules in your project.
  210. If your project is stable, you may want to disable generating
  211. the dependency tracking file every time you compile. You can
  212. do this by adding the following line to your 'Makefile':
  213. [source,make]
  214. NO_MAKEDEP ?= 1
  215. As you can see, the snippet above uses `?=` instead of a
  216. simple equal sign. This is to allow you to temporarily override
  217. this value when you do make substantial changes to your project
  218. (including a new header file, new module with dependencies, etc.)
  219. and want to rebuild the dependency tracking file. You'll be
  220. able to use the following command:
  221. [source,bash]
  222. $ NO_MAKEDEP= make
  223. Otherwise, `make clean app` will of course force the
  224. recompilation of your project.
  225. Erlang.mk can also keep track of the source files generated
  226. by other means, for example if you generate code from a data
  227. file in your repository.
  228. === Generating Erlang source
  229. Erlang.mk provides hooks at different stages of the build process.
  230. When your goal is to generate Erlang source files, you can
  231. add your own rules before or after the dependency tracking
  232. file is generated. To do this, you would add your hook before
  233. or after including the 'erlang.mk' file.
  234. The easiest way is after:
  235. [source,make]
  236. ----
  237. PROJECT = example
  238. include erlang.mk
  239. $(PROJECT).d:: src/generated_mod.erl
  240. src/generated_mod.erl:: gen-mod.sh
  241. $(gen_verbose) ./gen-mod.sh $@
  242. ----
  243. In this case we use `$(gen_verbose)` to hide the details of
  244. the build by default. Erlang.mk will simply say what file
  245. is it currently generating.
  246. When using an external script to generate the Erlang source
  247. file, it is recommended to depend on that script, so that
  248. the source file gets generated again when the script gets
  249. modified.
  250. If for whatever reason you prefer to hook before including
  251. Erlang.mk, don't forget to set the `.DEFAULT_GOAL` variable,
  252. otherwise nothing will get built:
  253. [source,make]
  254. ----
  255. PROJECT = example
  256. .DEFAULT_GOAL = all
  257. $(PROJECT).d:: src/generated_mod.erl
  258. include erlang.mk
  259. src/generated_mod.erl:: gen-mod.sh
  260. $(gen_verbose) ./gen-mod.sh $@
  261. ----
  262. === Cleaning
  263. Building typically involves creating a lot of new files. Some
  264. are reused in rebuilds, some are simply replaced. All can be
  265. removed safely.
  266. Erlang.mk provides two commands to remove them: `clean` and
  267. `distclean`. `clean` removes all the intermediate files that
  268. were created as a result of building, including the BEAM files,
  269. the dependency tracking file and the generated documentation.
  270. `distclean` removes these and more, including the downloaded
  271. dependencies, Dialyzer's PLT file and the generated release,
  272. putting your directory back to the state it was before you
  273. started working on it.
  274. To clean:
  275. [source,bash]
  276. $ make clean
  277. Or distclean:
  278. [source,bash]
  279. $ make distclean
  280. That is the question.
  281. Note that Erlang.mk will automatically clean some files as
  282. part of other targets, but it will never run `distclean` if
  283. you don't explicitly use it.