blob: 3212193b60ecb0519580172b3d6e9344dcd474ba [file] [edit]
#!/usr/bin/env bash
# Lean compiler
#
# A simple wrapper around a C++ compiler. Defaults to the compiler Lean was built with,
# which can be overridden with the environment variable `LEAN_CXX`. All parameters are passed
# as-is to the wrapped compiler. Note that we use a C++ compiler purely for linking against
# the C++ standard library Lean depends on, all input files are treated as C code.
#
# Interesting options:
# * `-U LEAN_MULTI_THREAD` can be used to optimize programs not making use of multi-threading
# * `--print-cflags`: print C compiler flags necessary for building against the Lean runtime and exit
# * `--print-ldlags`: print C compiler flags necessary for statically linking against the Lean library and exit
# * Set the `LEANC_GMP` environment variable to a path to `libgmp.a` (or `-l:libgmp.a` on Linux) to link GMP statically.
# Beware of the licensing consequences since GMP is LGPL.
set -e
bindir=$(dirname $0)
cflags=("-I$bindir/../include" @LEANC_EXTRA_FLAGS@)
ldflags=("-L$bindir/../lib/lean" "${LEANC_GMP:--lgmp}")
ldflags_ext=(@LEANC_STATIC_LINKER_FLAGS@)
args=("$@")
for arg in "$@"; do
# passed -shared ~> switch to shared linker flags
[[ $arg == "-shared" ]] && ldflags_ext=(@LEANC_SHARED_LINKER_FLAGS@) && args=("-x" "c" "$@" "-x" "none")
# Note the `-x c` for treating all input as C code
[[ $arg == "-c" ]] && ldflags=() && ldflags_ext=() && args=("-x" "c" "$@" "-x" "none")
[[ $arg == "--print-cflags" ]] && echo "${cflags[@]}" && exit
[[ $arg == "--print-ldflags" ]] && echo "${cflags[@]} ${ldflags_ext[@]} ${ldflags[@]}" && exit
done
[ -n "$LEAN_CXX" ] || LEAN_CXX=c++
$LEAN_CXX "${cflags[@]}" "${args[@]}" "${ldflags_ext[@]}" "${ldflags[@]}" -Wno-unused-command-line-argument