getting_started.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. [[getting_started]]
  2. == Getting started
  3. This chapter explains how to get started using Erlang.mk.
  4. === Creating a folder for your project
  5. The first step is always to create a new folder that will
  6. contain your project.
  7. [source,bash]
  8. $ mkdir hello_joe
  9. $ cd hello_joe
  10. Most people tend to put all their projects side by side in
  11. a common folder. We recommend keeping an organization similar
  12. to your remote repositories. For example, for GitHub users,
  13. put all your projects in a common folder with the same name
  14. as your username. For example '$HOME/ninenines/cowboy' for
  15. the Cowboy project.
  16. === Downloading Erlang.mk
  17. At the time of writing, Erlang.mk is unlikely to be present
  18. in your Erlang distribution, or even in your OS packages.
  19. The next step is therefore to download it:
  20. [source,bash]
  21. $ wget https://erlang.mk/erlang.mk
  22. Or:
  23. [source,bash]
  24. $ curl -O https://erlang.mk/erlang.mk
  25. Alternatively, just https://erlang.mk/erlang.mk[click on this link].
  26. Make sure you put the file inside the folder we created previously.
  27. === Getting started with OTP applications
  28. An OTP application is an Erlang application that has a supervision
  29. tree. In other words, it will always have processes running.
  30. This kind of project can be automatically generated by Erlang.mk.
  31. All you need to do is use the `bootstrap` target:
  32. [source,bash]
  33. $ make -f erlang.mk bootstrap
  34. Something similar to the following snippet will then appear
  35. on your screen:
  36. [source,bash]
  37. ----
  38. git clone https://github.com/ninenines/erlang.mk .erlang.mk.build
  39. Cloning into '.erlang.mk.build'...
  40. remote: Counting objects: 4035, done.
  41. remote: Compressing objects: 100% (12/12), done.
  42. remote: Total 4035 (delta 8), reused 4 (delta 4), pack-reused 4019
  43. Receiving objects: 100% (4035/4035), 1.10 MiB | 784.00 KiB/s, done.
  44. Resolving deltas: 100% (2442/2442), done.
  45. Checking connectivity... done.
  46. if [ -f build.config ]; then cp build.config .erlang.mk.build; fi
  47. cd .erlang.mk.build && make
  48. make[1]: Entering directory '/home/essen/tmp/hello_joe/.erlang.mk.build'
  49. awk 'FNR==1 && NR!=1{print ""}1' core/core.mk index/*.mk core/index.mk core/deps.mk plugins/protobuffs.mk core/erlc.mk core/docs.mk core/test.mk plugins/asciidoc.mk plugins/bootstrap.mk plugins/c_src.mk plugins/ci.mk plugins/ct.mk plugins/dialyzer.mk plugins/edoc.mk plugins/elvis.mk plugins/erlydtl.mk plugins/escript.mk plugins/eunit.mk plugins/relx.mk plugins/shell.mk plugins/triq.mk plugins/xref.mk plugins/cover.mk \
  50. | sed 's/^ERLANG_MK_VERSION = .*/ERLANG_MK_VERSION = 1.2.0-642-gccd2b9f/' > erlang.mk
  51. make[1]: Leaving directory '/home/essen/tmp/hello_joe/.erlang.mk.build'
  52. cp .erlang.mk.build/erlang.mk ./erlang.mk
  53. rm -rf .erlang.mk.build
  54. ----
  55. This is Erlang.mk bootstrapping itself. Indeed, the file you
  56. initially downloaded contains nothing more than the code needed
  57. to bootstrap. This operation is done only once. Consult the
  58. xref:updating[Updating Erlang.mk] chapter for more
  59. information.
  60. Of course, the generated project can now be compiled:
  61. [source,bash]
  62. $ make
  63. Cheers!
  64. === Getting started with OTP libraries
  65. An OTP library is an Erlang application that has no supervision
  66. tree. In other words, it is nothing but modules.
  67. This kind of project can also be generated by Erlang.mk, using
  68. the `bootstrap-lib` target:
  69. [source,bash]
  70. $ make -f erlang.mk bootstrap-lib
  71. Erlang.mk will once again bootstrap itself and generate all
  72. the files for your project. You can now compile it:
  73. [source,bash]
  74. $ make
  75. Enjoy!
  76. === Getting started with OTP releases
  77. An OTP release is the combination of the Erlang RunTime System (ERTS)
  78. along with all the libraries and files that your node will need
  79. to run. It is entirely self contained, and can often be sent as-is
  80. to your production system and run without any extra setup.
  81. Erlang.mk can of course bootstrap your project to generate releases.
  82. You can use the `bootstrap-rel` target for this purpose:
  83. [source,bash]
  84. $ make bootstrap-rel
  85. This target can be combined with `bootstrap` or `bootstrap-lib` to
  86. create a project that will build a release:
  87. [source,bash]
  88. $ make -f erlang.mk bootstrap-lib bootstrap-rel
  89. It is often very useful to keep the top-level project for
  90. commands useful during operations, and put the components
  91. of the system in separate applications that you will then
  92. depend on. Consult the xref:deps[Packages and dependencies]
  93. chapter for more information.
  94. When you run `make` from now on, Erlang.mk will compile your
  95. project and build the release:
  96. [source,bash]
  97. $ make
  98. APP hello_joe.app.src
  99. GEN distclean-relx-rel
  100. GEN /home/essen/tmp/hello_joe/relx
  101. ===> Starting relx build process ...
  102. ===> Resolving OTP Applications from directories:
  103. /home/essen/tmp/hello_joe/ebin
  104. /usr/lib/erlang/lib
  105. /home/essen/tmp/hello_joe/deps
  106. ===> Resolved hello_joe_release-1
  107. ===> Including Erts from /usr/lib/erlang
  108. ===> release successfully created!
  109. The first time you run this command, Erlang.mk will download
  110. _relx_, the release building tool. So don't worry if you see
  111. more output than above.
  112. If building the release is slow, no need to upgrade your
  113. hardware just yet. Just consult the xref:relx[Releases]
  114. chapter for various tips to speed up build time during
  115. development.
  116. You can start the release using the './_rel/hello_joe_release/bin/hello_joe_release'
  117. script, or simply run `make run`. The latter will also compile
  118. your project and build the release if it wasn't already:
  119. [source,bash]
  120. ----
  121. $ make run
  122. APP hello_joe.app.src
  123. GEN distclean-relx-rel
  124. ===> Starting relx build process ...
  125. ===> Resolving OTP Applications from directories:
  126. /home/essen/tmp/hello_joe/ebin
  127. /usr/lib/erlang/lib
  128. /home/essen/tmp/hello_joe/deps
  129. ===> Resolved hello_joe_release-1
  130. ===> Including Erts from /usr/lib/erlang
  131. ===> release successfully created!
  132. Exec: /home/essen/tmp/hello_joe/_rel/hello_joe_release/erts-7.0/bin/erlexec -boot /home/essen/tmp/hello_joe/_rel/hello_joe_release/releases/1/hello_joe_release -boot_var ERTS_LIB_DIR /home/essen/tmp/hello_joe/_rel/hello_joe_release/erts-7.0/../lib -env ERL_LIBS /home/essen/tmp/hello_joe/_rel/hello_joe_release/releases/1/lib -config /home/essen/tmp/hello_joe/_rel/hello_joe_release/releases/1/sys.config -args_file /home/essen/tmp/hello_joe/_rel/hello_joe_release/releases/1/vm.args -- console
  133. Root: /home/essen/tmp/hello_joe/_rel/hello_joe_release
  134. /home/essen/tmp/hello_joe/_rel/hello_joe_release
  135. heart_beat_kill_pid = 16389
  136. Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
  137. Eshell V7.0 (abort with ^G)
  138. (hello_joe@127.0.0.1)1>
  139. ----
  140. Simple as that!
  141. === Getting started from scratch
  142. If you already have an application, or you want to have full
  143. control over what files will be created, you can setup Erlang.mk
  144. manually.
  145. Erlang.mk is very easy to setup: all that you need to do is to
  146. create a folder, put Erlang.mk in it, and write a one line
  147. Makefile containing:
  148. [source,make]
  149. include erlang.mk
  150. For a step by step:
  151. [source,bash]
  152. ----
  153. $ mkdir hello_joe
  154. $ cd hello_joe
  155. $ curl https://erlang.mk/erlang.mk -o erlang.mk
  156. $ echo "include erlang.mk" > Makefile
  157. $ make
  158. ----
  159. From that point onward you can create an `src/` folder or start
  160. using templates.
  161. === Using spaces instead of tabs
  162. Erlang.mk defaults to tabs when creating files from templates.
  163. This is in part because of a personal preference, and in part
  164. because it is much easier to convert tabs to spaces than the
  165. opposite.
  166. Use the `SP` variable if you prefer spaces. Set it to the number
  167. of spaces per indentation level you want.
  168. For example, if you prefer two spaces per indentation level:
  169. [source,bash]
  170. $ make -f erlang.mk bootstrap SP=2
  171. When you bootstrap the project initially, the variable automatically
  172. gets added to the Makefile, so you only need to provide it when
  173. you get started.
  174. === Using templates
  175. It is no secret that Erlang's OTP behaviors tend to have some
  176. boilerplate. It is rarely an issue of course, except when
  177. creating new modules. That's why Erlang.mk not only comes with
  178. templates for generating projects, but also individual modules!
  179. You can list all available templates with the `list-templates`
  180. target:
  181. [source,bash]
  182. $ make list-templates
  183. Available templates: cowboy_http cowboy_loop cowboy_rest cowboy_ws gen_fsm gen_server gen_statem ranch_protocol supervisor
  184. To generate a module, let's say a `gen_server`, all you need to
  185. do is to call `make new` with the appropriate arguments:
  186. [source,bash]
  187. $ make new t=gen_server n=my_server
  188. This will create a module located in 'src/my_server.erl'
  189. using the `gen_server` template.
  190. This module is automatically compiled the next time you run
  191. `make`:
  192. [source,bash]
  193. $ make
  194. ERLC my_server.erl
  195. APP hello_joe.app.src
  196. All that's left to do is to open it in your favorite editor
  197. and make it do something!
  198. === Hiding Erlang.mk from git
  199. Erlang.mk is a large text file. It can easily take a large part of
  200. a `git diff` or a `git grep` command. You can avoid this by telling
  201. Git that 'erlang.mk' is a binary file.
  202. Add this to your '.gitattributes' file. This is a file that you
  203. can create at the root of your repository:
  204. ----
  205. erlang.mk -diff
  206. ----
  207. The 'erlang.mk' file will still appear in diffs and greps, but
  208. as a binary file, meaning its contents won't be shown by default
  209. anymore.
  210. === Getting help
  211. During development, if you don't remember the name of a target,
  212. you can always run `make help`:
  213. [source,bash]
  214. ----
  215. $ make help
  216. erlang.mk (version 1.2.0-642-gccd2b9f) is distributed under the terms of the ISC License.
  217. Copyright (c) 2013-2016 Loïc Hoguin <essen@ninenines.eu>
  218. Usage: [V=1] make [target]...
  219. Core targets:
  220. all Run deps, app and rel targets in that order
  221. app Compile the project
  222. deps Fetch dependencies (if needed) and compile them
  223. search q=... Search for a package in the built-in index
  224. rel Build a release for this project, if applicable
  225. docs Build the documentation for this project
  226. install-docs Install the man pages for this project
  227. check Compile and run all tests and analysis for this project
  228. tests Run the tests for this project
  229. clean Delete temporary and output files from most targets
  230. distclean Delete all temporary and output files
  231. help Display this help and exit
  232. erlang-mk Update erlang.mk to the latest version
  233. Bootstrap targets:
  234. bootstrap Generate a skeleton of an OTP application
  235. bootstrap-lib Generate a skeleton of an OTP library
  236. bootstrap-rel Generate the files needed to build a release
  237. new t=TPL n=NAME Generate a module NAME based on the template TPL
  238. list-templates List available templates
  239. ...
  240. ----
  241. This guide should provide any other answer. If not, please
  242. open a ticket on https://github.com/ninenines/erlang.mk/issues[the official repository]
  243. and we will work on improving the guide.
  244. Commercial support is available through Nine Nines. Please contact
  245. Loïc Hoguin by sending an email to mailto:contact@ninenines.eu[].