Skip to content

Commit 72f4569

Browse files
Add option to update Ubuntu sources for cross-compilation
Introduces the --update-sources flag to update Ubuntu sources.list for cross-compilation scenarios. Adds the update_ubuntu_sources function, which configures the appropriate sources based on Ubuntu version and target architecture, and integrates this step into the dependency installation process when requested.
1 parent a14098e commit 72f4569

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

scripts/linux_build.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ target_arch=""
2525
target_tuple=""
2626
ubuntu_test_repo=0
2727
use_aptitude=0
28+
update_sources=0
2829
step="all"
2930

3031
# common variables
@@ -104,6 +105,7 @@ Options:
104105
--target-tuple Target tuple for cross compilation (e.g., aarch64-linux-gnu, x86_64-linux-gnu).
105106
--ubuntu-test-repo Install ppa:ubuntu-toolchain-r/test repo on Ubuntu.
106107
--use-aptitude Use aptitude instead of apt for package management on Debian/Ubuntu systems.
108+
--update-sources Update Ubuntu sources.list for cross-compilation (Ubuntu only, off by default).
107109
--step Which step(s) to run: deps, cmake, validation, build, package, cleanup, or all (default: all)
108110
109111
Steps:
@@ -160,6 +162,7 @@ while getopts ":hs-:" opt; do
160162
;;
161163
ubuntu-test-repo) ubuntu_test_repo=1 ;;
162164
use-aptitude) use_aptitude=1 ;;
165+
update-sources) update_sources=1 ;;
163166
step=*)
164167
step="${OPTARG#*=}"
165168
;;
@@ -495,9 +498,123 @@ if [[ "$(printf '%s\n' "$installed_version" "$min_version" | sort -V | head -n1)
495498
fi
496499
}
497500

501+
function update_ubuntu_sources() {
502+
echo "Updating Ubuntu sources for cross-compilation..."
503+
504+
if [ "$distro" != "ubuntu" ]; then
505+
echo "Sources update only supported on Ubuntu, skipping..."
506+
return
507+
fi
508+
509+
if [ "$cross_compile" != 1 ] || [ -z "$target_arch" ]; then
510+
echo "Not cross-compiling, skipping sources update..."
511+
return
512+
fi
513+
514+
# Install lsb-release if not available for getting distribution info
515+
if ! command -v lsb_release &> /dev/null; then
516+
echo "Installing lsb-release for distribution detection..."
517+
${sudo_cmd} apt-get install -y lsb-release
518+
fi
519+
520+
# Detect distribution name and version
521+
local dist_name
522+
local ubuntu_version
523+
local ubuntu_major_version
524+
dist_name=$(lsb_release -cs)
525+
ubuntu_version=$(lsb_release -rs)
526+
ubuntu_major_version=${ubuntu_version%%.*}
527+
528+
echo "Detected Ubuntu distribution: $dist_name"
529+
echo "Detected Ubuntu version: $ubuntu_version"
530+
echo "Detected Ubuntu major version: $ubuntu_major_version"
531+
532+
# Determine mirror URL
533+
local mirror="https://ports.ubuntu.com/ubuntu-ports"
534+
535+
# Add target architecture
536+
echo "Adding architecture: $target_arch"
537+
${sudo_cmd} dpkg --add-architecture "$target_arch"
538+
539+
# Determine source file location based on Ubuntu version
540+
local source_file
541+
if [[ $ubuntu_major_version -ge 24 ]]; then
542+
source_file="/etc/apt/sources.list.d/ubuntu.sources"
543+
else
544+
source_file="/etc/apt/sources.list"
545+
fi
546+
547+
echo "Using sources file: $source_file"
548+
549+
# Backup original sources
550+
echo "Backing up original sources file..."
551+
${sudo_cmd} cp "$source_file" "${source_file}.bak"
552+
553+
# Print original sources for debugging
554+
echo "Original sources:"
555+
${sudo_cmd} cat "$source_file"
556+
echo "----"
557+
558+
# Update sources based on Ubuntu version
559+
if [[ $ubuntu_major_version -ge 24 ]]; then
560+
# Ubuntu 24.04+ uses the new .sources format
561+
local extra_sources
562+
extra_sources=$(cat <<VAREOF
563+
Types: deb
564+
URIs: mirror+file:/etc/apt/apt-mirrors.txt
565+
Suites: ${dist_name} ${dist_name}-updates ${dist_name}-backports ${dist_name}-security
566+
Components: main universe restricted multiverse
567+
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
568+
Architectures: $(dpkg --print-architecture)
569+
570+
Types: deb
571+
URIs: ${mirror}
572+
Suites: ${dist_name} ${dist_name}-updates ${dist_name}-backports ${dist_name}-security
573+
Components: main universe restricted multiverse
574+
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
575+
Architectures: ${target_arch}
576+
VAREOF
577+
)
578+
echo "$extra_sources" | ${sudo_cmd} tee "$source_file" > /dev/null
579+
else
580+
# Ubuntu 22.04 and earlier use the traditional sources.list format
581+
# Fix original sources to specify amd64 architecture
582+
${sudo_cmd} sed -i -e "s#deb mirror#deb [arch=$(dpkg --print-architecture)] mirror#g" "$source_file"
583+
584+
# Add cross-compilation sources
585+
local extra_sources
586+
extra_sources=$(cat <<VAREOF
587+
deb [arch=${target_arch}] ${mirror} ${dist_name} main restricted
588+
deb [arch=${target_arch}] ${mirror} ${dist_name}-updates main restricted
589+
deb [arch=${target_arch}] ${mirror} ${dist_name} universe
590+
deb [arch=${target_arch}] ${mirror} ${dist_name}-updates universe
591+
deb [arch=${target_arch}] ${mirror} ${dist_name} multiverse
592+
deb [arch=${target_arch}] ${mirror} ${dist_name}-updates multiverse
593+
deb [arch=${target_arch}] ${mirror} ${dist_name}-backports main restricted universe multiverse
594+
deb [arch=${target_arch}] ${mirror} ${dist_name}-security main restricted
595+
deb [arch=${target_arch}] ${mirror} ${dist_name}-security universe
596+
deb [arch=${target_arch}] ${mirror} ${dist_name}-security multiverse
597+
VAREOF
598+
)
599+
echo "$extra_sources" | ${sudo_cmd} tee -a "$source_file" > /dev/null
600+
fi
601+
602+
echo "----"
603+
echo "Updated sources:"
604+
${sudo_cmd} cat "$source_file"
605+
echo "----"
606+
607+
echo "Ubuntu sources updated successfully for cross-compilation"
608+
}
609+
498610
function run_step_deps() {
499611
echo "Running step: Install dependencies"
500612

613+
# Update Ubuntu sources for cross-compilation if requested
614+
if [ "$update_sources" == 1 ]; then
615+
update_ubuntu_sources
616+
fi
617+
501618
# Update the package list using apt-get first (even if aptitude is requested)
502619
if [ "$distro" == "debian" ] || [ "$distro" == "ubuntu" ]; then
503620
${sudo_cmd} apt-get update

0 commit comments

Comments
 (0)