|
| 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 | +help_exit() { |
| 11 | + echo "Usage: $0 dynamic|static linux|macosx [extra-libs]" >&2 |
| 12 | + exit 2 |
| 13 | +} |
| 14 | + |
| 15 | +[ $# -lt 2 ] && help_exit |
| 16 | + |
| 17 | +echo ";; generated by $0" |
| 18 | + |
| 19 | +case "$1" in |
| 20 | + dynamic) echo "()"; exit 0;; |
| 21 | + static) ;; |
| 22 | + *) echo "Invalid linking mode '$1'."; help_exit |
| 23 | +esac |
| 24 | + |
| 25 | +shift |
| 26 | +case "$1" in |
| 27 | + macosx) shift; EXTRA_LIBS="curses $*";; |
| 28 | + linux) shift; EXTRA_LIBS="$*";; |
| 29 | + --) shift; EXTRA_LIBS="$*";; |
| 30 | + *) echo "Not supported %{ocamlc-config:system} '$1'."; help_exit |
| 31 | +esac |
| 32 | + |
| 33 | +## Static linking configuration ## |
| 34 | + |
| 35 | +# The linked C libraries list may need updating on changes to the dependencies. |
| 36 | +# |
| 37 | +# To get the correct list for manual linking, the simplest way is to set the |
| 38 | +# flags to `-verbose`, while on the normal `autolink` mode, then extract them |
| 39 | +# from the gcc command-line. |
| 40 | +# The Makefile contains a target to automate this: `make detect-libs`. |
| 41 | + |
| 42 | +case $(uname -s) in |
| 43 | + Linux) |
| 44 | + case $(. /etc/os-release && echo $ID) in |
| 45 | + alpine) |
| 46 | + COMMON_LIBS="camlstr base_stubs ssl_threads_stubs ssl crypto cstruct_stubs lwt_unix_stubs bigarray unix c" |
| 47 | + # `m` and `pthread` are built-in musl |
| 48 | + echo '(-noautolink' |
| 49 | + echo ' -cclib -Wl,-Bstatic' |
| 50 | + echo ' -cclib -static-libgcc' |
| 51 | + for l in $EXTRA_LIBS $COMMON_LIBS; do |
| 52 | + echo " -cclib -l$l" |
| 53 | + done |
| 54 | + echo ' -cclib -static)' |
| 55 | + ;; |
| 56 | + *) |
| 57 | + echo "Error: static linking is only supported in Alpine, to avoids glibc constraints" >&2 |
| 58 | + exit 3 |
| 59 | + esac |
| 60 | + ;; |
| 61 | + Darwin) |
| 62 | + 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" |
| 63 | + # `m` and `pthread` are built-in in libSystem |
| 64 | + echo '(-noautolink' |
| 65 | + for l in $EXTRA_LIBS $COMMON_LIBS; do |
| 66 | + if [ "${l%.a}" != "${l}" ]; then echo " -cclib $l" |
| 67 | + else echo " -cclib -l$l" |
| 68 | + fi |
| 69 | + done |
| 70 | + echo ')' |
| 71 | + ;; |
| 72 | + *) |
| 73 | + echo "Static linking is not supported for your platform. See $0 to contribute." >&2 |
| 74 | + exit 3 |
| 75 | +esac |
0 commit comments