Skip to content

Commit 8c8ec9d

Browse files
committed
Refactor main build script
Allow use of some command line arguments. Make defaults for everything. Pass LDFLAGS
1 parent 461ce17 commit 8c8ec9d

File tree

1 file changed

+69
-39
lines changed

1 file changed

+69
-39
lines changed

tools/build_openblas.sh

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,62 @@
11
#!/bin/bash
22
# Build script for OpenBLAS on Windows
3-
# Expects environment variables:
4-
# OPENBLAS_ROOT
5-
# OPENBLAS_COMMIT
6-
# BUILD_BITS
7-
# START_DIR
8-
# Expects "lib.exe" and "gcc" to be on the path
3+
#
4+
# Usage: build_openblas.sh [openblas_root [build_bits [if_bits]]]
5+
#
6+
# e.g build_openblas.sh c:\\opt 64 32
7+
#
8+
# Uses the optional environment variables. We always prefer command line argument
9+
# values above to environment variable values:
10+
#
11+
# OPENBLAS_ROOT (default directory root for binaries, unspecified -> c:\opt).
12+
# BUILD_BITS (default binary architecture, 32 or 64, unspec -> 64).
13+
# INTERFACE64 (1 for 64-bit interface, anything else or undefined for 32,
14+
# This gives the default value if if_bits not specified above).
15+
# START_DIR (directory containing OpenBLAS source, unspec -> .. from here)
16+
# OPENBLAS_COMMIT (unspec -> current submodule commit)
17+
# LDFLAGS (example: "-lucrt -static -static-libgcc")
18+
#
19+
# Expects at leasts these binaries on the PATH:
20+
# realpath, cygpath, zip, gcc, make, ar, dlltool
21+
# usually as part of an msys installation.
922

10-
set -ex
23+
# Convert to Unix-style path
24+
openblas_root="$(cygpath ${1:-${OPENBLAS_ROOT:-c:\\opt}})"
25+
build_bits="${2:-${BUILD_BITS:-64}}"
26+
if [ "$INTERFACE64" == "1" ]; then if_default=64; else if_default=32; fi
27+
if_bits=${3:-${if_default}}
28+
# Our directory for later copying
29+
if [ -z "$START_DIR" ]; then
30+
our_wd="$(realpath $(dirname "${BASH_SOURCE[0]}")/..)"
31+
else
32+
our_wd=$(cygpath "$START_DIR")
33+
fi
1134

12-
# Paths in Unix format
13-
OPENBLAS_ROOT=$(cygpath "$OPENBLAS_ROOT")
35+
echo "Building from $our_wd, to $openblas_root"
36+
echo "Binaries are $build_bits bit, interface is $if_bits bit"
37+
echo "Using gcc at $(which gcc), --version:"
38+
gcc --version
1439

15-
# Our directory for later copying
16-
our_wd=$(cygpath "$START_DIR")
17-
cd $our_wd
1840
# Make output directory for build artifacts
19-
rm -rf builds
20-
mkdir builds
41+
builds_dir="$our_wd/builds"
42+
rm -rf $builds_dir
43+
mkdir $builds_dir
2144

22-
cd OpenBLAS
45+
cd "${our_wd}/OpenBLAS"
2346
git submodule update --init --recursive
2447

25-
# Check which gcc we're using
26-
which gcc
27-
gcc --version
2848

2949
# Get / clean code
3050
git fetch origin
31-
git checkout $OPENBLAS_COMMIT
51+
if [ -n "$OPENBLAS_COMMIT" ]; then
52+
git checkout $OPENBLAS_COMMIT
53+
fi
3254
git clean -fxd
3355
git reset --hard
34-
rm -rf $OPENBLAS_ROOT/$BUILD_BITS
56+
rm -rf $openblas_root/$build_bits
3557

3658
# Set architecture flags
37-
if [ "$BUILD_BITS" == 64 ]; then
59+
if [ "$build_bits" == 64 ]; then
3860
march="x86-64"
3961
# https://csharp.wekeepcoding.com/article/10463345/invalid+register+for+.seh_savexmm+in+Cygwin
4062
extra="-fno-asynchronous-unwind-tables"
@@ -51,9 +73,9 @@ cflags="-O2 -march=$march -mtune=generic $extra"
5173
fflags="$fextra $cflags -frecursive -ffpe-summary=invalid,zero"
5274

5375
# Set suffixed-ILP64 flags
54-
if [ "$INTERFACE64" == "1" ]; then
55-
interface64_flags="INTERFACE64=1 SYMBOLSUFFIX=64_"
56-
SYMBOLSUFFIX=64_
76+
if [ "$if_bits" == "64" ]; then
77+
SYMBOLSUFFIX="64_"
78+
interface64_flags="INTERFACE64=1 SYMBOLSUFFIX=${SYMBOLSUFFIX}"
5779
# We override FCOMMON_OPT, so we need to set default integer manually
5880
fflags="$fflags -fdefault-integer-8"
5981
else
@@ -66,42 +88,50 @@ OPENBLAS_VERSION=$(git describe --tags)
6688
# Build OpenBLAS
6789
# Variable used in creating output libraries
6890
export LIBNAMESUFFIX=${OPENBLAS_VERSION}-${GCC_TAG}
69-
make BINARY=$BUILD_BITS DYNAMIC_ARCH=1 USE_THREAD=1 USE_OPENMP=0 \
91+
make BINARY=$build_bits DYNAMIC_ARCH=1 USE_THREAD=1 USE_OPENMP=0 \
7092
NUM_THREADS=24 NO_WARMUP=1 NO_AFFINITY=1 CONSISTENT_FPCSR=1 \
7193
BUILD_LAPACK_DEPRECATED=1 TARGET=PRESCOTT BUFFERSIZE=20\
94+
LDFLAGS="$LDFLAGS" \
7295
COMMON_OPT="$cflags" \
7396
FCOMMON_OPT="$fflags" \
7497
MAX_STACK_ALLOC=2048 \
7598
$interface64_flags
76-
make PREFIX=$OPENBLAS_ROOT/$BUILD_BITS $interface64_flags install
99+
make PREFIX=$openblas_root/$build_bits $interface64_flags install
77100
DLL_BASENAME=libopenblas${SYMBOLSUFFIX}_${LIBNAMESUFFIX}
78-
if [ "$INTERFACE64" == "1" ]; then
101+
if [ "$if_bits" == "64" ]; then
79102
# OpenBLAS does not build a symbol-suffixed static library on Windows:
80103
# do it ourselves
81104
set -x # echo commands
82105
static_libname=$(find . -maxdepth 1 -type f -name '*.a' \! -name '*.dll.a' | tail -1)
83106
make -C exports $interface64_flags objcopy.def
84107
objcopy --redefine-syms exports/objcopy.def "${static_libname}" "${static_libname}.renamed"
85-
cp -f "${static_libname}.renamed" "$OPENBLAS_ROOT/$BUILD_BITS/lib/${static_libname}"
86-
cp -f "${static_libname}.renamed" "$OPENBLAS_ROOT/$BUILD_BITS/lib/${DLL_BASENAME}.a"
108+
cp -f "${static_libname}.renamed" "$openblas_root/$build_bits/lib/${static_libname}"
109+
cp -f "${static_libname}.renamed" "$openblas_root/$build_bits/lib/${DLL_BASENAME}.a"
87110
set +x
88111
fi
89-
cd $OPENBLAS_ROOT
112+
cd $openblas_root
90113
# Copy library link file for custom name
91-
cd $BUILD_BITS/lib
114+
cd $build_bits/lib
115+
cp ${our_wd}/OpenBLAS/exports/${DLL_BASENAME}.def ${DLL_BASENAME}.def
92116
# At least for the mingwpy wheel, we have to use the VC tools to build the
93117
# export library. Maybe fixed in later binutils by patch referred to in
94118
# https://sourceware.org/ml/binutils/2016-02/msg00002.html
95-
cp ${our_wd}/OpenBLAS/exports/${DLL_BASENAME}.def ${DLL_BASENAME}.def
96-
"lib.exe" /machine:${vc_arch} /def:${DLL_BASENAME}.def
119+
# "lib.exe" /machine:${vc_arch} /def:${DLL_BASENAME}.def
120+
# Maybe this will now work (after 2016 patch above).
121+
dlltool --input-def ${DLL_BASENAME}.def \
122+
--output-exp ${DLL_BASENAME}.exp \
123+
--dllname ${DLL_BASENAME}.dll \
124+
--output-lib ${DLL_BASENAME}.lib
125+
# Replace the DLL name with the generated name.
126+
sed -i "s/ -lopenblas$/ -l${DLL_BASENAME:3}/g" pkgconfig/openblas.pc
97127
cd ../..
98128
# Build template site.cfg for using this build
99-
cat > ${BUILD_BITS}/site.cfg.template << EOF
129+
cat > ${build_bits}/site.cfg.template << EOF
100130
[openblas${SYMBOLSUFFIX}]
101131
libraries = $DLL_BASENAME
102-
library_dirs = {openblas_root}\\${BUILD_BITS}\\lib
103-
include_dirs = {openblas_root}\\${BUILD_BITS}\\include
132+
library_dirs = {openblas_root}\\${build_bits}\\lib
133+
include_dirs = {openblas_root}\\${build_bits}\\include
104134
EOF
105-
ZIP_NAME="openblas${SYMBOLSUFFIX}-${OPENBLAS_VERSION}-${plat_tag}-${GCC_TAG}.zip"
106-
zip -r $ZIP_NAME $BUILD_BITS
107-
cp $ZIP_NAME $our_wd/builds
135+
zip_name="openblas${SYMBOLSUFFIX}-${OPENBLAS_VERSION}-${plat_tag}-${GCC_TAG}.zip"
136+
zip -r $zip_name $build_bits
137+
cp $zip_name ${builds_dir}

0 commit comments

Comments
 (0)