bootstrap.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #! /bin/bash
  2. # With contributions from Ian McDowell: https://github.com/IMcD23
  3. # Bail out on error
  4. set -e
  5. LIBFFI_SRC=https://www.mirrorservice.org/sites/sourceware.org/pub/libffi/libffi-3.2.1.tar.gz
  6. LLVM_SRCDIR=$(pwd)
  7. OSX_BUILDDIR=$(pwd)/build_osx
  8. IOS_BUILDDIR=$(pwd)/build-iphoneos
  9. SIM_BUILDDIR=$(pwd)/build-iphonesimulator
  10. FFI_SRCDIR=$(pwd)/libffi/
  11. echo "Downloading ios_system Framework:"
  12. IOS_SYSTEM_VER="2.6"
  13. HHROOT="https://github.com/holzschu"
  14. echo "Downloading header file:"
  15. curl -OL $HHROOT/ios_system/releases/download/$IOS_SYSTEM_VER/ios_error.h
  16. echo "Downloading ios_system Framework:"
  17. rm -rf ios_system.xcframework
  18. curl -OL $HHROOT/ios_system/releases/download/$IOS_SYSTEM_VER/ios_system.xcframework.zip
  19. unzip ios_system.xcframework.zip
  20. rm ios_system.xcframework.zip
  21. OSX_SDKROOT=$(xcrun --sdk macosx --show-sdk-path)
  22. IOS_SDKROOT=$(xcrun --sdk iphoneos --show-sdk-path)
  23. SIM_SDKROOT=$(xcrun --sdk iphonesimulator --show-sdk-path)
  24. # Parse arguments
  25. for i in "$@"
  26. do
  27. case $i in
  28. -c|--clean)
  29. CLEAN=YES
  30. shift
  31. ;;
  32. *)
  33. # unknown option
  34. ;;
  35. esac
  36. done
  37. # get clang, libcxx, libcxxabi
  38. git submodule update --init --recursive
  39. # Get libcxx and libcxxabi
  40. # End downloading source
  41. # compile for OSX (about 1h, 1GB of disk space)
  42. echo "Compiling for OSX:"
  43. if [ $CLEAN ]; then
  44. rm -rf $OSX_BUILDDIR
  45. fi
  46. if [ ! -d $OSX_BUILDDIR ]; then
  47. mkdir $OSX_BUILDDIR
  48. fi
  49. # building with -DLLVM_LINK_LLVM_DYLIB (= single big shared lib)
  50. # Easier to make a framework with
  51. pushd $OSX_BUILDDIR
  52. cmake -G Ninja \
  53. -DLLVM_TARGETS_TO_BUILD="AArch64;X86;WebAssembly" \
  54. -DLLVM_LINK_LLVM_DYLIB=ON \
  55. -DCMAKE_BUILD_TYPE=Release \
  56. -DCMAKE_OSX_SYSROOT=${OSX_SDKROOT} \
  57. -DCMAKE_C_COMPILER=$(xcrun --sdk macosx -f clang) \
  58. -DCMAKE_CXX_COMPILER=$(xcrun --sdk macosx -f clang++) \
  59. -DCMAKE_ASM_COMPILER=$(xcrun --sdk macosx -f cc) \
  60. -DCMAKE_LIBRARY_PATH=${OSX_SDKROOT}/lib/ \
  61. -DCMAKE_INCLUDE_PATH=${OSX_SDKROOT}/include/ \
  62. ..
  63. ninja
  64. popd
  65. # libtool: where? /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool
  66. # get libffi:
  67. export M4=/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin//m4
  68. echo "Compiling libffi:"
  69. pushd $FFI_SRCDIR
  70. # We need to patch libffi to allow compilation with Xcode 12, but only once (min iOS version >= 9)
  71. # patch -p 1 < ../libffi.patch
  72. echo "Compiling libffi:"
  73. xcodebuild -project libffi.xcodeproj -target libffi-iOS -sdk iphoneos -arch arm64 -configuration Debug -quiet
  74. xcodebuild -project libffi.xcodeproj -target libffi-iOS -sdk iphonesimulator -configuration Debug -quiet
  75. popd
  76. # Now, compile for iOS using the previous build:
  77. # About 1h, 12 GB of disk space
  78. # -DLLVM_ENABLE_THREADS=OFF is necessary to run commands multiple times
  79. # -I${OSX_BUILDDIR}/include/c++/v1/
  80. # Try to reduce inlining (doesn't work at compile time)
  81. # -D_LIBCPP_INLINE_VISIBILITY=\"\" -D_LIBCPP_ALWAYS_INLINE=\"\" -D_LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY=\"\"
  82. echo "Compiling for iOS:"
  83. if [ $CLEAN ]; then
  84. rm -rf $IOS_BUILDDIR
  85. fi
  86. if [ ! -d $IOS_BUILDDIR ]; then
  87. mkdir $IOS_BUILDDIR
  88. fi
  89. pushd $IOS_BUILDDIR
  90. cmake -G Ninja \
  91. -DLLVM_LINK_LLVM_DYLIB=ON \
  92. -DLLVM_TARGET_ARCH=AArch64 \
  93. -DLLVM_TARGETS_TO_BUILD="AArch64;X86;WebAssembly" \
  94. -DLLVM_DEFAULT_TARGET_TRIPLE=arm64-apple-darwin19.0.0 \
  95. -DLLVM_ENABLE_FFI=ON \
  96. -DLLVM_ENABLE_THREADS=OFF \
  97. -DLLVM_ENABLE_TERMINFO=OFF \
  98. -DLLVM_ENABLE_BACKTRACES=OFF \
  99. -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF \
  100. -DFFI_LIBRARY_PATH=${FFI_SRCDIR}/build/Debug-iphoneos/libffi.a \
  101. -DFFI_INCLUDE_DIR=${FFI_SRCDIR}/build_iphoneos-arm64/include \
  102. -DLLVM_TABLEGEN=${OSX_BUILDDIR}/bin/llvm-tblgen \
  103. -DCLANG_TABLEGEN=${OSX_BUILDDIR}/bin/clang-tblgen \
  104. -DCMAKE_OSX_SYSROOT=${IOS_SDKROOT} \
  105. -DCMAKE_C_COMPILER=${OSX_BUILDDIR}/bin/clang \
  106. -DCMAKE_LIBRARY_PATH=${OSX_BUILDDIR}/lib/ \
  107. -DCMAKE_INCLUDE_PATH=${OSX_BUILDDIR}/include/ \
  108. -DCMAKE_C_FLAGS="-arch arm64 -target arm64-apple-darwin19.0.0 -O2 -D_LIBCPP_STRING_H_HAS_CONST_OVERLOADS -I${OSX_BUILDDIR}/include/ -I${OSX_BUILDDIR}/include/c++/v1/ -I${LLVM_SRCDIR} -miphoneos-version-min=11 " \
  109. -DCMAKE_CXX_FLAGS="-arch arm64 -target arm64-apple-darwin19.0.0 -O2 -D_LIBCPP_STRING_H_HAS_CONST_OVERLOADS -I${OSX_BUILDDIR}/include/ -I${LLVM_SRCDIR} -miphoneos-version-min=11 " \
  110. -DCMAKE_MODULE_LINKER_FLAGS="-nostdlib -F${LLVM_SRCDIR}/ios_system.xcframework/ios-arm64_armv7 -O2 -framework ios_system -lobjc -lc -lc++" \
  111. -DCMAKE_SHARED_LINKER_FLAGS="-nostdlib -F${LLVM_SRCDIR}/ios_system.xcframework/ios-arm64_armv7 -O2 -framework ios_system -lobjc -lc -lc++" \
  112. -DCMAKE_EXE_LINKER_FLAGS="-nostdlib -F${LLVM_SRCDIR}/ios_system.xcframework/ios-arm64_armv7 -O2 -framework ios_system -lobjc -lc -lc++" \
  113. ..
  114. ninja
  115. # We could add X86 to target architectures, but that increases the app size too much
  116. # Now build the static libraries for the executables:
  117. # -stdlib=libc++: not required with OSX > Mavericks
  118. # -nostdlib: so ios_system is linked *before* libc and libc++
  119. # try with: -fvisibility=hidden -fvisibility-inlines-hidden in CFLAGS for the warning
  120. # -L lib = crashes every time (self-reference).
  121. # lli crashes, but only lli. When creating main() (before the first line)
  122. rm -f lib/liblli.a
  123. rm -f lib/libllc.a
  124. # Xcode gets confused if a static and a dynamic library share the same name:
  125. rm -f lib/libclang_tool.a
  126. rm -f lib/libopt.a
  127. ar -r lib/libclang_tool.a tools/clang/tools/driver/CMakeFiles/clang.dir/driver.cpp.o tools/clang/tools/driver/CMakeFiles/clang.dir/cc1_main.cpp.o tools/clang/tools/driver/CMakeFiles/clang.dir/cc1as_main.cpp.o tools/clang/tools/driver/CMakeFiles/clang.dir/cc1gen_reproducer_main.cpp.o
  128. ar -r lib/libopt.a tools/opt/CMakeFiles/opt.dir/AnalysisWrappers.cpp.o tools/opt/CMakeFiles/opt.dir/BreakpointPrinter.cpp.o tools/opt/CMakeFiles/opt.dir/Debugify.cpp.o tools/opt/CMakeFiles/opt.dir/GraphPrinters.cpp.o tools/opt/CMakeFiles/opt.dir/NewPMDriver.cpp.o tools/opt/CMakeFiles/opt.dir/PassPrinters.cpp.o tools/opt/CMakeFiles/opt.dir/PrintSCC.cpp.o tools/opt/CMakeFiles/opt.dir/opt.cpp.o
  129. # No need to make static libraries for these:
  130. # lli: tools/lli/CMakeFiles/lli.dir/lli.cpp.o
  131. # llvm-link: tools/llvm-link/CMakeFiles/llvm-link.dir/llvm-link.cpp.o
  132. # llvm-nm: tools/llvm-nm/CMakeFiles/llvm-nm.dir/llvm-nm.cpp.o
  133. # llvm-ar: tools/llvm-ar/CMakeFiles/llvm-ar.dir/llvm-ar.cpp.o
  134. # llvm-dis: tools/llvm-dis/CMakeFiles/llvm-dis.dir/llvm-dis.cpp.o
  135. # llc: tools/llc/CMakeFiles/llc.dir/llc.cpp.o
  136. # lld, wasm-ld, etc: done in Xcode.
  137. rm -rf frameworks.xcodeproj
  138. cp -r ../frameworks/frameworks.xcodeproj .
  139. # And then build the frameworks from these static libraries:
  140. xcodebuild -project frameworks.xcodeproj -alltargets -sdk iphoneos -configuration Release -quiet
  141. popd
  142. # Now, build for the simulator:
  143. echo "Compiling for the simulator:"
  144. if [ $CLEAN ]; then
  145. rm -rf $SIM_BUILDDIR
  146. fi
  147. if [ ! -d $SIM_BUILDDIR ]; then
  148. mkdir $SIM_BUILDDIR
  149. fi
  150. pushd $SIM_BUILDDIR
  151. cmake -G Ninja \
  152. -DLLVM_LINK_LLVM_DYLIB=ON \
  153. -DLLVM_TARGET_ARCH=X86 \
  154. -DLLVM_TARGETS_TO_BUILD="AArch64;X86;WebAssembly" \
  155. -DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-apple-darwin19.0.0 \
  156. -DLLVM_ENABLE_FFI=ON \
  157. -DLLVM_ENABLE_THREADS=OFF \
  158. -DLLVM_ENABLE_TERMINFO=OFF \
  159. -DLLVM_ENABLE_BACKTRACES=OFF \
  160. -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF \
  161. -DCMAKE_CROSSCOMPILING=TRUE \
  162. -DFFI_LIBRARY_PATH=${FFI_SRCDIR}/build/Debug-iphonesimulator/libffi.a \
  163. -DFFI_INCLUDE_DIR=${FFI_SRCDIR}/build_iphonesimulator-x86_64/include \
  164. -DLLVM_TABLEGEN=${OSX_BUILDDIR}/bin/llvm-tblgen \
  165. -DCLANG_TABLEGEN=${OSX_BUILDDIR}/bin/clang-tblgen \
  166. -DCMAKE_OSX_SYSROOT=${SIM_SDKROOT} \
  167. -DCMAKE_C_COMPILER=${OSX_BUILDDIR}/bin/clang \
  168. -DCMAKE_LIBRARY_PATH=${OSX_BUILDDIR}/lib/ \
  169. -DCMAKE_INCLUDE_PATH=${OSX_BUILDDIR}/include/ \
  170. -DCMAKE_C_FLAGS="-target x86_64-apple-darwin19.0.0 -O2 -D_LIBCPP_STRING_H_HAS_CONST_OVERLOADS -I${OSX_BUILDDIR}/include/ -I${OSX_BUILDDIR}/include/c++/v1/ -I${LLVM_SRCDIR} -mios-simulator-version-min=11.0 " \
  171. -DCMAKE_CXX_FLAGS="-target x86_64-apple-darwin19.0.0 -O2 -D_LIBCPP_STRING_H_HAS_CONST_OVERLOADS -I${OSX_BUILDDIR}/include/ -I${LLVM_SRCDIR} -mios-simulator-version-min=11.0 " \
  172. -DCMAKE_MODULE_LINKER_FLAGS="-nostdlib -F${LLVM_SRCDIR}/ios_system.xcframework/ios-i386_x86_64-simulator -O2 -framework ios_system -lobjc -lc -lc++" \
  173. -DCMAKE_SHARED_LINKER_FLAGS="-nostdlib -F${LLVM_SRCDIR}/ios_system.xcframework/ios-i386_x86_64-simulator -O2 -framework ios_system -lobjc -lc -lc++" \
  174. -DCMAKE_EXE_LINKER_FLAGS="-nostdlib -F${LLVM_SRCDIR}/ios_system.xcframework/ios-i386_x86_64-simulator -O2 -framework ios_system -lobjc -lc -lc++" \
  175. ..
  176. ninja
  177. # We could add X86 to target architectures, but that increases the app size too much
  178. # Now build the static libraries for the executables:
  179. # -stdlib=libc++: not required with OSX > Mavericks
  180. # -nostdlib: so ios_system is linked *before* libc and libc++
  181. # try with: -fvisibility=hidden -fvisibility-inlines-hidden in CFLAGS for the warning
  182. # -L lib = crashes every time (self-reference).
  183. # lli crashes, but only lli. When creating main() (before the first line)
  184. rm -f lib/liblli.a
  185. rm -f lib/libllc.a
  186. # Xcode gets confused if a static and a dynamic library share the same name:
  187. rm -f lib/libclang_tool.a
  188. rm -f lib/libopt.a
  189. ar -r lib/libclang_tool.a tools/clang/tools/driver/CMakeFiles/clang.dir/driver.cpp.o tools/clang/tools/driver/CMakeFiles/clang.dir/cc1_main.cpp.o tools/clang/tools/driver/CMakeFiles/clang.dir/cc1as_main.cpp.o tools/clang/tools/driver/CMakeFiles/clang.dir/cc1gen_reproducer_main.cpp.o
  190. ar -r lib/libopt.a tools/opt/CMakeFiles/opt.dir/AnalysisWrappers.cpp.o tools/opt/CMakeFiles/opt.dir/BreakpointPrinter.cpp.o tools/opt/CMakeFiles/opt.dir/Debugify.cpp.o tools/opt/CMakeFiles/opt.dir/GraphPrinters.cpp.o tools/opt/CMakeFiles/opt.dir/NewPMDriver.cpp.o tools/opt/CMakeFiles/opt.dir/PassPrinters.cpp.o tools/opt/CMakeFiles/opt.dir/PrintSCC.cpp.o tools/opt/CMakeFiles/opt.dir/opt.cpp.o
  191. # No need to make static libraries for these:
  192. # lli: tools/lli/CMakeFiles/lli.dir/lli.cpp.o
  193. # llvm-link: tools/llvm-link/CMakeFiles/llvm-link.dir/llvm-link.cpp.o
  194. # llvm-nm: tools/llvm-nm/CMakeFiles/llvm-nm.dir/llvm-nm.cpp.o
  195. # llvm-ar: tools/llvm-ar/CMakeFiles/llvm-ar.dir/llvm-ar.cpp.o
  196. # llvm-dis: tools/llvm-dis/CMakeFiles/llvm-dis.dir/llvm-dis.cpp.o
  197. # llc: tools/llc/CMakeFiles/llc.dir/llc.cpp.o
  198. # lld, wasm-ld, etc: done in Xcode.
  199. rm -rf frameworks.xcodeproj
  200. cp -r ../frameworks/frameworks.xcodeproj .
  201. # And then build the frameworks from these static libraries:
  202. xcodebuild -project frameworks.xcodeproj -alltargets -sdk iphonesimulator -configuration Release -quiet
  203. popd
  204. # 6)
  205. echo "Merging into xcframeworks:"
  206. for framework in ar lld llc clang dis libLLVM link lli nm opt
  207. do
  208. rm -rf $framework.xcframework
  209. xcodebuild -create-xcframework -framework build-iphoneos/build/Release-iphoneos/$framework.framework -framework build-iphonesimulator/build/Release-iphonesimulator/$framework.framework -output $framework.xcframework
  210. # while we're at it, let's compute the checksum:
  211. rm -f $framework.xcframework.zip
  212. zip -r $framework.xcframework.zip $framework.xcframework
  213. swift package compute-checksum $framework.xcframework.zip
  214. done