Skip to content

Commit 6fc17cd

Browse files
committed
fix(ios):Implement iOS stub packages for incompatible dependencies
Replace simple module replacement with comprehensive stub implementations for go-m1cpu and gopsutil packages. Create local stub directories with iOS-compatible implementations that provide the same API surface but return safe default values. Add retry logic in build script to automatically re-run dependency fixes when iOS compatibility issues are detected.
1 parent 0608a90 commit 6fc17cd

File tree

2 files changed

+151
-13
lines changed

2 files changed

+151
-13
lines changed

openlist-lib/scripts/fix_ios_dependencies.sh

Lines changed: 124 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,138 @@ echo "Working in module root: $(pwd)"
1919
echo "Checking for iOS incompatible dependencies..."
2020
go list -m all | grep -E "(go-m1cpu|gopsutil)" || echo "No problematic dependencies found in module list"
2121

22-
# Try to exclude problematic dependencies by replacing them with stubs
23-
echo "Attempting to exclude iOS incompatible dependencies..."
22+
# Create a temporary directory for stub packages
23+
STUB_DIR="$(pwd)/ios_stubs"
24+
mkdir -p "$STUB_DIR"
2425

25-
# Replace go-m1cpu with a no-op version if it exists
26+
# Create stub for go-m1cpu
2627
if go list -m all | grep -q "go-m1cpu"; then
27-
echo "Found go-m1cpu dependency, attempting to exclude..."
28-
# Try to replace with a version that doesn't use iOS incompatible APIs
29-
go mod edit -replace github.com/shoenig/go-m1cpu=github.com/shoenig/go-m1cpu@v0.1.5 || true
28+
echo "Creating stub for go-m1cpu..."
29+
mkdir -p "$STUB_DIR/github.com/shoenig/go-m1cpu"
30+
cat > "$STUB_DIR/github.com/shoenig/go-m1cpu/cpu.go" << 'EOF'
31+
//go:build ios || mobile
32+
33+
package cpu
34+
35+
// Stub implementation for iOS
36+
func IsAppleSilicon() bool {
37+
return false
38+
}
39+
40+
func IsIntel() bool {
41+
return false
42+
}
43+
44+
func ModelName() string {
45+
return "iOS Device"
46+
}
47+
EOF
48+
49+
cat > "$STUB_DIR/github.com/shoenig/go-m1cpu/go.mod" << 'EOF'
50+
module github.com/shoenig/go-m1cpu
51+
52+
go 1.19
53+
EOF
54+
55+
# Replace with local stub
56+
go mod edit -replace github.com/shoenig/go-m1cpu="$STUB_DIR/github.com/shoenig/go-m1cpu"
57+
fi
58+
59+
# Create stub for gopsutil
60+
if go list -m all | grep -q "gopsutil"; then
61+
echo "Creating stub for gopsutil..."
62+
mkdir -p "$STUB_DIR/github.com/shirou/gopsutil/v3/cpu"
63+
cat > "$STUB_DIR/github.com/shirou/gopsutil/v3/cpu/cpu.go" << 'EOF'
64+
//go:build ios || mobile
65+
66+
package cpu
67+
68+
import "context"
69+
70+
type InfoStat struct {
71+
CPU int32 `json:"cpu"`
72+
VendorID string `json:"vendorId"`
73+
Family string `json:"family"`
74+
Model string `json:"model"`
75+
Stepping int32 `json:"stepping"`
76+
PhysicalID string `json:"physicalId"`
77+
CoreID string `json:"coreId"`
78+
Cores int32 `json:"cores"`
79+
ModelName string `json:"modelName"`
80+
Mhz float64 `json:"mhz"`
81+
CacheSize int32 `json:"cacheSize"`
82+
Flags []string `json:"flags"`
83+
Microcode string `json:"microcode"`
84+
}
85+
86+
// Stub implementations for iOS
87+
func Info() ([]InfoStat, error) {
88+
return []InfoStat{{
89+
CPU: 0,
90+
ModelName: "iOS CPU",
91+
Cores: 1,
92+
}}, nil
93+
}
94+
95+
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
96+
return Info()
97+
}
98+
99+
func Percent(interval float64, percpu bool) ([]float64, error) {
100+
return []float64{0.0}, nil
101+
}
102+
103+
func PercentWithContext(ctx context.Context, interval float64, percpu bool) ([]float64, error) {
104+
return Percent(interval, percpu)
105+
}
106+
107+
func Times(percpu bool) ([]TimesStat, error) {
108+
return []TimesStat{}, nil
109+
}
110+
111+
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
112+
return Times(percpu)
113+
}
114+
115+
type TimesStat struct {
116+
CPU string `json:"cpu"`
117+
User float64 `json:"user"`
118+
System float64 `json:"system"`
119+
Idle float64 `json:"idle"`
120+
Nice float64 `json:"nice"`
121+
Iowait float64 `json:"iowait"`
122+
Irq float64 `json:"irq"`
123+
Softirq float64 `json:"softirq"`
124+
Steal float64 `json:"steal"`
125+
Guest float64 `json:"guest"`
126+
GuestNice float64 `json:"guestNice"`
127+
}
128+
129+
func Counts(logical bool) (int, error) {
130+
return 1, nil
131+
}
132+
133+
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
134+
return Counts(logical)
135+
}
136+
EOF
137+
138+
cat > "$STUB_DIR/github.com/shirou/gopsutil/v3/go.mod" << 'EOF'
139+
module github.com/shirou/gopsutil/v3
140+
141+
go 1.19
142+
EOF
143+
144+
# Replace with local stub
145+
go mod edit -replace github.com/shirou/gopsutil/v3="$STUB_DIR/github.com/shirou/gopsutil/v3"
30146
fi
31147

32148
# Clean and rebuild
33149
echo "Cleaning and rebuilding module..."
34150
go mod tidy
35151
go mod download
36152

37-
echo "Updated dependencies:"
153+
echo "Updated dependencies with stubs:"
38154
go list -m all | grep -E "(mobile|m1cpu|gopsutil)" || echo "No relevant dependencies found"
39155

40-
echo "iOS dependency fix completed"
156+
echo "iOS dependency fix completed with stubs"

openlist-lib/scripts/gobind_ios.sh

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,33 @@ if [ -f ../go.mod ]; then
8282
cat ios_build.log 2>/dev/null || echo "No build log available"
8383
echo "=== End build log ==="
8484

85-
# Try to get more specific error information
86-
echo "Checking for specific issues..."
87-
88-
# Check if it's a dependency issue
89-
if grep -q "cannot find package\|no Go files\|build constraints exclude all Go files" ios_build.log; then
85+
# Check for specific iOS compatibility issues
86+
if grep -q "go-m1cpu\|gopsutil\|libproc.h\|kIOMainPortDefault" ios_build.log; then
87+
echo "Detected iOS compatibility issues with system packages"
88+
echo "Re-running dependency fix with more aggressive exclusions..."
89+
90+
# Clean up any existing stubs
91+
rm -rf ios_stubs
92+
93+
# Re-run dependency fix
94+
./scripts/fix_ios_dependencies.sh
95+
96+
# Update mobile packages
97+
echo "Updating mobile packages..."
98+
go get -u golang.org/x/mobile/...
99+
go install golang.org/x/mobile/cmd/gobind@latest
100+
go install golang.org/x/mobile/cmd/gomobile@latest
101+
102+
# Try build again with no build tags (let stubs handle it)
103+
echo "Retrying build with stub packages..."
104+
gomobile bind -ldflags "-s -w" -v -target="ios" ./openlistlib 2>&1 | tee ios_build_retry.log
105+
106+
if [ $? -ne 0 ]; then
107+
echo "Build with stubs also failed:"
108+
cat ios_build_retry.log 2>/dev/null || echo "No retry log available"
109+
exit 1
110+
fi
111+
elif grep -q "cannot find package\|no Go files\|build constraints exclude all Go files" ios_build.log; then
90112
echo "Detected dependency or build constraint issues"
91113

92114
# Try with minimal tags

0 commit comments

Comments
 (0)