-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild.py
More file actions
221 lines (172 loc) · 6.53 KB
/
Copy pathbuild.py
File metadata and controls
221 lines (172 loc) · 6.53 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
#!/usr/bin/env python3
"""
Build Script for Solana Cold Wallet
Handles:
1. Installing Python dependencies from local_requirements.txt
2. Building the Rust secure signer library
"""
import subprocess
import sys
import os
from pathlib import Path
def print_step(msg: str):
print(f"\n{'='*60}")
print(f" {msg}")
print(f"{'='*60}\n")
def run_command(cmd: list, cwd: str = None, check: bool = True) -> bool:
"""Run a command and return success status"""
try:
print(f"Running: {' '.join(cmd)}")
result = subprocess.run(
cmd,
cwd=cwd,
check=check,
capture_output=False,
text=True
)
return result.returncode == 0
except subprocess.CalledProcessError as e:
print(f"Command failed with exit code {e.returncode}")
return False
except FileNotFoundError:
print(f"Command not found: {cmd[0]}")
return False
def install_python_dependencies() -> bool:
"""Install Python dependencies from local_requirements.txt"""
print_step("Installing Python Dependencies")
requirements_file = Path("local_requirements.txt")
if not requirements_file.exists():
print("Warning: local_requirements.txt not found")
return True
return run_command([
sys.executable, "-m", "pip", "install",
"-r", "local_requirements.txt",
"--quiet"
])
def check_rust_installed() -> bool:
"""Check if Rust/Cargo is installed"""
try:
result = subprocess.run(
["cargo", "--version"],
capture_output=True,
text=True
)
if result.returncode == 0:
print(f"Found: {result.stdout.strip()}")
return True
except FileNotFoundError:
pass
return False
def install_rust() -> bool:
"""Attempt to install Rust using rustup"""
print("Rust not found. Attempting to install...")
if sys.platform == "win32":
print("Please install Rust manually from: https://rustup.rs/")
return False
try:
result = subprocess.run(
["sh", "-c", "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y"],
check=True
)
os.environ["PATH"] = f"{Path.home()}/.cargo/bin:" + os.environ.get("PATH", "")
return True
except subprocess.CalledProcessError:
print("Failed to install Rust. Please install manually from: https://rustup.rs/")
return False
def build_rust_signer(release: bool = True) -> bool:
"""Build the Rust secure signer library"""
print_step("Building Rust Secure Signer")
signer_dir = Path("secure_signer")
if not signer_dir.exists():
print(f"Error: {signer_dir} directory not found")
return False
if not (signer_dir / "Cargo.toml").exists():
print(f"Error: Cargo.toml not found in {signer_dir}")
return False
if not check_rust_installed():
if not install_rust():
return False
cmd = ["cargo", "build", "--features", "ffi"]
if release:
cmd.append("--release")
success = run_command(cmd, cwd=str(signer_dir))
if success:
target_dir = "release" if release else "debug"
binary_path = signer_dir / "target" / target_dir / "solana-signer"
lib_path = signer_dir / "target" / target_dir / "libsecure_signer.so"
if sys.platform == "darwin":
lib_path = signer_dir / "target" / target_dir / "libsecure_signer.dylib"
elif sys.platform == "win32":
lib_path = signer_dir / "target" / target_dir / "secure_signer.dll"
binary_path = signer_dir / "target" / target_dir / "solana-signer.exe"
print(f"\nBuild successful!")
if binary_path.exists():
print(f" Binary: {binary_path}")
if lib_path.exists():
print(f" Library: {lib_path}")
return success
def run_rust_tests() -> bool:
"""Run Rust tests with permissive memory mode"""
print_step("Running Rust Tests")
signer_dir = Path("secure_signer")
if not signer_dir.exists():
print("Skipping tests: secure_signer directory not found")
return True
env = os.environ.copy()
env["SIGNER_ALLOW_INSECURE_MEMORY"] = "1"
try:
result = subprocess.run(
["cargo", "test", "--features", "ffi"],
cwd=str(signer_dir),
env=env,
check=False
)
return result.returncode == 0
except FileNotFoundError:
print("cargo not found, skipping tests")
return True
def build_all(release: bool = True, run_tests: bool = False) -> bool:
"""Run the complete build process"""
print("\n" + "="*60)
print(" SOLANA COLD WALLET BUILD")
print("="*60)
if not install_python_dependencies():
print("\nWarning: Some Python dependencies may not have installed correctly")
if not build_rust_signer(release=release):
print("\nError: Rust build failed")
return False
if run_tests:
if not run_rust_tests():
print("\nWarning: Some tests failed")
print_step("Build Complete!")
print("You can now run: python main.py")
return True
def is_built() -> bool:
"""Check if the Rust signer is already built"""
signer_dir = Path("secure_signer")
# Library name is based on crate name (solana_secure_signer)
if sys.platform == "darwin":
lib_name = "libsolana_secure_signer.dylib"
elif sys.platform == "win32":
lib_name = "solana_secure_signer.dll"
else:
lib_name = "libsolana_secure_signer.so"
release_lib = signer_dir / "target" / "release" / lib_name
debug_lib = signer_dir / "target" / "debug" / lib_name
return release_lib.exists() or debug_lib.exists()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Build Solana Cold Wallet")
parser.add_argument("--debug", action="store_true", help="Build in debug mode")
parser.add_argument("--test", action="store_true", help="Run tests after building")
parser.add_argument("--check", action="store_true", help="Only check if build exists")
args = parser.parse_args()
if args.check:
if is_built():
print("Rust signer is built")
sys.exit(0)
else:
print("Rust signer needs to be built")
sys.exit(1)
success = build_all(release=not args.debug, run_tests=args.test)
sys.exit(0 if success else 1)