Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom llvm build scripts without term info and zlib to reduce kclvm release size. #478

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions scripts/build-llvm/build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
$LLVM_VERSION = $args[0]
$LLVM_REPO_URL = $args[1]

if ([string]::IsNullOrEmpty($LLVM_REPO_URL)) {
$LLVM_REPO_URL = "https://github.com/llvm/llvm-project.git"
}

if ([string]::IsNullOrEmpty($LLVM_VERSION)) {
Write-Output "Usage: $PSCommandPath <llvm-version> <llvm-repository-url>"
Write-Output ""
Write-Output "# Arguments"
Write-Output " llvm-version The name of a LLVM release branch without the 'release/' prefix"
Write-Output " llvm-repository-url The URL used to clone LLVM sources (default: https://github.com/llvm/llvm-project.git)"

exit 1
}

# Clone the LLVM project.
if (-not (Test-Path -Path "llvm-project" -PathType Container)) {
git clone "$LLVM_REPO_URL" llvm-project
}

Set-Location llvm-project
git fetch origin
git checkout "release/$LLVM_VERSION"
git reset --hard origin/"release/$LLVM_VERSION"

# Create a directory to build the project.
New-Item -Path "build" -Force -ItemType "directory"
Set-Location build

# Create a directory to receive the complete installation.
New-Item -Path "install" -Force -ItemType "directory"

# Adjust compilation based on the OS.
$CMAKE_ARGUMENTS = ""

# Adjust cross compilation
$CROSS_COMPILE = ""

# Run `cmake` to configure the project.
cmake `
-G "Visual Studio 16 2019" `
-DCMAKE_BUILD_TYPE=MinSizeRel `
-DCMAKE_INSTALL_PREFIX=destdir `
-DLLVM_ENABLE_PROJECTS="clang;lld" `
-DLLVM_ENABLE_TERMINFO=OFF `
-DLLVM_ENABLE_ZLIB=OFF `
-DLLVM_INCLUDE_DOCS=OFF `
-DLLVM_INCLUDE_EXAMPLES=OFF `
-DLLVM_INCLUDE_GO_TESTS=OFF `
-DLLVM_INCLUDE_TESTS=OFF `
-DLLVM_INCLUDE_TOOLS=ON `
-DLLVM_INCLUDE_UTILS=OFF `
-DLLVM_OPTIMIZED_TABLEGEN=ON `
-DLLVM_TARGETS_TO_BUILD="X86;AArch64" `
$CROSS_COMPILE `
$CMAKE_ARGUMENTS `
../llvm

# Showtime!
cmake --build . --config Release

# Not using DESTDIR here, quote from
# https://cmake.org/cmake/help/latest/envvar/DESTDIR.html
# > `DESTDIR` may not be used on Windows because installation prefix
# > usually contains a drive letter like in `C:/Program Files` which cannot
# > be prepended with some other prefix.
cmake --install . --strip --config Release
84 changes: 84 additions & 0 deletions scripts/build-llvm/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

# Display all commands before executing them.
set -o errexit
set -o errtrace

LLVM_VERSION=$1
LLVM_REPO_URL=${2:-https://github.com/llvm/llvm-project.git}
LLVM_CROSS="$3"

if [[ -z "$LLVM_REPO_URL" || -z "$LLVM_VERSION" ]]
then
echo "Usage: $0 <llvm-version> <llvm-repository-url> [aarch64]"
echo
echo "# Arguments"
echo " llvm-version The name of a LLVM release branch without the 'release/' prefix"
echo " llvm-repository-url The URL used to clone LLVM sources (default: https://github.com/llvm/llvm-project.git)"
echo " aarch64 To cross-compile an aarch64 version of LLVM"

exit 1
fi

# Clone the LLVM project.
if [ ! -d llvm-project ]
then
git clone "$LLVM_REPO_URL" llvm-project
fi


cd llvm-project
git fetch origin
git checkout "release/$LLVM_VERSION"
git reset --hard origin/"release/$LLVM_VERSION"

# Create a directory to build the project.
mkdir -p build
cd build

# Create a directory to receive the complete installation.
mkdir -p install

# Adjust compilation based on the OS.
CMAKE_ARGUMENTS=""

case "${OSTYPE}" in
darwin*) ;;
linux*) ;;
*) ;;
esac

# Adjust cross compilation
CROSS_COMPILE=""

case "${LLVM_CROSS}" in
aarch64*) CROSS_COMPILE="-DLLVM_HOST_TRIPLE=aarch64-linux-gnu" ;;
*) ;;
esac

# Run `cmake` to configure the project.
cmake \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DCMAKE_INSTALL_PREFIX="/" \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DLLVM_ENABLE_TERMINFO=OFF \
-DLLVM_ENABLE_ZLIB=OFF \
-DLLVM_INCLUDE_DOCS=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_INCLUDE_GO_TESTS=OFF \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_INCLUDE_TOOLS=ON \
-DLLVM_INCLUDE_UTILS=OFF \
-DLLVM_OPTIMIZED_TABLEGEN=ON \
-DLLVM_TARGETS_TO_BUILD="X86;AArch64" \
"${CROSS_COMPILE}" \
"${CMAKE_ARGUMENTS}" \
../llvm

# Showtime!
cmake --build . --config MinSizeRel
DESTDIR=destdir cmake --install . --strip --config MinSizeRel

# move usr/bin/* to bin/ or llvm-config will be broken
mv destdir/usr/bin/* destdir/bin/