Skip to content

Commit

Permalink
feat: Statically linked binary releases
Browse files Browse the repository at this point in the history
This patch modifies the Docker workflow to build a statically linked
portable binary of Alt-Ergo, following [1].

Building the Linux binaries is fairly quick and hence performed on
push/pull_request. It can also be manually requested.

[1] : https://ocamlpro.com/blog/2021_09_02_generating_static_and_portable_executables_with_ocaml/
  • Loading branch information
bclement-ocp committed Mar 20, 2024
1 parent adafb41 commit 21a9058
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
name: Build with ocp Docker container
name: Build statically linked binaries

# The goal of this workflow is to test the installation of the project with opam
# on a specific light docker container instead of using default GH-actions one.
on:
workflow_dispatch:
pull_request:
push:
branches:
- fix-ci
- next
- main

jobs:
install_docker:
name: opam install on a specific docker container
musl:
name: Build statically linked binary with musl

runs-on: ubuntu-latest

# Retrieve and use a light docker container on which ocaml 4.10 is installed
# Retrieve and use a light docker container on which ocaml 4.14 is installed
# as a system compiler. This container also contains opam 2.1.
container:
image: ocamlpro/ocaml:4.10
image: ocamlpro/ocaml:4.14-flambda
options: --user root

steps:

# Checkout the code of the current branch
- name: Checkout code
uses: actions/checkout@v4

# Switch to ocaml user
- run: su ocaml
- name: Switch to ocaml user
run: su ocaml

# This line is needed to acces and use opam. We are unable to set the user
# to `ocaml` with the container parameters
Expand All @@ -38,12 +36,19 @@ jobs:
# the ocaml user do not have rights on it.
- run: CURRENTDIR=$(basename $(pwd)); git config --global --add safe.directory /__w/$CURRENTDIR/$CURRENTDIR

- name: Update opam repository
run: opam update
- name: Install static dependencies
run: sudo apk add zlib-static

# Create a switch with the system compiler (no compilation needed).
# And then, install external (no need for depext with opam 2.1) and libs deps.
- run: opam switch create . ocaml-system --locked --deps-only --ignore-constraints-on alt-ergo-lib,alt-ergo-parsers

# Install the project packages
- run: opam install . --locked
- run: opam exec -- dune subst

- name: Build statically linked binary
run: LINK_MODE=static opam exec -- dune build --release src/bin/text/Main_text.exe

- run: cp src/bin/text/Main_text.exe src/bin/text/alt-ergo

- uses: actions/upload-artifact@v4
with:
name: alt-ergo-x86_64-linux-musl
path: src/bin/text/alt-ergo
6 changes: 5 additions & 1 deletion src/bin/text/dune
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
(documentation
(package alt-ergo))

(rule
(with-stdout-to link_flags.dune
(run ./gen-link-flags.sh %{env:LINK_MODE=dynamic} %{ocaml-config:system})))

(executable
(name Main_text)
(public_name alt-ergo)
(package alt-ergo)
(libraries alt_ergo_common)
(link_flags (:include flags.dune))
(link_flags (:standard (:include link_flags.dune)))
(modules Main_text)
(promote (until-clean)))

Expand Down
1 change: 0 additions & 1 deletion src/bin/text/flags.dune

This file was deleted.

54 changes: 54 additions & 0 deletions src/bin/text/gen-link-flags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh

set -ue

LINK_MODE="$1"
OS="$2"
FLAGS=
CCLIB=

case "$LINK_MODE" in
dynamic)
;; # No extra flags needed
static)
case "$OS" in
linux)
CCLIB="-static -no-pie";;
*)
echo "No known static compilation flags for '$OS'" >&2
exit 1
esac;;
mixed)
FLAGS="-noautolink"
CCLIB="-lstdcompat_stubs -lcamlzip -lnums -lzarith -lcamlstr -lunix -lz"
LIBS="gmp"
case "$OS" in
linux)
for lib in $LIBS; do
CCLIB="$CCLIB -l$lib"
done
CCLIB="-Wl,-Bstatic $CCLIB -Wl,-Bdynamic";;
macosx)
for lib in $LIBS; do
if [[ $lib == lib* ]]; then
archive="$lib.a"
else
archive="lib$lib.a"
fi
CCLIB="$CCLIB $(pkg-config $lib --variable libdir)/$archive"
done;;
*)
echo "No known mixed compilation flags for '$OS'" >&2
exit 1
esac;;

*)
echo "Invalid link mode '$LINK_MODE'" >&2
exit 2
esac

echo '('
echo ' -linkall'
for f in $FLAGS; do echo " $f"; done
for f in $CCLIB; do echo " -cclib $f"; done
echo ')'

0 comments on commit 21a9058

Please sign in to comment.