-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild_wheel.sh
executable file
·692 lines (631 loc) · 22.9 KB
/
build_wheel.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#!/bin/env bash
THIS_SCRIPT=$0
SCRIPT_DIR=$(dirname -- "$(readlink -f -- "$THIS_SCRIPT")")
YEAR="${EBVERSIONGENTOO:-2017}"
EXCLUDE_PYTHON_VERSIONS="/2\.\|/3.[5678]"
if [[ "$YEAR" == "2017" ]]; then
GCC_VERSION=7.3.0
elif [[ "$YEAR" == "2020" ]]; then
GCC_VERSION=9.3.0
CYTHON_VERSION=.0.29.36
NUMPY_MODULE="oldest-supported-numpy/.2022a"
else
GCC_VERSION=12.3
EXCLUDE_PYTHON_VERSIONS="/2\.\|/3.[56789]"
CYTHON_VERSION=.3.0.10
NUMPY_MODULE="numpy/.2.1.1" # oldest-supported-numpy is now depcrecated with v2.0+
fi
if [[ -z "$PYTHON_VERSIONS" ]]; then
PYTHON_VERSIONS=$(module --terse spider python | grep -v "$EXCLUDE_PYTHON_VERSIONS" | grep -Po "\d\.\d+" | sort -Vu | sed 's#^#python/#')
fi
function print_usage {
echo "Usage: $0 --package <python package name> "
echo " [--version <specific version]"
echo " [--recursive=<1|0>]"
echo " [--python=<comma separated list of python versions>]"
echo " [--keep-build-dir]"
echo " [--verbose=<1,2,3>]"
echo " [--job]"
echo " [--cpus=<number of cpus>] (default: 1)"
echo " [--mem-cpu=<memory per cpu>[mM|gG]] (default: 3G)"
}
# Translate a version number into a comparable number, supports up to 4 digits
# Supports : 4.24, 4.24.0, 4.24.0.0 which all translate to 4024000000
function translate_version {
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }';
}
TEMP=$(getopt -o h --longoptions help,keep-build-dir,autocopy,verbose:,recursive:,package:,version:,no-verify,python:,job,cpus:,mem-cpu: -n $0 -- "$@")
if [ $? != 0 ] ; then print_usage; exit 1 ; fi
eval set -- "$TEMP"
ARG_RECURSIVE=1
ARG_KEEP_BUILD_DIR=0
ARG_VERBOSE_LEVEL=0
ARG_AUTOCOPY=0
ARG_NO_VERIFY=0
ARG_JOB=0
ARG_NCPUS=1
ARG_MEM_CPU=3G
while true; do
case "$1" in
--recursive)
ARG_RECURSIVE=$2; shift 2;;
--package)
ARG_PACKAGE=$2; shift 2;;
--version)
ARG_VERSION=$2; shift 2;;
--python)
ARG_PYTHON_VERSIONS=$2; shift 2;;
--keep-build-dir)
ARG_KEEP_BUILD_DIR=1; shift ;;
--autocopy)
ARG_AUTOCOPY=1; shift ;;
--no-verify)
ARG_NO_VERIFY=1; shift ;;
--job)
ARG_JOB=1; shift ;;
--cpus)
ARG_NCPUS=$2; shift 2;;
--mem-cpu)
ARG_MEM_CPU=$2; shift 2;;
--verbose)
ARG_VERBOSE_LEVEL=$2; shift 2;;
-h|--help)
print_usage; exit 0 ;;
--)
shift; break ;;
*) echo "Unknown parameter $1"; print_usage; exit 1 ;;
esac
done
STARTING_DIRECTORY=$(pwd)
PACKAGE=$ARG_PACKAGE
VERSION=$ARG_VERSION
RECURSIVE=$ARG_RECURSIVE
if [[ -z "$PACKAGE" ]]; then
print_usage
exit 1
fi
if [[ $ARG_JOB -eq 1 ]]; then
jobname="$PACKAGE${VERSION:+-$VERSION}"
# submit non-interactive job, remove job related arguments
sbatch --time=24:00:00 --mem-per-cpu=$ARG_MEM_CPU --cpus-per-task=$ARG_NCPUS --nodes=1 --job-name=$jobname --output="$jobname-%j.log" <<-EOF
#!/bin/bash
bash build_wheel.sh $(sed -e "s/--job//" -E -e "s/--cpus\s'[0-9]+'//" -e "s/--mem-cpu\s'[0-9]+.?'//" -e "s/--$//" <<< $TEMP)
EOF
exit $?
fi
if [[ ! -z "$ARG_PYTHON_VERSIONS" ]]; then
PYTHON_VERSIONS=""
for v in ${ARG_PYTHON_VERSIONS//,/ }; do
PYTHON_VERSIONS="python/$v $PYTHON_VERSIONS"
done
fi
PYTHON_IMPORT_NAME="$PACKAGE"
PACKAGE_FOLDER_NAME="$PACKAGE"
PACKAGE_DOWNLOAD_NAME="$PACKAGE"
UPDATE_REQUIREMENTS=""
RPATH_TO_ADD=""
BDIST_WHEEL_ARGS=""
PIP_WHEEL_ARGS=""
PRE_DOWNLOAD_COMMANDS=""
TMP_WHEELHOUSE=$(pwd)
PATCHES=""
# Make sure $PACKAGE_DOWNLOAD_ARGUMENT is not expanded right away
# Do not collect binaries and don't install dependencies
PACKAGE_DOWNLOAD_CMD="pip download -v --no-cache --no-binary \$PACKAGE --no-use-pep517 --no-build-isolation --no-deps \$PACKAGE_DOWNLOAD_ARGUMENT"
PRE_BUILD_COMMANDS_DEFAULT='sed -i -e "s/\([^\.]\)distutils.core/\1setuptools/g" setup.py'
PYTHON_DEPS_DEFAULT=""
MODULE_BUILD_DEPS_DEFAULT="$NUMPY_MODULE python-build-bundle pytest cython/$CYTHON_VERSION"
PYTHON27_ONLY="cogent OBITools gdata qcli emperor RSeQC preprocess Amara pysqlite IPTest ipaddress functools32 blmath bamsurgeon"
if [[ $PYTHON27_ONLY =~ " $PACKAGE " ]]; then
PYTHON_VERSIONS="python/2.7"
fi
if [[ -n "$VERSION" ]]; then
PACKAGE_DOWNLOAD_ARGUMENT="$PACKAGE==$VERSION"
else
PACKAGE_DOWNLOAD_ARGUMENT="$PACKAGE"
fi
CONFIGDIR=$SCRIPT_DIR/config
PACKAGE_PATTERN=$(echo $PACKAGE | sed -e 's/[_-]/\?/g') # Ignores packages with - or _ replace with ? char for pattern.
# Check case-insensitively if package-version.sh exists.
if [[ -n $(find $CONFIGDIR -iname "${PACKAGE_PATTERN}-${VERSION}.sh") ]]; then
config_name=$(find $CONFIGDIR -iname "${PACKAGE_PATTERN}-${VERSION}.sh")
echo "INFO: Sourced configuration $config_name"
source $config_name
# Check case-insensitively if package.sh exists.
elif [[ -n $(find $CONFIGDIR -iname "${PACKAGE_PATTERN}.sh") ]]; then
config_name=$(find $CONFIGDIR -iname "${PACKAGE_PATTERN}.sh")
echo "INFO: Sourced configuration $config_name"
source $config_name
else
echo "INFO: no configuration file sourced."
fi
# define some ANSI sequences for colorful output.
COL_RED="\033[31;1m" # red
COL_GRN="\033[32;1m" # green
COL_YEL="\033[33;1m" # yellow
COL_RST="\033[0m" # reset
function single_test_import {
CONST_NAME="$1"
NAME="$2"
TESTS="$3"
FORCE=$4
if [[ "$NAME" != "$CONST_NAME" || $FORCE -eq 1 ]]; then
echo -n "Testing import with name $NAME... "
if [[ $ARG_VERBOSE_LEVEL -gt 1 ]]; then
$PYTHON_CMD -c "import $NAME; $TESTS"
RET=$?
else
$PYTHON_CMD -c "import $NAME; $TESTS" 2>/dev/null
RET=$?
fi
test $RET -eq 0 && echo -e "${COL_GRN}Success!${COL_RST}" || echo "Failed"
return $RET
else
return 1
fi
}
function test_import {
NAME=$1
TESTS=$2
# dashes in names are always replaced by underscore
NAME=${NAME//-/_}
CONST_NAME="$NAME"
single_test_import "$CONST_NAME" "$NAME" "$TESTS" 1
RET=$?
if [[ $RET -ne 0 ]]; then
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME//python_/}"
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME//_python/}"
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME//py_/}"
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME//Py_/}"
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME//_py/}"
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME//_Py/}"
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME//Py/}"
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME//py/}"
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME%2}" #surprisingly, many packages have a name that ends with 2, but import without the 2
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME}2" #the other way also happens...
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME//scikit_/sk}" #special case for all of the scikit- packages
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME//_/.}" #replacing _ by . sometimes happens
NAMES_TO_TEST="$NAMES_TO_TEST ${CONST_NAME//_/}" #remove _ sometimes happens
# add a version of all in lower cases
NAMES_TO_TEST="$NAMES_TO_TEST ${NAMES_TO_TEST,,}"
# add a version of all in upper cases
NAMES_TO_TEST="$NAMES_TO_TEST ${NAMES_TO_TEST^^}"
# remove duplicates
for TEST_NAME in $NAMES_TO_TEST; do
if [[ ! $NAMES_TO_TEST2 =~ [[:space:]]$TEST_NAME[[:space:]] ]]; then
NAMES_TO_TEST2=" $NAMES_TO_TEST2 $TEST_NAME "
fi
done
NAMES_TO_TEST=$NAMES_TO_TEST2
echo "Testing imports with the following names $NAMES_TO_TEST"
if [[ $RET -ne 0 ]]; then
RET=1
for TEST_NAME in $NAMES_TO_TEST; do
single_test_import "$CONST_NAME" "$TEST_NAME" "$TESTS"
RET=$?
if [[ $RET -eq 0 ]]; then break; fi
done
fi
fi
return $RET
}
function wrapped_pip_install {
TMPFILE=$RANDOM.out
if [[ $ARG_VERBOSE_LEVEL -ge 2 ]]; then
echo Running command: pip install $@ --no-cache --find-links=$TMP_WHEELHOUSE
pip install $@ --no-cache --find-links=$TMP_WHEELHOUSE |& tee $TMPFILE
elif [[ $ARG_VERBOSE_LEVEL -ge 1 ]]; then
echo Running command: pip install $@ --no-cache --find-links=$TMP_WHEELHOUSE
pip install $@ --no-cache --find-links=$TMP_WHEELHOUSE &> $TMPFILE
else
pip install $@ --no-cache --find-links=$TMP_WHEELHOUSE &> $TMPFILE
fi
DOWNLOADED_DEPS=$(grep Downloading $TMPFILE | awk '{print $2}')
if [[ ! -z "$DOWNLOADED_DEPS" && $RECURSIVE -eq 1 ]]; then
echo "========================================================="
echo "The following dependencies were downloaded. Building them: $DOWNLOADED_DEPS"
for w in $DOWNLOADED_DEPS; do
echo "========================================================="
wheel_name=$(basename $w | grep -Po '^([\w_-]+|[\w\.]*)-' | sed 's/.$//')
wheel_version=$(basename $w | cut -d'-' -f2 | cut -d'+' -f1 | sed -e "s/.tar.gz$//")
echo Building $wheel_name
log_command pushd $STARTING_DIRECTORY
echo $w
if [[ $w =~ .*none-any.* ]]; then
IS_NONE_ANY="yes"
else
IS_NONE_ANY="no"
fi
if [[ -e "$CONFIGDIR/${wheel_name}-${wheel_version}.sh" || -e "$CONFIGDIR/${wheel_name}.sh" || "${IS_NONE_ANY}" == "no" ]]; then
if [[ ! -z "$ARG_PYTHON_VERSIONS" ]]; then
log_command bash $THIS_SCRIPT --package=$wheel_name --version $wheel_version --recursive=0 --python=$ARG_PYTHON_VERSIONS --verbose=$ARG_VERBOSE_LEVEL
else
log_command bash $THIS_SCRIPT --package=$wheel_name --version $wheel_version --recursive=0 --verbose=$ARG_VERBOSE_LEVEL
fi
else
echo "Wheel is none-any, using unmanylinuxize.sh"
log_command bash ./unmanylinuxize.sh --package $wheel_name --version $wheel_version
fi
log_command popd
echo "========================================================="
done
echo "Resuming building the main package"
echo "========================================================="
fi
rm $TMPFILE
}
function log_command {
if [[ $ARG_VERBOSE_LEVEL -ge 1 ]]; then
echo "Running command: $@"
fi
if [[ $ARG_VERBOSE_LEVEL -ge 3 ]]; then
eval $@
elif [[ $ARG_VERBOSE_LEVEL -ge 2 ]]; then
eval $@ 2>/dev/null
else
eval $@ &>/dev/null
fi
}
function setup()
{
echo "=============================="
echo "Setting up build environment"
if [[ -n "$MODULE_RUNTIME_DEPS" ]]; then
log_command module load $MODULE_RUNTIME_DEPS
fi
if [[ -n "$MODULE_BUILD_DEPS" ]]; then
log_command module load $MODULE_BUILD_DEPS
fi
if [[ ! -z "$PRE_SETUP_COMMANDS" ]]; then
log_command $PRE_SETUP_COMMANDS
fi
log_command module list
log_command python -m venv build_$PVDIR || virtualenv build_$PVDIR || pyvenv build_$PVDIR
source build_$PVDIR/bin/activate
if [[ -n "$PYTHON_DEPS_DEFAULT" ]]; then
wrapped_pip_install $PYTHON_DEPS_DEFAULT
fi
if [[ -n "$PYTHON_DEPS" ]]; then
wrapped_pip_install $PYTHON_DEPS
fi
log_command pip freeze
echo "=============================="
}
function download()
{
echo "=============================="
if [[ ! -z "$PRE_DOWNLOAD_COMMANDS" ]]; then
log_command $PRE_DOWNLOAD_COMMANDS
fi
echo "Downloading source"
mkdir $PVDIR
ARCHNAME=$(PIP_CONFIG_FILE= eval $PACKAGE_DOWNLOAD_CMD |& tee download.log | grep "Saved " | awk '{print $2}')
if [[ $PACKAGE_DOWNLOAD_METHOD == "Git" ]]; then
ARCHNAME=$PACKAGE_DOWNLOAD_NAME
fi
if [[ -z "$ARCHNAME" ]]; then
grep -l "Disabling PEP 517 processing" $PWD/download.log
if [[ $? -eq 0 ]]; then
echo "Package $PACKAGE_DOWNLOAD_NAME does not support disabling PEP 517. Trying again without --no-use-pep517"
PACKAGE_DOWNLOAD_CMD=${PACKAGE_DOWNLOAD_CMD//--no-use-pep517/}
ARCHNAME=$(PIP_CONFIG_FILE= eval $PACKAGE_DOWNLOAD_CMD |& tee download.log | grep "Saved " | awk '{print $2}')
fi
fi
if [[ -z "$ARCHNAME" ]]; then
echo "Trying to download without --no-binary to see if it is a py3-none-any wheel."
PACKAGE_DOWNLOAD_CMD=${PACKAGE_DOWNLOAD_CMD//--no-binary \$PACKAGE/}
PACKAGE_DOWNLOAD_CMD=${PACKAGE_DOWNLOAD_CMD//--no-use-pep517/}
ARCHNAME=$(PIP_CONFIG_FILE= eval $PACKAGE_DOWNLOAD_CMD |& tee download.log | grep "Saved " | awk '{print $2}')
if [[ $ARCHNAME =~ .*-py3-none-any.* ]]; then
echo $ARCHNAME is py3-none-any, no build needed
else
unset $ARCHNAME
fi
fi
echo "Downloaded '$ARCHNAME'"
if [[ -z "$ARCHNAME" ]]; then
echo -e "${COL_RED}Error while downloading package. Aborting...${COL_RST}"
echo "See : $PWD/download.log"
exit 1
fi
if [[ ! -z $POST_DOWNLOAD_COMMANDS ]]; then
log_command $POST_DOWNLOAD_COMMANDS
fi
echo "=============================="
}
function verify_and_patch_arch_flags()
{
echo "=============================="
echo "Testing source code for CPU architecture instructions in $PWD"
files_native=$(grep -rl -- "-march=native" .)
files_xHost=$(grep -rl -- "-xHost" .)
if [[ -n "$files_native" ]]; then
declare -A gcc_targets
gcc_targets=(
["avx"]="corei7-avx"
["avx2"]="core-avx2"
["avx512"]="skylake-avx512"
["sse3"]="nocona"
)
target=${gcc_targets[$RSNT_ARCH]}
ARCH_PRESENCE=$RSNT_ARCH
if [[ "$YEAR" == "2023" ]]; then
gcc_targets["avx2"]="x86-64-v3"
gcc_targets["avx512"]="x86-64-v4"
target=${gcc_targets[$RSNT_ARCH]}
ARCH_PRESENCE=$target
fi
echo "-march=native found in files $files_native, replacing with -march=$target to build for $RSNT_ARCH"
sed -i -e "s/-march=native/-march=$target/" $files_native
fi
if [[ -n "$files_xHost" ]]; then
echo "NOTE: -xHost found in files $files_xHost, expecting to be built with Intel compiler ?"
fi
echo "=============================="
}
function patch_function()
{
PATCHESDIR=$SCRIPT_DIR/patches
if [[ ! -z "$PATCHES" ]]; then
echo "=============================="
echo "Patching"
for p in $PATCHES;
do
log_command patch --verbose -p1 < ${PATCHESDIR}/$p > /dev/null
done
echo "Patching done"
echo "=============================="
fi
}
function build()
{
echo "=============================="
echo "Building"
if [[ ! -z "$PRE_BUILD_COMMANDS" ]]; then
log_command $PRE_BUILD_COMMANDS
fi
log_command $PRE_BUILD_COMMANDS_DEFAULT
if [[ $ARG_NO_VERIFY -eq 0 ]]; then
verify_and_patch_arch_flags
fi
# change the name of the wheel to add a suffix
if [[ -n "$PACKAGE_SUFFIX" ]]; then
sed -i -e "s/name=\"$PACKAGE\"/name=\"$PACKAGE$PACKAGE_SUFFIX\"/g" -e "s/name='$PACKAGE'/name='$PACKAGE$PACKAGE_SUFFIX'/g" $(find . -name "setup.py")
fi
echo "Building the wheel...."
if [[ -f "pyproject.toml" ]]; then
log_command pip wheel -vvv --no-deps --no-build-isolation $PIP_WHEEL_ARGS . &> build.log
elif [[ -f "setup.py" ]]; then
log_command $PYTHON_CMD setup.py bdist_wheel $BDIST_WHEEL_ARGS &> build.log
fi
if [[ $? -ne 0 ]]; then
echo -e "${COL_RED}An error occured.${COL_RST}"
echo "Build log is in $(pwd)/build.log"
else
echo -e "${COL_GRN}Success.${COL_RST}"
fi
if [[ -d dist ]]; then
log_command cp dist/*.whl .
fi
WHEEL_NAME=$(ls *.whl)
if [[ -z $WHEEL_NAME ]]; then
cat build.log
fi
# add a computecanada local_version
if [[ -z "$UPDATE_REQUIREMENTS" ]]; then
log_command $SCRIPT_DIR/manipulate_wheels.py --insert_local_version --inplace --wheels $WHEEL_NAME && rm $WHEEL_NAME
else
log_command $SCRIPT_DIR/manipulate_wheels.py -v --insert_local_version --inplace --wheels $WHEEL_NAME --update_req $UPDATE_REQUIREMENTS && rm $WHEEL_NAME
fi
WHEEL_NAME=$(ls *.whl)
log_command "$POST_BUILD_COMMANDS"
if [[ -n "$RPATH_TO_ADD" || -n "$RPATH_ADD_ORIGIN" ]]; then
setrpaths_cmd="/cvmfs/soft.computecanada.ca/easybuild/bin/setrpaths.sh --path ${WHEEL_NAME}"
if [[ ! -z "$RPATH_TO_ADD" ]]; then
setrpaths_cmd="${setrpaths_cmd} --add_path ${RPATH_TO_ADD} --any_interpreter"
fi
if [[ ! -z "$RPATH_ADD_ORIGIN" ]]; then
setrpaths_cmd="${setrpaths_cmd} --add_origin"
fi
log_command $setrpaths_cmd
fi
log_command cp -v $WHEEL_NAME $TMP_WHEELHOUSE
echo "Building done"
echo "=============================="
}
function test_whl()
{
echo "=============================="
echo "Testing..."
deactivate
if [[ -n "$MODULE_BUILD_DEPS" ]]; then
module unload $MODULE_BUILD_DEPS
fi
if [[ -n "$MODULE_RUNTIME_DEPS" ]]; then
module load $MODULE_RUNTIME_DEPS
fi
source build_$PVDIR/bin/activate
log_command module list
echo "Installing wheel"
wrapped_pip_install $TMP_WHEELHOUSE/$WHEEL_NAME
if [[ ! -z "$PRE_TEST_COMMANDS" ]]; then
log_command $PRE_TEST_COMMANDS
fi
if [[ -n "$TEST_COMMAND" ]]; then
log_command $TEST_COMMAND
elif [[ -n "$PYTHON_IMPORT_NAME" ]]; then
test_import "$PYTHON_IMPORT_NAME" "$PYTHON_TESTS"
fi
SUCCESS=$?
deactivate
chmod o+r $TMP_WHEELHOUSE/$WHEEL_NAME
if [[ $SUCCESS -ne 0 ]]; then
echo -e "${COL_RED}Error happened${COL_RST}"
cd ..
exit $SUCCESS
fi
echo "Testing done"
echo "=============================="
}
function adjust_numpy_requirements_based_on_link_info()
{
# don't modify numpy wheels themselves
if [[ $WHEEL_NAME =~ ^numpy-.* ]]; then
return
fi
# only linux_x86_64 wheels will contain .so'
if [[ $WHEEL_NAME =~ .*linux_x86_64.* ]]; then
tmpdir=/tmp/wheel_builder_$BASHPID_$RANDOM
mkdir $tmpdir && pushd $tmpdir
log_command unzip -qn $TMP_WHEELHOUSE/$WHEEL_NAME
num_so=$(find . -name '*.so' | wc -l)
if [[ $num_so -gt 0 ]]; then
num_links=$(grep -l "module compiled against API version .* but this version of numpy is .*" $(find . -name '*.so') | wc -l)
else
num_links=0
fi
popd
rm -rf $tmpdir
if [[ $num_links -gt 0 ]]; then
numpy_build_version=$(python -c "import numpy; print(numpy.__version__)" | grep -P -oe '\d+.\d+') # Get the numpy version (major.minor) used to build the wheel
if [[ $numpy_build_version =~ ^2.* ]]; then # only pin minimal numpy if it is a 1.x version
echo "Numpy 2.x found, not pinning the minimal version of numpy."
return
fi
echo "Found $num_links shared objects that mention a specific version of API of numpy. Pinning the minimum required version of numpy to $numpy_build_version"
if [[ $(grep -ic $PACKAGE $SCRIPT_DIR/packages_w_numpy_api.txt) -eq 0 ]]; then
echo "Recording '$PACKAGE' in 'packages_w_numpy_api.txt'."
echo "$PACKAGE" >> $SCRIPT_DIR/packages_w_numpy_api.txt
echo -e "${COL_YEL}Please commit the file 'packages_w_numpy_api.txt'.${COL_RST}"
fi
log_command $SCRIPT_DIR/manipulate_wheels.py --print_req --wheels $TMP_WHEELHOUSE/$WHEEL_NAME
if [[ $numpy_build_version =~ ^1.* ]]; then
echo "Built with numpy 1.x; pinning numpy lower than 2.0"
log_command $SCRIPT_DIR/manipulate_wheels.py --inplace --force --wheels $TMP_WHEELHOUSE/$WHEEL_NAME --set_lt_numpy 2.0
fi
log_command $SCRIPT_DIR/manipulate_wheels.py --inplace --force --wheels $TMP_WHEELHOUSE/$WHEEL_NAME --set_min_numpy $numpy_build_version
log_command $SCRIPT_DIR/manipulate_wheels.py --print_req --wheels $TMP_WHEELHOUSE/$WHEEL_NAME
fi
fi
}
function adjust_torch_requirements_based_on_link_info()
{
# don't modify torch wheels themselves
if [[ $WHEEL_NAME =~ ^torch-.* ]]; then
return
fi
# only linux_x86_64 wheels will contain .so'
if [[ $WHEEL_NAME =~ .*linux_x86_64.* ]]; then
echo "Testing if wheel links on libtorch..."
local tmpdir=$(mktemp -d)
log_command unzip -qn $TMP_WHEELHOUSE/$WHEEL_NAME -d $tmpdir
find $tmpdir -type f -executable -exec ldd {} \+ | fgrep 'libtorch.so'
local res=$?
if [[ $res -eq 0 || ! -z "$TORCH_VERSION" ]]; then
echo "Link dependency on libtorch found. Pinning version of torch"
torch_build_version=$(pip show torch | grep Version | awk '{print $2}' | sed -e "s/\([^+]*\)+*.*/\1/g")
torch_build_version=${torch_build_version::-2} # X.Y.Z -> X.Y
log_command $SCRIPT_DIR/manipulate_wheels.py --print_req --wheels $TMP_WHEELHOUSE/$WHEEL_NAME
# Some wheels depends on torch but do not has the requirement, add it or update it.
has_torch_req=$(log_command $SCRIPT_DIR/manipulate_wheels.py --print_req --wheels $TMP_WHEELHOUSE/$WHEEL_NAME | grep -cE '^torch$')
if [[ $has_torch_req -eq 0 ]]; then
log_command $SCRIPT_DIR/manipulate_wheels.py --inplace --force --wheels $TMP_WHEELHOUSE/$WHEEL_NAME --add_req "\"torch (~=${torch_build_version}.0)\""
else
# Pin compatible version: ~=1.12.0 -> upmost micro version we currently have.
log_command $SCRIPT_DIR/manipulate_wheels.py --inplace --force --wheels $TMP_WHEELHOUSE/$WHEEL_NAME --update_req "\"torch (~=${torch_build_version}.0)\""
fi
log_command $SCRIPT_DIR/manipulate_wheels.py --print_req --wheels $TMP_WHEELHOUSE/$WHEEL_NAME
# Does it need to be tagged, would it override an existing wheel of the same version?
local wheel_pattern=$(echo ${WHEEL_NAME//+computecanada/-} | sed -e 's/--/\*/')
if [[ $(find /cvmfs/soft.computecanada.ca/custom/python/wheelhouse/ -name "${wheel_pattern}" | wc -l) -gt 0 ]]; then
echo "Found existing wheel that would have been overriden. Tagging the wheel."
local tag="torch${torch_build_version//./}"
log_command $SCRIPT_DIR/manipulate_wheels.py --inplace --force --wheels $TMP_WHEELHOUSE/$WHEEL_NAME --add_tag $tag
local new_wheel=${WHEEL_NAME//+computecanada/+$tag.computecanada}
rm $TMP_WHEELHOUSE/$WHEEL_NAME
WHEEL_NAME=$new_wheel
fi
else
echo "No link dependency on libtorch found."
fi
fi
}
echo "Building wheel for $PACKAGE"
WORKDIR=/shared_tmp/build_wheels_tmp.$$
mkdir $WORKDIR
log_command pushd $WORKDIR
if [[ -z "$EBROOTGENTOO" ]]; then
module --force purge
module load nixpkgs gcc/$GCC_VERSION
else
ARCH_TO_LOAD="$EBVERSIONARCH"
# Figure out cheaply if the wheel needs an arch module to build
for mod in $MODULE_BUILD_DEPS; do
if [[ "${mod##arch/}" != "$mod" ]]; then
ARCH_TO_LOAD="${mod##arch/}"
fi
done
module --force purge
# if there are module dependencies, we really should build with our primary architecture rather than the compatibility one
if [[ -z "$MODULE_BUILD_DEPS" && -z "$MODULE_RUNTIME_DEPS" && "$YEAR" == "2020" ]]; then
module load arch/${ARCH_TO_LOAD:-sse3}
else
module load arch/${ARCH_TO_LOAD:-avx2}
fi
log_command module load gentoo/$YEAR gcc/$GCC_VERSION $MODULE_BUILD_DEPS_DEFAULT
fi
for pv in $PYTHON_VERSIONS; do
if [[ $pv =~ python/2 ]]; then
PYTHON_CMD=python2
else
PYTHON_CMD=python3
fi
PVDIR=${pv//\//-}
echo "Loading module $pv"
log_command module load $pv
log_command module load $MODULE_BUILD_DEPS_DEFAULT
setup
download
# # skip packages that are already in whl format
if [[ $ARCHNAME == *.whl ]]; then
# Patch the content of the wheel file.
log_command "$PATCH_WHEEL_COMMANDS"
# add a computecanada local_version
$SCRIPT_DIR/manipulate_wheels.py --insert_local_version --wheels $ARCHNAME --inplace && rm $ARCHNAME
WHEEL_NAME=$(ls *.whl)
cp -v $WHEEL_NAME $TMP_WHEELHOUSE
else
echo "Extracting archive $ARCHNAME..."
unzip $ARCHNAME -d $PVDIR &>/dev/null || tar xfv $ARCHNAME -C $PVDIR &>/dev/null
echo "Extraction done."
log_command pushd $PVDIR
log_command pushd $PACKAGE_FOLDER_NAME* || log_command pushd *
patch_function
build
log_command popd
log_command popd
rm $ARCHNAME
fi
log_command popd
adjust_numpy_requirements_based_on_link_info
adjust_torch_requirements_based_on_link_info
log_command pushd $WORKDIR
test_whl
if [[ $WHEEL_NAME =~ .*-py3-.* || $WHEEL_NAME =~ .*py2.py3.* ]]; then
echo "Wheel $WHEEL_NAME is compatible with all further versions of python. Breaking"
break
fi
done
log_command popd
if [[ $ARG_KEEP_BUILD_DIR -ne 1 ]]; then
rm -rf $WORKDIR
fi
if [[ ! -z "$ARCH_PRESENCE" ]]; then
echo -e "${COL_YEL}WARNING: ${PACKAGE} was built for ${ARCH_PRESENCE}.${COL_RST}"
echo "The wheel can be copied with : $SCRIPT_DIR/cp_wheels.sh --remove --arch $ARCH_PRESENCE"
fi
if [[ $ARG_AUTOCOPY -ne 1 ]]; then
echo "If you are satisfied with the built wheel, you can copy them to /cvmfs/soft.computecanada.ca/custom/python/wheelhouse/[generic,avx2,avx,sse3] and synchronize CVMFS"
else
$SCRIPT_DIR/cp_wheels.sh --remove
fi