1# Building modified LLVM library from source 2 3## Sources 4 5Sources are available at [gitee](https://gitee.com/openharmony/third_party_llvm-project) 6 7## Build script 8 9The [script](build_llvm.sh) does not take command line arguments, instead build parameters should be set using environment variables. 10 11Some variables are mandatory, others are optional. 12 13### Environment variables list 14 15```bash 16### Required variables 17BUILD_DIR=<directory where building process takes place> 18LLVM_SOURCES=<directory with sources>/llvm 19VERSION=<version string, which is included in build/install directory names>(default: "main") 20PACKAGE_VERSION=<kit version>(default: $VERSION) # must match REQUIRED_LLVM_VERSION in libllvmbackend/CMakeLists.txt 21 22### Select targets to build, at least one must be set to "true" 23BUILD_X86_DEBUG=<debug version for x86_64 architecure>(default: false) 24BUILD_X86_RELEASE=<release version for x86_64 architecure>(default: false) 25BUILD_AARCH64_DEBUG=<debug version for arm64 architecure>(default: false) 26BUILD_AARCH64_RELEASE=<release version for arm64 architecure>(default: false) 27BUILD_OHOS_RELEASE=<release version for OHOS platform (OHOS SDK build)>(default: false) 28BUILD_OHOS_RELEASE_GN=<release version for OHOS GN platform (GN build)>(default: false) 29 30### Optional variables 31INSTALL_DIR=<directory for installation, empty means "do not install"> (default: "") 32 33DO_STRIPPING=<when install, strip libraries before installation>(default: true) 34DO_TAR=<when install, also pack llvm-$VERSION-*-*.tar.xz archives for corresponding folders>(default: true) 35 36OHOS_SDK=<path to native OHOS SDK>, required for any OHOS build. 37 38OHOS_PREBUILTS=<path to OHOS pre-build directory>, required for OHOS GN build. 39 40### Build tools 41CC=<C compiler> (default: "/usr/bin/clang-14") 42CXX=<C++ compiler> (default: "/usr/bin/clang++-14") 43STRIP=<strip utility> (default: "/usr/bin/llvm-strip-14") 44OPTIMIZE_DEBUG=<compile debug version with -O2>(default: true) 45``` 46 47**Note! `PACKAGE_VERSION` must be properly set during build to match string 48constant `REQUIRED_LLVM_VERSION` from `libllvmbackend/CMakeLists.txt`.** 49 50## Example for local user build 51 52```bash 53cd /home/user/src 54git clone https://gitee.com/openharmony/third_party_llvm-project.git 55 56INSTALL_DIR="/home/user/inst" \ 57BUILD_DIR="/home/user/build" \ 58LLVM_SOURCES="/home/user/src/llvm-for-ark/llvm" \ 59VERSION="15.0.4-ark99-beta9" \ 60PACKAGE_VERSION="15.0.4-ark99" \ 61OPTIMIZE_DEBUG=false \ 62BUILD_X86_DEBUG=true \ 63BUILD_AARCH64_DEBUG=true \ 64bash -x ./build_llvm.sh 65``` 66 67In this example, only `x86_64` and `arm64` debug versions are built. Then, they can be specified for Ark build, like: 68* host build: `-DLLVM_TARGET_PATH=/home/user/build/llvm-15.0.4-ark99-beta9-debug-x86_64` 69* cross-arm64 build: 70 * `-DLLVM_TARGET_PATH=/home/user/build/llvm-15.0.4-ark99-beta9-debug-aarch64` 71 * `-DLLVM_HOST_PATH=/home/user/build/llvm-15.0.4-ark99-beta9-debug-x86_64` 72 73## Example with packaging all necessary versions 74 75```bash 76INSTALL_DIR="/mnt/scratch/install" \ 77BUILD_DIR="/mnt/scratch/build" \ 78LLVM_SOURCES="/mnt/scratch/src/llvm-for-ark/llvm" \ 79VERSION="15.0.4-ark99-beta9" \ 80PACKAGE_VERSION="15.0.4-ark99" \ 81OHOS_SDK="/opt/ohos-sdk/native" \ 82OHOS_PREBUILTS="/home/user/ohos/prebuilts" \ 83BUILD_X86_DEBUG=true \ 84BUILD_X86_RELEASE=true \ 85BUILD_AARCH64_DEBUG=true \ 86BUILD_AARCH64_RELEASE=true \ 87BUILD_OHOS_RELEASE=true \ 88BUILD_OHOS_RELEASE_GN=true \ 89bash -x ./build_llvm.sh 90``` 91