使用Opam来管理项目依赖关系 [英] Using Opam to manage project dependencies

查看:332
本文介绍了使用Opam来管理项目依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OCaml的完整新手。我使用过的其他语言(例如Scala,Clojure,Node.js上的JavaScript)具有包管理器,允许一个项目作为一个干净的slate启动,它具有一组已知版本的依赖关系。



我试图用Opam做一些这样的事情。理想情况下,我想要一个列出依赖关系的文件(可能是OCaml版本),以便协作者可以使用

  git clone myproject 
< magic opam command>
ocamlbuild

并拥有一个工作版本,没有在全球安装任何东西。

我明白正确的方法是使用 Opam开关,但我不知道在实践中该怎么做。看起来交换机是绑定到一个编译器版本,而不是每个项目(还有别名),我也找不到是否存在一种项目的Opam配置文件。


$ b简而言之,说一个人想要启动一个新项目,这取决于Core和Yojson(为了例子)。要清洁可重复构建的步骤是什么?这里,clean意味着它不会干扰现有的已安装的库,而且可重现意味着它可以在一个干净的机器上工作,使用一个新克隆的项目。

解决方案

对于系统库来说,这不是很干净。否则,您需要启动自己的VM或其他容器。但是,对于OCaml环境,您可以通过项目根目录中的 opam 文件来实现目标。在描述了所有依赖项(包括系统一)之后,您可以将引脚您的项目,这将安装所有的依赖项,编译项目并将其部署到opam栈。所以工作流程如下:

  $#install opam 1.2 
$#install aspcud(可选,但强烈推荐)
$ opam switch install fresh -A 4.02.1
$ opam pin add proj / path / to / proj -n
$ opam depext --install proj
#可选部分:
$ edit proj / src / main.ml#开发
$ opam升级proj

现在让我们逐步了解这个工作流程。



安装编译器



  $ opam switch install fresh -A 4.02。 1 

此命令创建一个新的编译器安装。这里 fresh 没有什么特别的意义,它只是安装的任意名称。通常,我使用 date +%y%m%d命令,而不是新鲜的,创建一个名称,由当年,月,日。



固定项目



  $ opam pin add proj / path /到/ proj -n 

此命令将向OPAM系统介绍您的项目。这就像创建自己的小型软件仓库,只包含一个软件包 proj 。名称 proj 当然只是您的项目的名称,无论是什么。 pin 使用向OPAM系统描述项目的 opam 文件,您可以使用说明。或者您可以允许 pin 命令为您创建。它会驱动你轻轻的过程,问一些问题。您可以在这个博客文章中阅读更多关于固定的信息。



安装包和依赖项



在上一个命令中,我们添加 -n 标志,因为我们想要在小步骤中移动,所以在固定之后就会停止 pin 命令安装你的包。

  $ opam depext --install proj 

此命令将计算您的包依赖关系的传递性关闭,并安装它们,包括您的系统依赖关系(如果您在ubuntu或fedora上,并且如果您已经在 opam 文件)。



在项目上工作



假设你想开发代码。有一个OPAM友好的工作流程:

  $ edit proj / src / main.ml#开发
$ opam升级proj

这将重新安装您的软件包(并重新安装软件包的所有依赖项,如果它们存在)。



另一方面,如果您的项目不是很小,这个工作流程将会有严重的缺陷,因为它会复制所有的源代码,并从头编译。但是如果你有一套并行工作的依赖软件包,那么这是一种方法。



编译和其他内容



但这只是关于包管理和包间依赖关系。 OPAM对于特定的构建系统是绝对不可知的,不要对任何一个构建系统给予任何帮助(但是仍然有一些工具支持)。所以,你可以自己编写一个 Makefile ,或者你自己设置一套Shell脚本调用 ocamlbuild 这绝对是您。但是如果我在你身边,我会用OASIS来管理我的建筑过程。 OASIS本身不是构建系统,其目的是以跨平台方式管理构建系统。但默认情况下,它使用 ocamlbuild ,并且它可以顺利地整合在一起。此外,它与OPAM集成(实际上是OPAM与OASIS集成)。有一个 oasis2opam 包,它将从 _oasis opam 文件$ c>文件。那么如何创建 _oasis 文件,它描述了项目的构建过程,那么您可以使用用手工创建您自己喜欢的文本emacs,或者您可以允许OASIS为您使用绿洲快速入门创建它。



最后,我建议您查看现有项目并将其用作灵感来源。您可以从我维护的 BAP 项目开始,但它具有相当复杂的配置。所以,可能会有一个更简单的例子。


I am a complete newbie to OCaml. Other languages I have used (for instance Scala, Clojure, Javascript on Node.js) have package managers that allow one to start a project as a clean slate that has a declared set of dependencies of known versions.

I am trying to do something of the sort with Opam. Ideally, I would like to have a file that lists dependencies (and possibly OCaml version) so that a collaborator can start a project with

git clone myproject
<magic opam command>
ocamlbuild

and have a working version, without having installed anything globally.

I understand that the right way to do this would be to use Opam switches, but I am not sure what to do in practice. It seems that switches are tied to a compiler version, rather than per-project (altough there are aliases), and also I could not find whether there exists a sort of project Opam configuration file.

In short: say one wants to start a new project depending on Core and Yojson (for the sake of example). What would be the steps to have a clean reproducible build? Here, clean means that it won't interfere with existing installed libraries, and reproducible means that it will work on a clean machine with a freshly-cloned project.

解决方案

It can't be really clean with respect to system libraries. Otherwise you need to start your own VM or some other container. But with respect to OCaml environment, you can achieve your goal with opam file in the root of your project. After you've described all your dependencies (including system one) in it, you can pin your project, and this will install all your dependencies, compile your project and deploy it to the opam stack. So the workflow is the following:

 $ # install opam 1.2
 $ # install aspcud (optionally, but highly recommended)
 $ opam switch install fresh -A 4.02.1
 $ opam pin add proj /path/to/proj -n
 $ opam depext --install proj
 # optional part:
 $ edit proj/src/main.ml # do the development
 $ opam upgrade proj

Now let's walk through this workflow in a step-by-step manner.

Install compiler

 $ opam switch install fresh -A 4.02.1 

This command creates a new compiler installation. Here fresh has no special meaning it is just an arbitrary name for the installation. Usually, I use date +"%y%m%d" command instead of fresh that creates a name consisting of current year, month and day.

Pin the project

$ opam pin add proj /path/to/proj -n

This command will introduce your project to OPAM system. It is like creating your own small repository of packages, containing only one package, the proj. The name proj is of course just a name of your project, whatever it is. pin uses opam file that describes your project to the OPAM system, you can create it manually using this instructions. Or you can allow pin command to create it for you. It will drive you gently through the process, asking some questions. You can read more about pinning in this blog post.

Installing package and dependencies

In the previous command we add -n flag, that will stop pin command from installing your package just after the pinning, because we want to move in small steps.

$ opam depext --install proj

This command will compute a transitive closure of your package dependencies and install them, including your system dependencies (if you're on ubuntu or fedora, and if you've specified your dependencies in opam file in a previous step).

Working on the project

Suppose you want to develop the code. There is an OPAM friendly workflow:

 $ edit proj/src/main.ml # do the development
 $ opam upgrade proj

This will reinstall your packages (and reinstall all dependents of your package, if they exist).

This workflow, on the other hand, has a severe drawback if your project is not tiny, as it will copy all the sources and compile them from scratch. But if you have a set of dependent packages on which you're working in parallel, then it is a way to do this.

Compiling and other stuff

But this is only about package management and inter-package dependencies. OPAM is absolutely agnostic to a particular build system, and don't give any favor to any of them (but still have tooling support for some of them). So, you can write a Makefile by yourself or have your own set of shell scripts invoking ocamlbuild this is absolutely up to you. But if I were in you, than I will use OASIS to manage my building process. OASIS is not a build system by itself, its purpose is to manage build systems in a cross-platform way. But by default it uses ocamlbuild and it integrates with it smoothly. Also, it has an integration with OPAM (in fact it is OPAM who has the integration with OASIS). There is a oasis2opam package, that will create an opam file from _oasis file. What about creating _oasis file, that describes building process of your project, then you can either create it by hand with your favorite text emacs, or you can allow OASIS to create it for you with oasis quickstart.

As a final note, I would suggest you to view existing projects and use them as a source of inspiration. You can start with BAP project, that I'm maintaining, but it has rather complex configuration. So, there might be a simpler examples.

这篇关于使用Opam来管理项目依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆