Add logging for comparison behaviors
[hiphop-php.git] / hphp / hack / opam_setup.sh
blobb764401b0425e2f75689dd7636d498adb18f001f
1 #!/bin/bash
3 # Copyright (c) 2017, Facebook, Inc.
4 # All rights reserved.
6 # This source code is licensed under the MIT license found in the
7 # LICENSE file in the "hack" directory of this source tree.
9 set -euf
11 unset DUNE_BUILD_DIR
12 OCAML_PREFIX=$(dirname "$1")
13 SOURCE_ROOT="$2"
14 BUILD_ROOT="${3:-"${SOURCE_ROOT}/_build"}"
15 export PATH="$OCAML_PREFIX:$PATH"
16 # detect if we are building inside FB by checking a specific dune file
17 if [ -e "$SOURCE_ROOT/src/facebook/dune" ]; then
18 OPAMROOT="$SOURCE_ROOT/facebook/opam"
19 else
20 OPAMROOT="${BUILD_ROOT}/opam"
22 export OPAMROOT="$OPAMROOT"
23 mkdir -p "$OPAMROOT"
24 export OPAMYES="1"
26 # shellcheck disable=SC1090
27 source "$SOURCE_ROOT/opam_helpers.sh"
28 # shellcheck disable=SC1090
29 source "$SOURCE_ROOT/ocaml_deps_data.sh"
31 # Shamelessly copied from
32 # https://github.com/facebook/infer/blob/master/scripts/opam_utils.sh
33 # Many thanks to the infer team :D
35 # assumes opam is available and initialized
36 opam_switch_create_if_needed () {
37 local name=$1
38 local switch=$2
39 local switch_exists=no
40 for installed_switch in $(opam switch list --short); do
41 if [ "$installed_switch" == "$name" ]; then
42 switch_exists=yes
43 break
45 done
46 if [ "$switch_exists" = "no" ]; then
47 opam switch create "$name" "$switch"
48 eval "$(opam env)"
52 opam_require_version_2
54 # End of shame
57 HACK_OPAM_SWITCH="${HACK_OCAML_VERSION}"
58 HACK_OPAM_DEFAULT_NAME="hack-switch"
59 HACK_OPAM_NAME=${HACK_OPAM_NAME:-$HACK_OPAM_DEFAULT_NAME}
60 SKIP_MINI_REPO=${SKIP_MINI_REPO:-0}
61 OCAML_PATCH=${OCAML_PATCH:-""}
63 # building the right compiler name, depending on the version.
64 # It might be ocaml-base-compiler.VERSION
65 # or ocaml-variants.VERSION+OPTION
66 OCAML_COMPILER_NAME="ocaml"
67 OCAML_BASE_NAME="ocaml"
69 if [[ "$HACK_OCAML_VERSION" == *"+"* ]]; then
70 OCAML_BASE_NAME="$OCAML_BASE_NAME-variants"
71 OCAML_COMPILER_NAME="$OCAML_BASE_NAME.$HACK_OCAML_VERSION"
72 else
73 OCAML_BASE_NAME="$OCAML_BASE_NAME-base-compiler"
74 OCAML_COMPILER_NAME="$OCAML_BASE_NAME.$HACK_OCAML_VERSION"
77 if [[ "${SKIP_MINI_REPO}" -eq 1 ]]; then
78 echo "SKIP_MINI_REPO is set."
79 echo "This setup will fetch from the internet."
80 echo "Make sure you know what you are doing."
81 export http_proxy=http://fwdproxy:8080
82 export https_proxy=http://fwdproxy:8080
85 MINI_REPO_FETCH_SCRIPT="${SOURCE_ROOT}/facebook/fetch_opam2_repo_hack.sh"
87 # OSS does not provide bubblewrap yet so we disable it
88 if [[ -f "${MINI_REPO_FETCH_SCRIPT}" && "${SKIP_MINI_REPO}" -eq 0 ]]; then
89 MINI_REPO_DIR="$("${MINI_REPO_FETCH_SCRIPT}")"
90 MINI_REPO_TARBALL="${MINI_REPO_DIR}.tar.gz"
91 rm -rf "$MINI_REPO_DIR" ||:
92 tar xzf "$MINI_REPO_TARBALL" -C "$SOURCE_ROOT/facebook"
93 opam init --disable-sandboxing --reinit offline_clone "$MINI_REPO_DIR" --no-setup --bare
95 if [ -n "$OCAML_PATCH" ]; then
96 # Patching ocaml base compiler so we can pass it CFLAGS/LDFLAGS
97 # The goal is to make it use the right glibc, not the system one
98 opam switch create "$HACK_OPAM_NAME" --empty
99 pushd "$OPAMROOT/repo/offline_clone" || exit 1
100 mkdir -p base-compiler-source
101 cd base-compiler-source
102 opam source "$OCAML_COMPILER_NAME"
103 cd "$OCAML_COMPILER_NAME"
104 patchname="$OCAML_COMPILER_NAME.patch"
105 cp "$OCAML_PATCH" "$patchname"
106 patch < "$patchname"
108 opam pin "$OCAML_COMPILER_NAME" .
109 popd || exit 1
110 opam switch set-base "$OCAML_BASE_NAME"
112 else
113 opam init --disable-sandboxing --reinit --no-setup --bare
116 opam_switch_create_if_needed "$HACK_OPAM_NAME" "$HACK_OPAM_SWITCH"
117 opam switch "$HACK_OPAM_NAME"
118 eval "$(opam env)"
120 opam install "${HACK_OPAM_DEPS[@]}"
122 dune_version=$(dune --version)
123 echo ""
124 echo "opam switch correctly installed at $OPAMROOT"
125 echo "dune version is $dune_version"