Skip to content

Commit

Permalink
feat: bundle LLVM on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Kha committed Oct 22, 2021
1 parent 3b70b39 commit 5ce91d3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
25 changes: 6 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
release: true
shell: nix-shell --arg pkgs "import (fetchTarball \"channel:nixos-19.03\") {{}}" --argstr llvmPackages latest --run "bash -euxo pipefail {0}"
llvm-url: https://github.com/llvm/llvm-project/releases/download/llvmorg-13.0.0/clang+llvm-13.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz
prepare-llvm: script/prepare-llvm-linux.sh clang*.tar.*
- name: Linux
os: ubuntu-latest
check-stage3: true
Expand All @@ -52,6 +53,7 @@ jobs:
CMAKE_OPTIONS: -G "Unix Makefiles" -DAUTO_THREAD_FINALIZATION=OFF
# for reasons unknown, interactivetests are flaky on Windows
CTEST_OPTIONS: --repeat until-pass:2
prepare-llvm: script/prepare-llvm-mingw.sh
# complete all jobs
fail-fast: false
name: ${{ matrix.name }}
Expand Down Expand Up @@ -103,10 +105,8 @@ jobs:
mkdir build
cd build
OPTIONS=()
if [[ '${{ matrix.llvm-url }}' && "${{ matrix.name }}" == "Linux release" ]]; then
wget -q ${{ matrix.llvm-url }}
eval "OPTIONS+=($(../script/prepare-llvm-linux.sh clang*.tar.*))"
fi
[[ -z '${{ matrix.llvm-url }}' ]] || wget -q ${{ matrix.llvm-url }}
[[ -z '${{ matrix.prepare-llvm }}' ]] || eval "OPTIONS+=($(../${{ matrix.prepare-llvm }}))"
if [[ $GITHUB_EVENT_NAME == 'schedule' && -n '${{ matrix.release }}' && -n '${{ secrets.PUSH_NIGHTLY_TOKEN }}' ]]; then
git remote add nightly https://foo:'${{ secrets.PUSH_NIGHTLY_TOKEN }}'@github.com/${{ github.repository_owner }}/lean4-nightly.git
git fetch nightly --tags
Expand All @@ -120,11 +120,10 @@ jobs:
# contortion to support empty OPTIONS with old macOS bash
cmake .. ${{ matrix.CMAKE_OPTIONS }} ${OPTIONS[@]+"${OPTIONS[@]}"}
make -j4
- name: Check binaries
if: matrix.name == 'Linux release'
- name: Check Binaries
if: matrix.os != 'macos-latest'
run: |
ldd -v build/stage1/bin/* || true
ls -l build/stage1/lib/
- name: Patch
if: matrix.name == 'macOS'
run: |
Expand All @@ -136,18 +135,6 @@ jobs:
otool -L $f
done
ls -l lib/
- name: Patch
if: matrix.name == 'Windows'
run: |
cd build/stage1
cp $(ldd bin/lean.exe | cut -f3 -d' ' | grep mingw) bin/
ldd bin/lean.exe
ls -l bin/
- name: Test Binary without Nix Shell
if: matrix.name != 'Windows'
shell: bash {0}
run: |
build/stage1/bin/lean -h
- name: Pack
run: |
cd build/stage1
Expand Down
37 changes: 37 additions & 0 deletions script/prepare-llvm-mingw.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
set -uo pipefail

# use with clang64 MinGW shell!
# run from root build directory (from inside nix-shell or otherwise defining GLIBC/GCC_DEV/GCC_LIBS/GMP) as in
# ```
# eval cmake ../.. $(../../script/prepare-llvm-mingw.sh)
# ```

# use full mingw clang for compiling C++ code, but subset for compiling C code and distribution

root="$(realpath .)/stage1"
mkdir -p stage1/{bin,lib,lib/windows,include/clang}
# a C compiler!
cp $(which clang) stage1/bin/
# a linker!
cp $(which ld.lld) stage1/bin/
# many dependencies!
cp $(ldd stage1/bin/* | cut -f3 -d' ' | grep clang64) stage1/bin
# lean.h dependencies
cp /clang64/lib/clang/*/include/{std*,__std*,limits}.h stage1/include/clang
# single Windows dependency
echo '
// https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-seterrormode
#define SEM_FAILCRITICALERRORS 0x0001
__declspec(dllimport) __stdcall unsigned int SetErrorMode(unsigned int uMode);' > stage1/include/clang/windows.h
# COFF dependencies
cp /clang64/x86_64-w64-mingw32/lib/{crtbegin,crtend,crt2,dllcrt2}.o stage1/lib/
# further dependencies
cp /clang64/x86_64-w64-mingw32/lib/lib{m,bcrypt,mingw32,moldname,mingwex,msvcrt,pthread,advapi32,shell32,user32,kernel32}.* /clang64/lib/lib{c++,gmp,unwind}.* stage1/lib/
(cd /clang64; cp --parents lib/clang/*/lib/windows/libclang_rt.builtins-x86_64.a "$root")
cp /clang64/bin/libgmp-* stage1/bin/
echo -n " -DLEAN_USE_LIBCXX=ON"
echo -n " -DLEANC_INTERNAL_FLAGS='--sysroot ROOT -I ROOT/include/clang' -DLEANC_CC=ROOT/bin/clang.exe -DCMAKE_C_COMPILER=$PWD/stage1/bin/clang.exe"
echo -n " -DLEANC_INTERNAL_LINKER_FLAGS='-L ROOT/lib -fuse-ld=ROOT/bin/ld.lld.exe'"
echo -n " -DAUTO_THREAD_FINALIZATION=OFF -DSTAGE0_AUTO_THREAD_FINALIZATION=OFF"
#echo -n " -DLEAN_TEST_VARS='LD_LIBRARY_PATH=$PWD/stage1/lib:\${LD_LIBRARY_PATH:-}'"

0 comments on commit 5ce91d3

Please sign in to comment.