Skip to content

Instantly share code, notes, and snippets.

@corporatepiyush
Created August 24, 2024 08:29
Show Gist options
  • Save corporatepiyush/df625871e4aa21bb2a0d03a019a56c8f to your computer and use it in GitHub Desktop.
Save corporatepiyush/df625871e4aa21bb2a0d03a019a56c8f to your computer and use it in GitHub Desktop.
echo 'export DEB_CFLAGS_MAINT_APPEND="-O3 -march=native -ftree-vectorize -flto -fprefetch-loop-arrays"' | sudo tee -a /etc/dpkg/buildflags.conf
echo 'export DEB_CXXFLAGS_MAINT_APPEND="-O3 -march=native -ftree-vectorize -flto -fprefetch-loop-arrays"' | sudo tee -a /etc/dpkg/buildflags.conf
#!/bin/bash
# Directory to hold source packages
SRC_DIR=~/src
mkdir -p $SRC_DIR
cd $SRC_DIR
# Export the optimization flags
export DEB_CFLAGS_MAINT_APPEND="-O3 -march=native -mtune=native -ftree-vectorize -flto -fprefetch-loop-arrays"
export DEB_CXXFLAGS_MAINT_APPEND="-O3 -march=native -mtune=native -ftree-vectorize -flto -fprefetch-loop-arrays"
echo "Starting the recompilation process with CPU-specific and advanced optimizations for all installed packages..."
echo "Source directory: $SRC_DIR"
echo "Optimization flags: $DEB_CFLAGS_MAINT_APPEND, $DEB_CXXFLAGS_MAINT_APPEND"
# Get the list of installed packages
PACKAGES=$(dpkg --get-selections | grep -v deinstall | awk '{print $1}')
# Total packages count
TOTAL_PACKAGES=$(echo "$PACKAGES" | wc -l)
echo "Total packages to process: $TOTAL_PACKAGES"
PROCESSED=0
SKIPPED=0
RECOMPILED=0
# Loop through each package
for pkg in $PACKAGES; do
PROCESSED=$((PROCESSED + 1))
echo "[$PROCESSED/$TOTAL_PACKAGES] Processing $pkg..."
# Download the source package
if ! apt source $pkg; then
echo "Error: Failed to download source for $pkg. Skipping..."
SKIPPED=$((SKIPPED + 1))
continue
fi
# Enter the source package directory
if ! cd $pkg-*/; then
echo "Error: Failed to enter source directory for $pkg. Skipping..."
SKIPPED=$((SKIPPED + 1))
continue
fi
# Install the build dependencies
echo "Installing build dependencies for $pkg..."
if ! sudo apt build-dep -y $pkg; then
echo "Error: Failed to install build dependencies for $pkg. Skipping..."
cd $SRC_DIR
rm -rf $pkg-*/
SKIPPED=$((SKIPPED + 1))
continue
fi
# Build the package with CPU-specific and advanced optimization flags
echo "Building $pkg with CPU-specific and advanced optimizations..."
if ! DEB_BUILD_OPTIONS="parallel=$(nproc)" dpkg-buildpackage -b -uc -us; then
echo "Error: Failed to build $pkg. Skipping..."
cd $SRC_DIR
rm -rf $pkg-*/
SKIPPED=$((SKIPPED + 1))
continue
fi
# Install the newly built package
echo "Installing the newly built package for $pkg..."
if ! sudo dpkg -i ../$pkg*.deb; then
echo "Error: Failed to install the built package for $pkg. Skipping..."
cd $SRC_DIR
rm -rf $pkg-*/
SKIPPED=$((SKIPPED + 1))
continue
fi
echo "$pkg processed successfully."
RECOMPILED=$((RECOMPILED + 1))
# Clean up and go back to the source directory
cd $SRC_DIR
rm -rf $pkg-*/
done
# Clean up
sudo apt-get clean
echo "Recompilation and installation process completed."
echo "Total packages processed: $PROCESSED"
echo "Total packages skipped due to errors: $SKIPPED"
echo "Total packages recompiled with CPU-specific and advanced optimizations: $RECOMPILED"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment