Skip to content

Commit

Permalink
[build] Modify build script to support a cross-compiling toolchain
Browse files Browse the repository at this point in the history
Also modified cmake/Modules/GetClangResourceDir.cmake to work around a missing
definition for PACKAGE_VERSION when cross-compiling OpenMP libraries.
  • Loading branch information
bryanpkc committed Nov 26, 2024
1 parent 3f5de27 commit 9712029
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
81 changes: 60 additions & 21 deletions build-llvm-project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
TARGET="X86"
BUILD_TYPE="Release"
INSTALL_PREFIX="/usr/local"
CROSS_TARGETS=""
NPROC=1
USE_CCACHE="0"
DO_INSTALL="0"
USE_SUDO="0"
C_COMPILER_PATH="/usr/bin/gcc"
CXX_COMPILER_PATH="/usr/bin/g++"
LLVM_ENABLE_PROJECTS="clang;openmp"
LLVM_EXTRA_PROJECTS=""
EXTRA_CMAKE_OPTS=""
VERBOSE=""

Expand All @@ -19,56 +20,50 @@ set -e # Exit script on first error.
function print_usage {
echo "Usage: ./build-llvm-project.sh [options]";
echo "";
echo "Build and install classic-flang-llvm-project.";
echo "Build and install classic-flang-llvm-project (including clang, lld, and openmp).";
echo "Run this script in a directory with project sources.";
echo "Example:";
echo " $ git clone https://github.com/flang-compiler/classic-flang-llvm-project";
echo " $ cd classic-flang-llvm-project";
echo " $ .github/workflows/build-llvm-project.sh -t X86 -p /install/prefix/ \\";
echo " $ -a /usr/bin/gcc-10 -b /usr/bin/g++-10 -i -s";
echo " $ ./build-llvm-project.sh -t X86 -p /opt/classic-flang/ -i -s";
echo "";
echo "Options:";
echo " -t Target to build for (X86, AArch64, PowerPC). Default: X86";
echo " -d Set the CMake build type. Default: Release";
echo " -p Install prefix. Default: /usr/local";
echo " -X Cross-compile OpenMP for given list of target triples. Default: none";
echo " -n Number of parallel jobs. Default: 1";
echo " -c Use ccache. Default: 0 - do not use ccache";
echo " -i Install the build. Default 0 - just build, do not install";
echo " -s Use sudo to install. Default: 0 - do not use sudo";
echo " -a C compiler path. Default: /usr/bin/gcc";
echo " -b C++ compiler path. Default: /usr/bin/g++";
echo " -e List of the LLVM sub-projects to build. Default: clang;openmp";
echo " -e List of additional LLVM sub-projects to build. Default: none";
echo " -x Extra CMake options. Default: ''";
echo " -v Enable verbose output";
}

while getopts "t:d:p:n:cisa:b:e:x:v?" opt; do
while getopts "t:d:p:X:n:cisa:b:e:x:v?" opt; do
case "$opt" in
t) TARGET=$OPTARG;;
d) BUILD_TYPE=$OPTARG;;
p) INSTALL_PREFIX=$OPTARG;;
X) CROSS_TARGETS=$OPTARG;;
n) NPROC=$OPTARG;;
c) USE_CCACHE="1";;
i) DO_INSTALL="1";;
s) USE_SUDO="1";;
a) C_COMPILER_PATH=$OPTARG;;
b) CXX_COMPILER_PATH=$OPTARG;;
e) LLVM_ENABLE_PROJECTS=$OPTARG;;
e) LLVM_EXTRA_PROJECTS=$OPTARG;;
x) EXTRA_CMAKE_OPTS="$OPTARG";;
v) VERBOSE="1";;
?) print_usage; exit 0;;
esac
done

CMAKE_OPTIONS="-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DCMAKE_C_COMPILER=$C_COMPILER_PATH \
-DCMAKE_CXX_COMPILER=$CXX_COMPILER_PATH \
-DLLVM_TARGETS_TO_BUILD=$TARGET \
-DLLVM_ENABLE_CLASSIC_FLANG=ON \
-DFLANG_BUILD_NEW_DRIVER=OFF"
# Warning: the -DLLVM_ENABLE_PROJECTS option is specified with cmake
# to avoid issues with nested quotation marks
-DCMAKE_BUILD_TYPE=$BUILD_TYPE"

if [ $USE_CCACHE == "1" ]; then
echo "Build using ccache"
Expand All @@ -77,16 +72,19 @@ if [ $USE_CCACHE == "1" ]; then
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache"
fi

if [ -n "$EXTRA_CMAKE_OPTS" ]; then
CMAKE_OPTIONS="$CMAKE_OPTIONS $EXTRA_CMAKE_OPTS"
fi

# Build and install
# Build and install.
mkdir -p build && cd build
if [ -n "$VERBOSE" ]; then
set -x
fi
cmake $CMAKE_OPTIONS -DLLVM_ENABLE_PROJECTS=$LLVM_ENABLE_PROJECTS ../llvm
cmake $CMAKE_OPTIONS \
-DCMAKE_C_COMPILER=$C_COMPILER_PATH \
-DCMAKE_CXX_COMPILER=$CXX_COMPILER_PATH \
-DLLVM_ENABLE_CLASSIC_FLANG=ON \
-DLLVM_ENABLE_PROJECTS="clang;lld;openmp;$LLVM_EXTRA_PROJECTS" \
-DLLVM_TARGETS_TO_BUILD="$TARGET" \
$EXTRA_CMAKE_OPTS \
../llvm
set +x
make -j$NPROC VERBOSE=$VERBOSE
if [ $DO_INSTALL == "1" ]; then
Expand All @@ -100,3 +98,44 @@ if [ $DO_INSTALL == "1" ]; then
fi
cd ..

# Cross-compile OpenMP libraries if requested.
IFS=';' read -ra CROSS_TARGET_LIST <<< "$CROSS_TARGETS"
for T in ${CROSS_TARGET_LIST[@]}; do
mkdir -p "build/openmp-$T"
pushd "build/openmp-$T"
CMAKE_OPTIONS="$CMAKE_OPTIONS \
-DCMAKE_AR=$INSTALL_PREFIX/bin/llvm-ar \
-DCMAKE_ASM_COMPILER=$INSTALL_PREFIX/bin/clang \
-DCMAKE_ASM_COMPILER_TARGET=$T \
-DCMAKE_C_COMPILER=$INSTALL_PREFIX/bin/clang \
-DCMAKE_C_COMPILER_TARGET=$T \
-DCMAKE_CXX_COMPILER=$INSTALL_PREFIX/bin/clang++ \
-DCMAKE_CXX_COMPILER_TARGET=$T \
-DCMAKE_RANLIB=$INSTALL_PREFIX/bin/llvm-ranlib \
-DCMAKE_SHARED_LINKER_FLAGS=-fuse-ld=lld"
if [ -n "$VERBOSE" ]; then
set -x
fi
cmake $CMAKE_OPTIONS \
-DLLVM_DEFAULT_TARGET_TRIPLE=$T \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON \
-DLLVM_ENABLE_RUNTIMES="openmp" \
-DLIBOMP_OMPT_SUPPORT=OFF \
-DOPENMP_ENABLE_LIBOMPTARGET=OFF \
-DOPENMP_ENABLE_OMPT_TOOLS=OFF \
-DOPENMP_LLVM_TOOLS_DIR=$PWD/../bin \
$EXTRA_CMAKE_OPTS \
../../runtimes
set +x
make -j$NPROC VERBOSE=$VERBOSE
if [ $DO_INSTALL -eq 1 ]; then
if [ $USE_SUDO -eq 1 ]; then
echo "Install with sudo"
sudo make install
else
echo "Install without sudo"
make install
fi
fi
popd
done
6 changes: 5 additions & 1 deletion cmake/Modules/GetClangResourceDir.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ function(get_clang_resource_dir out_var)
set(ret_dir bin/${CLANG_RESOURCE_DIR})
else()
if (NOT CLANG_VERSION_MAJOR)
string(REGEX MATCH "^[0-9]+" CLANG_VERSION_MAJOR ${PACKAGE_VERSION})
if (NOT PACKAGE_VERSION)
string(REGEX MATCH "^[0-9]+" CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
else()
string(REGEX MATCH "^[0-9]+" CLANG_VERSION_MAJOR ${PACKAGE_VERSION})
endif()
endif()
set(ret_dir lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION_MAJOR})
endif()
Expand Down

0 comments on commit 9712029

Please sign in to comment.