OCaml 打包准则

来自 Arch Linux 中文维基

本文或本节需要翻译。要贡献翻译,请访问简体中文翻译团队

附注: 尚有一些段落未翻译完全(在 Talk:OCaml 打包准则# 中讨论)
Arch 打包准则

32 位CLRCMakeCrossDKMSEclipseElectronFontFree PascalGNOMEGoHaskellJavaKDEKernelLispMesonMinGWNode.jsNonfreeOCamlPerlPHPPythonRRubyRustVCSWebWine

为使用 OCaml 编写的软件创建 PKGBUILD

命名[编辑 | 编辑源代码]

若是库则采用 ocaml-modulename 的格式,若是程序则采用程序名。不论如何,名字都应当是全小写的。

文件的位置[编辑 | 编辑源代码]

[编辑 | 编辑源代码]

OCaml 库应当安装到 /usr/lib/ocaml。到 /usr/lib/ocaml/site-lib 已经弃用。

OCaml 库应当使用 ocaml-findlib 来安装。ocaml-findlib 在包种包含了元信息来使库管理更加简单。这是一种惯例,现在很多 OCaml 都需要它。

ocaml-findlib 从应被包含在源码包中的 META 的文件中取出必要的数据。If this file is not included, one should either be obtained from the corresponding Debian, Ubuntu, or Fedora package, or created for the package by the maintainer. A request to include the file should also be made to the upstream developers of the package.

The OCAMLFIND_DESTDIR variable should be used when installing packages with ocaml-findlib. See the example PKGBUILD below for details.

OASIS[编辑 | 编辑源代码]

OCaml packages that install executables using OASIS ignore DESTDIR. This is a known limitation of OASIS (issue #493). One way to enable DESTDIR-like functionality is to run the configure script with the --destdir argument, like so:

build() {
    cd "${srcdir}/${srcname}-${pkgver}"
    ./configure --prefix /usr --destdir "$pkgdir"

    # build commands
}

OCaml bytecode and levels[编辑 | 编辑源代码]

OCaml can run code on multiple "levels", the top level interprets OCaml Code without compiling, the bytecode level creates machine independent bytecode and the native level creates machine code binaries (just like C/C++).

When building OCaml Packages you need to be aware if the build process is compiling native machine code, bytecode, or as in many cases both. This creates a number of situations which can cause problems with package options and the right dependencies.

If bytecode is produced at all then the PKGBUILD must contain the following to protect the bytecode:

options=('!strip')

If the package does not contain bytecode and only distributes a binary, then ocaml is not needed as a dependency, but it of course is required as a makedepends since the ocaml package provides the OCaml compiler. If the package contains both native code and bytecode then ocaml should be a dependency and a makedepends.

OCaml code is rarely (if ever) distributed as bytecode only and will almost always include native code: the only case where using any as the arch is advisable is when only un-compiled source code is distributed, usually with a library, though many libraries still distribute native code.

The moral of the story here is to be aware of what it is you are distributing, chances are your package contains both native machine code and bytecode.

使用 Dune 的 PKGBUILD 实例[编辑 | 编辑源代码]

Dune 是一种新的构建系统,现在已经越来越多的被用于 OCaml 项目中。

One thing to be aware is that a single project can build several "packages" in the OPAM/findlib sense, each with its own directory in /usr/lib/ocaml/. See ocaml-cairo for an example. For release builds, all "packages" have to be explicitly listed.

# Contributor: Your Name <youremail@domain.com>

pkgname=ocaml-<package name>
pkgver=4.2
pkgrel=1
license=()
arch=('x86_64')
pkgdesc="An OCaml Package"
url=""
depends=('ocaml')
makedepends=('dune')
source=()
options=('!strip')
sha256sums=()

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  # The "-p" flag is necessary for release builds, see the Dune manpage. Dune will complain if you forget some packages.
  dune build -p package1,package1-extension,package2
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  DESTDIR="${pkgdir}" dune install --prefix "/usr" --libdir "lib/ocaml"

  # Dune installs documentation in /usr/doc, fix that.
  install -dm755 "${pkgdir}/usr/share/"
  mv "${pkgdir}/usr/doc" "${pkgdir}/usr/share/"
}

使用 findlib 的 PKGBUILD 实例[编辑 | 编辑源代码]

# Contributor: Your Name <youremail@domain.com>

pkgname=ocaml-<package name>
pkgver=4.2
pkgrel=1
license=()
arch=('x86_64')
pkgdesc="An OCaml Package"
url=""
depends=('ocaml')
makedepends=('ocaml-findlib')
source=()
options=('!strip')
md5sums=()

OCAMLFIND_DESTDIR="${pkgdir}$(ocamlfind printconf destdir)"

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  mkdir -p "$OCAMLFIND_DESTDIR"
  ./configure --prefix=/usr
  make
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  env DESTDIR="${pkgdir}" \
    OCAMLFIND_DESTDIR="$OCAMLFIND_DESTDIR" \
    make install
}

需要留意的是许多 OCaml 包都需要给 makemake install 一些额外信息。同时,请去除 !strip 选项,并在包不编译为字节码时指定架构。