app.asciidoc 13 KB

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