Skip to content

Commit 04aa93c

Browse files
committed
One more attempt to fix cross compile
1 parent 3bcde3d commit 04aa93c

File tree

1 file changed

+82
-39
lines changed

1 file changed

+82
-39
lines changed

.github/workflows/cross-compile.yml

Lines changed: 82 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,75 +37,118 @@ jobs:
3737
echo "Runner Ubuntu Codename: ${CODENAME}"
3838
HOST_ARCH=$(dpkg --print-architecture)
3939
echo "Runner Host Architecture: ${HOST_ARCH}"
40+
UBUNTU_SOURCES_FILE="/etc/apt/sources.list.d/ubuntu.sources" # Standard for Ubuntu 22.04+
4041
4142
# Replace Azure mirror with official Ubuntu repositories.
42-
# This helps ensure consistency if the base image uses Azure mirrors.
4343
echo "Replacing Azure mirrors with official Ubuntu mirrors..."
44-
sudo sed -i 's/azure.archive.ubuntu.com/archive.ubuntu.com/g' /etc/apt/sources.list
44+
if [ -f "${UBUNTU_SOURCES_FILE}" ]; then
45+
sudo sed -i 's|azure.archive.ubuntu.com|archive.ubuntu.com|g' "${UBUNTU_SOURCES_FILE}"
46+
sudo sed -i 's|azure.ports.ubuntu.com|ports.ubuntu.com|g' "${UBUNTU_SOURCES_FILE}" # if applicable for .sources
47+
fi
48+
if [ -f "/etc/apt/sources.list" ]; then
49+
sudo sed -i 's|azure.archive.ubuntu.com|archive.ubuntu.com|g' /etc/apt/sources.list
50+
sudo sed -i 's|azure.ports.ubuntu.com|ports.ubuntu.com|g' /etc/apt/sources.list # if applicable for .list
51+
fi
4552
# Also attempt to replace in any existing sources.list.d files.
46-
sudo find /etc/apt/sources.list.d/ -type f -name "*.list" -exec sudo sed -i 's/azure.archive.ubuntu.com/archive.ubuntu.com/g' {} \; || echo "Sed on sources.list.d files for Azure mirror replacement had issues or no files found, continuing."
47-
48-
# Restrict existing Ubuntu sources to host architecture to prevent issues when adding foreign architectures.
49-
# This ensures that 'apt-get update' after 'dpkg --add-architecture arm64' doesn't try to find arm64 packages
50-
# from amd64-focused repositories which might lead to 404 errors.
51-
echo "Restricting existing Ubuntu sources in /etc/apt/sources.list to [arch=${HOST_ARCH}]..."
52-
sudo sed -i -E "/^deb(-src)?\s+http:\/\/[^[:space:]]*ubuntu\.com/ { /\[arch=/! s/^(deb(-src)?\s+)/\1[arch=${HOST_ARCH}] / }" /etc/apt/sources.list
53+
sudo find /etc/apt/sources.list.d/ -type f -name "*.list" -exec sudo sed -i 's|azure.archive.ubuntu.com|archive.ubuntu.com|g' {} \; || echo "Sed on sources.list.d/*.list for Azure mirror replacement had issues or no files found, continuing."
54+
sudo find /etc/apt/sources.list.d/ -type f -name "*.list" -exec sudo sed -i 's|azure.ports.ubuntu.com|ports.ubuntu.com|g' {} \; || echo "Sed on sources.list.d/*.list for Azure ports mirror replacement had issues or no files found, continuing."
55+
56+
57+
echo "Configuring APT sources for host architecture (${HOST_ARCH})..."
58+
if [ -f "${UBUNTU_SOURCES_FILE}" ]; then
59+
echo "Modifying ${UBUNTU_SOURCES_FILE} to restrict to [arch=${HOST_ARCH}] for main Ubuntu repos..."
60+
# Backup original sources file
61+
sudo cp "${UBUNTU_SOURCES_FILE}" "${UBUNTU_SOURCES_FILE}.bak"
62+
# Reconstruct the sources file using echo commands
63+
{
64+
echo "Types: deb"
65+
echo "URIs: http://archive.ubuntu.com/ubuntu/"
66+
echo "Suites: ${CODENAME} ${CODENAME}-updates ${CODENAME}-backports"
67+
echo "Components: main restricted universe multiverse"
68+
echo "Architectures: ${HOST_ARCH}"
69+
echo "Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg"
70+
echo ""
71+
echo "Types: deb"
72+
echo "URIs: http://security.ubuntu.com/ubuntu/"
73+
echo "Suites: ${CODENAME}-security"
74+
echo "Components: main restricted universe multiverse"
75+
echo "Architectures: ${HOST_ARCH}"
76+
echo "Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg"
77+
} | sudo tee "${UBUNTU_SOURCES_FILE}" > /dev/null
78+
echo "Note: ${UBUNTU_SOURCES_FILE} was regenerated. Check .bak file if original non-standard entries were needed."
79+
80+
elif [ -f "/etc/apt/sources.list" ]; then
81+
echo "Restricting existing Ubuntu sources in /etc/apt/sources.list to [arch=${HOST_ARCH}]..."
82+
sudo sed -i -E "/^deb(-src)?\\s+http:\\/\\/[^[:space:]]*ubuntu\\.com/ { /\\[arch=/! s/^(deb(-src)?\\s+)/\\1[arch=${HOST_ARCH}] / }" /etc/apt/sources.list
83+
else
84+
echo "Warning: Neither ${UBUNTU_SOURCES_FILE} nor /etc/apt/sources.list found. APT configuration might be non-standard."
85+
fi
5386
54-
echo "Restricting existing Ubuntu sources in /etc/apt/sources.list.d/ to [arch=${HOST_ARCH}]..."
87+
echo "Restricting other Ubuntu sources in /etc/apt/sources.list.d/*.list to [arch=${HOST_ARCH}]..."
5588
sudo find /etc/apt/sources.list.d/ -type f -name "*.list" -print0 | \
5689
while IFS= read -r -d $'\0' file; do
57-
# Only attempt to modify files that actually contain ubuntu.com source lines and don't already have [arch=arm64] for ports.
58-
# This check avoids modifying our own arm64-ports-sources.list if it were created earlier by mistake or in a retry.
59-
if grep -q -E "^deb(-src)?\s+http:\/\/[^[:space:]]*ubuntu\.com" "$file" && ! grep -q -E "\[arch=arm64\].*ports\.ubuntu\.com" "$file"; then
90+
if [[ "$file" == *"/arm64-ports-sources.list"* ]]; then
91+
echo "Skipping $file (our arm64 ports list)."
92+
continue
93+
fi
94+
# Check if the file contains main ubuntu repos, is not already arch-specific for *any* arch, and isn't a ports.ubuntu.com repo
95+
if grep -q -E "^deb(-src)?\\s+https?:\\/\\/[^[:space:]]*ubuntu\\.com" "$file" && \
96+
! grep -q -E "^deb(-src)?\\s+\\[arch=" "$file" && \
97+
! grep -q -E "ports\\.ubuntu\\.com" "$file"; then
6098
echo "Modifying $file to restrict to [arch=${HOST_ARCH}] for Ubuntu sources"
61-
sudo sed -i -E "/^deb(-src)?\s+http:\/\/[^[:space:]]*ubuntu\.com/ { /\[arch=/! s/^(deb(-src)?\s+)/\1[arch=${HOST_ARCH}] / }" "$file"
99+
sudo sed -i -E "/^deb(-src)?\\s+https?:\\/\\/[^[:space:]]*ubuntu\\.com/ { s/^(deb(-src)?\\s+)/\\1[arch=${HOST_ARCH}] / }" "$file"
62100
else
63-
echo "Skipping $file for host architecture restriction (either not an Ubuntu source or already arch-specific)."
101+
echo "Skipping $file for host architecture restriction (e.g., not a main Ubuntu source, already arch-specific, or a ports.ubuntu.com)."
64102
fi
65103
done || echo "Processing sources.list.d for arch restriction had issues or no applicable files found, continuing."
66104
67105
# Debug: Show sources.list content after modification
68-
echo "--- /etc/apt/sources.list after host arch restriction (first 20 lines) ---"
69-
sudo head -n 20 /etc/apt/sources.list
106+
echo "--- Main Ubuntu sources file (${UBUNTU_SOURCES_FILE:-/etc/apt/sources.list}) after host arch restriction ---"
107+
if [ -f "${UBUNTU_SOURCES_FILE}" ]; then sudo cat "${UBUNTU_SOURCES_FILE}"; elif [ -f "/etc/apt/sources.list" ]; then sudo head -n 20 /etc/apt/sources.list; fi
70108
echo "--- Relevant files in /etc/apt/sources.list.d/ after host arch restriction ---"
71-
for f_path in $(sudo find /etc/apt/sources.list.d/ -type f -name "*.list" 2>/dev/null || true); do
72-
if [ -f "$f_path" ]; then
109+
# List all .sources and .list files for thorough debugging
110+
sudo find /etc/apt/sources.list.d/ -type f \( -name "*.list" -o -name "*.sources" \) -print0 | \
111+
while IFS= read -r -d $'\0' f_path; do
73112
echo "--- Content of $f_path ---"
74113
sudo cat "$f_path"
75114
echo "----------------------"
76-
fi
77-
done
115+
done
78116
echo "---------------------------------------------------------------------------"
79117
80118
# Initial package list update FOR HOST ARCHITECTURE ONLY
81119
echo "Updating package lists for host architecture (${HOST_ARCH}) only..."
82120
sudo apt-get update -y
83121
84122
# Install common dependencies required for both host and possibly for build scripts.
85-
# libxcb1-dev is for the host if building host tools or for native target.
86-
# pkg-config is a general build tool.
123+
echo "Installing host architecture dependencies (libxcb1-dev, pkg-config)..."
87124
sudo apt-get install -y libxcb1-dev pkg-config
88125
89126
if [[ "${{ matrix.target }}" == "aarch64-unknown-linux-gnu" ]]; then
90-
# Add arm64 architecture for cross-compilation
127+
echo "Setting up for aarch64-unknown-linux-gnu cross-compilation..."
91128
sudo dpkg --add-architecture arm64
92129
93-
# Configure apt sources for arm64 to use ports.ubuntu.com
94-
# This is critical as standard mirrors may not carry arm64 packages correctly.
95-
# Remove any pre-existing arm64 source list to avoid conflicts.
96130
ARM64_SOURCES_FILE="/etc/apt/sources.list.d/arm64-ports-sources.list"
97-
sudo rm -f "${ARM64_SOURCES_FILE}"
98-
echo "deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports ${CODENAME} main restricted universe multiverse" | sudo tee "${ARM64_SOURCES_FILE}"
99-
echo "deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports ${CODENAME}-updates main restricted universe multiverse" | sudo tee -a "${ARM64_SOURCES_FILE}"
100-
echo "deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports ${CODENAME}-security main restricted universe multiverse" | sudo tee -a "${ARM64_SOURCES_FILE}"
101-
# Optionally, include backports if needed:
102-
# echo "deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports ${CODENAME}-backports main restricted universe multiverse" | sudo tee -a "${ARM64_SOURCES_FILE}"
103-
104-
# Update package lists again to include arm64 sources
131+
echo "Configuring APT sources for arm64 architecture in ${ARM64_SOURCES_FILE}..."
132+
sudo rm -f "${ARM64_SOURCES_FILE}" # Remove if exists to ensure clean state
133+
# Using echo commands to construct the arm64 sources file
134+
{
135+
echo "deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports ${CODENAME} main restricted universe multiverse"
136+
echo "deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports ${CODENAME}-updates main restricted universe multiverse"
137+
echo "deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports ${CODENAME}-security main restricted universe multiverse"
138+
# echo "deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports ${CODENAME}-backports main restricted universe multiverse"
139+
} | sudo tee "${ARM64_SOURCES_FILE}" > /dev/null
140+
echo "APT sources for arm64 configured."
141+
echo "--- Content of ${ARM64_SOURCES_FILE} ---"
142+
sudo cat "${ARM64_SOURCES_FILE}"
143+
echo "--------------------------------------"
144+
145+
echo "Updating package lists to include arm64 sources..."
146+
# It's important that previous apt update for host_arch succeeded and sources are clean
105147
sudo apt-get update -y
106148
107-
# Install cross-compiler toolchain and arm64 version of libraries
149+
echo "Installing arm64 cross-compiler and libraries (gcc-aarch64-linux-gnu, g++-aarch64-linux-gnu, libxcb1-dev:arm64)..."
108150
sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu libxcb1-dev:arm64
151+
echo "Arm64 dependencies installed."
109152
fi
110153
111154
# Debugging: Verify library installation and pkg-config context
@@ -114,9 +157,9 @@ jobs:
114157
echo "Listing aarch64 libxcb libraries in /usr/lib/aarch64-linux-gnu/:"
115158
ls -l /usr/lib/aarch64-linux-gnu/libxcb* || echo "No aarch64 libxcb files found at expected path."
116159
echo "Relevant environment variables for Cargo's pkg-config:"
117-
echo " CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=${CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER:-<not set>}"
118-
echo " PKG_CONFIG_PATH_aarch64_unknown_linux_gnu=${PKG_CONFIG_PATH_aarch64_unknown_linux_gnu:-<not set>}"
119-
echo " PKG_CONFIG_SYSROOT_DIR_aarch64_unknown_linux_gnu=${PKG_CONFIG_SYSROOT_DIR_aarch64_unknown_linux_gnu:-<not set>}"
160+
echo " CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=\${CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER:-<not set>}"
161+
echo " PKG_CONFIG_PATH_aarch64_unknown_linux_gnu=\${PKG_CONFIG_PATH_aarch64_unknown_linux_gnu:-<not set>}"
162+
echo " PKG_CONFIG_SYSROOT_DIR_aarch64_unknown_linux_gnu=\${PKG_CONFIG_SYSROOT_DIR_aarch64_unknown_linux_gnu:-<not set>}"
120163
echo "-------------------------------------------------"
121164
elif [[ "${{ matrix.target }}" == "x86_64-unknown-linux-gnu" ]]; then
122165
echo "--- Debugging for x86_64-unknown-linux-gnu ---"

0 commit comments

Comments
 (0)