OCaml 打包準則 - Arch Linux 中文维基

跳至內容

# OCaml 打包準則

出自 Arch Linux 中文维基

本文或本節需要翻譯。要貢獻翻譯，請訪問簡體中文翻譯團隊。

附註： 尚有一些段落未翻譯完全（在 Talk:OCaml 打包準則# 中討論）
翻譯狀態：

- 本文（或部分內容）譯自 OCaml package guidelines，最近一次同步於 2025-07-14，若英文版本有所更改，則您可以幫助同步與翻譯更改的內容。
- 您可以在 ArchWiki 的對應頁面找到本文翻譯的原始修訂歷史。
- 本文可能與英文原文存在出入。

Arch 打包準則

32 位 – 安全 – CLR – CMake – DKMS – Eclipse – Electron – Free Pascal – GNOME – Go – Haskell – Java – 交叉編譯工具 – KDE – Lisp – Meson – MinGW – 內核模塊 – Node.js – Nonfree – OCaml – Perl – PHP – Python – R – Ruby – Rust – Shell – VCS – Web – Wine – 字體

為使用 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 "${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 "${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 "${pkgname}-${pkgver}"

       DESTDIR="${pkgdir}" dune install --prefix "/usr" --libdir "/usr/lib/ocaml" --docdir "/usr/share/doc"

## 使用 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 "${pkgname}-${pkgver}"
      mkdir -p "$OCAMLFIND_DESTDIR"
      ./configure --prefix=/usr
      make
    }

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

需要留意的是許多 OCaml 包都需要給 `make` 與 `make install` 一些額外信息。同時，請去除 `!strip` 選項，並在包不編譯為字節碼時指定架構。

分類：​

- 包開發

隱藏分類：​

- 使用「模板:Translateme」標記的頁面或章節

OCaml 打包準則
新增話題