用于简单 OCaml 项目的基本 Oasis 或 Opam 文件

Basic Oasis or Opam file for a simple OCaml project

我是 OCaml 的新用户。我已经要求 学习如何设置基本的 OCaml 项目,但我仍然遇到问题。跳到最后一个 TL;DR

例如,我正在尝试学习 Irmin。在 home page of Irmin 我看到我必须做

opam install irmin git cohttp

这样做,我最终得到了 Irmin 版本 0.8.3。不幸的是,我无法按照他们的示例进行操作,因为显然 Irmin 的版本为 0.9.4,而且 API 似乎已更改。所以,我想开始一个干净的项目,它只依赖 Irmin 0.9.4

首先,我设置了一个 opam 开关

 opam switch install playground -A 4.02.1

一定要从头开始工作。然后,我创建了一个基本的 _oasis 文件,如下所示

OASISFormat: 0.4
Name:        Playground
Version:     0.1.0
Synopsis:    OCaml playground
Authors:     Andrea Ferretti
License:     Apache-2.0
BuildTools: ocamlbuild

BuildDepends:
  irmin,
  irmin.unix,
  lwt,
  lwt.unix

然后我从 example.ml 中的 Irmin 主页复制了示例。

open Lwt
open Irmin_unix
let store = Irmin.basic (module Irmin_git.FS) (module Irmin.Contents.String)
let config = Irmin_git.config ~root:"/tmp/irmin/test" ~bare:true ()
let prog =
  Irmin.create store config task >>= fun t ->
  Irmin.update (t "Updating foo/bar")  ["foo"; "bar"] "hi!" >>= fun () ->
  Irmin.read_exn (t "Reading foo/bar") ["foo"; "bar"] >>= fun x ->
  Printf.printf "Read: %s\n%!" x;
  return_unit
let () = Lwt_main.run prog

如果我 oasis setup 然后 ocamlbuild example.ml 我会得到一堆错误

W: Cannot get variable ext_obj
W: Cannot get variable ext_lib
W: Cannot get variable ext_dll
W: Cannot get variable ocamlfind

简而言之,我的问题是:

What is the simplest way to set up a clean project that depends on Irmin 0.9.4, being sure that it is self-contained (it will not rely on preinstalled libraries other than those installed by the build process)?

因此,首先您的 _oasis 文件中没有任何活动目标。我的意思是,OASIS 是关于构建可执行文件和库的。你也没有描述。这意味着,您的 BuildDepends 没有任何效果,因为它只对 LibraryExecutable 条目有意义。因此,第一个近似值如下:

OASISFormat: 0.4
Name:        Playground
Version:     0.1.0
Synopsis:    OCaml playground
Authors:     Andrea Ferretti
License:     Apache-2.0
BuildTools:   ocamlbuild
BuildDepends: irmin.unix, lwt.unix

Executable "example"
  Path: .
  MainIs: example.ml
  CompiledObject: best

Doing so, I end up with Irmin version 0.8.3. Unfortunately, I cannot follow their examples, because apparently Irmin is at version 0.9.4, and it looks like the API has changed. So, I would like to start a clean project having as dependency just Irmin 0.9.4

如果你的版本不是最新的,那么你可以尝试说服 opam constraint solver,你真的需要那个特定的版本,比如:

 opam install irmin.0.9.4 git cohttp

此外,opam 内部约束求解器不是很复杂,因此请确保您的系统上安装了 aspcud。以下将在全新 ubuntu 安装中安装带有 aspcud 的最新 opam:

sudo add-apt-repository --yes ppa:avsm/ppa
sudo apt-get update
sudo apt-get --yes install opam

What is the simplest way to set up a clean project that depends on Irmin 0.9.4, being sure that it is self-contained (it will not rely on preinstalled libraries other than those installed by the build process)?

好吧,简单是个人意见的问题。例如,你可以在没有任何绿洲的情况下做同样的事情,只使用 opam 文件,你将 build 字段设置为 ["ocamlbuild"; "-use-ocamlfind"; "-pkgs irmin.unix,lwt.unix"; "example.native"],但它的扩展性如何...... .