|
| 1 | +#!/bin/bash |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +# github_checkout $REPO $VERSION $GIT_CLONE_PARAMS clones or re-uses an existing clone of the |
| 15 | +# specified repo, checking out the requested version. |
| 16 | +function github_checkout { |
| 17 | + local REPO=$1 |
| 18 | + shift |
| 19 | + local VERSION=$1 |
| 20 | + shift |
| 21 | + local GIT_CLONE_PARAMS=$@ |
| 22 | + local DIRNAME=$(basename $REPO) |
| 23 | + cd "${DEPENDENCY_DIR}" |
| 24 | + if [ -z "${DIRNAME}" ]; then |
| 25 | + echo "Failed to get repo name from ${REPO}" |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | + if [ -d "${DIRNAME}" ] && prompt "${DIRNAME} already exists. Delete?"; then |
| 29 | + rm -rf "${DIRNAME}" |
| 30 | + fi |
| 31 | + if [ ! -d "${DIRNAME}" ]; then |
| 32 | + git clone -q -b $VERSION $GIT_CLONE_PARAMS "https://github.com/${REPO}.git" |
| 33 | + fi |
| 34 | + cd "${DIRNAME}" |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +# get_cxx_flags [$CPU_ARCH] |
| 39 | +# Sets and exports the variable VELOX_CXX_FLAGS with appropriate compiler flags. |
| 40 | +# If $CPU_ARCH is set then we use that else we determine best possible set of flags |
| 41 | +# to use based on current cpu architecture. |
| 42 | +# The goal of this function is to consolidate all architecture specific flags to one |
| 43 | +# location. |
| 44 | +# The values that CPU_ARCH can take are as follows: |
| 45 | +# arm64 : Target Apple silicon. |
| 46 | +# aarch64: Target general 64 bit arm cpus. |
| 47 | +# avx: Target Intel CPUs with AVX. |
| 48 | +# sse: Target Intel CPUs with sse. |
| 49 | +# Echo's the appropriate compiler flags which can be captured as so |
| 50 | +# CXX_FLAGS=$(get_cxx_flags) or |
| 51 | +# CXX_FLAGS=$(get_cxx_flags "avx") |
| 52 | + |
| 53 | +function get_cxx_flags { |
| 54 | + local CPU_ARCH=$1 |
| 55 | + |
| 56 | + local OS |
| 57 | + OS=$(uname) |
| 58 | + local MACHINE |
| 59 | + MACHINE=$(uname -m) |
| 60 | + |
| 61 | + if [ -z "$CPU_ARCH" ]; then |
| 62 | + |
| 63 | + if [ "$OS" = "Darwin" ]; then |
| 64 | + |
| 65 | + if [ "$MACHINE" = "x86_64" ]; then |
| 66 | + local CPU_CAPABILITIES |
| 67 | + CPU_CAPABILITIES=$(sysctl -a | grep machdep.cpu.features | awk '{print tolower($0)}') |
| 68 | + |
| 69 | + if [[ $CPU_CAPABILITIES =~ "avx" ]]; then |
| 70 | + CPU_ARCH="avx" |
| 71 | + else |
| 72 | + CPU_ARCH="sse" |
| 73 | + fi |
| 74 | + |
| 75 | + elif [[ $(sysctl -a | grep machdep.cpu.brand_string) =~ "Apple" ]]; then |
| 76 | + # Apple silicon. |
| 77 | + CPU_ARCH="arm64" |
| 78 | + fi |
| 79 | + else [ "$OS" = "Linux" ]; |
| 80 | + |
| 81 | + local CPU_CAPABILITIES |
| 82 | + CPU_CAPABILITIES=$(cat /proc/cpuinfo | grep flags | head -n 1| awk '{print tolower($0)}') |
| 83 | + |
| 84 | + if [[ "$CPU_CAPABILITIES" =~ "avx" ]]; then |
| 85 | + CPU_ARCH="avx" |
| 86 | + elif [[ "$CPU_CAPABILITIES" =~ "sse" ]]; then |
| 87 | + CPU_ARCH="sse" |
| 88 | + elif [ "$MACHINE" = "aarch64" ]; then |
| 89 | + CPU_ARCH="aarch64" |
| 90 | + fi |
| 91 | + fi |
| 92 | + fi |
| 93 | + |
| 94 | + case $CPU_ARCH in |
| 95 | + |
| 96 | + "arm64") |
| 97 | + echo -n "-mcpu=apple-m1+crc -std=c++17" |
| 98 | + ;; |
| 99 | + |
| 100 | + "avx") |
| 101 | + echo -n "-mavx2 -mfma -mavx -mf16c -mlzcnt -std=c++17" |
| 102 | + ;; |
| 103 | + |
| 104 | + "sse") |
| 105 | + echo -n "-msse4.2 -std=c++17" |
| 106 | + ;; |
| 107 | + |
| 108 | + "aarch64") |
| 109 | + echo -n "-mcpu=neoverse-n1 -std=c++17" |
| 110 | + ;; |
| 111 | + *) |
| 112 | + echo -n "Architecture not supported!" |
| 113 | + esac |
| 114 | + |
| 115 | +} |
| 116 | + |
| 117 | +function cmake_install { |
| 118 | + local NAME=$(basename "$(pwd)") |
| 119 | + local BINARY_DIR=_build |
| 120 | + if [ -d "${BINARY_DIR}" ] && prompt "Do you want to rebuild ${NAME}?"; then |
| 121 | + rm -rf "${BINARY_DIR}" |
| 122 | + fi |
| 123 | + mkdir -p "${BINARY_DIR}" |
| 124 | + CPU_TARGET="${CPU_TARGET:-avx}" |
| 125 | + COMPILER_FLAGS=$(get_cxx_flags $CPU_TARGET) |
| 126 | + |
| 127 | + # CMAKE_POSITION_INDEPENDENT_CODE is required so that Velox can be built into dynamic libraries \ |
| 128 | + cmake -Wno-dev -B"${BINARY_DIR}" \ |
| 129 | + -GNinja \ |
| 130 | + -DCMAKE_POSITION_INDEPENDENT_CODE=ON \ |
| 131 | + -DCMAKE_CXX_STANDARD=17 \ |
| 132 | + "${INSTALL_PREFIX+-DCMAKE_PREFIX_PATH=}${INSTALL_PREFIX-}" \ |
| 133 | + "${INSTALL_PREFIX+-DCMAKE_INSTALL_PREFIX=}${INSTALL_PREFIX-}" \ |
| 134 | + -DCMAKE_CXX_FLAGS="$COMPILER_FLAGS" \ |
| 135 | + -DBUILD_TESTING=OFF \ |
| 136 | + "$@" |
| 137 | + ninja -C "${BINARY_DIR}" install |
| 138 | +} |
| 139 | + |
0 commit comments