Automate installation of DXVK, D9VK + Wine/Wine Staging & update GPU drivers + PlayonLinux wineprefixes (Debian/Ubuntu/Mint/Arch Linux/Manjaro)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

556 lines
16 KiB

6 years ago
  1. #!/bin/env bash
  2. # Set up Wine Staging + DXVK on Arch Linux & Variants
  3. # Copyright (C) 2018 Pekka Helenius
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. ########################################################
  18. # DO NOT RUN INDIVIDUALLY, ONLY VIA ../updatewine.sh PARENT SCRIPT!
  19. ########################################################
  20. # Root directory of this script file
  21. ARCH_BUILDROOT="${PWD}"
  22. # datedir variable supplied by ../updatewine.sh script file
  23. datedir="${1}"
  24. ########################################################
  25. # Divide input args into array indexes
  26. i=0
  27. for p in ${@:2}; do
  28. params[$i]=${p}
  29. let i++
  30. done
  31. ########################################################
  32. # Parse input git override hashes
  33. # This order is mandatory!
  34. # If you change the order or contents of 'githash_overrides'
  35. # array in ../updatewine.sh, make sure to update these
  36. # variables!
  37. #
  38. git_commithash_dxvk=${params[0]}
  39. git_commithash_wine=${params[3]}
  40. git_branch_dxvk=${params[4]}
  41. git_branch_wine=${params[7]}
  42. ########################################################
  43. # Parse input arguments, filter user parameters
  44. # The range is defined in ../updatewine.sh
  45. # All input arguments are:
  46. # <datedir> 4*<githash_override> <args>
  47. # 0 1 2 3 4 5 ...
  48. # Filter all but <args>, i.e. the first 0-4 arguments
  49. i=0
  50. for arg in ${params[@]:8}; do
  51. args[$i]="${arg}"
  52. let i++
  53. done
  54. for check in ${args[@]}; do
  55. case ${check} in
  56. --no-staging)
  57. NO_STAGING=
  58. ;;
  59. --no-install)
  60. NO_INSTALL=
  61. # Do not check for PlayOnLinux wine prefixes
  62. NO_POL=
  63. ;;
  64. --no-wine)
  65. NO_WINE=
  66. ;;
  67. --no-dxvk)
  68. NO_DXVK=
  69. ;;
  70. --no-pol)
  71. NO_POL=
  72. ;;
  73. esac
  74. done
  75. ########################################################
  76. # http://wiki.bash-hackers.org/snipplets/print_horizontal_line#a_line_across_the_entire_width_of_the_terminal
  77. function INFO_SEP() { printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' - ; }
  78. ###########################################################
  79. # If the script is interrupted (Ctrl+C/SIGINT), do the following
  80. function Arch_intCleanup() {
  81. rm -rf ${ARCH_BUILDROOT}/{0-wine-staging-git/{wine-patches,*.tar.xz},0-dxvk-git/{dxvk-git,*.tar.xz}}
  82. exit 0
  83. }
  84. # Allow interruption of the script at any time (Ctrl + C)
  85. trap "Arch_intCleanup" INT
  86. ###########################################################
  87. # Check existence of ccache package
  88. function ccacheCheck() {
  89. if [[ $(pacman -Q | awk '{print $1}' | grep -wE "ccache" | wc -l) -eq 0 ]]; then
  90. echo -e "\e[1mNOTE:\e[0m Please consider using 'ccache' for faster compilation times.\nInstall it by typing 'sudo pacman -S ccache'\n"
  91. fi
  92. }
  93. ###########################################################
  94. # Validate all core build files for Wine and/or DXVK exist
  95. function checkFiles() {
  96. local wine_files=('30-win32-aliases.conf' 'PKGBUILD')
  97. local dxvk_files=('PKGBUILD')
  98. function validatefiles() {
  99. local list=${1}
  100. local name=${2}
  101. local path=${3}
  102. for file in ${list[@]}; do
  103. if [[ ! -f "${path}/${file}" ]]; then
  104. echo -e "\e[1mERROR:\e[0m Could not locate file ${} for ${name}. Aborting\n"
  105. exit 1
  106. fi
  107. done
  108. }
  109. if [[ ! -v NO_WINE ]]; then
  110. validatefiles "${wine_files[*]}" Wine "${ARCH_BUILDROOT}/0-wine-staging-git"
  111. fi
  112. if [[ ! -v NO_DXVK ]]; then
  113. validatefiles "${dxvk_files[*]}" DXVK "${ARCH_BUILDROOT}/0-dxvk-git"
  114. fi
  115. }
  116. ###########################################################
  117. # Disable or enable Wine Staging, depending on user's
  118. # choice
  119. function checkStaging() {
  120. # Enable Wine Staging
  121. if [[ ! -v NO_STAGING ]]; then
  122. sed -i 's/enable_staging=[0-9]/enable_staging=1/' "${ARCH_BUILDROOT}/0-wine-staging-git/PKGBUILD"
  123. wine_name="wine-staging-git"
  124. # Enable Wine, disable Staging
  125. else
  126. sed -i 's/enable_staging=[0-9]/enable_staging=0/' "${ARCH_BUILDROOT}/0-wine-staging-git/PKGBUILD"
  127. wine_name="wine"
  128. fi
  129. }
  130. ###########################################################
  131. # Check package dependencies beforehand, just to avoid
  132. # annoying situations which could occur later while the script
  133. # is already running.
  134. # Just for "packages which are not found" array <=> ERRPKGS
  135. # We need to set it outside of checkDepends function
  136. # because it is a global variable for all checked packages
  137. l=0
  138. function checkDepends() {
  139. # The first and the second argument
  140. local packagedir=${1}
  141. local package=${2}
  142. # We get necessary variables to check from this file
  143. local file="./${packagedir}/PKGBUILD"
  144. # All but the (zero), the first and the second argument
  145. # We check the value of these file variables
  146. local file_vars=${@:3}
  147. for var in ${file_vars[*]}; do
  148. # Get the variable and set it as a new variable in the current shell
  149. # This is applicable only to variable arrays! Do not use if the variable is not an array.
  150. local field=$(awk "/^${var}/,/)/" ${file} | sed -r "s/^${var}=|[)|(|']//g")
  151. local i=0
  152. for parse in ${field[*]}; do
  153. if [[ ! $parse =~ ^# ]]; then
  154. local PKGS[$i]=$(printf '%s' $parse | sed 's/[=|>|<].*$//')
  155. let i++
  156. fi
  157. done
  158. # Sort list and delete duplicate index values
  159. local PKGS=($(sort -u <<< "${PKGS[*]}"))
  160. for pkg in ${PKGS[*]}; do
  161. if [[ $(printf $(pacman -Q ${pkg} &>/dev/null)$?) -ne 0 ]]; then
  162. ERRPKGS[$l]=${pkg}
  163. echo -e "\e[91mERROR:\e[0m Dependency '${pkg}' not found, required by '${package}' (${file} => ${var})"
  164. let l++
  165. fi
  166. done
  167. done
  168. echo -e "\e[92m==>\e[0m\e[1m Dependency check for ${package} done.\e[0m\n"
  169. }
  170. function check_alldeps() {
  171. if [[ -v ERRPKGS ]]; then
  172. echo -e "\e[1mERROR:\e[0m The following dependencies are missing:\n\e[91m\
  173. $(for o in ${ERRPKGS[@]}; do printf '%s\n' ${o}; done)\
  174. \e[0m\n"
  175. exit 1
  176. fi
  177. }
  178. ###########################################################
  179. # Prepare building environment for the current runtime
  180. function prepare_env() {
  181. # Copy Wine & DXVK patch files
  182. cp -rf ${ARCH_BUILDROOT}/../wine_custom_patches ${ARCH_BUILDROOT}/0-wine-staging-git/wine-patches
  183. cp -rf ${ARCH_BUILDROOT}/../dxvk_custom_patches ${ARCH_BUILDROOT}/0-dxvk-git/dxvk-patches
  184. # Create identifiable directory for this build
  185. mkdir -p ${ARCH_BUILDROOT}/compiled_pkg/"${datedir}"
  186. }
  187. ########################################################
  188. # Parse Wine hash override if Staging is set to be installed
  189. function check_gitOverride_wine() {
  190. # If staging is to be installed and Wine git is frozen to a specific commit
  191. # We need to determine exact commit to use for Wine Staging
  192. # to avoid any mismatches
  193. #
  194. # Basically, when user has defined 'git_commithash_wine' variable (commit), we
  195. # iterate through Wine commits and try to determine previously set
  196. # Wine Staging commit. We use that Wine Staging commit instead of
  197. # the one user has defined in 'git_commithash_wine' variable
  198. #
  199. if [[ ! -v NO_STAGING ]] && [[ "${git_commithash_wine}" != HEAD ]]; then
  200. function form_commit_array() {
  201. cd "${commit_dir}"
  202. if [[ $? -ne 0 ]]; then
  203. echo -e "\e[1mERROR:\e[0m Couldn't access Wine folder ${commit_dir} to check commits. Aborting\n"
  204. exit 1
  205. fi
  206. local array_name=${1}
  207. local commits_raw=$(eval ${2})
  208. local i=0
  209. for commit in ${commits_raw[*]}; do
  210. eval ${array_name}[$i]="${commit}"
  211. let i++
  212. done
  213. if [[ $? -ne 0 ]]; then
  214. echo -e "\e[1mERROR:\e[0m Couldn't parse Wine commits in ${commit_dir}. Aborting\n"
  215. exit 1
  216. fi
  217. cd "${ARCH_BUILDROOT}/0-wine-staging-git/"
  218. }
  219. function staging_change_freeze_commit() {
  220. local wine_commits_raw="git log --pretty=oneline | awk '{print \$1}' | tr '\n' ' '"
  221. # TODO this check may break quite easily
  222. # It depends on the exact comment syntax Wine Staging developers are using (Rebase against ...)
  223. # Length and order of these two "array" variables MUST MATCH!
  224. local staging_refcommits_raw="git log --pretty=oneline | awk '{ if ((length(\$NF)==40 || length(\$NF)==41) && \$(NF-1)==\"against\") print \$1; }'"
  225. local staging_rebasecommits_raw="git log --pretty=oneline | awk '{ if ((length(\$NF)==40 || length(\$NF)==41) && \$(NF-1)==\"against\") print substr(\$NF,1,40); }' | tr '\n' ' '"
  226. # Syntax: <function> <array_name> <raw_commit_list>
  227. commit_dir="${ARCH_BUILDROOT}/0-wine-staging-git/wine-git"
  228. form_commit_array wine_commits "${wine_commits_raw}"
  229. commit_dir="${ARCH_BUILDROOT}/0-wine-staging-git/wine-staging-git"
  230. form_commit_array staging_refcommits "${staging_refcommits_raw}"
  231. form_commit_array staging_rebasecommits "${staging_rebasecommits_raw}"
  232. # User has selected vanilla Wine commit to freeze to
  233. # We must get the previous Staging commit from rebase_commits array, and
  234. # change git_commithash_wine value to that
  235. # Get all vanilla Wine commits
  236. # Filter all newer than defined in 'git_commithash_wine'
  237. #
  238. echo -e "Determining valid Wine Staging git commit. This takes a while.\n"
  239. local i=0
  240. for dropcommit in ${wine_commits[@]}; do
  241. if [[ "${dropcommit}" == "${git_commithash_wine}" ]]; then
  242. break
  243. else
  244. local wine_dropcommits[$i]="${dropcommit}"
  245. let i++
  246. fi
  247. done
  248. wine_commits=("${wine_commits[@]:${#wine_dropcommits[*]}}")
  249. # For the filtered array list, iterate through 'staging_rebasecommits' array list until
  250. # we get a match
  251. for vanilla_commit in ${wine_commits[@]}; do
  252. local k=0
  253. for rebase_commit in ${staging_rebasecommits[@]}; do
  254. if [[ "${vanilla_commit}" == "${rebase_commit}" ]]; then
  255. # This is the commit we use for vanilla Wine
  256. git_commithash_wine="${vanilla_commit}"
  257. # This is equal commit we use for Wine Staging
  258. git_commithash_winestaging="${staging_refcommits[$k]}"
  259. break 2
  260. fi
  261. let k++
  262. done
  263. done
  264. }
  265. git_branch_wine=master
  266. staging_change_freeze_commit
  267. elif [[ ! -v NO_STAGING ]] && [[ "${git_commithash_wine}" == HEAD ]]; then
  268. git_branch_wine=master
  269. git_commithash_winestaging=HEAD
  270. fi
  271. }
  272. ###########################################################
  273. function set_gitOverride() {
  274. local git_name=${1}
  275. local git_commithash=${2}
  276. local pkgbuild_file=${3}
  277. # Match string ${git_name}#commit=<replacethis>
  278. # where replace <replacethis>, but exclude ' " and ) after that
  279. #
  280. # TODO consider when there is nothing/no string after = symbol
  281. sed -i "s!\(${git_name}#commit=\)\(.*[^'|^\"|^\)]\)!\1${git_commithash}!" "${pkgbuild_file}"
  282. }
  283. ###########################################################
  284. # Remove any existing pkg,src or tar.xz packages left by previous pacman commands
  285. function cleanUp() {
  286. rm -rf ${ARCH_BUILDROOT}/*/{pkg,src,*.tar.xz,*.patch,*.diff}
  287. }
  288. ###########################################################
  289. # Build & install package
  290. function build_pkg() {
  291. local pkgname=${1}
  292. local pkgname_friendly=${2}
  293. local pkgdir=${3}
  294. local cleanlist=${4}
  295. # Create package and install it to the system
  296. # We need to download git sources beforehand in order
  297. # to determine git commit hashes
  298. cd "${ARCH_BUILDROOT}"/${pkgdir}
  299. bash -c "updpkgsums && makepkg -o"
  300. # Check git commit hashes
  301. if [[ $? -eq 0 ]] && \
  302. [[ ${5} == gitcheck ]]; then
  303. if [[ ${pkgname} == wine ]]; then
  304. check_gitOverride_wine
  305. local pkgbuild_file="${ARCH_BUILDROOT}/${pkgdir}/PKGBUILD"
  306. set_gitOverride "wine.git" "${git_commithash_wine}" ${pkgbuild_file}
  307. sed -i "s/\(^_wine_commit=\).*/\1${git_commithash_wine}/" ${pkgbuild_file}
  308. sed -i "s/\(^_git_branch_wine=\).*/\1${git_branch_wine}/" ${pkgbuild_file}
  309. if [[ ! -v NO_STAGING ]]; then
  310. set_gitOverride "wine-staging.git" "${git_commithash_winestaging}" ${pkgbuild_file}
  311. sed -i "s/\(^_staging_commit=\).*/\1${git_commithash_winestaging}/" ${pkgbuild_file}
  312. fi
  313. elif [[ ${pkgname} == dxvk ]]; then
  314. local pkgbuild_file="${ARCH_BUILDROOT}/${pkgdir}/PKGBUILD"
  315. set_gitOverride "dxvk.git" "${git_commithash_dxvk}" ${pkgbuild_file}
  316. sed -i "s/\(^_git_branch_dxvk=\).*/\1${git_branch_dxvk}/" ${pkgbuild_file}
  317. sed -i "s/\(^_dxvk_commit=\).*/\1${git_commithash_dxvk}/" ${pkgbuild_file}
  318. fi
  319. fi
  320. if [[ $? -eq 0 ]]; then bash -c "updpkgsums && makepkg"; else exit 1; fi
  321. # After successful compilation...
  322. if [[ $(ls ./${pkgname}-*tar.xz 2>/dev/null | wc -l) -ne 0 ]]; then
  323. if [[ ! -v NO_INSTALL ]]; then
  324. yes | sudo pacman -U ${pkgname}-*.tar.xz
  325. fi
  326. mv ${pkgname}-*.tar.xz ${ARCH_BUILDROOT}/compiled_pkg/${datedir}/ && \
  327. echo -e "\nCompiled ${pkgname_friendly} is stored at '$(readlink -f ${ARCH_BUILDROOT}/compiled_pkg/${datedir}/)/'\n"
  328. for rml in ${cleanlist[*]}; do
  329. rm -rf "${ARCH_BUILDROOT}/${pkgdir}/${rml}"
  330. done
  331. else
  332. echo -e "\e[1mERROR:\e[0m Error occured during ${pkgname} compilation.\n"
  333. for rml in ${cleanlist[*]}; do
  334. rm -rf "${ARCH_BUILDROOT}/${pkgdir}/${rml}"
  335. done
  336. exit 1
  337. fi
  338. cd "${ARCH_BUILDROOT}"
  339. }
  340. ##########################################################
  341. # Update user's PlayOnLinux Wine prefixes if present
  342. function updatePOL() {
  343. # Check whether we will update user's PoL wine prefixes
  344. if [[ ! -v NO_POL ]]; then
  345. # Check existence of PoL default folder in user's homedir
  346. if [[ ! -d "$HOME/.PlayOnLinux" ]]; then
  347. echo -e "\e[1mWARNING:\e[0m Couldn't find PoL directories in $USER's homedir.\n"
  348. return 0
  349. fi
  350. fi
  351. if [[ ! -v NO_WINE ]]; then
  352. # If a new Wine Staging version was installed and 'System' version of Wine has been used in
  353. # PoL wineprefix configurations, update those existing PoL wineprefixes
  354. for wineprefix in $(find $HOME/.PlayOnLinux/wineprefix -mindepth 1 -maxdepth 1 -type d); do
  355. if [[ -d ${wineprefix}/dosdevices ]]; then
  356. # If VERSION string exists, skip updating that prefix.
  357. if [[ $(printf $(grep -ril "VERSION" ${wineprefix}/playonlinux.cfg &> /dev/null)$?) -ne 0 ]]; then
  358. WINEPREFIX=${wineprefix} wineboot -u
  359. fi
  360. fi
  361. done
  362. fi
  363. if [[ ! -v NO_DXVK ]]; then
  364. for wineprefix in $(find $HOME/.PlayOnLinux/wineprefix -mindepth 1 -maxdepth 1 -type d); do
  365. if [[ -d ${wineprefix}/dosdevices ]]; then
  366. WINEPREFIX=${wineprefix} setup_dxvk
  367. fi
  368. done
  369. fi
  370. }
  371. ##########################################################
  372. # Clean these temporary folders & files
  373. # TODO Shall we remove git folders or keep them?
  374. dxvk_wine_cleanlist=('*.patch' '*.diff' 'pkg' 'src' '*-patches' '*.tar.xz') # dxvk-git wine-*git
  375. ##########################################################
  376. # Validate all buildtime files
  377. checkFiles
  378. # Check whether we build Wine or Wine Staging
  379. checkStaging
  380. # Check whether we have ccache installed
  381. ccacheCheck
  382. # Clean all previous trash we may have
  383. cleanUp
  384. # Prepare building environment: copy patches and create timestamped folder for compiled packages
  385. prepare_env
  386. #########################
  387. # Check Wine & DXVK dependencies, depending on whether these packages
  388. # are to be built
  389. echo -e "\e[1mINFO:\e[0m Checking dependencies for packages.\n"
  390. if [[ ! -v NO_WINE ]]; then
  391. checkDepends "0-wine-staging-git" "${wine_name}" _depends makedepends
  392. fi
  393. if [[ ! -v NO_DXVK ]]; then
  394. checkDepends "0-dxvk-git" "dxvk-git" depends makedepends
  395. fi
  396. check_alldeps
  397. #########################
  398. # Compile Wine & DXVK, depending on whether these packages
  399. # are to be built
  400. # Although the folder name is '0-wine-staging-git', we can still build vanilla Wine
  401. if [[ ! -v NO_WINE ]]; then
  402. build_pkg wine "${wine_name}" "0-wine-staging-git" "${dxvk_wine_cleanlist[*]}" gitcheck
  403. fi
  404. if [[ ! -v NO_DXVK ]]; then
  405. build_pkg dxvk DXVK "0-dxvk-git" "${dxvk_wine_cleanlist[*]}" gitcheck
  406. fi
  407. #########################
  408. # Update user's PlayonLinux wine prefixes if needed
  409. if [[ ! -v NO_POL ]]; then
  410. echo -e "\e[1mINFO:\e[0m Updating your PlayOnLinux Wine prefixes.\n"
  411. updatePOL
  412. fi