deps.asciidoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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-subfolder | git repo commit subfolder | Clone the Git repository, checkout the given version and use one of its subfolders as a dependency
  120. | git-submodule | git-submodule | Initialize and update the Git submodule
  121. | hg | hg repo commit | Clone the Mercurial repository and update to the given version
  122. | svn | svn repo | Checkout the given SVN repository
  123. | cp | cp path/to/repo | Recursively copy a local directory
  124. | ln | ln path/to/repo | Symbolically link a local directory
  125. | hex | hex version | Download the given project version from hex.pm
  126. | fail | N/A | Always fail, reserved for internal use
  127. | legacy | N/A | Legacy Erlang.mk fetcher, reserved for internal use
  128. |===
  129. The `git` and `hg` methods both have a repository and commit.
  130. You can use any valid commit, tag or branch in that repository
  131. for the commit value.
  132. For example, to fetch Cowboy with tag 2.0.0-pre.2 from Git:
  133. [source,make]
  134. dep_cowboy = git https://github.com/ninenines/cowboy 2.0.0-pre.2
  135. Or to fetch Ehsa tag 4.0.3 from Mercurial:
  136. [source,make]
  137. dep_ehsa = hg https://bitbucket.org/a12n/ehsa 4.0.3
  138. Git also comes with a concept of submodules. Erlang.mk can
  139. automatically initializes and updates submodules for dependencies,
  140. as long as they were added beforehand using `git submodule add`:
  141. [source,make]
  142. dep_cowboy = git-submodule
  143. The `svn` method only has a repository value, but that's
  144. simply because the SVN repository URL can also contain
  145. the path and commit.
  146. This would fetch an example project from the trunk:
  147. [source,make]
  148. dep_ex1 = svn https://example.com/svn/trunk/project/ex1
  149. And this would fetch a separate example project from a
  150. specific commit:
  151. [source,make]
  152. dep_ex2 = svn svn://example.com/svn/branches/erlang-proj/ex2@264
  153. You can copy a directory from your machine using the `cp` method.
  154. It only takes the path to copy from:
  155. [source,make]
  156. dep_cowboy = cp $(HOME)/ninenines/cowboy
  157. Finally, you can use a package from the
  158. link:https://hex.pm/[Hex repository]:
  159. [source,make]
  160. dep_cowboy = hex 1.0.3
  161. ==== Custom fetch methods
  162. If none of the existing methods fit your use, you can simply
  163. define your own. Erlang.mk will consider all variables that
  164. are named as `dep_fetch_$(METHOD)` to be available fetch
  165. methods. You can do anything inside this variable, as long
  166. as you create a folder named '$(DEPS_DIR)/$(call dep_name,$1)'.
  167. Or in layman terms, if your dependency is Cowboy, this would
  168. become 'deps/cowboy'.
  169. To give an example, this is what the Git method does:
  170. [source,make]
  171. ----
  172. define dep_fetch_git
  173. git clone -q -n -- $(call dep_repo,$1) $(DEPS_DIR)/$(call dep_name,$1); \
  174. cd $(DEPS_DIR)/$(call dep_name,$1) && git checkout -q $(call dep_commit,$1);
  175. endef
  176. ----
  177. Note that, like dependency information, this custom fetch method
  178. must be written before including 'erlang.mk'.
  179. === How deps are fetched and built
  180. The order in which dependencies are fetched and built is well
  181. defined. This means that Erlang.mk will get the same applications
  182. regardless of the command or options being used.
  183. In tree traversal terms, where the list of dependencies is a
  184. tree, Erlang.mk fetches everything using the pre-order traversal
  185. method. The steps can be summarized like this, starting from
  186. the root application:
  187. . Fetch all dependencies for the application
  188. . Build first dependency
  189. . Build Nth dependency
  190. . Build last dependency
  191. Every time a dependency is built, these same steps are followed,
  192. recursively.
  193. Do note that the first step, fetching all dependencies of
  194. an application, is not guaranteed to be ordered. The reason
  195. for this is that it is not possible to have the same dependency
  196. listed twice in a single application, and therefore there can
  197. be no conflicts. Remember, this step only fetches, at no point
  198. are different applications built in parallel.
  199. What about conflicts between the dependencies of different
  200. applications? Simple. Since builds are ordered, this means
  201. that the first version of an application that is fetched
  202. will be the one that wins.
  203. This means that if project A depends on projects B and C,
  204. in this order, and that both B and C depend on a different
  205. version of D, it will always be B's version of D that wins,
  206. because we fetch the dependencies of B before fetching
  207. those from C.
  208. Similarly, if project A depends on projects B, C and D,
  209. regardless of the order, and A, B and C depend on a
  210. different version of D, it will always be A's version
  211. that wins, because we fetch all dependencies of A before
  212. fetching those from B or C.
  213. === Fetching and listing dependencies only
  214. You can fetch all dependencies recursively without building anything,
  215. with the `make fetch-deps` command. It follows the same rules described
  216. in the section above.
  217. You can list all dependencies recursively, again without building
  218. anything, with the `make list-deps` command. It will obviously need
  219. to fetch all dependencies exactly like `make fetch-deps`. Once
  220. everything is fetched, it prints a sorted list of absolute paths to the
  221. dependencies.
  222. By default, `fetch-deps` and `list-deps` work on the `BUILD_DEPS`
  223. and `DEPS` lists only. To also fetch/list `TEST_DEPS`, `DOC_DEPS`,
  224. `REL_DEPS` and/or `SHELL_DEPS`, you have two possibilities:
  225. * You can use `make fetch-test-deps`, `make fetch-doc-deps`, `make
  226. fetch-rel-deps` and `make fetch-shell-deps` commands respectively.
  227. If you want to list them, you can use `make list-test-deps`, `make
  228. list-doc-deps`, `make list-rel-deps` and `make list-shell-deps`
  229. respectively.
  230. * You can use `make fetch-deps` or `make list-deps` with the Makefile
  231. variable `DEP_TYPES` set to a list of dependency types you want.
  232. The types are `test`, `doc`, `rel` and `shell` respectively. For
  233. example, you can list test and doc dependencies with `make list-deps
  234. DEP_TYPES='test doc'`.
  235. Note that only first level `TEST_DEPS`, `DOC_DEPS`, `REL_DEPS` and
  236. `SHELL_DEPS` are included, not dependencies' one. In other word,
  237. `make list-test-deps` lists the `TEST_DEPS` of your project, but not
  238. `TEST_DEPS` of the projects yours depend on.
  239. No matter which method you use, `BUILD_DEPS` and `DEPS` are always
  240. included.
  241. Internally, the `make fetch-*` commands store the complete list of
  242. dependencies in files named `$(ERLANG_MK_RECURSIVE_DEPS_LIST)`,
  243. `$(ERLANG_MK_RECURSIVE_TEST_DEPS_LIST)`,
  244. `$(ERLANG_MK_RECURSIVE_DOC_DEPS_LIST)`,
  245. `$(ERLANG_MK_RECURSIVE_REL_DEPS_LIST)` and
  246. `$(ERLANG_MK_RECURSIVE_SHELL_DEPS_LIST)`. Those files are simply printed
  247. by the `make list-*` commands.
  248. `make list-*` commands are made for human beings. If you need the list
  249. of dependencies in a Makefile or a script, you should use the content
  250. of those files directly instead. The reason is that `make fetch-*` and
  251. `make list-*` may have unwanted content in their output, such as actual
  252. fetching of dependencies.
  253. === Ignoring unwanted dependencies
  254. Sometimes, you may want to ignore dependencies entirely.
  255. Not even fetch them. You may want to do this because a
  256. project you depend on depends on an application you do
  257. not need (like a dependency for building documentation
  258. or testing). Or maybe the dependency is already installed
  259. on your system.
  260. To ignore a dependency, simply add it to the `IGNORE_DEPS`
  261. variable:
  262. [source,make]
  263. IGNORE_DEPS += edown proper
  264. This will only ignore dependencies that are needed for
  265. building. It is therefore safe to write:
  266. [source,make]
  267. IGNORE_DEPS += edown proper
  268. TEST_DEPS = proper
  269. The PropEr application will be fetched as intended when
  270. running `make tests` or `make check`. It will however
  271. not be fetched when running `make` or `make deps`.
  272. === Dependencies directory
  273. Dependencies are fetched in '$(DEPS_DIR)'. By default this is
  274. the 'deps' directory. You can change this default, but you
  275. should only do so if it was not defined previously. Erlang.mk
  276. uses this variable to tell dependencies where to fetch their
  277. own dependencies.
  278. You will therefore need to use `?=` instead of `=`. Of course,
  279. if you know you will never use this project as a dependency,
  280. `=` will work. But to avoid it biting you later on, do this:
  281. [source,make]
  282. DEPS_DIR ?= $(CURDIR)/libs
  283. The `$(CURDIR)` part is important, otherwise dependencies of
  284. dependencies will be fetched in the wrong directory.
  285. Erlang.mk will also export the `REBAR_DEPS_DIR` variable for
  286. compatibility with Rebar build tools, as long as they are
  287. recent enough.
  288. === Many applications in one repository
  289. In addition to the dependencies that are fetched, Erlang.mk
  290. also allows you to have dependencies local to your repository.
  291. This kind of layout is sometimes called multi-application
  292. repositories, or repositories with multiple applications.
  293. They work exactly the same as remote dependencies, except:
  294. * They are not fetched
  295. * They are not autopatched
  296. * They are not deleted on `make distclean`
  297. * They are not automatically added to the application resource file
  298. To properly fill the application resource file and compile apps in
  299. the right order, you will need to define the `LOCAL_DEPS` variable
  300. for each relevant application, the same as for OTP applications. Apps
  301. can depend on each other in this way, and their compilation order
  302. will follow the same rules as regular dependencies in `DEPS`.
  303. The top-level `LOCAL_DEPS` variable, if defined, will determine which
  304. apps (along with their dependencies) to build, and also which apps
  305. should be added to the top-level application resource file, if there
  306. is one. This may be useful, for example, for specifying a different
  307. set of apps to build for different releases. If `LOCAL_DEPS` is not
  308. defined, then all apps in the '$(APPS_DIR)' will be built, but none
  309. will be automatically added to the top-level application resource
  310. file.
  311. If there is a conflict between a local dependency and a
  312. remote dependency, then the local dependency always wins;
  313. an error will be triggered when trying to fetch the
  314. conflicting remote dependency.
  315. To start using dependencies local to the repository, simply
  316. create a folder named '$(APPS_DIR)'. By default, this folder
  317. is the 'apps/' directory.
  318. You can use Erlang.mk to bootstrap local dependencies by
  319. using the command `make new-app` or `make new-lib`. This
  320. command will create the necessary directories and bootstrap
  321. the application.
  322. For example, to create a full fledged OTP application as
  323. a local dependency:
  324. [source,bash]
  325. $ make new-app in=webchat
  326. Or, the same as an OTP library:
  327. [source,bash]
  328. $ make new-lib in=webchat
  329. Templates also work with local dependencies, from the root
  330. directory of the project. You do need however to tell
  331. Erlang.mk to create the files in the correct application:
  332. [source,bash]
  333. $ make new t=gen_server n=my_server in=webchat
  334. === Repositories with no application at the root level
  335. It's possible to use Erlang.mk with only applications in
  336. '$(APPS_DIR)', and nothing at the root of the repository.
  337. Just create a folder, put the 'erlang.mk' file in it,
  338. write a Makefile that includes it, and start creating
  339. your applications.
  340. Similarly, it's possible to have a repository with only
  341. dependencies found in '$(DEPS_DIR)'. You just need to
  342. create a Makefile and specify the dependencies you want.
  343. This allows you to create a repository for handling the
  344. building of releases, for example.
  345. === Autopatch
  346. Erlang.mk will automatically patch all the dependencies it
  347. fetches. It needs to do this to ensure that the dependencies
  348. become compatible with not only Erlang.mk, but also with
  349. the version of Erlang.mk that is currently used.
  350. When fetching a dependency, the following operations are
  351. performed:
  352. * Fetch the dependency using the configured fetch method
  353. * If it contains a 'configure.ac' or 'configure.in' file, run `autoreconf -Wall -vif -I m4`
  354. * If it contains a 'configure' script, run it
  355. * Run autopatch on the project
  356. Autopatch first checks if there is any project-specific patch
  357. enabled. There are currently two: `RABBITMQ_CLIENT_PATCH` for
  358. the `amqp_client` dependency, and `RABBITMQ_SERVER_PATCH` for
  359. the `rabbit` dependency. These are needed only for RabbitMQ
  360. versions before 3.6.0 (assuming you are using upstream RabbitMQ,
  361. and not a fork).
  362. Otherwise, autopatch performs different operations depending
  363. on the kind of project it finds the dependency to be.
  364. * Rebar projects are automatically converted to use Erlang.mk
  365. as their build tool. This essentially patches Rebar out, and
  366. fixes and converts the project to be compatible with Erlang.mk.
  367. * Erlang.mk projects have their 'Makefile' patched, if necessary,
  368. to include the top-level project's Erlang.mk. This is to ensure
  369. that functionality works across all dependencies, even if the
  370. dependency's Erlang.mk is outdated. The patched Makefile
  371. can be safely committed if necessary.
  372. * Other Erlang projects get a small Erlang.mk Makefile
  373. generated automatically.
  374. * Projects with no source directory and no Makefile get an
  375. empty Makefile generated, for compatibility purposes.
  376. * Other projects with no Makefile are left untouched.
  377. You can disable the replacing of the 'erlang.mk' file by
  378. defining the `NO_AUTOPATCH_ERLANG_MK` variable:
  379. [source,make]
  380. NO_AUTOPATCH_ERLANG_MK = 1
  381. You can also disable autopatch entirely for a few select
  382. projects using the `NO_AUTOPATCH` variable:
  383. [source,make]
  384. NO_AUTOPATCH = cowboy ranch cowlib
  385. === Skipping deps
  386. It is possible to temporarily skip all dependency operations.
  387. This is done by defining the `SKIP_DEPS` variable. Use cases
  388. include being somewhere with no connection to download them,
  389. or perhaps a peculiar setup.
  390. A typical usage would be:
  391. [source,bash]
  392. $ make SKIP_DEPS=1
  393. When the variable is defined:
  394. * Dependencies will not be compiled or downloaded when required
  395. * The dependency directory '$(DEPS_DIR)' will not be removed on `make distclean`
  396. This variable only applies to remote dependencies.