build_wine.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #!/usr/bin/env bash
  2. ########################################################################
  3. ##
  4. ## A script for Wine compilation.
  5. ## By default it uses two Ubuntu bootstraps (x32 and x64), which it enters
  6. ## with bubblewrap (root rights are not required).
  7. ##
  8. ## This script requires: git, wget, autoconf, xz, bubblewrap
  9. ##
  10. ## You can change the environment variables below to your desired values.
  11. ##
  12. ########################################################################
  13. # Prevent launching as root
  14. if [ $EUID = 0 ] && [ -z "$ALLOW_ROOT" ]; then
  15. echo "Do not run this script as root!"
  16. echo
  17. echo "If you really need to run it as root and you know what you are doing,"
  18. echo "set the ALLOW_ROOT environment variable."
  19. exit 1
  20. fi
  21. # Wine version to compile.
  22. # You can set it to "latest" to compile the latest available version.
  23. # You can also set it to "git" to compile the latest git revision.
  24. #
  25. # This variable affects only vanilla and staging branches. Other branches
  26. # use their own versions.
  27. export WINE_VERSION="${WINE_VERSION:-latest}"
  28. # Available branches: vanilla, staging, proton, staging-tkg, staging-tkg-ntsync
  29. export WINE_BRANCH="${WINE_BRANCH:-staging}"
  30. # Available proton branches: proton_3.7, proton_3.16, proton_4.2, proton_4.11
  31. # proton_5.0, proton_5.13, experimental_5.13, proton_6.3, experimental_6.3
  32. # proton_7.0, experimental_7.0, proton_8.0, experimental_8.0, experimental_9.0
  33. # bleeding-edge
  34. # Leave empty to use the default branch.
  35. export PROTON_BRANCH="${PROTON_BRANCH:-proton_10.0}"
  36. # Sometimes Wine and Staging versions don't match (for example, 5.15.2).
  37. # Leave this empty to use Staging version that matches the Wine version.
  38. export STAGING_VERSION="${STAGING_VERSION:-}"
  39. # Specify custom arguments for the Staging's patchinstall.sh script.
  40. # For example, if you want to disable ntdll-NtAlertThreadByThreadId
  41. # patchset, but apply all other patches, then set this variable to
  42. # "--all -W ntdll-NtAlertThreadByThreadId"
  43. # Leave empty to apply all Staging patches
  44. export STAGING_ARGS="${STAGING_ARGS:-}"
  45. # Make 64-bit Wine builds with the new WoW64 mode (32-on-64)
  46. export EXPERIMENTAL_WOW64="${EXPERIMENTAL_WOW64:-false}"
  47. # Set this to a path to your Wine source code (for example, /home/username/wine-custom-src).
  48. # This is useful if you already have the Wine source code somewhere on your
  49. # storage and you want to compile it.
  50. #
  51. # You can also set this to a GitHub clone url instead of a local path.
  52. #
  53. # If you don't want to compile a custom Wine source code, then just leave this
  54. # variable empty.
  55. export CUSTOM_SRC_PATH=""
  56. # Set to true to download and prepare the source code, but do not compile it.
  57. # If this variable is set to true, root rights are not required.
  58. export DO_NOT_COMPILE="false"
  59. # Set to true to use ccache to speed up subsequent compilations.
  60. # First compilation will be a little longer, but subsequent compilations
  61. # will be significantly faster (especially if you use a fast storage like SSD).
  62. #
  63. # Note that ccache requires additional storage space.
  64. # By default it has a 5 GB limit for its cache size.
  65. #
  66. # Make sure that ccache is installed before enabling this.
  67. export USE_CCACHE="false"
  68. export WINE_BUILD_OPTIONS="--without-ldap --without-oss --disable-winemenubuilder --disable-win16 --disable-tests"
  69. # A temporary directory where the Wine source code will be stored.
  70. # Do not set this variable to an existing non-empty directory!
  71. # This directory is removed and recreated on each script run.
  72. export BUILD_DIR="${HOME}"/build_wine
  73. # Change these paths to where your Ubuntu bootstraps reside
  74. export BOOTSTRAP_X64=/opt/chroots/bionic64_chroot
  75. export BOOTSTRAP_X32=/opt/chroots/bionic32_chroot
  76. export scriptdir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
  77. export CC="gcc-11"
  78. export CXX="g++-11"
  79. export CROSSCC_X32="i686-w64-mingw32-gcc"
  80. export CROSSCXX_X32="i686-w64-mingw32-g++"
  81. export CROSSCC_X64="x86_64-w64-mingw32-gcc"
  82. export CROSSCXX_X64="x86_64-w64-mingw32-g++"
  83. export CFLAGS_X32="-march=i686 -msse2 -mfpmath=sse -O2 -ftree-vectorize"
  84. export CFLAGS_X64="-march=x86-64 -msse3 -mfpmath=sse -O2 -ftree-vectorize"
  85. export LDFLAGS="-Wl,-O1,--sort-common,--as-needed"
  86. export CROSSCFLAGS_X32="${CFLAGS_X32}"
  87. export CROSSCFLAGS_X64="${CFLAGS_X64}"
  88. export CROSSLDFLAGS="${LDFLAGS}"
  89. if [ "$USE_CCACHE" = "true" ]; then
  90. export CC="ccache ${CC}"
  91. export CXX="ccache ${CXX}"
  92. export i386_CC="ccache ${CROSSCC_X32}"
  93. export x86_64_CC="ccache ${CROSSCC_X64}"
  94. export CROSSCC_X32="ccache ${CROSSCC_X32}"
  95. export CROSSCXX_X32="ccache ${CROSSCXX_X32}"
  96. export CROSSCC_X64="ccache ${CROSSCC_X64}"
  97. export CROSSCXX_X64="ccache ${CROSSCXX_X64}"
  98. if [ -z "${XDG_CACHE_HOME}" ]; then
  99. export XDG_CACHE_HOME="${HOME}"/.cache
  100. fi
  101. mkdir -p "${XDG_CACHE_HOME}"/ccache
  102. mkdir -p "${HOME}"/.ccache
  103. fi
  104. build_with_bwrap () {
  105. if [ "${1}" = "32" ]; then
  106. BOOTSTRAP_PATH="${BOOTSTRAP_X32}"
  107. else
  108. BOOTSTRAP_PATH="${BOOTSTRAP_X64}"
  109. fi
  110. if [ "${1}" = "32" ] || [ "${1}" = "64" ]; then
  111. shift
  112. fi
  113. bwrap --ro-bind "${BOOTSTRAP_PATH}" / --dev /dev --ro-bind /sys /sys \
  114. --proc /proc --tmpfs /tmp --tmpfs /home --tmpfs /run --tmpfs /var \
  115. --tmpfs /mnt --tmpfs /media --bind "${BUILD_DIR}" "${BUILD_DIR}" \
  116. --bind-try "${XDG_CACHE_HOME}"/ccache "${XDG_CACHE_HOME}"/ccache \
  117. --bind-try "${HOME}"/.ccache "${HOME}"/.ccache \
  118. --setenv PATH "/opt/mingw/x86_64/bin:/opt/mingw/i686/bin:/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin" \
  119. "$@"
  120. }
  121. if ! command -v git 1>/dev/null; then
  122. echo "Please install git and run the script again"
  123. exit 1
  124. fi
  125. if ! command -v autoconf 1>/dev/null; then
  126. echo "Please install autoconf and run the script again"
  127. exit 1
  128. fi
  129. if ! command -v wget 1>/dev/null; then
  130. echo "Please install wget and run the script again"
  131. exit 1
  132. fi
  133. if ! command -v xz 1>/dev/null; then
  134. echo "Please install xz and run the script again"
  135. exit 1
  136. fi
  137. # Replace the "latest" parameter with the actual latest Wine version
  138. if [ "${WINE_VERSION}" = "latest" ] || [ -z "${WINE_VERSION}" ]; then
  139. WINE_VERSION="$(wget -q -O - "https://raw.githubusercontent.com/wine-mirror/wine/master/VERSION" | tail -c +14)"
  140. fi
  141. # Stable and Development versions have a different source code location
  142. # Determine if the chosen version is stable or development
  143. if [ "$(echo "$WINE_VERSION" | cut -d "." -f2 | cut -c1)" = "0" ]; then
  144. WINE_URL_VERSION=$(echo "$WINE_VERSION" | cut -d "." -f 1).0
  145. else
  146. WINE_URL_VERSION=$(echo "$WINE_VERSION" | cut --d "." -f 1).x
  147. fi
  148. rm -rf "${BUILD_DIR}"
  149. mkdir -p "${BUILD_DIR}"
  150. cd "${BUILD_DIR}" || exit 1
  151. echo
  152. echo "Downloading the source code and patches"
  153. echo "Preparing Wine for compilation"
  154. echo
  155. if [ -n "${CUSTOM_SRC_PATH}" ]; then
  156. is_url="$(echo "${CUSTOM_SRC_PATH}" | head -c 6)"
  157. if [ "${is_url}" = "git://" ] || [ "${is_url}" = "https:" ]; then
  158. git clone "${CUSTOM_SRC_PATH}" wine
  159. else
  160. if [ ! -f "${CUSTOM_SRC_PATH}"/configure ]; then
  161. echo "CUSTOM_SRC_PATH is set to an incorrect or non-existent directory!"
  162. echo "Please make sure to use a directory with the correct Wine source code."
  163. exit 1
  164. fi
  165. cp -r "${CUSTOM_SRC_PATH}" wine
  166. fi
  167. WINE_VERSION="$(cat wine/VERSION | tail -c +14)"
  168. BUILD_NAME="${WINE_VERSION}"-custom
  169. elif [ "$WINE_BRANCH" = "staging-tkg" ] || [ "$WINE_BRANCH" = "staging-tkg-ntsync" ]; then
  170. if [ "$WINE_BRANCH" = "staging-tkg" ] && [ "${EXPERIMENTAL_WOW64}" = "true" ]; then
  171. git clone https://github.com/Kron4ek/wine-tkg wine -b wow64
  172. else
  173. if [ "$WINE_BRANCH" = "staging-tkg" ]; then
  174. git clone https://github.com/Kron4ek/wine-tkg wine
  175. else
  176. git clone https://github.com/Kron4ek/wine-tkg wine -b ntsync
  177. fi
  178. fi
  179. WINE_VERSION="$(cat wine/VERSION | tail -c +14)"
  180. BUILD_NAME="${WINE_VERSION}"-"${WINE_BRANCH}"
  181. elif [ "$WINE_BRANCH" = "proton" ]; then
  182. if [ -z "${PROTON_BRANCH}" ]; then
  183. git clone https://github.com/ValveSoftware/wine
  184. else
  185. git clone https://github.com/ValveSoftware/wine -b "${PROTON_BRANCH}"
  186. fi
  187. WINE_VERSION="$(cat wine/VERSION | tail -c +14)-$(git -C wine rev-parse --short HEAD)"
  188. if [[ "${PROTON_BRANCH}" == "experimental_"* ]] || [ "${PROTON_BRANCH}" = "bleeding-edge" ]; then
  189. patch -d wine -Np1 < "${scriptdir}"/proton-opencl.patch
  190. BUILD_NAME=proton-exp-"${WINE_VERSION}"
  191. else
  192. BUILD_NAME=proton-"${WINE_VERSION}"
  193. fi
  194. else
  195. if [ "${WINE_VERSION}" = "git" ]; then
  196. git clone https://gitlab.winehq.org/wine/wine.git wine
  197. BUILD_NAME="${WINE_VERSION}-$(git -C wine rev-parse --short HEAD)"
  198. else
  199. BUILD_NAME="${WINE_VERSION}"
  200. wget -q --show-progress "https://dl.winehq.org/wine/source/${WINE_URL_VERSION}/wine-${WINE_VERSION}.tar.xz"
  201. tar xf "wine-${WINE_VERSION}.tar.xz"
  202. mv "wine-${WINE_VERSION}" wine
  203. fi
  204. if [ "${WINE_BRANCH}" = "staging" ]; then
  205. if [ "${WINE_VERSION}" = "git" ]; then
  206. git clone https://github.com/wine-staging/wine-staging wine-staging-"${WINE_VERSION}"
  207. upstream_commit="$(cat wine-staging-"${WINE_VERSION}"/staging/upstream-commit | head -c 7)"
  208. git -C wine checkout "${upstream_commit}"
  209. BUILD_NAME="${WINE_VERSION}-${upstream_commit}-staging"
  210. else
  211. if [ -n "${STAGING_VERSION}" ]; then
  212. WINE_VERSION="${STAGING_VERSION}"
  213. fi
  214. BUILD_NAME="${WINE_VERSION}"-staging
  215. wget -q --show-progress "https://github.com/wine-staging/wine-staging/archive/v${WINE_VERSION}.tar.gz"
  216. tar xf v"${WINE_VERSION}".tar.gz
  217. if [ ! -f v"${WINE_VERSION}".tar.gz ]; then
  218. git clone https://github.com/wine-staging/wine-staging wine-staging-"${WINE_VERSION}"
  219. fi
  220. fi
  221. if [ -f wine-staging-"${WINE_VERSION}"/patches/patchinstall.sh ]; then
  222. staging_patcher=("${BUILD_DIR}"/wine-staging-"${WINE_VERSION}"/patches/patchinstall.sh
  223. DESTDIR="${BUILD_DIR}"/wine)
  224. else
  225. staging_patcher=("${BUILD_DIR}"/wine-staging-"${WINE_VERSION}"/staging/patchinstall.py)
  226. fi
  227. cd wine || exit 1
  228. if [ -n "${STAGING_ARGS}" ]; then
  229. "${staging_patcher[@]}" ${STAGING_ARGS}
  230. else
  231. "${staging_patcher[@]}" --all
  232. fi
  233. if [ $? -ne 0 ]; then
  234. echo
  235. echo "Wine-Staging patches were not applied correctly!"
  236. exit 1
  237. fi
  238. cd "${BUILD_DIR}" || exit 1
  239. fi
  240. fi
  241. if [ ! -d wine ]; then
  242. clear
  243. echo "No Wine source code found!"
  244. echo "Make sure that the correct Wine version is specified."
  245. exit 1
  246. fi
  247. cd wine || exit 1
  248. dlls/winevulkan/make_vulkan
  249. tools/make_requests
  250. tools/make_specfiles
  251. autoreconf -f
  252. cd "${BUILD_DIR}" || exit 1
  253. if [ "${DO_NOT_COMPILE}" = "true" ]; then
  254. clear
  255. echo "DO_NOT_COMPILE is set to true"
  256. echo "Force exiting"
  257. exit
  258. fi
  259. if ! command -v bwrap 1>/dev/null; then
  260. echo "Bubblewrap is not installed on your system!"
  261. echo "Please install it and run the script again"
  262. exit 1
  263. fi
  264. if [ ! -d "${BOOTSTRAP_X64}" ] || [ ! -d "${BOOTSTRAP_X32}" ]; then
  265. clear
  266. echo "Bootstraps are required for compilation!"
  267. exit 1
  268. fi
  269. BWRAP64="build_with_bwrap 64"
  270. BWRAP32="build_with_bwrap 32"
  271. export CROSSCC="${CROSSCC_X64}"
  272. export CROSSCXX="${CROSSCXX_X64}"
  273. export CFLAGS="${CFLAGS_X64}"
  274. export CXXFLAGS="${CFLAGS_X64}"
  275. export CROSSCFLAGS="${CROSSCFLAGS_X64}"
  276. export CROSSCXXFLAGS="${CROSSCFLAGS_X64}"
  277. mkdir "${BUILD_DIR}"/build64
  278. cd "${BUILD_DIR}"/build64 || exit
  279. ${BWRAP64} "${BUILD_DIR}"/wine/configure --enable-win64 ${WINE_BUILD_OPTIONS} --prefix "${BUILD_DIR}"/wine-"${BUILD_NAME}"-amd64
  280. ${BWRAP64} make -j$(nproc) install
  281. export CROSSCC="${CROSSCC_X32}"
  282. export CROSSCXX="${CROSSCXX_X32}"
  283. export CFLAGS="${CFLAGS_X32}"
  284. export CXXFLAGS="${CFLAGS_X32}"
  285. export CROSSCFLAGS="${CROSSCFLAGS_X32}"
  286. export CROSSCXXFLAGS="${CROSSCFLAGS_X32}"
  287. mkdir "${BUILD_DIR}"/build32-tools
  288. cd "${BUILD_DIR}"/build32-tools || exit
  289. PKG_CONFIG_LIBDIR=/usr/lib/i386-linux-gnu/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/lib/i386-linux-gnu/pkgconfig ${BWRAP32} "${BUILD_DIR}"/wine/configure ${WINE_BUILD_OPTIONS} --prefix "${BUILD_DIR}"/wine-"${BUILD_NAME}"-x86
  290. ${BWRAP32} make -j$(nproc) install
  291. export CFLAGS="${CFLAGS_X64}"
  292. export CXXFLAGS="${CFLAGS_X64}"
  293. export CROSSCFLAGS="${CROSSCFLAGS_X64}"
  294. export CROSSCXXFLAGS="${CROSSCFLAGS_X64}"
  295. mkdir "${BUILD_DIR}"/build32
  296. cd "${BUILD_DIR}"/build32 || exit
  297. PKG_CONFIG_LIBDIR=/usr/lib/i386-linux-gnu/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/lib/i386-linux-gnu/pkgconfig ${BWRAP32} "${BUILD_DIR}"/wine/configure --with-wine64="${BUILD_DIR}"/build64 --with-wine-tools="${BUILD_DIR}"/build32-tools ${WINE_BUILD_OPTIONS} --prefix "${BUILD_DIR}"/wine-${BUILD_NAME}-amd64
  298. ${BWRAP32} make -j$(nproc) install
  299. echo
  300. echo "Compilation complete"
  301. echo "Creating and compressing archives..."
  302. cd "${BUILD_DIR}" || exit
  303. if touch "${scriptdir}"/write_test; then
  304. rm -f "${scriptdir}"/write_test
  305. result_dir="${scriptdir}"
  306. else
  307. result_dir="${HOME}"
  308. fi
  309. export XZ_OPT="-9"
  310. if [ "${EXPERIMENTAL_WOW64}" = "true" ]; then
  311. mv wine-${BUILD_NAME}-amd64 wine-${BUILD_NAME}-amd64-wow64
  312. builds_list="wine-${BUILD_NAME}-amd64-wow64"
  313. else
  314. builds_list="wine-${BUILD_NAME}-x86 wine-${BUILD_NAME}-amd64"
  315. fi
  316. for build in ${builds_list}; do
  317. if [ -d "${build}" ]; then
  318. if [ -f wine/wine-tkg-config.txt ]; then
  319. cp wine/wine-tkg-config.txt "${build}"
  320. fi
  321. if [ "${EXPERIMENTAL_WOW64}" = "true" ]; then
  322. if [ -f "${build}"/bin/wine64 ]; then
  323. rm "${build}"/bin/wine "${build}"/bin/wine-preloader
  324. cp "${build}"/bin/wine64 "${build}"/bin/wine
  325. else
  326. rm "${build}"/lib/wine/i386-unix/wine "${build}"/lib/wine/i386-unix/wine-preloader
  327. fi
  328. fi
  329. tar -Jcf "${build}".tar.xz "${build}"
  330. mv "${build}".tar.xz "${result_dir}"
  331. fi
  332. done
  333. rm -rf "${BUILD_DIR}"
  334. echo
  335. echo "Done"
  336. echo "The builds should be in ${result_dir}"