build 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/bin/bash
  2. [% c("var/set_default_env") -%]
  3. builddir=/var/tmp/build
  4. distdir=/var/tmp/dist/mingw-w64-clang
  5. mkdir -p $distdir
  6. cd /var/tmp/dist
  7. tar -xf $rootdir/[% c('input_files_by_name/clang') %]
  8. mv clang/* mingw-w64-clang/
  9. tar -xf $rootdir/[% c('input_files_by_name/cmake') %]
  10. mkdir -p $builddir
  11. cd $builddir
  12. tar -xf $rootdir/mingw-w64-clang-[% c('version') %].tar.gz
  13. mv mingw-w64-clang-[% c('version') %] mingw-w64-clang
  14. tar -xf $rootdir/[% c('input_files_by_name/clang-source') %]
  15. tar -xf $rootdir/[% c('input_files_by_name/llvm-mingw') %]
  16. # Adding the wrappers and symlinks we need
  17. cd $distdir/bin
  18. exception_flags=""
  19. [% IF c("var/windows-i686") %]
  20. exception_flags="-fsjlj-exceptions"
  21. [% END -%]
  22. compiler_flags="--sysroot \$DIR/../[% c("arch") %]-w64-mingw32 -rtlib=compiler-rt -stdlib=libc++ -fuse-ld=lld $exception_flags -fuse-cxa-atexit -Qunused-arguments"
  23. cat <<EOF >[% c("arch") %]-w64-mingw32-clang
  24. #!/bin/sh
  25. DIR="\$(cd "\$(dirname "\$0")" && pwd)"
  26. \$DIR/clang -target [% c("arch") %]-w64-mingw32 $compiler_flags "\$@"
  27. EOF
  28. chmod +x [% c("arch") %]-w64-mingw32-clang
  29. cat <<EOF >[% c("arch") %]-w64-mingw32-clang++
  30. #!/bin/sh
  31. DIR="\$(cd "\$(dirname "\$0")" && pwd)"
  32. \$DIR/clang -target [% c("arch") %]-w64-mingw32 --driver-mode=g++ $compiler_flags "\$@"
  33. EOF
  34. chmod +x [% c("arch") %]-w64-mingw32-clang++
  35. ln -s llvm-nm [% c("arch") %]-w64-mingw32-nm
  36. ln -s llvm-strip [% c("arch") %]-w64-mingw32-strip
  37. ln -s llvm-readobj [% c("arch") %]-w64-mingw32-readobj
  38. ln -s llvm-objcopy [% c("arch") %]-w64-mingw32-objcopy
  39. ./clang $builddir/llvm-mingw/wrappers/windres-wrapper.c -O2 -Wl,-s -o [% c("arch") %]-w64-mingw32-windres
  40. # Building mingw-w64
  41. export PATH="$distdir/bin:/var/tmp/dist/cmake/bin:$PATH"
  42. CC="[% c("arch") %]-w64-mingw32-clang"
  43. CXX="[% c("arch") %]-w64-mingw32-clang++"
  44. # This is the default value of _WIN32_WINNT. Gecko configure script explicitly
  45. # sets this, so this is not used to build Gecko itself. We default to 0x601,
  46. # which is Windows 7.
  47. default_win32_winnt=0x601
  48. [% IF c("var/windows-i686") %]
  49. crt_flags="--enable-lib32 --disable-lib64"
  50. compiler_rt_machine="i386"
  51. [% ELSE -%]
  52. crt_flags="--disable-lib32 --enable-lib64"
  53. compiler_rt_machine="x86_64"
  54. [% END -%]
  55. cd $builddir/mingw-w64-clang
  56. patch -p1 < $rootdir/mingw-winrt.patch
  57. patch -p1 < $rootdir/mingw-dwrite_3.patch
  58. cd mingw-w64-headers
  59. mkdir build && cd build
  60. ../configure --host=[% c("arch") %]-w64-mingw32 \
  61. --enable-sdk=all \
  62. --enable-idl \
  63. --with-default-msvcrt=ucrt \
  64. --with-default-win32-winnt=$default_win32_winnt \
  65. --prefix=$distdir/[% c("arch") %]-w64-mingw32
  66. make -j[% c("buildconf/num_procs") %] install
  67. cd $builddir/mingw-w64-clang/mingw-w64-crt
  68. mkdir build && cd build
  69. ../configure --host=[% c("arch") %]-w64-mingw32 \
  70. $crt_flags \
  71. --with-default-msvcrt=ucrt \
  72. CC="$CC" \
  73. AR=llvm-ar \
  74. RANLIB=llvm-ranlib \
  75. DLLTOOL=llvm-dlltool \
  76. --prefix=$distdir/[% c("arch") %]-w64-mingw32
  77. make -j[% c("buildconf/num_procs") %]
  78. make -j[% c("buildconf/num_procs") %] install
  79. cd $builddir/mingw-w64-clang/mingw-w64-tools/widl
  80. mkdir build && cd build
  81. ../configure --target=[% c("arch") %]-w64-mingw32 --prefix=$distdir
  82. make -j[% c("buildconf/num_procs") %]
  83. make -j[% c("buildconf/num_procs") %] install
  84. # compiler-rt
  85. cd $builddir/clang-source/projects/compiler-rt
  86. mkdir build && cd build
  87. cmake -DCMAKE_BUILD_TYPE=Release \
  88. -DCMAKE_C_COMPILER=$CC \
  89. -DCMAKE_SYSTEM_NAME=Windows \
  90. -DCMAKE_AR=$distdir/bin/llvm-ar \
  91. -DCMAKE_RANLIB=$distdir/bin/llvm-ranlib \
  92. -DCMAKE_C_COMPILER_WORKS=1 \
  93. -DCMAKE_C_COMPILER_TARGET=$compiler_rt_machine-windows-gnu \
  94. -DCOMPILER_RT_DEFAULT_TARGET_ONLY=TRUE \
  95. $builddir/clang-source/projects/compiler-rt/lib/builtins
  96. make -j[% c("buildconf/num_procs") %]
  97. rtdir=$distdir/lib/clang/[% pc("clang-source", "version") %]/lib/windows
  98. mkdir -p $rtdir
  99. cp lib/windows/libclang_rt.builtins-$compiler_rt_machine.a $rtdir/
  100. # libunwind
  101. cd $builddir/clang-source/libunwind
  102. mkdir build && cd build
  103. cmake -DCMAKE_BUILD_TYPE=Release \
  104. -DCMAKE_INSTALL_PREFIX=$distdir/[% c("arch") %]-w64-mingw32 \
  105. -DCMAKE_C_COMPILER=$CC \
  106. -DCMAKE_CXX_COMPILER=$CXX \
  107. -DCMAKE_CROSSCOMPILING=TRUE \
  108. -DCMAKE_SYSROOT=$distdir/[% c("arch") %]-w64-mingw32 \
  109. -DCMAKE_SYSTEM_NAME=Windows \
  110. -DCMAKE_C_COMPILER_WORKS=TRUE \
  111. -DCMAKE_CXX_COMPILER_WORKS=TRUE \
  112. -DLLVM_COMPILER_CHECKED=True \
  113. -DCMAKE_AR=$distdir/bin/llvm-ar \
  114. -DCMAKE_RANLIB=$distdir/bin/llvm-ranlib \
  115. -DLLVM_NO_OLD_LIBSTDCXX=TRUE \
  116. -DCXX_SUPPORTS_CXX11=TRUE \
  117. -DCXX_SUPPORTS_CXX_STD=True \
  118. -DLIBUNWIND_USE_COMPILER_RT=TRUE \
  119. -DLIBUNWIND_ENABLE_THREADS=TRUE \
  120. -DLIBUNWIND_ENABLE_SHARED=FALSE \
  121. -DLIBUNWIND_ENABLE_CROSS_UNWINDING=FALSE \
  122. -DCMAKE_CXX_FLAGS="-Wno-dll-attribute-on-redeclaration -nostdinc++ -I$builddir/clang-source/projects/libcxx/include -DPSAPI_VERSION=2" \
  123. -DCMAKE_C_FLAGS="-Wno-dll-attribute-on-redeclaration" \
  124. $builddir/clang-source/libunwind
  125. make -j[% c("buildconf/num_procs") %]
  126. make -j[% c("buildconf/num_procs") %] install
  127. # libcxxabi
  128. cd $builddir/clang-source/projects/libcxxabi
  129. mkdir build && cd build
  130. cmake -DCMAKE_BUILD_TYPE=Release \
  131. -DCMAKE_INSTALL_PREFIX=$distdir/[% c("arch") %]-w64-mingw32 \
  132. -DCMAKE_C_COMPILER=$CC \
  133. -DCMAKE_CXX_COMPILER=$CXX \
  134. -DCMAKE_CROSSCOMPILING=TRUE \
  135. -DCMAKE_SYSTEM_NAME=Windows \
  136. -DCMAKE_C_COMPILER_WORKS=TRUE \
  137. -DCMAKE_CXX_COMPILER_WORKS=TRUE \
  138. -DCMAKE_SYSROOT=$distdir/[% c("arch") %]-w64-mingw32 \
  139. -DLLVM_COMPILER_CHECKED=True \
  140. -DCMAKE_AR=$distdir/bin/llvm-ar \
  141. -DCMAKE_RANLIB=$distdir/bin/llvm-ranlib \
  142. -DLIBCXXABI_USE_COMPILER_RT=ON \
  143. -DLIBCXXABI_ENABLE_EXCEPTIONS=ON \
  144. -DLIBCXXABI_ENABLE_THREADS=ON \
  145. -DLIBCXXABI_TARGET_TRIPLE=[% c("arch") %]-w64-mingw32 \
  146. -DLIBCXXABI_ENABLE_SHARED=OFF \
  147. -DLIBCXXABI_LIBCXX_INCLUDES=$builddir/clang-source/projects/libcxx/include \
  148. -DLLVM_NO_OLD_LIBSTDCXX=TRUE \
  149. -DCXX_SUPPORTS_CXX11=TRUE \
  150. -DCXX_SUPPORTS_CXX_STD=True \
  151. -DCMAKE_CXX_FLAGS="-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCPP_HAS_THREAD_API_WIN32" \
  152. $builddir/clang-source/projects/libcxxabi
  153. make -j[% c("buildconf/num_procs") %] VERBOSE=1
  154. # libcxx
  155. # the build expects ../llvm to exist so we add a symlink
  156. # See https://gitlab.torproject.org/tpo/applications/tor-browser-build/-/merge_requests/240#note_2729207
  157. ln -s /var/tmp/build/clang-source /var/tmp/build/clang-source/projects/llvm
  158. cd $builddir/clang-source/projects/libcxx
  159. mkdir build && cd build
  160. cmake -DCMAKE_BUILD_TYPE=Release \
  161. -DCMAKE_INSTALL_PREFIX=$distdir/[% c("arch") %]-w64-mingw32 \
  162. -DCMAKE_C_COMPILER=$CC \
  163. -DCMAKE_CXX_COMPILER=$CXX \
  164. -DCMAKE_CROSSCOMPILING=TRUE \
  165. -DCMAKE_SYSTEM_NAME=Windows \
  166. -DCMAKE_C_COMPILER_WORKS=TRUE \
  167. -DCMAKE_CXX_COMPILER_WORKS=TRUE \
  168. -DLLVM_COMPILER_CHECKED=True \
  169. -DCMAKE_AR=$distdir/bin/llvm-ar \
  170. -DCMAKE_RANLIB=$distdir/bin/llvm-ranlib \
  171. -DLIBCXX_USE_COMPILER_RT=ON \
  172. -DLIBCXX_INSTALL_HEADERS=ON \
  173. -DLIBCXX_ENABLE_EXCEPTIONS=ON \
  174. -DLIBCXX_ENABLE_THREADS=ON \
  175. -DLIBCXX_HAS_WIN32_THREAD_API=ON \
  176. -DLIBCXX_ENABLE_MONOTONIC_CLOCK=ON \
  177. -DLIBCXX_ENABLE_SHARED=OFF \
  178. -DLIBCXX_SUPPORTS_STD_EQ_CXX11_FLAG=TRUE \
  179. -DLIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB=TRUE \
  180. -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF \
  181. -DLIBCXX_ENABLE_FILESYSTEM=OFF \
  182. -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=TRUE \
  183. -DLIBCXX_CXX_ABI=libcxxabi \
  184. -DLIBCXX_CXX_ABI_INCLUDE_PATHS=$builddir/clang-source/projects/libcxxabi/include \
  185. -DLIBCXX_CXX_ABI_LIBRARY_PATH=$builddir/clang-source/projects/libcxxabi/build/lib \
  186. -DCMAKE_CXX_FLAGS="-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS" \
  187. $builddir/clang-source/projects/libcxx
  188. make -j[% c("buildconf/num_procs") %] VERBOSE=1
  189. make -j[% c("buildconf/num_procs") %] install
  190. # libc++.a depends on libunwind.a. While the linker will automatically link to
  191. # libc++.a in C++ mode, it won't pick libunwind.a, requiring the caller to
  192. # explicitly pass -lunwind. To work around that, we merge libunwind.a into
  193. # libc++.a.
  194. merge_libs() {
  195. cat <<EOF |llvm-ar -M
  196. CREATE tmp.a
  197. ADDLIB $1
  198. ADDLIB $2
  199. SAVE
  200. END
  201. EOF
  202. llvm-ranlib tmp.a
  203. mv tmp.a $1
  204. }
  205. merge_libs $distdir/[% c("arch") %]-w64-mingw32/lib/libc++.a $distdir/[% c("arch") %]-w64-mingw32/lib/libunwind.a
  206. # Packaging up everything
  207. cd $distdir
  208. cd ..
  209. [% c('tar', {
  210. tar_src => [ project ],
  211. tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
  212. }) %]