interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / om / tls / build.sh

97 lines · 3382 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# om/tls/build.sh -- compatibility entry point over pure seeded-Omega seed/build/tlsbuild.om.
4# Usage: sh om/tls/build.sh OMC PROGRAM.om OUT
5# sh om/tls/build.sh -p OMC BIN
6# Works both in-tree and when copied into an isolated mutant scratch directory, preserves caller
7# relative paths (no cd), and honors OMTLS_RUN.
8set -eu
9HERE=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
10
11# Locate seed/build without assuming dirname($0)/../.. is the repository root.
12SB=""
13d="$HERE"
14while :; do
15 if [ -f "$d/seed/build/common.om" ] && [ -f "$d/seed/build/tlsbuild.om" ]; then
16 SB="$d/seed/build"
17 break
18 fi
19 parent=$(dirname "$d")
20 [ "$parent" = "$d" ] && break
21 d="$parent"
22done
23if [ -z "$SB" ] && [ -f "$PWD/seed/build/common.om" ] && [ -f "$PWD/seed/build/tlsbuild.om" ]; then
24 SB="$PWD/seed/build"
25fi
26if [ -z "$SB" ] && [ -n "${OMEGA_ROOT:-}" ] && [ -f "$OMEGA_ROOT/seed/build/common.om" ] && [ -f "$OMEGA_ROOT/seed/build/tlsbuild.om" ]; then
27 SB="$OMEGA_ROOT/seed/build"
28fi
29
30# Resolve OMTLS_RUN to an executable path when set (execprog does not search PATH).
31RUN_BIN=""
32if [ -n "${OMTLS_RUN:-}" ]; then
33 case "$OMTLS_RUN" in
34 /*) RUN_BIN="$OMTLS_RUN" ;;
35 *) RUN_BIN=$(command -v "$OMTLS_RUN" 2>/dev/null || printf '%s' "$OMTLS_RUN") ;;
36 esac
37fi
38
39OMC_ARG="${1:-}"
40if [ "$OMC_ARG" = "-p" ]; then OMC_ARG="${2:-}"; fi
41
42# Use pure seeded-Omega tlsbuild.om when seed/build is available, OMC is a single executable path,
43# and we can execute AArch64 binaries (native Linux-aarch64 or via OMTLS_RUN).
44can_run_om=0
45if [ -n "$SB" ] && [ -n "$OMC_ARG" ] && [ -x "$OMC_ARG" ]; then
46 if [ "$(uname -s)-$(uname -m)" = "Linux-aarch64" ]; then
47 can_run_om=1
48 fi
49fi
50
51if [ "$can_run_om" -eq 1 ]; then
52 BIN="${TMPDIR:-/tmp}/om-tlsbuild.$$"
53 trap 'rm -f "$BIN"' EXIT INT TERM
54 cat "$SB/common.om" "$SB/tlsbuild.om" | "$OMC_ARG" > "$BIN"
55 chmod 755 "$BIN"
56 rc=0
57 OMTLS_DIR="$HERE" OMTLS_RUN="$RUN_BIN" "$BIN" "$@" || rc=$?
58 exit "$rc"
59fi
60
61# Standalone / cross-runner fallback (e.g. isolated temp copy without seed/build or multi-word OMC).
62units() { for u in sha2 hkdf drbg x25519 sha3 mlkem chacha aesgcm ec ecdh rsa x509 rev fips tls; do cat "$HERE/$u.om"; done; }
63run_omc() {
64 if [ -x "$1" ]; then "$1"
65 else
66 # shellcheck disable=SC2086
67 $1
68 fi
69}
70integ() {
71 { printf 'om2\n'; units; printf 'fn main to int\nret fips_itool\nend\n'; } > "$2.itool.om"
72 if run_omc "$1" < "$2.itool.om" > "$2.itool" 2> "$2.itool.err"; then :; else
73 rc=$?; cat "$2.itool.err" >&2; rm -f "$2.itool" "$2.itool.om" "$2.itool.err"; return "$rc"
74 fi
75 chmod +x "$2.itool"
76 rc=0; ${OMTLS_RUN:+"$OMTLS_RUN"} "$2.itool" "$2" || rc=$?
77 rm -f "$2.itool" "$2.itool.om" "$2.itool.err"
78 [ "$rc" -eq 0 ] || echo "build.sh: integrity step failed on $2 (tool exit $rc)" >&2
79 return "$rc"
80}
81if [ "${1:-}" = -p ]; then
82 omc=${2:?usage: build.sh -p OMC BIN}; bin=${3:?usage: build.sh -p OMC BIN}
83 integ "$omc" "$bin"; exit
84fi
85omc=${1:?usage: build.sh OMC PROGRAM.om OUT}; prog=${2:?}; out=${3:?}
86{ printf 'om2\n'; units; cat "$prog"; } > "$out.unit.om"
87rm -f "$out"
88if run_omc "$omc" < "$out.unit.om" > "$out.tmp" 2> "$out.err"; then
89 rm -f "$out.err"
90else
91 rc=$?; rm -f "$out.tmp"; cat "$out.err" >&2; exit "$rc"
92fi
93if integ "$omc" "$out.tmp"; then
94 mv "$out.tmp" "$out"; chmod +x "$out"
95else
96 rc=$?; rm -f "$out.tmp"; exit "$rc"
97fi