getting_started.asciidoc 9.8 KB

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