File size: 5,428 Bytes
5bbfb59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

# Setup script for Rust Android development
# This script:
# 1. Installs necessary Rust targets for Android
# 2. Auto-detects system configuration
# 3. Generates .cargo/config.toml with correct linker paths

set -e

echo "πŸ¦€ Setting up Rust for Android development..."

# Function to detect operating system
detect_os() {
    case "$(uname -s)" in
        Darwin*)    echo "darwin-x86_64" ;;
        Linux*)     echo "linux-x86_64" ;;
        CYGWIN*|MINGW*|MSYS*) echo "windows-x86_64" ;;
        *)          echo "unknown" ;;
    esac
}

# Function to find Android SDK path
find_android_sdk() {
    if [ -n "$ANDROID_HOME" ]; then
        echo "$ANDROID_HOME"
    elif [ -n "$ANDROID_SDK_ROOT" ]; then
        echo "$ANDROID_SDK_ROOT"
    elif [ -d "$HOME/Android/Sdk" ]; then
        echo "$HOME/Android/Sdk"
    elif [ -d "$HOME/Library/Android/sdk" ]; then
        echo "$HOME/Library/Android/sdk"
    else
        echo ""
    fi
}

# Function to find the latest NDK version
find_ndk_version() {
    local sdk_path="$1"
    local ndk_dir="$sdk_path/ndk"
    
    if [ -d "$ndk_dir" ]; then
        # Find the latest version (highest version number)
        ls -1 "$ndk_dir" | sort -V | tail -n 1
    else
        echo ""
    fi
}

# Function to extract API level from build.gradle and version catalog
extract_api_level() {
    local build_gradle="build.gradle"
    local version_catalog="../gradle/libs.versions.toml"
    
    # First, check if build.gradle uses version catalog
    if [ -f "$build_gradle" ] && grep -q "libs.versions.minSdk" "$build_gradle"; then
        # Extract from version catalog
        if [ -f "$version_catalog" ]; then
            grep "^minSdk" "$version_catalog" | cut -d '"' -f 2
        else
            echo "21"  # Default fallback
        fi
    elif [ -f "$build_gradle" ]; then
        # Look for direct minSdkVersion in build.gradle
        grep -E "minSdkVersion|minSdk" "$build_gradle" | head -n 1 | sed 's/.*[[:space:]]\([0-9]\+\).*/\1/'
    else
        echo "21"  # Default fallback
    fi
}

# Detect system configuration
OS_PLATFORM=$(detect_os)
echo "πŸ“± Detected OS: $OS_PLATFORM"

if [ "$OS_PLATFORM" = "unknown" ]; then
    echo "❌ Unsupported operating system. Please configure manually."
    exit 1
fi

# Find Android SDK
ANDROID_SDK_PATH=$(find_android_sdk)
if [ -z "$ANDROID_SDK_PATH" ]; then
    echo "❌ Android SDK not found. Please set ANDROID_HOME or ANDROID_SDK_ROOT environment variable."
    echo "   Or install Android SDK to ~/Android/Sdk (Linux) or ~/Library/Android/sdk (macOS)"
    exit 1
fi

echo "πŸ“ Found Android SDK: $ANDROID_SDK_PATH"

# Find NDK version
NDK_VERSION=$(find_ndk_version "$ANDROID_SDK_PATH")
if [ -z "$NDK_VERSION" ]; then
    echo "❌ NDK not found in $ANDROID_SDK_PATH/ndk"
    echo "   Please install NDK through Android Studio SDK Manager"
    exit 1
fi

echo "πŸ”¨ Found NDK version: $NDK_VERSION"

# Extract API level
API_LEVEL=$(extract_api_level)
echo "πŸ“‹ Using API level: $API_LEVEL"

# Set executable suffix for Windows
EXE_SUFFIX=""
if [ "$OS_PLATFORM" = "windows-x86_64" ]; then
    EXE_SUFFIX=".exe"
fi

# Construct NDK path
NDK_PATH="$ANDROID_SDK_PATH/ndk/$NDK_VERSION/toolchains/llvm/prebuilt/$OS_PLATFORM/bin"

# Verify NDK path exists
if [ ! -d "$NDK_PATH" ]; then
    echo "❌ NDK toolchain not found at: $NDK_PATH"
    echo "   Please check your NDK installation"
    exit 1
fi

echo "πŸ› οΈ  NDK toolchain path: $NDK_PATH"

# Install Rust targets for Android
echo "πŸ“¦ Installing Rust targets for Android..."
rustup target add aarch64-linux-android
rustup target add armv7-linux-androideabi
rustup target add i686-linux-android
rustup target add x86_64-linux-android

# Create .cargo directory if it doesn't exist
mkdir -p .cargo

# Generate config.toml from template
echo "βš™οΈ  Generating .cargo/config.toml..."
if [ ! -f ".cargo/config.toml.template" ]; then
    echo "❌ Template file .cargo/config.toml.template not found"
    exit 1
fi

# Use sed to replace placeholders in template
sed -e "s|{{NDK_PATH}}|$NDK_PATH|g" \
    -e "s|{{API_LEVEL}}|$API_LEVEL|g" \
    -e "s|{{EXE_SUFFIX}}|$EXE_SUFFIX|g" \
    -e "s|{{PLATFORM}}|$OS_PLATFORM|g" \
    -e "s|{{NDK_VERSION}}|$NDK_VERSION|g" \
    -e "s|{{ANDROID_SDK_PATH}}|$ANDROID_SDK_PATH|g" \
    .cargo/config.toml.template > .cargo/config.toml

echo "βœ… Successfully generated .cargo/config.toml"

# Verify generated linkers exist
echo "πŸ” Verifying linkers..."
LINKERS=(
    "aarch64-linux-android${API_LEVEL}-clang${EXE_SUFFIX}"
    "armv7a-linux-androideabi${API_LEVEL}-clang${EXE_SUFFIX}"
    "i686-linux-android${API_LEVEL}-clang${EXE_SUFFIX}"
    "x86_64-linux-android${API_LEVEL}-clang${EXE_SUFFIX}"
)

for linker in "${LINKERS[@]}"; do
    if [ -f "$NDK_PATH/$linker" ]; then
        echo "  βœ… $linker"
    else
        echo "  ❌ $linker (not found)"
    fi
done

echo ""
echo "πŸŽ‰ Rust Android setup complete!"
echo ""
echo "πŸ“‹ Configuration summary:"
echo "  - OS Platform: $OS_PLATFORM"
echo "  - Android SDK: $ANDROID_SDK_PATH"
echo "  - NDK Version: $NDK_VERSION"
echo "  - API Level: $API_LEVEL"
echo "  - Config file: .cargo/config.toml"
echo ""
echo "πŸš€ You can now build for Android using:"
echo "   cargo build --target aarch64-linux-android"
echo "   cargo build --target armv7-linux-androideabi"
echo "   cargo build --target i686-linux-android"
echo "   cargo build --target x86_64-linux-android"