forked from nanoporetech/modkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmac_compile_modkit.sh
More file actions
1168 lines (993 loc) · 42.3 KB
/
Copy pathmac_compile_modkit.sh
File metadata and controls
1168 lines (993 loc) · 42.3 KB
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# ================================================================================
# Modkit Compilation Script for macOS (Apple Silicon)
# ================================================================================
# This script automates the compilation of Oxford
# Nanopore's modkit bioinformatics tool on macOS with GPU acceleration support.
#
# Prerequisites: Apple Silicon Mac running macOS 11.0 or later
# macOS 12.3 or later is required only when MODKIT_MPS_SUPPORT=1
#
# What this script does:
# 0. Installs Xcode Command Line Tools
# 1. Installs Homebrew package manager
# 2. Installs rustup (Rust toolchain installer)
# 3. Installs Rust compiler (rustc) and Cargo build tool
# 4. Clones the modkit GitHub repository
# 5. Checks out the latest release version
# 6. Creates a Python virtual environment (only when MODKIT_MPS_SUPPORT=1)
# 7. Installs PyTorch in the virtual environment (only when MODKIT_MPS_SUPPORT=1)
# 8. Generates environment setup script (always; libtorch vars only when MODKIT_MPS_SUPPORT=1)
# 9. Builds modkit (with MPS GPU support when MODKIT_MPS_SUPPORT=1)
#
# Usage:
# bash mac_compile_modkit.sh <installation_directory> [modkit_version]
# bash mac_compile_modkit.sh --help
#
# Examples:
# bash mac_compile_modkit.sh /path/to/install # Install latest version to location
# bash mac_compile_modkit.sh ~/tools v0.5.0 # Install specific version to location
#
# MPS (GPU) support control:
# MODKIT_MPS_SUPPORT=0|1 Default: 0 (disabled). Set to 1 to build with Metal GPU support.
#
# Optional Python controls (only used when MODKIT_MPS_SUPPORT=1):
# MODKIT_PYTHON_PROVIDER=auto|system|pyenv|uv
# MODKIT_PYTHON_VERSION=3.11.9
# MODKIT_USE_UV=auto|0|1
#
# Examples:
# bash mac_compile_modkit.sh ~/tools
# bash mac_compile_modkit.sh ~/tools v0.5.0
# MODKIT_MPS_SUPPORT=1 bash mac_compile_modkit.sh ~/tools
# MODKIT_MPS_SUPPORT=1 MODKIT_PYTHON_PROVIDER=system bash mac_compile_modkit.sh ~/tools
# MODKIT_MPS_SUPPORT=1 MODKIT_PYTHON_PROVIDER=pyenv MODKIT_PYTHON_VERSION=3.11.9 bash mac_compile_modkit.sh ~/tools
# MODKIT_MPS_SUPPORT=1 MODKIT_PYTHON_PROVIDER=uv MODKIT_PYTHON_VERSION=3.12 bash mac_compile_modkit.sh ~/tools
# MODKIT_MPS_SUPPORT=1 MODKIT_USE_UV=0 bash mac_compile_modkit.sh ~/tools
# ================================================================================
set -euo pipefail # Exit on error, undefined variables, and pipe failures
# ================================================================================
# Configuration
# ================================================================================
print_usage() {
cat << EOF
Usage:
bash $0 <installation_directory> [modkit_version]
bash $0 --help
Arguments:
installation_directory Required. Path where modkit will be installed.
modkit_version Optional. Git tag to checkout. Default: latest.
MPS (GPU) support:
MODKIT_MPS_SUPPORT 0 | 1
Default: 0 (disabled). Set to 1 to compile with
Metal Performance Shaders GPU support. Requires
Python and PyTorch to be available.
Optional Python controls (only used when MODKIT_MPS_SUPPORT=1):
MODKIT_PYTHON_PROVIDER auto | system | pyenv | uv
Default: auto
MODKIT_PYTHON_VERSION Optional Python version request.
For uv: 3.11, 3.12, 3.12.2 are acceptable.
For pyenv: prefer an exact version, e.g. 3.11.9.
MODKIT_USE_UV auto | 0 | 1
auto: use uv for venv/pip if uv is available.
0: use python -m venv and python -m pip.
1: require/use uv venv and uv pip.
Examples:
bash $0 ~/tools
bash $0 ~/tools v0.5.0
MODKIT_MPS_SUPPORT=1 bash $0 ~/tools
MODKIT_MPS_SUPPORT=1 MODKIT_PYTHON_PROVIDER=system bash $0 ~/tools
MODKIT_MPS_SUPPORT=1 MODKIT_PYTHON_PROVIDER=pyenv MODKIT_PYTHON_VERSION=3.11.9 bash $0 ~/tools
MODKIT_MPS_SUPPORT=1 MODKIT_PYTHON_PROVIDER=uv MODKIT_PYTHON_VERSION=3.12 bash $0 ~/tools
MODKIT_MPS_SUPPORT=1 MODKIT_USE_UV=0 bash $0 ~/tools
EOF
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
print_usage
exit 0
fi
# Installation directory mandatory
if [[ $# -lt 1 ]]; then
print_usage
exit 1
fi
INSTALL_DIR="$1"
# Modkit version to install default latest release
MODKIT_VERSION="${2:-latest}"
# MPS (GPU) support: set to 1 to enable Metal Performance Shaders build
MODKIT_MPS_SUPPORT="${MODKIT_MPS_SUPPORT:-0}" # 0 | 1
# Python toolchain controls (only used when MODKIT_MPS_SUPPORT=1)
MODKIT_PYTHON_PROVIDER="${MODKIT_PYTHON_PROVIDER:-auto}" # auto | system | pyenv | uv
MODKIT_PYTHON_VERSION="${MODKIT_PYTHON_VERSION:-}" # optional
MODKIT_USE_UV="${MODKIT_USE_UV:-auto}" # auto | 0 | 1
# Resolved later by resolve_python_toolchain
MODKIT_PYTHON_BIN=""
MODKIT_PYTHON_PROVIDER_EFFECTIVE=""
MODKIT_UV_ENABLED=0
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# ================================================================================
# Helper Functions
# ================================================================================
print_step() {
echo ""
echo -e "${BLUE}===================================================================${NC}"
echo -e "${BLUE}STEP $1: $2${NC}"
echo -e "${BLUE}===================================================================${NC}"
}
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠ $1${NC}"
}
print_error() {
echo -e "${RED}✗ Error: $1${NC}"
}
command_exists() {
command -v "$1" &> /dev/null
}
press_enter_to_continue() {
echo ""
read -p "Press Enter to continue..."
}
ensure_command_or_brew_install() {
local CMD="$1"
local FORMULA="${2:-$1}"
if command_exists "${CMD}"; then
return 0
fi
if command_exists brew; then
echo "Installing ${FORMULA} via Homebrew..."
brew install "${FORMULA}"
else
print_error "${CMD} is required but was not found, and Homebrew is unavailable"
exit 1
fi
}
resolve_python_toolchain() {
echo ""
echo "Resolving Python toolchain..."
case "${MODKIT_PYTHON_PROVIDER}" in
auto|system|pyenv|uv) ;;
*)
print_error "Invalid MODKIT_PYTHON_PROVIDER='${MODKIT_PYTHON_PROVIDER}'. Use auto, system, pyenv, or uv."
exit 1
;;
esac
case "${MODKIT_PYTHON_PROVIDER}" in
system)
MODKIT_PYTHON_PROVIDER_EFFECTIVE="system"
MODKIT_PYTHON_BIN="$(command -v python3 || true)"
;;
pyenv)
ensure_command_or_brew_install pyenv
MODKIT_PYTHON_PROVIDER_EFFECTIVE="pyenv"
if [[ -n "${MODKIT_PYTHON_VERSION}" ]]; then
pyenv install -s "${MODKIT_PYTHON_VERSION}"
local PYENV_PREFIX
PYENV_PREFIX="$(pyenv prefix "${MODKIT_PYTHON_VERSION}")"
MODKIT_PYTHON_BIN="${PYENV_PREFIX}/bin/python3"
else
MODKIT_PYTHON_BIN="$(pyenv which python3)"
fi
;;
uv)
ensure_command_or_brew_install uv
MODKIT_PYTHON_PROVIDER_EFFECTIVE="uv"
if [[ -n "${MODKIT_PYTHON_VERSION}" ]]; then
uv python install "${MODKIT_PYTHON_VERSION}"
MODKIT_PYTHON_BIN="$(uv python find "${MODKIT_PYTHON_VERSION}")"
else
MODKIT_PYTHON_BIN="$(uv python find python3 2>/dev/null || uv python find)"
fi
;;
auto)
if [[ -n "${MODKIT_PYTHON_VERSION}" && $(command_exists uv && echo yes || echo no) == "yes" ]]; then
MODKIT_PYTHON_PROVIDER_EFFECTIVE="uv"
uv python install "${MODKIT_PYTHON_VERSION}"
MODKIT_PYTHON_BIN="$(uv python find "${MODKIT_PYTHON_VERSION}")"
elif [[ -n "${MODKIT_PYTHON_VERSION}" && $(command_exists pyenv && echo yes || echo no) == "yes" ]]; then
MODKIT_PYTHON_PROVIDER_EFFECTIVE="pyenv"
pyenv install -s "${MODKIT_PYTHON_VERSION}"
local PYENV_PREFIX
PYENV_PREFIX="$(pyenv prefix "${MODKIT_PYTHON_VERSION}")"
MODKIT_PYTHON_BIN="${PYENV_PREFIX}/bin/python3"
else
# Suggestion 1 fix: warn if version was requested but cannot be honoured
if [[ -n "${MODKIT_PYTHON_VERSION}" ]]; then
print_warning "MODKIT_PYTHON_VERSION='${MODKIT_PYTHON_VERSION}' was requested but neither uv nor pyenv is available."
print_warning "Falling back to system python3. The installed Python may not match the requested version."
read -p "Continue anyway? (y/n): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborted. Install uv (brew install uv) or pyenv (brew install pyenv) and retry."
exit 1
fi
fi
MODKIT_PYTHON_PROVIDER_EFFECTIVE="system"
MODKIT_PYTHON_BIN="$(command -v python3 || true)"
fi
;;
esac
if [[ -z "${MODKIT_PYTHON_BIN}" || ! -x "${MODKIT_PYTHON_BIN}" ]]; then
print_error "Could not resolve a usable Python executable"
exit 1
fi
case "${MODKIT_USE_UV}" in
1|true|yes)
ensure_command_or_brew_install uv
MODKIT_UV_ENABLED=1
;;
0|false|no)
MODKIT_UV_ENABLED=0
;;
auto)
if command_exists uv; then
MODKIT_UV_ENABLED=1
else
MODKIT_UV_ENABLED=0
fi
;;
*)
print_error "Invalid MODKIT_USE_UV='${MODKIT_USE_UV}'. Use auto, 0, or 1."
exit 1
;;
esac
echo "Python configuration:"
echo " Provider requested: ${MODKIT_PYTHON_PROVIDER}"
echo " Provider used: ${MODKIT_PYTHON_PROVIDER_EFFECTIVE}"
echo " Version requested: ${MODKIT_PYTHON_VERSION:-default}"
echo " Python executable: ${MODKIT_PYTHON_BIN}"
echo " Python version: $("${MODKIT_PYTHON_BIN}" --version 2>&1)"
echo " uv for venv/pip: $([[ "${MODKIT_UV_ENABLED}" == "1" ]] && echo yes || echo no)"
}
# ================================================================================
# STEP 0: Install Xcode Command Line Tools
# ================================================================================
install_xcode_tools() {
print_step 0 "Installing Xcode Command Line Tools"
if xcode-select -p &> /dev/null; then
print_success "Xcode Command Line Tools already installed at: $(xcode-select -p)"
else
echo "Installing Xcode Command Line Tools..."
echo "A dialog will appear - please click 'Install' and wait for completion."
xcode-select --install
echo ""
echo "Waiting for Xcode Command Line Tools installation to complete..."
echo "This may take several minutes (timeout: 10 minutes)."
# Wait for installation to complete with a timeout
local MAX_WAIT=600 # 10 minutes in seconds
local WAITED=0
while ! xcode-select -p &> /dev/null; do
sleep 5
WAITED=$((WAITED + 5))
if [[ ${WAITED} -ge ${MAX_WAIT} ]]; then
print_error "Timed out waiting for Xcode Command Line Tools installation"
echo "Please try one of the following:"
echo " 1. Re-run: xcode-select --install"
echo " 2. Install via: System Settings > Software Update"
echo " 3. Download from: https://developer.apple.com/download/more/"
echo "Then re-run this script."
exit 1
fi
done
print_success "Xcode Command Line Tools installed successfully"
fi
# Verify installation
if xcode-select -p &> /dev/null; then
print_success "Verification: Xcode tools are available"
else
print_error "Xcode Command Line Tools installation failed"
exit 1
fi
}
# ================================================================================
# STEP 1: Install Homebrew
# ================================================================================
install_homebrew() {
print_step 1 "Installing Homebrew Package Manager"
if command_exists brew; then
print_success "Homebrew already installed at: $(which brew)"
echo "Current version: $(brew --version | head -n1)"
else
echo "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to PATH based on architecture (only if not already in .zprofile)
if [[ $(uname -m) == "arm64" ]]; then
# Apple Silicon
local BREW_LINE='eval "$(/opt/homebrew/bin/brew shellenv)"'
if ! grep -qF "${BREW_LINE}" "${HOME}/.zprofile" 2>/dev/null; then
echo "${BREW_LINE}" >> "${HOME}/.zprofile"
fi
eval "$(/opt/homebrew/bin/brew shellenv)"
else
# Intel
local BREW_LINE='eval "$(/usr/local/bin/brew shellenv)"'
if ! grep -qF "${BREW_LINE}" "${HOME}/.zprofile" 2>/dev/null; then
echo "${BREW_LINE}" >> "${HOME}/.zprofile"
fi
eval "$(/usr/local/bin/brew shellenv)"
fi
print_success "Homebrew installed successfully"
fi
# Verify installation
if command_exists brew; then
print_success "Verification: Homebrew is available"
brew --version
else
print_error "Homebrew installation failed"
exit 1
fi
}
# ================================================================================
# STEP 2: Install rustup using Homebrew
# ================================================================================
install_rustup() {
print_step 2 "Installing rustup (Rust Toolchain Installer)"
if command_exists rustup; then
print_success "rustup already installed at: $(which rustup)"
echo "Current version: $(rustup --version)"
elif command_exists rustup-init; then
print_success "rustup-init already available at: $(which rustup-init)"
echo "Will be used in the next step to install the Rust toolchain."
else
echo "Installing rustup-init via Homebrew..."
brew install rustup-init
print_success "rustup-init installed via Homebrew"
fi
}
# ================================================================================
# STEP 3: Install Rust and Cargo using rustup
# ================================================================================
install_rust_cargo() {
print_step 3 "Installing Rust Compiler and Cargo Build Tool"
if command_exists rustc && command_exists cargo; then
print_success "Rust and Cargo already installed"
echo " rustc version: $(rustc --version)"
echo " cargo version: $(cargo --version)"
else
echo "Running rustup-init to install Rust toolchain..."
rustup-init -y --default-toolchain stable
# Source cargo environment
source "${HOME}/.cargo/env"
print_success "Rust toolchain installed successfully"
fi
# Ensure cargo is in PATH for current session
if [[ -f "${HOME}/.cargo/env" ]]; then
source "${HOME}/.cargo/env"
fi
# Verify installation
if command_exists rustc && command_exists cargo; then
print_success "Verification: Rust and Cargo are available"
echo " rustc: $(which rustc) - $(rustc --version)"
echo " cargo: $(which cargo) - $(cargo --version)"
else
print_error "Rust/Cargo installation failed"
echo "Please run: source ${HOME}/.cargo/env"
exit 1
fi
}
# ================================================================================
# STEP 4: Clone Modkit GitHub Repository
# ================================================================================
clone_modkit_repo() {
print_step 4 "Cloning Modkit GitHub Repository"
mkdir -p "${INSTALL_DIR}"
cd "${INSTALL_DIR}"
MODKIT_REPO_DIR="${INSTALL_DIR}/modkit"
if [[ -d "${MODKIT_REPO_DIR}/.git" ]]; then
print_warning "Modkit repository already exists at: ${MODKIT_REPO_DIR}"
echo "Updating existing repository..."
cd "${MODKIT_REPO_DIR}"
git fetch --all --tags
# Reset any stale working tree changes so the subsequent checkout is clean
git reset --hard
git clean -fd
print_success "Repository updated and working tree cleaned"
else
echo "Cloning modkit repository..."
git clone https://github.com/nanoporetech/modkit.git
cd modkit
print_success "Repository cloned successfully"
fi
if [[ -d "${MODKIT_REPO_DIR}/.git" ]]; then
print_success "Verification: Repository available at ${MODKIT_REPO_DIR}"
else
print_error "Failed to clone modkit repository"
exit 1
fi
}
# ================================================================================
# STEP 5: Checkout Latest Release (or specified version)
# ================================================================================
checkout_version() {
print_step 5 "Checking Out Modkit Version"
cd "${MODKIT_REPO_DIR}"
if [[ "${MODKIT_VERSION}" == "latest" ]]; then
echo "Fetching latest release version..."
LATEST_TAG=$(git tag -l "v*" --sort=-v:refname 2>/dev/null | head -n1 || echo "")
if [[ -z "${LATEST_TAG}" ]]; then
LATEST_TAG=$(git tag --sort=-v:refname 2>/dev/null | head -n1 || echo "")
fi
if [[ -z "${LATEST_TAG}" ]]; then
print_warning "No release tags found, using main branch"
git checkout main
git pull origin main
MODKIT_VERSION="main"
else
echo "Latest release: ${LATEST_TAG}"
git checkout "${LATEST_TAG}"
MODKIT_VERSION="${LATEST_TAG}"
fi
else
echo "Checking out version: ${MODKIT_VERSION}"
git checkout "${MODKIT_VERSION}"
fi
print_success "Using modkit version: ${MODKIT_VERSION}"
echo "Current commit: $(git rev-parse --short HEAD)"
# Verify Cargo.toml version matches the git tag as a sanity check
local CARGO_VERSION
CARGO_VERSION=$(grep -m1 '^version' "${MODKIT_REPO_DIR}/modkit/Cargo.toml" 2>/dev/null \
| sed 's/.*"\(.*\)".*/\1/' || echo "unknown")
echo "Cargo crate version: ${CARGO_VERSION}"
if [[ "${MODKIT_VERSION}" != "main" && \
"${MODKIT_VERSION}" != "v${CARGO_VERSION}" && \
"${MODKIT_VERSION}" != "${CARGO_VERSION}" ]]; then
print_warning "Git tag '${MODKIT_VERSION}' does not match Cargo.toml version '${CARGO_VERSION}'."
print_warning "The compiled binary will report version ${CARGO_VERSION}."
print_warning "This is an upstream discrepancy in the modkit repository."
else
print_success "Git tag matches Cargo.toml version: ${CARGO_VERSION}"
fi
}
# ================================================================================
# STEP 6: Create Python Virtual Environment
# ================================================================================
create_venv() {
print_step 6 "Creating Python Virtual Environment"
VENV_DIR="${INSTALL_DIR}/venv_modkit"
local FINGERPRINT_FILE="${VENV_DIR}/.python_info"
echo "Using Python: ${MODKIT_PYTHON_BIN} - $("${MODKIT_PYTHON_BIN}" --version 2>&1)"
# Suggestion 2 fix: validate existing venv against current provider/version
if [[ -d "${VENV_DIR}" ]]; then
if [[ -f "${FINGERPRINT_FILE}" ]]; then
local SAVED_PROVIDER SAVED_VERSION
SAVED_PROVIDER=$(grep '^provider=' "${FINGERPRINT_FILE}" | cut -d= -f2)
SAVED_VERSION=$(grep '^python_bin=' "${FINGERPRINT_FILE}" | cut -d= -f2)
if [[ "${SAVED_PROVIDER}" != "${MODKIT_PYTHON_PROVIDER_EFFECTIVE}" || \
"${SAVED_VERSION}" != "${MODKIT_PYTHON_BIN}" ]]; then
print_warning "Existing venv was created with a different Python configuration:"
print_warning " Saved provider: ${SAVED_PROVIDER} (current: ${MODKIT_PYTHON_PROVIDER_EFFECTIVE})"
print_warning " Saved python_bin: ${SAVED_VERSION}"
print_warning " Current python_bin: ${MODKIT_PYTHON_BIN}"
read -p "Delete and recreate the venv with the new configuration? (y/n): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
rm -rf "${VENV_DIR}"
echo "Deleted old virtual environment."
else
print_warning "Keeping existing venv. Build may use wrong Python."
fi
else
print_success "Existing venv matches current configuration. Reusing."
fi
else
print_warning "Virtual environment exists but has no fingerprint file."
print_warning "Cannot verify it matches the current Python configuration. Reusing."
fi
fi
if [[ ! -d "${VENV_DIR}" ]]; then
echo "Creating virtual environment at: ${VENV_DIR}"
if [[ "${MODKIT_UV_ENABLED}" == "1" ]]; then
uv venv --python "${MODKIT_PYTHON_BIN}" "${VENV_DIR}"
else
"${MODKIT_PYTHON_BIN}" -m venv "${VENV_DIR}"
fi
# Write fingerprint
printf 'provider=%s\npython_bin=%s\n' \
"${MODKIT_PYTHON_PROVIDER_EFFECTIVE}" \
"${MODKIT_PYTHON_BIN}" > "${FINGERPRINT_FILE}"
print_success "Virtual environment created"
fi
# Verify virtual environment
if [[ -f "${VENV_DIR}/bin/activate" && -x "${VENV_DIR}/bin/python" ]]; then
print_success "Verification: Virtual environment ready"
echo " Venv Python: ${VENV_DIR}/bin/python - $("${VENV_DIR}/bin/python" --version 2>&1)"
else
print_error "Failed to create virtual environment"
exit 1
fi
}
# ================================================================================
# STEP 7: Activate Environment and Install PyTorch
# ================================================================================
install_pytorch() {
print_step 7 "Installing PyTorch in Virtual Environment"
local VENV_PYTHON="${VENV_DIR}/bin/python"
# Deactivate conda only if an environment is actually active.
if [[ -n "${CONDA_DEFAULT_ENV:-}" ]] && command_exists conda; then
print_warning "Conda environment '${CONDA_DEFAULT_ENV}' is active. Deactivating to avoid conflicts..."
eval "$(conda shell.bash hook)"
conda deactivate 2>/dev/null || true
print_success "Conda environment deactivated"
fi
# Activate virtual environment
echo "Activating virtual environment..."
source "${VENV_DIR}/bin/activate"
print_success "Virtual environment activated: ${VIRTUAL_ENV}"
# Install PyTorch
echo "Installing PyTorch and NumPy and dependencies (this may take a few minutes)..."
if [[ "${MODKIT_UV_ENABLED}" == "1" ]]; then
uv pip install --python "${VENV_PYTHON}" torch numpy
else
"${VENV_PYTHON}" -m pip install --upgrade pip
"${VENV_PYTHON}" -m pip install torch numpy
fi
print_success "PyTorch and NumPy installed successfully"
# Verify PyTorch installation
echo "Verifying PyTorch installation..."
"${VENV_PYTHON}" -c "import torch; print(f'PyTorch version: {torch.__version__}')" || {
print_error "PyTorch verification failed"
exit 1
}
# Check MPS (Metal Performance Shaders) availability for Apple Silicon
if [[ $(uname -m) == "arm64" ]]; then
echo "Checking Metal Performance Shaders (GPU) support..."
"${VENV_PYTHON}" -c "import torch; print(f'MPS available: {torch.backends.mps.is_available()}')"
fi
print_success "Verification: PyTorch is working correctly"
}
# ================================================================================
# STEP 8: Generate Environment Setup Script
# ================================================================================
setup_environment_variables() {
SETUP_SCRIPT="${INSTALL_DIR}/setup_modkit_env.sh"
if [[ "${MODKIT_MPS_SUPPORT}" == "1" ]]; then
print_step 8 "Setting Up Environment Variables for libtorch"
echo "Generating MPS environment setup script at: ${SETUP_SCRIPT}"
cat > "${SETUP_SCRIPT}" << 'SETUP_EOF'
#!/bin/bash
# ================================================================================
# Modkit Environment Setup Script (MPS build)
# ================================================================================
# IMPORTANT: This script must be SOURCED, not executed.
#
# Usage:
# source setup_modkit_env.sh <installation_directory> [--verbose]
#
# Options:
# --verbose Print environment configuration details. Omit for silent setup.
#
# After sourcing:
# modkit --version
# modkit pileup input.bam output.bed
# modkit open-chromatin predict --device mps -i input.bam -o output.bed
# ================================================================================
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "Error: This script must be sourced, not executed."
echo "Usage: source ${0} <installation_directory> [--verbose]"
exit 1
fi
# Parse arguments
_MODKIT_VERBOSE=0
_MODKIT_INSTALL_DIR=""
for _arg in "$@"; do
case "${_arg}" in
--verbose|-v) _MODKIT_VERBOSE=1 ;;
*) _MODKIT_INSTALL_DIR="${_arg}" ;;
esac
done
unset _arg
if [[ -z "${_MODKIT_INSTALL_DIR}" ]]; then
echo "Usage: source setup_modkit_env.sh <installation_directory> [--verbose]"
unset _MODKIT_VERBOSE _MODKIT_INSTALL_DIR
return 1
fi
MODKIT_INSTALL_DIR="${_MODKIT_INSTALL_DIR}"
unset _MODKIT_INSTALL_DIR
if [[ ! -d "${MODKIT_INSTALL_DIR}" ]]; then
echo "Error: Installation directory not found: ${MODKIT_INSTALL_DIR}"
unset MODKIT_INSTALL_DIR _MODKIT_VERBOSE
return 1
fi
MODKIT_VENV_DIR="${MODKIT_INSTALL_DIR}/venv_modkit"
MODKIT_REPO_DIR="${MODKIT_INSTALL_DIR}/modkit"
MODKIT_BINARY="${MODKIT_REPO_DIR}/target/release/modkit"
if [[ ! -d "${MODKIT_VENV_DIR}" ]]; then
echo "Error: Virtual environment not found: ${MODKIT_VENV_DIR}"
unset MODKIT_INSTALL_DIR MODKIT_VENV_DIR MODKIT_REPO_DIR MODKIT_BINARY _MODKIT_VERBOSE
return 1
fi
# Deactivate conda only if actually active
if command -v conda &> /dev/null && [[ -n "${CONDA_DEFAULT_ENV:-}" ]]; then
[[ "${_MODKIT_VERBOSE}" == "1" ]] && \
echo "Deactivating conda environment '${CONDA_DEFAULT_ENV}'..."
eval "$(conda shell.bash hook)"
conda deactivate 2>/dev/null || true
fi
# Activate the Python virtual environment (suppress output)
source "${MODKIT_VENV_DIR}/bin/activate"
# Detect Python version from venv
MODKIT_PYTHON_VER=$("${MODKIT_VENV_DIR}/bin/python" -c \
"import sys; print(f'python{sys.version_info.major}.{sys.version_info.minor}')")
export LIBTORCH_USE_PYTORCH=1
export LIBTORCH_BYPASS_VERSION_CHECK=1
export LIBTORCH="${MODKIT_VENV_DIR}/lib/${MODKIT_PYTHON_VER}/site-packages/torch"
export DYLD_LIBRARY_PATH="${LIBTORCH}/lib${DYLD_LIBRARY_PATH:+:${DYLD_LIBRARY_PATH}}"
export LD_LIBRARY_PATH="${LIBTORCH}/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
export PATH="${MODKIT_REPO_DIR}/target/release:${PATH}"
if [[ "${_MODKIT_VERBOSE}" == "1" ]]; then
echo ""
echo "Modkit environment configured:"
echo " LIBTORCH_USE_PYTORCH = ${LIBTORCH_USE_PYTORCH}"
echo " LIBTORCH_BYPASS_VERSION_CHECK = ${LIBTORCH_BYPASS_VERSION_CHECK}"
echo " LIBTORCH = ${LIBTORCH}"
echo " DYLD_LIBRARY_PATH = ${DYLD_LIBRARY_PATH}"
echo " LD_LIBRARY_PATH = ${LD_LIBRARY_PATH}"
echo " PATH (prepended) = ${MODKIT_REPO_DIR}/target/release"
echo " Python = ${MODKIT_VENV_DIR}/bin/python" \
"($("${MODKIT_VENV_DIR}/bin/python" --version 2>&1), ${MODKIT_PYTHON_VER})"
if [[ -d "${LIBTORCH}" ]]; then
echo " libtorch path = OK"
else
echo " WARNING: LIBTORCH path does not exist: ${LIBTORCH}"
fi
if [[ -f "${MODKIT_BINARY}" ]]; then
echo " modkit binary = ${MODKIT_BINARY}"
echo ""
echo "Ready. Run 'modkit --version' to verify."
else
echo " modkit binary = NOT FOUND"
fi
fi
unset MODKIT_INSTALL_DIR MODKIT_VENV_DIR MODKIT_REPO_DIR MODKIT_BINARY MODKIT_PYTHON_VER _MODKIT_VERBOSE
SETUP_EOF
chmod +x "${SETUP_SCRIPT}"
print_success "Environment setup script generated at: ${SETUP_SCRIPT}"
echo "Sourcing environment setup script..."
local SAVED_MODKIT_REPO_DIR="${MODKIT_REPO_DIR}"
local SAVED_VENV_DIR="${VENV_DIR}"
# When sourcing during install, use --verbose so the installer still shows full output
source "${SETUP_SCRIPT}" "${INSTALL_DIR}" --verbose
MODKIT_REPO_DIR="${SAVED_MODKIT_REPO_DIR}"
VENV_DIR="${SAVED_VENV_DIR}"
echo ""
echo "Verifying paths..."
if [[ -d "${LIBTORCH}" ]]; then
print_success "LIBTORCH path exists: ${LIBTORCH}"
else
print_error "LIBTORCH path not found: ${LIBTORCH}"
exit 1
fi
if [[ -d "${LIBTORCH}/lib" ]]; then
print_success "libtorch libraries found"
echo "Sample libraries:"
ls "${LIBTORCH}/lib" | grep -E "\.dylib$" | head -5
else
print_error "libtorch lib directory not found"
exit 1
fi
echo ""
echo "To set up this environment in a new terminal session, run:"
echo " source \"${SETUP_SCRIPT}\" \"${INSTALL_DIR}\""
else
print_step 8 "Generating Environment Setup Script"
echo "Generating environment setup script at: ${SETUP_SCRIPT}"
cat > "${SETUP_SCRIPT}" << 'SETUP_EOF'
#!/bin/bash
# ================================================================================
# Modkit Environment Setup Script
# ================================================================================
# IMPORTANT: This script must be SOURCED, not executed.
#
# Usage:
# source setup_modkit_env.sh <installation_directory> [--verbose]
#
# Options:
# --verbose Print environment configuration details. Omit for silent setup.
#
# After sourcing:
# modkit --version
# modkit pileup input.bam output.bed
# ================================================================================
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "Error: This script must be sourced, not executed."
echo "Usage: source ${0} <installation_directory> [--verbose]"
exit 1
fi
# Parse arguments
_MODKIT_VERBOSE=0
_MODKIT_INSTALL_DIR=""
for _arg in "$@"; do
case "${_arg}" in
--verbose|-v) _MODKIT_VERBOSE=1 ;;
*) _MODKIT_INSTALL_DIR="${_arg}" ;;
esac
done
unset _arg
if [[ -z "${_MODKIT_INSTALL_DIR}" ]]; then
echo "Usage: source setup_modkit_env.sh <installation_directory> [--verbose]"
unset _MODKIT_VERBOSE _MODKIT_INSTALL_DIR
return 1
fi
MODKIT_INSTALL_DIR="${_MODKIT_INSTALL_DIR}"
unset _MODKIT_INSTALL_DIR
if [[ ! -d "${MODKIT_INSTALL_DIR}" ]]; then
echo "Error: Installation directory not found: ${MODKIT_INSTALL_DIR}"
unset MODKIT_INSTALL_DIR _MODKIT_VERBOSE
return 1
fi
MODKIT_REPO_DIR="${MODKIT_INSTALL_DIR}/modkit"
MODKIT_BINARY="${MODKIT_REPO_DIR}/target/release/modkit"
export PATH="${MODKIT_REPO_DIR}/target/release:${PATH}"
if [[ "${_MODKIT_VERBOSE}" == "1" ]]; then
echo ""
echo "Modkit environment configured:"
echo " PATH (prepended) = ${MODKIT_REPO_DIR}/target/release"
if [[ -f "${MODKIT_BINARY}" ]]; then
echo " modkit binary = ${MODKIT_BINARY}"
echo ""
echo "Ready. Run 'modkit --version' to verify."
else
echo " modkit binary = NOT FOUND"
fi
fi
unset MODKIT_INSTALL_DIR MODKIT_REPO_DIR MODKIT_BINARY _MODKIT_VERBOSE
SETUP_EOF
chmod +x "${SETUP_SCRIPT}"
print_success "Environment setup script generated at: ${SETUP_SCRIPT}"
echo ""
echo "To add modkit to PATH in a new terminal session, run:"
echo " source \"${SETUP_SCRIPT}\" \"${INSTALL_DIR}\""
fi
}
# ================================================================================
# STEP 9: Build Modkit with macOS GPU Support (continued)
# ================================================================================
build_modkit() {
if [[ "${MODKIT_MPS_SUPPORT}" == "1" ]]; then
print_step 9 "Building Modkit with macOS GPU (MPS) Support"
else
print_step 9 "Building Modkit"
fi
cd "${MODKIT_REPO_DIR}"
local BUILD_FEATURES
if [[ "${MODKIT_MPS_SUPPORT}" == "1" ]]; then
BUILD_FEATURES="accelerate,tch"
else
BUILD_FEATURES="accelerate"
fi
echo "Build configuration:"
echo " Features: ${BUILD_FEATURES}"
echo " Mode: release (optimized)"
if [[ "${MODKIT_MPS_SUPPORT}" == "1" ]]; then
echo " GPU Support: Metal Performance Shaders (MPS)"
else
echo " GPU Support: none"
fi
echo " Architecture: $(uname -m)"
echo ""
echo "This will take several minutes (5-15 minutes depending on your Mac)..."
echo "Please be patient while Cargo compiles modkit and its dependencies."
echo ""
# Run cargo build
if cargo build --release --features "${BUILD_FEATURES}"; then
print_success "Modkit compiled successfully!"
else
print_error "Compilation failed"
echo ""
echo "Troubleshooting tips:"
echo " 1. Set up the build environment first:"
echo " source \"${INSTALL_DIR}/setup_modkit_env.sh\" \"${INSTALL_DIR}\""
if [[ "${MODKIT_MPS_SUPPORT}" == "1" ]]; then
echo " 2. Check that PyTorch is installed:"
echo " \"${VENV_DIR}/bin/python\" -m pip list | grep torch"
fi
echo " 3. Clean and rebuild:"
echo " cd \"${MODKIT_REPO_DIR}\" && cargo clean && cargo build --release --features \"${BUILD_FEATURES}\""
exit 1
fi
# Verify binary was created
MODKIT_BINARY="${MODKIT_REPO_DIR}/target/release/modkit"
if [[ -f "${MODKIT_BINARY}" ]]; then
print_success "Binary created at: ${MODKIT_BINARY}"
# Get file size
BINARY_SIZE=$(du -h "${MODKIT_BINARY}" | cut -f1)
echo " Binary size: ${BINARY_SIZE}"
# Test binary
echo ""
echo "Testing modkit binary..."
if "${MODKIT_BINARY}" --version; then
print_success "Modkit is working correctly!"
else
print_warning "Binary exists but version check failed"
echo "You may need to set DYLD_LIBRARY_PATH when running modkit"
fi
# Check available subcommands
echo ""
echo "Available modkit commands:"
"${MODKIT_BINARY}" --help | grep -A 20 "SUBCOMMANDS:" || true
else
print_error "Binary not found at expected location: ${MODKIT_BINARY}"
exit 1
fi
}
# ================================================================================
# Save Installation Info
# ================================================================================
save_installation_info() {
INFO_FILE="${INSTALL_DIR}/installation_info.txt"
SETUP_SCRIPT="${INSTALL_DIR}/setup_modkit_env.sh"
cat > "${INFO_FILE}" << EOF
Modkit Installation Information
================================
Installation Date: $(date)
macOS Version: $(sw_vers -productVersion)
Architecture: $(uname -m)
Hostname: $(hostname)
Installation Paths:
-------------------
Installation Directory: ${INSTALL_DIR}
Modkit Repository: ${MODKIT_REPO_DIR}
Modkit Binary: ${MODKIT_REPO_DIR}/target/release/modkit
Environment Setup Script: ${SETUP_SCRIPT}
Versions:
---------
Modkit Version: ${MODKIT_VERSION}
Rust Version: $(rustc --version)
Cargo Version: $(cargo --version)
MPS Support Enabled: $([[ "${MODKIT_MPS_SUPPORT}" == "1" ]] && echo "yes" || echo "no")
EOF
if [[ "${MODKIT_MPS_SUPPORT}" == "1" ]]; then
local UV_VERSION_INFO PYENV_VERSION_INFO VENV_PYTHON
VENV_PYTHON="${VENV_DIR}/bin/python"
UV_VERSION_INFO="$(command_exists uv && uv --version || echo "Not available")"
PYENV_VERSION_INFO="$(command_exists pyenv && pyenv --version || echo "Not available")"
cat >> "${INFO_FILE}" << EOF
Virtual Environment: ${VENV_DIR}