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.

557 lines
16 KiB

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