1#!/usr/bin/env bash
2
3# Licensed to the Apache Software Foundation (ASF) under one
4# or more contributor license agreements.  See the NOTICE file
5# distributed with this work for additional information
6# regarding copyright ownership.  The ASF licenses this file
7# to you under the Apache License, Version 2.0 (the
8# "License"); you may not use this file except in compliance
9# with the License.  You may obtain a copy of the License at
10#
11#   http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing,
14# software distributed under the License is distributed on an
15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16# KIND, either express or implied.  See the License for the
17# specific language governing permissions and limitations
18# under the License.
19
20# This script builds the static library of openblas that can be used as dependency of mxnet.
21set -ex
22OPENBLAS_VERSION=0.3.13
23if [[ ((! -e $DEPS_PATH/lib/libopenblas.a) && -z "$CMAKE_STATICBUILD") ||
24          ((! -e $DEPS_PATH/lib/libopenblas.so) && -v CMAKE_STATICBUILD) ]]; then
25    # download and build openblas
26    >&2 echo "Building openblas..."
27
28    download \
29        https://github.com/xianyi/OpenBLAS/archive/v${OPENBLAS_VERSION}.zip \
30        ${DEPS_PATH}/openblas.zip
31    unzip -q $DEPS_PATH/openblas.zip -d $DEPS_PATH
32    pushd .
33    cd $DEPS_PATH/OpenBLAS-$OPENBLAS_VERSION
34
35    # Adding NO_DYNAMIC=1 flag causes make install to fail
36    if [[ ! $ARCH == 'aarch64' ]]; then
37        CXX="g++ -fPIC" CC="gcc -fPIC" $MAKE DYNAMIC_ARCH=1 USE_OPENMP=1
38    else
39        $MAKE DYNAMIC_ARCH=1 USE_OPENMP=1
40    fi
41
42    if [[ -v CMAKE_STATICBUILD ]]; then
43        # We link and redistribute libopenblas.so for cmake staticbuild
44        # cf https://gitlab.kitware.com/cmake/cmake/issues/16221#note_143330
45        patchelf --set-rpath '$ORIGIN' --force-rpath libopenblas.so
46    fi
47
48    $MAKE PREFIX=$DEPS_PATH install
49    if [[ -z "$CMAKE_STATICBUILD" ]]; then
50        # Manually removing .so to avoid linking against it
51        rm $DEPS_PATH/lib/libopenblasp-r${OPENBLAS_VERSION}.so
52    fi
53
54    popd
55    if [[ -z "$CMAKE_STATICBUILD" ]]; then
56        ln -s libopenblas.a $DEPS_PATH/lib/libcblas.a
57        ln -s libopenblas.a $DEPS_PATH/lib/liblapack.a
58    fi
59fi
60