3 # Copyright (c) 2017, Facebook, Inc.
6 # This source code is licensed under the MIT license found in the
7 # LICENSE file in the "hack" directory of this source tree.
14 echo "Usage: $0 /path/to/bin/opam [/path/to/build/dir]"
18 SOURCE_ROOT
="$(dirname "$0")"
20 BUILD_ROOT
="${2:-"${SOURCE_ROOT}/_build"}"
22 OPAM_EXECUTABLE_DIR
="$(dirname "$OPAM_EXECUTABLE")"
24 export PATH
="$OPAM_EXECUTABLE_DIR:$PATH"
25 # detect if we are building inside FB by checking a specific dune file
26 if [ -e "$SOURCE_ROOT/src/facebook/dune" ]; then
27 # FB script must have already set OPAMROOT, and we reuse it
29 if [ -z ${OPAMROOT+x} ]; then
30 echo "OPAMROOT must be set by dune.sh"
33 echo "OPAMROOT = $OPAMROOT"
36 OPAMROOT
="${BUILD_ROOT}/opam"
38 export OPAMROOT
="$OPAMROOT"
42 # Prevents opam from trying to invoke brew install and dpkg, because all the
43 # dependencies should have been installed by Nix, Apt or Homebrew.
44 export OPAMASSUMEDEPEXTS
="1"
45 export OPAMNODEPEXTS
="1"
47 # shellcheck disable=SC1090
48 source "$SOURCE_ROOT/opam_helpers.sh"
49 # shellcheck disable=SC1090
50 source "$SOURCE_ROOT/ocaml_deps_data.sh"
52 # Shamelessly copied from
53 # https://github.com/facebook/infer/blob/master/scripts/opam_utils.sh
54 # Many thanks to the infer team :D
56 # assumes opam is available and initialized
57 opam_switch_create_if_needed
() {
60 local switch_exists
=no
61 for installed_switch
in $
(opam switch list
--short); do
62 if [ "$installed_switch" == "$name" ]; then
67 if [ "$switch_exists" = "no" ]; then
68 opam switch create
"$name" "$switch"
73 opam_require_version_2
78 HACK_OPAM_SWITCH
="${HACK_OCAML_VERSION}"
79 HACK_OPAM_DEFAULT_NAME
="hack-switch"
80 HACK_OPAM_NAME
=${HACK_OPAM_NAME:-$HACK_OPAM_DEFAULT_NAME}
81 SKIP_MINI_REPO
=${SKIP_MINI_REPO:-0}
82 OCAML_PATCH
=${OCAML_PATCH:-""}
84 if [[ "${SKIP_MINI_REPO}" -eq 1 ]]; then
85 echo "SKIP_MINI_REPO is set."
86 echo "This setup will fetch from the internet."
87 echo "Make sure you know what you are doing."
88 export http_proxy
=http
://fwdproxy
:8080
89 export https_proxy
=http
://fwdproxy
:8080
92 MINI_REPO_FETCH_SCRIPT
="${SOURCE_ROOT}/facebook/fetch_opam2_repo_hack.sh"
94 # OSS does not provide bubblewrap yet so we disable it
95 if [[ -f "${MINI_REPO_FETCH_SCRIPT}" && "${SKIP_MINI_REPO}" -eq 0 ]]; then
96 MINI_REPO_DIR
="$("${MINI_REPO_FETCH_SCRIPT}")"
97 MINI_REPO_TARBALL
="${MINI_REPO_DIR}.tar.gz"
98 rm -rf "$MINI_REPO_DIR" ||
:
99 TARGET_OPAM_DIR
="$SOURCE_ROOT/facebook/$(basename "$MINI_REPO_DIR")"
100 mkdir
"$TARGET_OPAM_DIR"
101 tar xzf
"$MINI_REPO_TARBALL" -C "$TARGET_OPAM_DIR"
102 opam init
--disable-sandboxing --reinit offline_clone
"$MINI_REPO_DIR" --no-setup --bare
104 opam init
--disable-sandboxing --reinit --no-setup --bare
107 opam_switch_create_if_needed
"$HACK_OPAM_NAME" "$HACK_OPAM_SWITCH"
108 opam switch
"$HACK_OPAM_NAME"
111 opam
install "${HACK_OPAM_DEPS[@]}"
113 dune_version
=$
(dune
--version)
115 echo "opam switch correctly installed at $OPAMROOT"
116 echo "dune version is $dune_version"