Skip to content

Commit

Permalink
Proper linking flags!
Browse files Browse the repository at this point in the history
  • Loading branch information
denismerigoux committed Mar 20, 2024
1 parent 8ffadb3 commit d698667
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
5 changes: 2 additions & 3 deletions makefiles/mlang.mk
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ dune: FORCE
ifeq ($(call is_in,),)
$(call make_in,,$@)
else
dune build $(DUNE_OPTIONS)
LINKING_MODE=$(LINKING_MODE) dune build $(DUNE_OPTIONS)
endif

build: FORCE | format dune

# Run only in an opam switch with musl and static options activated
build-static: DUNE_OPTIONS+=--profile=static
build-static: LINKING_MODE=static
build-static: FORCE build

##################################################
Expand Down
14 changes: 9 additions & 5 deletions src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
(flags
(:standard -warn-error -A))))

(rule
(with-stdout-to
linking-flags-mlang.sexp
(run ./gen-linking-flags.sh %{env:LINKING_MODE=dynamic}
%{ocaml-config:system})))

(executable
(name main)
(package mlang)
(public_name mlang)
(libraries mlang)
(flags
(:standard -verbose -noautolink -cclib -Wl,-Bstatic -cclib -lgmp_caml
-cclib -lmpfr -cclib -lgmp -cclib -lcamlidl -cclib -lnums -cclib
-lthreadsnat -cclib -lparmap_stubs -cclib -lANSITerminal_stubs -cclib
-Wl,-Bdynamic -cclib -lpthread -cclib -lunix -cclib -ldl)))
(:standard
(:include linking-flags-mlang.sexp)))
(libraries mlang))
41 changes: 41 additions & 0 deletions src/gen-linking-flags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/sh
set -ue

# Copied on https://ocamlpro.com/fr/blog/2021_09_02_generating_static_and_portable_executables_with_ocaml/

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

case "$LINKING_MODE" in
'')
;; # No extra flags needed
static)
case "$OS" in
linux)
FLAGS="-noautolink"
CCLIB="-Wl,-Bstatic -lgmp_caml -lmpfr -lgmp -lnums \
-lthreadsnat -lparmap_stubs -lANSITerminal_stubs \
-Wl,-Bdynamic -lpthread -lunix"
LIBS=""
OCAML_LIBS="camlidl"
for lib in $LIBS; do
CCLIB="$CCLIB $(pkg-config $lib --variable libdir)/$lib.a"
done
for lib in $OCAML_LIBS; do
CCLIB="$CCLIB $(ocamlfind query $lib)/lib$lib.a"
done;;
*)
echo "No known static compilation flags for '$OS'" >&2
exit 1
esac;;
*)
echo "Invalid linking mode '$LINKING_MODE'" >&2
exit 2
esac

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

0 comments on commit d698667

Please sign in to comment.