|
root / base / usr / src / tools / build-ocaml.sh
build-ocaml.sh Bash 386 lines 12.3 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/bin/bash
#
# build-ocaml.sh — Build the OCaml toolchain for Hammerhead
#
# Builds OCaml 5.4, dune, menhir, and support libraries from source.
# Called by hh-build as a pre-build toolchain step.
# Mirrors the Tier 3 logic in tools/bootstrap.sh.
#
# Usage: build-ocaml.sh [--jobs N] [--force]
#   --jobs N    Parallel make jobs (default: 4)
#   --force     Rebuild even if stamp file is current
#
# Environment (set by hh-build before calling):
#   SRC         — path to usr/src
#   CONTRIB     — path to usr/src/contrib ($SRC/contrib)
#   GNUC_ROOT   — GCC root (/usr or /usr/gcc/14)
#
# Source tarballs must be pre-extracted into:
#   $CONTRIB/ocaml/         — OCaml 5.4.0
#   $CONTRIB/dune/          — dune 3.20.2
#   $CONTRIB/menhir/        — menhir 20250912
#   $CONTRIB/ocaml-libs/    — findlib, ocamlbuild, and OCaml support libraries
#
# Installs to /usr/local (OCaml convention: /usr/local/bin, /usr/local/lib/ocaml)
#

set -euo pipefail

JOBS=4
FORCE=false

while [[ $# -gt 0 ]]; do
    case "$1" in
        --jobs)  JOBS="$2"; shift 2 ;;
        -j*)     JOBS="${1#-j}"; shift ;;
        --force) FORCE=true; shift ;;
        *)       echo "Unknown argument: $1" >&2; exit 1 ;;
    esac
done

# ── Paths ────────────────────────────────────────────────────────────────

: "${SRC:?SRC must be set}"
: "${CONTRIB:=$SRC/contrib}"
: "${GNUC_ROOT:=/usr}"

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# tools/bootstrap/patches/ contains illumos-specific patches for dune
REPO_ROOT="$(cd "$SCRIPT_DIR/../../../.." && pwd)"
PATCHES="$REPO_ROOT/tools/bootstrap/patches"

STAMPFILE="$SRC/tools/.ocaml-toolchain.stamp"

OCAML_SRC="$CONTRIB/ocaml"
DUNE_SRC="$CONTRIB/dune"
MENHIR_SRC="$CONTRIB/menhir"
OCAML_LIBS_SRC="$CONTRIB/ocaml-libs"

OCAML_PREFIX=/usr/local
OCAML_LIBDIR="$OCAML_PREFIX/lib/ocaml"

# ── Logging ──────────────────────────────────────────────────────────────

log()  { echo "==> [build-ocaml] $*"; }
log2() { echo "  -> $*"; }
die()  { echo "FATAL: $*" >&2; exit 1; }

# ── Stamp Check ──────────────────────────────────────────────────────────

if [[ "$FORCE" != "true" && -f "$STAMPFILE" ]]; then
    # Check if any source is newer than the stamp
    newest_source=0
    for d in "$OCAML_SRC" "$DUNE_SRC" "$MENHIR_SRC" "$OCAML_LIBS_SRC"; do
        if [[ -d "$d" ]]; then
            ts=$(stat -c %Y "$d" 2>/dev/null || stat -f %m "$d" 2>/dev/null || echo 0)
            (( ts > newest_source )) && newest_source=$ts
        fi
    done
    stamp_ts=$(stat -c %Y "$STAMPFILE" 2>/dev/null || stat -f %m "$STAMPFILE" 2>/dev/null || echo 0)
    if (( stamp_ts >= newest_source )); then
        log "OCaml toolchain up to date (stamp: $STAMPFILE)"
        exit 0
    fi
fi

# ── Verify Sources ───────────────────────────────────────────────────────

[[ -d "$OCAML_SRC" ]] || die "OCaml source not found at $OCAML_SRC"
[[ -d "$DUNE_SRC" ]]  || die "Dune source not found at $DUNE_SRC"

# ── Environment ──────────────────────────────────────────────────────────

export CC="$GNUC_ROOT/bin/gcc -m64"
export CXX="$GNUC_ROOT/bin/g++ -m64"
export AS="$GNUC_ROOT/bin/as --64"
export PATH="$OCAML_PREFIX/bin:$GNUC_ROOT/bin:$PATH"
export OCAMLLIB="$OCAML_LIBDIR"
export OCAMLFIND_DESTDIR="$OCAML_LIBDIR"

# ── simple_install helper (for .install file packages) ───────────────────

simple_install() {
    local install_file="$1"
    local prefix="$2"
    local section="" src dst

    [[ -f "$install_file" ]] || { log2 "No .install file: $install_file"; return 1; }

    while IFS= read -r line; do
        line="${line#"${line%%[![:space:]]*}"}"
        [[ -z "$line" || "$line" == "]" ]] && continue

        if [[ "$line" =~ ^([a-z_]+):$ ]] || [[ "$line" =~ ^([a-z_]+):\ *\[$ ]]; then
            section="${BASH_REMATCH[1]}"
            continue
        fi

        if [[ "$line" =~ ^\"([^\"]+)\" ]]; then
            src="${BASH_REMATCH[1]}"
            dst=""
            if [[ "$line" =~ \{\"([^\"]+)\"\} ]]; then
                dst="${BASH_REMATCH[1]}"
            else
                dst="$(basename "$src")"
            fi

            local target_dir
            case "$section" in
                lib)      target_dir="$prefix/lib/ocaml/$(basename "${install_file%.install}")" ;;
                stublibs) target_dir="$prefix/lib/ocaml/stublibs" ;;
                bin)      target_dir="$prefix/bin" ;;
                doc)      target_dir="$prefix/doc/$(basename "${install_file%.install}")" ;;
                man)      target_dir="$prefix/man" ;;
                *)        continue ;;
            esac

            if [[ -f "$src" ]]; then
                mkdir -p "$target_dir/$(dirname "$dst")"
                cp -f "$src" "$target_dir/$dst"
            fi
        fi
    done < "$install_file"
}

# ── Build OCaml ──────────────────────────────────────────────────────────

build_ocaml() {
    log "Building OCaml 5.4.0"
    cd "$OCAML_SRC"

    export LDFLAGS="${LDFLAGS:-} -Wl,--export-dynamic"

    ./configure --prefix="$OCAML_PREFIX"
    gmake -j"$JOBS" world.opt
    gmake install

    # Fix bytecode script shebangs
    log2 "Fixing OCaml bytecode shebangs"
    for f in "$OCAML_PREFIX"/bin/*; do
        if head -1 "$f" 2>/dev/null | grep -q "#!/usr/local/bin/ocamlrun"; then
            : # Shebangs are correct for installed location
        fi
    done
}

# ── Build findlib ────────────────────────────────────────────────────────

build_findlib() {
    local src="$OCAML_LIBS_SRC/findlib-1.9.8"
    [[ -d "$src" ]] || { log2 "findlib source not found, skipping"; return; }
    log "Building ocaml-findlib 1.9.8"
    cd "$src"

    ./configure \
        -bindir "$OCAML_PREFIX/bin" \
        -sitelib "$OCAML_LIBDIR" \
        -mandir "$OCAML_PREFIX/man" \
        -config "$OCAML_LIBDIR/findlib.conf" \
        -no-custom
    gmake all
    gmake opt
    gmake install
}

# ── Build ocamlbuild ────────────────────────────────────────────────────

build_ocamlbuild() {
    local src="$OCAML_LIBS_SRC/ocamlbuild-0.15.0"
    [[ -d "$src" ]] || { log2 "ocamlbuild source not found, skipping"; return; }
    log "Building ocaml-ocamlbuild 0.15.0"
    cd "$src"

    gmake -f configure.make all \
        OCAMLBUILD_PREFIX="$OCAML_PREFIX" \
        OCAMLBUILD_BINDIR="$OCAML_PREFIX/bin" \
        OCAMLBUILD_LIBDIR="$OCAML_LIBDIR" \
        OCAMLBUILD_MANDIR="$OCAML_PREFIX/man"
    gmake
    gmake install \
        PREFIX="$OCAML_PREFIX" \
        BINDIR="$OCAML_PREFIX/bin" \
        LIBDIR="$OCAML_LIBDIR" \
        MANDIR="$OCAML_PREFIX/man" \
        CHECK_IF_PREINSTALLED=false
}

# ── Build dune ───────────────────────────────────────────────────────────

build_dune() {
    log "Building dune 3.20.2"
    cd "$DUNE_SRC"

    # Apply illumos patches
    log2 "Applying illumos patches"
    for path in \
        "otherlibs/stdune/src/readdir.c" \
        "otherlibs/stdune/dune_filesystem_stubs/readdir.c"; do
        if [[ -f "$path" && -f "$PATCHES/dune-readdir.c" ]]; then
            cp "$PATCHES/dune-readdir.c" "$path"
            log2 "  Replaced $path"
            break
        fi
    done

    local winsize="vendor/notty/src-unix/native/winsize.c"
    if [[ -f "$winsize" && -f "$PATCHES/dune-winsize.c" ]]; then
        cp "$PATCHES/dune-winsize.c" "$winsize"
        log2 "  Replaced $winsize"
    fi

    local wait4="otherlibs/stdune/src/wait4_stubs.c"
    if [[ -f "$wait4" && -f "$PATCHES/dune-wait4_stubs.c" ]]; then
        cp "$PATCHES/dune-wait4_stubs.c" "$wait4"
        log2 "  Replaced $wait4"
    fi

    ocaml boot/bootstrap.ml
    gmake release
    gmake install PREFIX="$OCAML_PREFIX"
}

# ── Build OCaml support libraries ────────────────────────────────────────

build_topkg() {
    local src="$OCAML_LIBS_SRC/topkg-1.1.1"
    [[ -d "$src" ]] || return
    log "Building ocaml-topkg 1.1.1"
    cd "$src"
    ocaml pkg/pkg.ml build --pkg-name topkg
    simple_install topkg.install "$OCAML_PREFIX"
}

build_seq() {
    log "Installing ocaml-seq META file"
    mkdir -p "$OCAML_LIBDIR/seq"
    cat > "$OCAML_LIBDIR/seq/META" << 'SEQEOF'
description = "OCaml Seq iterator type (part of stdlib)"
version = "base"
requires = ""
SEQEOF
}

build_stdlib_shims() {
    local src="$OCAML_LIBS_SRC/stdlib-shims-0.3.0"
    [[ -d "$src" ]] || return
    log "Building ocaml-stdlib-shims 0.3.0"
    cd "$src"
    dune build
    dune install --prefix="$OCAML_PREFIX"
}

build_syntax_shims() {
    local src="$OCAML_LIBS_SRC/ocaml-syntax-shims-1.0.0"
    [[ -d "$src" ]] || return
    log "Building ocaml-syntax-shims 1.0.0"
    cd "$src"
    dune build
    dune install --prefix="$OCAML_PREFIX"
}

build_cmdliner() {
    local src="$OCAML_LIBS_SRC/cmdliner-1.3.0"
    [[ -d "$src" ]] || return
    log "Building ocaml-cmdliner 1.3.0"
    cd "$src"
    mkdir -p _build
    gmake -j1 all PREFIX="$OCAML_PREFIX"
    gmake install \
        LIBDIR="$OCAML_LIBDIR/cmdliner" \
        DOCDIR="$OCAML_PREFIX/doc/cmdliner"
    gmake install-doc \
        LIBDIR="$OCAML_LIBDIR/cmdliner" \
        DOCDIR="$OCAML_PREFIX/doc/cmdliner"
}

build_astring() {
    local src="$OCAML_LIBS_SRC/astring-0.8.5"
    [[ -d "$src" ]] || return
    log "Building ocaml-astring 0.8.5"
    cd "$src"
    ocaml pkg/pkg.ml build
    simple_install astring.install "$OCAML_PREFIX"
}

build_fmt() {
    local src="$OCAML_LIBS_SRC/fmt-0.9.0"
    [[ -d "$src" ]] || return
    log "Building ocaml-fmt 0.9.0"
    cd "$src"
    ocaml pkg/pkg.ml build
    simple_install fmt.install "$OCAML_PREFIX"
}

build_uutf() {
    local src="$OCAML_LIBS_SRC/uutf-1.0.3"
    [[ -d "$src" ]] || return
    log "Building ocaml-uutf 1.0.3"
    cd "$src"
    ocaml pkg/pkg.ml build
    simple_install uutf.install "$OCAML_PREFIX"
}

build_re() {
    local src="$OCAML_LIBS_SRC/ocaml-re-1.12.0"
    [[ -d "$src" ]] || return
    log "Building ocaml-re 1.12.0"
    cd "$src"
    dune build -p re
    dune install re --prefix="$OCAML_PREFIX"
}

build_alcotest() {
    local src="$OCAML_LIBS_SRC/alcotest-1.8.0"
    [[ -d "$src" ]] || return
    log "Building ocaml-alcotest 1.8.0"
    cd "$src"

    # Patch for illumos: add sys/termios.h for struct winsize
    if [[ -f src/alcotest/alcotest_stubs.c ]]; then
        if ! grep -q 'sys/termios.h' src/alcotest/alcotest_stubs.c; then
            sed -i 's|#include <sys/ioctl.h>|#include <sys/ioctl.h>\n#include <sys/termios.h>|' \
                src/alcotest/alcotest_stubs.c
            log2 "Patched alcotest_stubs.c for illumos"
        fi
    fi

    dune build -p alcotest
    dune install alcotest --prefix="$OCAML_PREFIX"
}

build_menhir() {
    [[ -d "$MENHIR_SRC" ]] || { log2 "menhir source not found, skipping"; return; }
    log "Building menhir 20250912"
    cd "$MENHIR_SRC"
    dune build @install
    dune install --prefix="$OCAML_PREFIX"
}

# ── Main ─────────────────────────────────────────────────────────────────

log "Building OCaml toolchain"
log2 "OCAML_SRC=$OCAML_SRC"
log2 "DUNE_SRC=$DUNE_SRC"
log2 "PREFIX=$OCAML_PREFIX"

build_ocaml
build_findlib
build_ocamlbuild
build_dune
build_topkg
build_seq
build_stdlib_shims
build_syntax_shims
build_cmdliner
build_astring
build_fmt
build_uutf
build_re
build_alcotest
build_menhir

# Write stamp file
mkdir -p "$(dirname "$STAMPFILE")"
date > "$STAMPFILE"
log "OCaml toolchain complete (stamp: $STAMPFILE)"