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 # shellcheck disable=SC1090
43 source "$SOURCE_ROOT/opam_helpers.sh"
44 # shellcheck disable=SC1090
45 source "$SOURCE_ROOT/ocaml_deps_data.sh"
47 # Shamelessly copied from
48 # https://github.com/facebook/infer/blob/master/scripts/opam_utils.sh
49 # Many thanks to the infer team :D
51 # assumes opam is available and initialized
52 opam_switch_create_if_needed
() {
55 local switch_exists
=no
56 for installed_switch
in $
(opam switch list
--short); do
57 if [ "$installed_switch" == "$name" ]; then
62 if [ "$switch_exists" = "no" ]; then
63 opam switch create
"$name" "$switch"
68 opam_require_version_2
73 HACK_OPAM_SWITCH
="${HACK_OCAML_VERSION}"
74 HACK_OPAM_DEFAULT_NAME
="hack-switch"
75 HACK_OPAM_NAME
=${HACK_OPAM_NAME:-$HACK_OPAM_DEFAULT_NAME}
76 SKIP_MINI_REPO
=${SKIP_MINI_REPO:-0}
77 OCAML_PATCH
=${OCAML_PATCH:-""}
79 if [[ "${SKIP_MINI_REPO}" -eq 1 ]]; then
80 echo "SKIP_MINI_REPO is set."
81 echo "This setup will fetch from the internet."
82 echo "Make sure you know what you are doing."
83 export http_proxy
=http
://fwdproxy
:8080
84 export https_proxy
=http
://fwdproxy
:8080
87 MINI_REPO_FETCH_SCRIPT
="${SOURCE_ROOT}/facebook/fetch_opam2_repo_hack.sh"
89 # OSS does not provide bubblewrap yet so we disable it
90 if [[ -f "${MINI_REPO_FETCH_SCRIPT}" && "${SKIP_MINI_REPO}" -eq 0 ]]; then
91 MINI_REPO_DIR
="$("${MINI_REPO_FETCH_SCRIPT}")"
92 MINI_REPO_TARBALL
="${MINI_REPO_DIR}.tar.gz"
93 rm -rf "$MINI_REPO_DIR" ||
:
94 TARGET_OPAM_DIR
="$SOURCE_ROOT/facebook/$(basename "$MINI_REPO_DIR")"
95 mkdir
"$TARGET_OPAM_DIR"
96 tar xzf
"$MINI_REPO_TARBALL" -C "$TARGET_OPAM_DIR"
97 opam init
--disable-sandboxing --reinit offline_clone
"$MINI_REPO_DIR" --no-setup --bare
99 opam init
--disable-sandboxing --reinit --no-setup --bare
102 opam_switch_create_if_needed
"$HACK_OPAM_NAME" "$HACK_OPAM_SWITCH"
103 opam switch
"$HACK_OPAM_NAME"
106 opam
install "${HACK_OPAM_DEPS[@]}"
108 dune_version
=$
(dune
--version)
110 echo "opam switch correctly installed at $OPAMROOT"
111 echo "dune version is $dune_version"