Skip to content

Commit 0222a4a

Browse files
committed
Static builds: use an environment variable rather than files laying around
Thanks @gasche for the hint about this hidden dune feature :)
1 parent a59798b commit 0222a4a

File tree

5 files changed

+70
-72
lines changed

5 files changed

+70
-72
lines changed

.github/workflows/static-builds.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ jobs:
5252
opam switch create . ocaml-base-compiler 'dune<2' --deps-only
5353
- name: Build the binaries
5454
run: |
55-
./scripts/setup-static-build.sh
56-
opam exec -- make
55+
opam exec -- make LINKING_MODE=static
5756
otool -L _build/install/default/bin/*
5857
- name: Archive static binaries
5958
uses: actions/upload-artifact@v2

scripts/setup-static-build.sh

Lines changed: 0 additions & 62 deletions
This file was deleted.

scripts/static-build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ tar c $(git ls-files) | \
1111
docker run --rm -i \
1212
ocamlpro/ocaml:4.05 \
1313
sh -uexc \
14-
'tar x >&2 && ./scripts/setup-static-build.sh &&
14+
'tar x >&2 &&
1515
opam switch create . ocaml-system "dune<2" --deps-only >&2 &&
16-
opam exec make >&2 &&
16+
opam exec make LINKING_MODE=static >&2 &&
1717
tar c -hC _build/install/default/bin .' | \
1818
tar vx

src/main/dune

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@
5656

5757
(rule
5858
(targets linking_main.sexp)
59-
(mode fallback)
60-
(action (with-stdout-to %{targets} (echo "()"))))
59+
(action (with-stdout-to %{targets}
60+
(run ./linking_flags.sh %{env:LINKING_MODE=dynamic} laolao_stubs threads camlrun))))
6161
(rule
6262
(targets linking_client.sexp)
63-
(mode fallback)
64-
(action (with-stdout-to %{targets} (echo "()"))))
63+
(action (with-stdout-to %{targets}
64+
(run ./linking_flags.sh %{env:LINKING_MODE=dynamic} threads camlrun))))
6565
(rule
6666
(targets linking_server.sexp)
67-
(mode fallback)
68-
(action (with-stdout-to %{targets} (echo "()"))))
67+
(action (with-stdout-to %{targets}
68+
(run ./linking_flags.sh %{env:LINKING_MODE=dynamic} laolao_stubs threadsnat))))

src/main/linking_flags.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/sh
2+
set -ue
3+
4+
# This script is called by dune to generate the linking flags for static builds
5+
# (on the limited set of supported platforms). It only returns an empty set of
6+
# flags for the default dynamic linking mode.
7+
8+
LC_ALL=C
9+
10+
echo ";; generated by $0"
11+
12+
case "$1" in
13+
dynamic) echo "()"; exit 0;;
14+
static) ;;
15+
*) echo "Invalid linking mode '$1'. Usage: $0 dynamic|static [extra-libs]" >&2; exit 2
16+
esac
17+
18+
shift
19+
EXTRA_LIBS="$*"
20+
21+
## Static linking configuration ##
22+
23+
# The linked C libraries list may need updating on changes to the dependencies.
24+
#
25+
# To get the correct list for manual linking, the simplest way is to set the
26+
# flags to `-verbose`, while on the normal `autolink` mode, then extract them
27+
# from the gcc command-line.
28+
29+
case $(uname -s) in
30+
Linux)
31+
case $(. /etc/os-release && echo $ID) in
32+
alpine)
33+
COMMON_LIBS="camlstr base_stubs ssl_threads_stubs ssl crypto cstruct_stubs lwt_unix_stubs bigarray unix c"
34+
# `m` and `pthreads` are built-in musl
35+
echo '(-noautolink'
36+
echo ' -cclib -Wl,-Bstatic'
37+
echo ' -cclib -static-libgcc'
38+
for l in $EXTRA_LIBS $COMMON_LIBS; do
39+
echo " -cclib -l$l"
40+
done
41+
echo ' -cclib -static)'
42+
;;
43+
*)
44+
echo "Error: static linking is only supported in Alpine, to avoids glibc constraints" >&2
45+
exit 3
46+
esac
47+
;;
48+
Darwin)
49+
COMMON_LIBS="camlstr base_stubs ssl_threads_stubs /usr/local/opt/openssl/lib/libssl.a /usr/local/opt/openssl/lib/libcrypto.a cstruct_stubs lwt_unix_stubs bigarray unix pthread"
50+
echo '(-noautolink'
51+
for l in $EXTRA_LIBS $COMMON_LIBS; do
52+
if [ "${l%.a}" != "${l}" ]; then echo " -cclib $l"
53+
else echo " -cclib -l$l"
54+
fi
55+
done
56+
echo ')'
57+
;;
58+
*)
59+
echo "Static linking is not supported for your platform. See $0 to contribute." >&2
60+
exit 3
61+
esac

0 commit comments

Comments
 (0)