deps.asciidoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. [[deps]]
  2. == Packages and dependencies
  3. Erlang.mk can fetch and compile the dependencies that your
  4. project requires. Erlang.mk improves upon the concepts
  5. introduced by Rebar, so they should be familiar to many
  6. seasoned Erlang developers.
  7. Erlang.mk is not a package manager, nor is it trying to be,
  8. but it does include an index of Erlang packages to make
  9. discovering useful projects easier.
  10. This chapter will explain how to use packages, add
  11. dependencies to your project or bundle them directly
  12. in a single repository.
  13. === Searching packages
  14. Erlang.mk gives you access to nearly 500 packages, with more
  15. being added regularly.
  16. To find a package, search for it:
  17. [source,bash]
  18. $ make search q=pool
  19. This will return all packages matching this word, like worker
  20. pool and acceptor pool projects.
  21. You can also list everything and use regular command line
  22. tools to find what you need, for example:
  23. [source,bash]
  24. $ make search | less
  25. // @todo Simplify adding packages, add a new chapter explaining
  26. // everything, then link to this new chapter from here.
  27. === Adding dependencies to your project
  28. Once you find the package you need, adding it as a dependency
  29. to your project is a one-liner:
  30. [source,make]
  31. DEPS = cowboy
  32. And that's it! The next time you run `make`, Erlang.mk will
  33. fetch and compile Cowboy. Erlang.mk will also ensure Cowboy
  34. is available whenever you use the shell, run tests and any
  35. other operations.
  36. Erlang.mk will fill in the application resource file with
  37. all applications found in `DEPS`. But not all dependencies
  38. are Erlang applications, and not all dependencies need to
  39. be a runtime dependency. That's where the `BUILD_DEPS`
  40. variable comes in: it works just like `DEPS`, except the
  41. dependencies listed there will not be added as runtime
  42. dependencies.
  43. For example, you could add a parse transform project like
  44. this to make it available only at build time:
  45. [source,make]
  46. BUILD_DEPS = erlando
  47. Or you could depend on a C project directly, if you are
  48. building a NIF:
  49. [source,make]
  50. BUILD_DEPS = leveldb
  51. dep_leveldb = git https://github.com/basho/leveldb 2.1.3
  52. This dependency will be built before your application, so
  53. you could easily copy the resulting shared file into your
  54. 'priv/' directory as part of the build process. More information
  55. about that in the xref:ports[NIFs and port drivers]
  56. chapter.
  57. Another variable, `LOCAL_DEPS`, allows specifying runtime
  58. dependencies which are part of Erlang/OTP itself, but also
  59. dependencies that are included in the repository. Since they
  60. are already on your system, there is no need to fetch them.
  61. Do note that there is no way to choose the version, the
  62. application used will be the one already on your system.
  63. You could depend on the Crypto application, for example:
  64. [source,make]
  65. LOCAL_DEPS = crypto
  66. Erlang.mk comes with additional types of dependencies.
  67. It has `TEST_DEPS` for dependencies used only for testing:
  68. [source,make]
  69. TEST_DEPS = ct_helper
  70. dep_ct_helper = git https://github.com/ninenines/ct_helper master
  71. `DOC_DEPS` for dependencies used only when building documentation:
  72. [source,make]
  73. DOC_DEPS = edown
  74. `REL_DEPS` for dependencies required to build the release,
  75. or to include extra applications in the release:
  76. [source,make]
  77. REL_DEPS = recon
  78. And `SHELL_DEPS` for dependencies to make available when running
  79. the `make shell` command:
  80. [source,make]
  81. SHELL_DEPS = tddreloader
  82. All these will be documented in more details in their respective
  83. chapters.
  84. ==== Modifying the dependency source or version
  85. By default, Erlang.mk will look into its package index to
  86. find the project you are looking for, if you only provide
  87. its name. This is this case:
  88. [source,make]
  89. DEPS = cowboy
  90. If you need a different version, you need to define another
  91. variable. There are two ways to do this, each being useful
  92. for different reasons.
  93. If you simply want to change the commit number, all you
  94. need to do is to define the `dep_$(DEP_NAME)_commit`
  95. variable. In the case of Cowboy, this would look like this:
  96. [source,make]
  97. DEPS = cowboy
  98. dep_cowboy_commit = 2.0.0-pre.2
  99. Erlang.mk will use the package index to get all information
  100. about Cowboy, except the commit number which will be overriden.
  101. If you need to set the fetch method or repository information
  102. too, for example because you want to use your own fork, or
  103. simply because the project is missing from the index, you
  104. can define the `dep_$(DEP_NAME)` variable with everything:
  105. [source,make]
  106. DEPS = cowboy
  107. dep_cowboy = git https://github.com/essen/cowboy 2.0.0-pre.2
  108. This will fetch Cowboy from your fork at the given commit.
  109. ==== Fetch methods
  110. Erlang.mk comes with a number of different fetch methods.
  111. You can fetch from Git, Mercurial, SVN, to name a few.
  112. There are fetch methods that will work everywhere, and
  113. fetch methods that will only work in a given environment.
  114. The following table lists all existing methods:
  115. [cols="<,2*^",options="header"]
  116. |===
  117. | Name | Format | Description
  118. | git | git repo commit | Clone the Git repository and checkout the given version
  119. | git-submodule | git-submodule | Initialize and update the Git submodule
  120. | hg | hg repo commit | Clone the Mercurial repository and update to the given version
  121. | svn | svn repo | Checkout the given SVN repository
  122. | cp | cp path/to/repo | Recursively copy a local directory
  123. | ln | ln path/to/repo | Symbolically link a local directory
  124. | hex | hex version | Download the given project version from hex.pm
  125. | fail | N/A | Always fail, reserved for internal use
  126. | legacy | N/A | Legacy Erlang.mk fetcher, reserved for internal use
  127. |===
  128. The `git` and `hg` methods both have a repository and commit.
  129. You can use any valid commit, tag or branch in that repository
  130. for the commit value.
  131. For example, to fetch Cowboy with tag 2.0.0-pre.2 from Git:
  132. [source,make]
  133. dep_cowboy = git https://github.com/ninenines/cowboy 2.0.0-pre.2
  134. Or to fetch Ehsa tag 4.0.3 from Mercurial:
  135. [source,make]
  136. dep_ehsa = hg https://bitbucket.org/a12n/ehsa 4.0.3
  137. Git also comes with a concept of submodules. Erlang.mk can
  138. automatically initializes and updates submodules for dependencies,
  139. as long as they were added beforehand using `git submodule add`:
  140. [source,make]
  141. dep_cowboy = git-submodule
  142. The `svn` method only has a repository value, but that's
  143. simply because the SVN repository URL can also contain
  144. the path and commit.
  145. This would fetch an example project from the trunk:
  146. [source,make]
  147. dep_ex1 = svn https://example.com/svn/trunk/project/ex1
  148. And this would fetch a separate example project from a
  149. specific commit:
  150. [source,make]
  151. dep_ex2 = svn svn://example.com/svn/branches/erlang-proj/ex2@264
  152. You can copy a directory from your machine using the `cp` method.
  153. It only takes the path to copy from:
  154. [source,make]
  155. dep_cowboy = cp $(HOME)/ninenines/cowboy
  156. Finally, you can use a package from the
  157. link:https://hex.pm/[Hex repository]:
  158. [source,make]
  159. dep_cowboy = hex 1.0.3
  160. ==== Custom fetch methods
  161. If none of the existing methods fit your use, you can simply
  162. define your own. Erlang.mk will consider all variables that
  163. are named as `dep_fetch_$(METHOD)` to be available fetch
  164. methods. You can do anything inside this variable, as long
  165. as you create a folder named '$(DEPS_DIR)/$(call dep_name,$1)'.
  166. Or in layman terms, if your dependency is Cowboy, this would
  167. become 'deps/cowboy'.
  168. To give an example, this is what the Git method does:
  169. [source,make]
  170. ----
  171. define dep_fetch_git
  172. git clone -q -n -- $(call dep_repo,$1) $(DEPS_DIR)/$(call dep_name,$1); \
  173. cd $(DEPS_DIR)/$(call dep_name,$1) && git checkout -q $(call dep_commit,$1);
  174. endef
  175. ----
  176. Note that, like dependency information, this custom fetch method
  177. must be written before including 'erlang.mk'.
  178. === How deps are fetched and built
  179. The order in which dependencies are fetched and built is well
  180. defined. This means that Erlang.mk will get the same applications
  181. regardless of the command or options being used.
  182. In tree traversal terms, where the list of dependencies is a
  183. tree, Erlang.mk fetches everything using the pre-order traversal
  184. method. The steps can be summarized like this, starting from
  185. the root application:
  186. . Fetch all dependencies for the application
  187. . Build first dependency
  188. . Build Nth dependency
  189. . Build last dependency
  190. Every time a dependency is built, these same steps are followed,
  191. recursively.
  192. Do note that the first step, fetching all dependencies of
  193. an application, is not guaranteed to be ordered. The reason
  194. for this is that it is not possible to have the same dependency
  195. listed twice in a single application, and therefore there can
  196. be no conflicts. Remember, this step only fetches, at no point
  197. are different applications built in parallel.
  198. What about conflicts between the dependencies of different
  199. applications? Simple. Since builds are ordered, this means
  200. that the first version of an application that is fetched
  201. will be the one that wins.
  202. This means that if project A depends on projects B and C,
  203. in this order, and that both B and C depend on a different
  204. version of D, it will always be B's version of D that wins,
  205. because we fetch the dependencies of B before fetching
  206. those from C.
  207. Similarly, if project A depends on projects B, C and D,
  208. regardless of the order, and A, B and C depend on a
  209. different version of D, it will always be A's version
  210. that wins, because we fetch all dependencies of A before
  211. fetching those from B or C.
  212. === Fetching and listing dependencies only
  213. You can fetch all dependencies recursively without building anything,
  214. with the `make fetch-deps` command. It follows the same rules described
  215. in the section above.
  216. You can list all dependencies recursively, again without building
  217. anything, with the `make list-deps` command. It will obviously need
  218. to fetch all dependencies exactly like `make fetch-deps`. Once
  219. everything is fetched, it prints a sorted list of absolute paths to the
  220. dependencies.
  221. By default, `fetch-deps` and `list-deps` work on the `BUILD_DEPS`
  222. and `DEPS` lists only. To also fetch/list `TEST_DEPS`, `DOC_DEPS`,
  223. `REL_DEPS` and/or `SHELL_DEPS`, you have two possibilities:
  224. * You can use `make fetch-test-deps`, `make fetch-doc-deps`, `make
  225. fetch-rel-deps` and `make fetch-shell-deps` commands respectively.
  226. If you want to list them, you can use `make list-test-deps`, `make
  227. list-doc-deps`, `make list-rel-deps` and `make list-shell-deps`
  228. respectively.
  229. * You can use `make fetch-deps` or `make list-deps` with the Makefile
  230. variable `DEP_TYPES` set to a list of dependency types you want.
  231. The types are `test`, `doc`, `rel` and `shell` respectively. For
  232. example, you can list test and doc dependencies with `make list-deps
  233. DEP_TYPES='test doc'`.
  234. Note that only first level `TEST_DEPS`, `DOC_DEPS`, `REL_DEPS` and
  235. `SHELL_DEPS` are included, not dependencies' one. In other word,
  236. `make list-test-deps` lists the `TEST_DEPS` of your project, but not
  237. `TEST_DEPS` of the projects yours depend on.
  238. No matter which method you use, `BUILD_DEPS` and `DEPS` are always
  239. included.
  240. Internally, the `make fetch-*` commands store the complete list of
  241. dependencies in files named `$(ERLANG_MK_RECURSIVE_DEPS_LIST)`,
  242. `$(ERLANG_MK_RECURSIVE_TEST_DEPS_LIST)`,
  243. `$(ERLANG_MK_RECURSIVE_DOC_DEPS_LIST)`,
  244. `$(ERLANG_MK_RECURSIVE_REL_DEPS_LIST)` and
  245. `$(ERLANG_MK_RECURSIVE_SHELL_DEPS_LIST)`. Those files are simply printed
  246. by the `make list-*` commands.
  247. `make list-*` commands are made for human beings. If you need the list
  248. of dependencies in a Makefile or a script, you should use the content
  249. of those files directly instead. The reason is that `make fetch-*` and
  250. `make list-*` may have unwanted content in their output, such as actual
  251. fetching of dependencies.
  252. === Ignoring unwanted dependencies
  253. Sometimes, you may want to ignore dependencies entirely.
  254. Not even fetch them. You may want to do this because a
  255. project you depend on depends on an application you do
  256. not need (like a dependency for building documentation
  257. or testing). Or maybe the dependency is already installed
  258. on your system.
  259. To ignore a dependency, simply add it to the `IGNORE_DEPS`
  260. variable:
  261. [source,make]
  262. IGNORE_DEPS += edown proper
  263. This will only ignore dependencies that are needed for
  264. building. It is therefore safe to write:
  265. [source,make]
  266. IGNORE_DEPS += edown proper
  267. TEST_DEPS = proper
  268. The PropEr application will be fetched as intended when
  269. running `make tests` or `make check`. It will however
  270. not be fetched when running `make` or `make deps`.
  271. === Dependencies directory
  272. Dependencies are fetched in '$(DEPS_DIR)'. By default this is
  273. the 'deps' directory. You can change this default, but you
  274. should only do so if it was not defined previously. Erlang.mk
  275. uses this variable to tell dependencies where to fetch their
  276. own dependencies.
  277. You will therefore need to use `?=` instead of `=`. Of course,
  278. if you know you will never use this project as a dependency,
  279. `=` will work. But to avoid it biting you later on, do this:
  280. [source,make]
  281. DEPS_DIR ?= $(CURDIR)/libs
  282. The `$(CURDIR)` part is important, otherwise dependencies of
  283. dependencies will be fetched in the wrong directory.
  284. Erlang.mk will also export the `REBAR_DEPS_DIR` variable for
  285. compatibility with Rebar build tools, as long as they are
  286. recent enough.
  287. === Many applications in one repository
  288. In addition to the dependencies that are fetched, Erlang.mk
  289. also allows you to have dependencies local to your repository.
  290. This kind of layout is sometimes called multi-application
  291. repositories, or repositories with multiple applications.
  292. They work exactly the same as remote dependencies, except:
  293. * They are not fetched
  294. * They are not autopatched
  295. * They are not deleted on `make distclean`
  296. * They are not automatically added to the application resource file
  297. To properly fill the application resource file and compile apps in
  298. the right order, you will need to define the `LOCAL_DEPS` variable
  299. for each relevant application, the same as for OTP applications. Apps
  300. can depend on each other in this way, and their compilation order
  301. will follow the same rules as regular dependencies in `DEPS`.
  302. The top-level `LOCAL_DEPS` variable, if defined, will determine which
  303. apps (along with their dependencies) to build, and also which apps
  304. should be added to the top-level application resource file, if there
  305. is one. This may be useful, for example, for specifying a different
  306. set of apps to build for different releases. If `LOCAL_DEPS` is not
  307. defined, then all apps in the '$(APPS_DIR)' will be built, but none
  308. will be automatically added to the top-level application resource
  309. file.
  310. If there is a conflict between a local dependency and a
  311. remote dependency, then the local dependency always wins;
  312. an error will be triggered when trying to fetch the
  313. conflicting remote dependency.
  314. To start using dependencies local to the repository, simply
  315. create a folder named '$(APPS_DIR)'. By default, this folder
  316. is the 'apps/' directory.
  317. You can use Erlang.mk to bootstrap local dependencies by
  318. using the command `make new-app` or `make new-lib`. This
  319. command will create the necessary directories and bootstrap
  320. the application.
  321. For example, to create a full fledged OTP application as
  322. a local dependency:
  323. [source,bash]
  324. $ make new-app in=webchat
  325. Or, the same as an OTP library:
  326. [source,bash]
  327. $ make new-lib in=webchat
  328. Templates also work with local dependencies, from the root
  329. directory of the project. You do need however to tell
  330. Erlang.mk to create the files in the correct application:
  331. [source,bash]
  332. $ make new t=gen_server n=my_server in=webchat
  333. === Repositories with no application at the root level
  334. It's possible to use Erlang.mk with only applications in
  335. '$(APPS_DIR)', and nothing at the root of the repository.
  336. Just create a folder, put the 'erlang.mk' file in it,
  337. write a Makefile that includes it, and start creating
  338. your applications.
  339. Similarly, it's possible to have a repository with only
  340. dependencies found in '$(DEPS_DIR)'. You just need to
  341. create a Makefile and specify the dependencies you want.
  342. This allows you to create a repository for handling the
  343. building of releases, for example.
  344. === Autopatch
  345. Erlang.mk will automatically patch all the dependencies it
  346. fetches. It needs to do this to ensure that the dependencies
  347. become compatible with not only Erlang.mk, but also with
  348. the version of Erlang.mk that is currently used.
  349. When fetching a dependency, the following operations are
  350. performed:
  351. * Fetch the dependency using the configured fetch method
  352. * If it contains a 'configure.ac' or 'configure.in' file, run `autoreconf -Wall -vif -I m4`
  353. * If it contains a 'configure' script, run it
  354. * Run autopatch on the project
  355. Autopatch first checks if there is any project-specific patch
  356. enabled. There are currently two: `RABBITMQ_CLIENT_PATCH` for
  357. the `amqp_client` dependency, and `RABBITMQ_SERVER_PATCH` for
  358. the `rabbit` dependency. These are needed only for RabbitMQ
  359. versions before 3.6.0 (assuming you are using upstream RabbitMQ,
  360. and not a fork).
  361. Otherwise, autopatch performs different operations depending
  362. on the kind of project it finds the dependency to be.
  363. * Rebar projects are automatically converted to use Erlang.mk
  364. as their build tool. This essentially patches Rebar out, and
  365. fixes and converts the project to be compatible with Erlang.mk.
  366. * Erlang.mk projects have their 'Makefile' patched, if necessary,
  367. to include the top-level project's Erlang.mk. This is to ensure
  368. that functionality works across all dependencies, even if the
  369. dependency's Erlang.mk is outdated. The patched Makefile
  370. can be safely committed if necessary.
  371. * Other Erlang projects get a small Erlang.mk Makefile
  372. generated automatically.
  373. * Projects with no source directory and no Makefile get an
  374. empty Makefile generated, for compatibility purposes.
  375. * Other projects with no Makefile are left untouched.
  376. You can disable the replacing of the 'erlang.mk' file by
  377. defining the `NO_AUTOPATCH_ERLANG_MK` variable:
  378. [source,make]
  379. NO_AUTOPATCH_ERLANG_MK = 1
  380. You can also disable autopatch entirely for a few select
  381. projects using the `NO_AUTOPATCH` variable:
  382. [source,make]
  383. NO_AUTOPATCH = cowboy ranch cowlib
  384. === Skipping deps
  385. It is possible to temporarily skip all dependency operations.
  386. This is done by defining the `SKIP_DEPS` variable. Use cases
  387. include being somewhere with no connection to download them,
  388. or perhaps a peculiar setup.
  389. A typical usage would be:
  390. [source,bash]
  391. $ make SKIP_DEPS=1
  392. When the variable is defined:
  393. * Dependencies will not be compiled or downloaded when required
  394. * The dependency directory '$(DEPS_DIR)' will not be removed on `make distclean`
  395. This variable only applies to remote dependencies.