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.

559 lines
16 KiB

6 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. echo -e "Please install them and try again.\n"
  178. exit 1
  179. fi
  180. }
  181. ###########################################################
  182. # Prepare building environment for the current runtime
  183. function prepare_env() {
  184. # Copy Wine & DXVK patch files
  185. cp -rf ${ARCH_BUILDROOT}/../wine_custom_patches ${ARCH_BUILDROOT}/0-wine-staging-git/wine-patches
  186. cp -rf ${ARCH_BUILDROOT}/../dxvk_custom_patches ${ARCH_BUILDROOT}/0-dxvk-git/dxvk-patches
  187. # Create identifiable directory for this build
  188. mkdir -p ${ARCH_BUILDROOT}/compiled_pkg/"${datedir}"
  189. }
  190. ########################################################
  191. # Parse Wine hash override if Staging is set to be installed
  192. function check_gitOverride_wine() {
  193. # If staging is to be installed and Wine git is frozen to a specific commit
  194. # We need to determine exact commit to use for Wine Staging
  195. # to avoid any mismatches
  196. #
  197. # Basically, when user has defined 'git_commithash_wine' variable (commit), we
  198. # iterate through Wine commits and try to determine previously set
  199. # Wine Staging commit. We use that Wine Staging commit instead of
  200. # the one user has defined in 'git_commithash_wine' variable
  201. #
  202. if [[ ! -v NO_STAGING ]] && [[ "${git_commithash_wine}" != HEAD ]]; then
  203. function form_commit_array() {
  204. cd "${commit_dir}"
  205. if [[ $? -ne 0 ]]; then
  206. echo -e "\e[1mERROR:\e[0m Couldn't access Wine folder ${commit_dir} to check commits. Aborting\n"
  207. exit 1
  208. fi
  209. local array_name=${1}
  210. local commits_raw=$(eval ${2})
  211. local i=0
  212. for commit in ${commits_raw[*]}; do
  213. eval ${array_name}[$i]="${commit}"
  214. let i++
  215. done
  216. if [[ $? -ne 0 ]]; then
  217. echo -e "\e[1mERROR:\e[0m Couldn't parse Wine commits in ${commit_dir}. Aborting\n"
  218. exit 1
  219. fi
  220. cd "${ARCH_BUILDROOT}/0-wine-staging-git/"
  221. }
  222. function staging_change_freeze_commit() {
  223. local wine_commits_raw="git log --pretty=oneline | awk '{print \$1}' | tr '\n' ' '"
  224. # TODO this check may break quite easily
  225. # It depends on the exact comment syntax Wine Staging developers are using (Rebase against ...)
  226. # Length and order of these two "array" variables MUST MATCH!
  227. local staging_refcommits_raw="git log --pretty=oneline | awk '{ if ((length(\$NF)==40 || length(\$NF)==41) && \$(NF-1)==\"against\") print \$1; }'"
  228. 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' ' '"
  229. # Syntax: <function> <array_name> <raw_commit_list>
  230. commit_dir="${ARCH_BUILDROOT}/0-wine-staging-git/wine-git"
  231. form_commit_array wine_commits "${wine_commits_raw}"
  232. commit_dir="${ARCH_BUILDROOT}/0-wine-staging-git/wine-staging-git"
  233. form_commit_array staging_refcommits "${staging_refcommits_raw}"
  234. form_commit_array staging_rebasecommits "${staging_rebasecommits_raw}"
  235. # User has selected vanilla Wine commit to freeze to
  236. # We must get the previous Staging commit from rebase_commits array, and
  237. # change git_commithash_wine value to that
  238. # Get all vanilla Wine commits
  239. # Filter all newer than defined in 'git_commithash_wine'
  240. #
  241. echo -e "Determining valid Wine Staging git commit. This takes a while.\n"
  242. local i=0
  243. for dropcommit in ${wine_commits[@]}; do
  244. if [[ "${dropcommit}" == "${git_commithash_wine}" ]]; then
  245. break
  246. else
  247. local wine_dropcommits[$i]="${dropcommit}"
  248. let i++
  249. fi
  250. done
  251. wine_commits=("${wine_commits[@]:${#wine_dropcommits[*]}}")
  252. # For the filtered array list, iterate through 'staging_rebasecommits' array list until
  253. # we get a match
  254. for vanilla_commit in ${wine_commits[@]}; do
  255. local k=0
  256. for rebase_commit in ${staging_rebasecommits[@]}; do
  257. if [[ "${vanilla_commit}" == "${rebase_commit}" ]]; then
  258. # This is the commit we use for vanilla Wine
  259. git_commithash_wine="${vanilla_commit}"
  260. # This is equal commit we use for Wine Staging
  261. git_commithash_winestaging="${staging_refcommits[$k]}"
  262. break 2
  263. fi
  264. let k++
  265. done
  266. done
  267. }
  268. git_branch_wine=master
  269. staging_change_freeze_commit
  270. elif [[ ! -v NO_STAGING ]] && [[ "${git_commithash_wine}" == HEAD ]]; then
  271. git_branch_wine=master
  272. git_commithash_winestaging=HEAD
  273. fi
  274. }
  275. ###########################################################
  276. function set_gitOverride() {
  277. local git_name=${1}
  278. local git_commithash=${2}
  279. local pkgbuild_file=${3}
  280. # Match string ${git_name}#commit=<replacethis>
  281. # where replace <replacethis>, but exclude ' " and ) after that
  282. #
  283. # TODO consider when there is nothing/no string after = symbol
  284. sed -i "s!\(${git_name}#commit=\)\(.*[^'|^\"|^\)]\)!\1${git_commithash}!" "${pkgbuild_file}"
  285. }
  286. ###########################################################
  287. # Remove any existing pkg,src or tar.xz packages left by previous pacman commands
  288. function cleanUp() {
  289. rm -rf ${ARCH_BUILDROOT}/*/{pkg,src,*.tar.xz,*.patch,*.diff}
  290. }
  291. ###########################################################
  292. # Build & install package
  293. function build_pkg() {
  294. local pkgname=${1}
  295. local pkgname_friendly=${2}
  296. local pkgdir=${3}
  297. local cleanlist=${4}
  298. # Create package and install it to the system
  299. # We need to download git sources beforehand in order
  300. # to determine git commit hashes
  301. cd "${ARCH_BUILDROOT}"/${pkgdir}
  302. bash -c "updpkgsums && makepkg -o"
  303. local pkgbuild_file="${ARCH_BUILDROOT}/${pkgdir}/PKGBUILD"
  304. # Check git commit hashes
  305. if [[ $? -eq 0 ]] && \
  306. [[ ${5} == gitcheck ]]; then
  307. if [[ ${pkgname} == wine ]]; then
  308. check_gitOverride_wine
  309. set_gitOverride "wine.git" "${git_commithash_wine}" ${pkgbuild_file}
  310. sed -i "s/\(^_wine_commit=\).*/\1${git_commithash_wine}/" ${pkgbuild_file}
  311. sed -i "s/\(^_git_branch_wine=\).*/\1${git_branch_wine}/" ${pkgbuild_file}
  312. if [[ ! -v NO_STAGING ]]; then
  313. set_gitOverride "wine-staging.git" "${git_commithash_winestaging}" ${pkgbuild_file}
  314. sed -i "s/\(^_staging_commit=\).*/\1${git_commithash_winestaging}/" ${pkgbuild_file}
  315. fi
  316. elif [[ ${pkgname} == dxvk ]]; then
  317. set_gitOverride "dxvk.git" "${git_commithash_dxvk}" ${pkgbuild_file}
  318. sed -i "s/\(^_git_branch_dxvk=\).*/\1${git_branch_dxvk}/" ${pkgbuild_file}
  319. sed -i "s/\(^_dxvk_commit=\).*/\1${git_commithash_dxvk}/" ${pkgbuild_file}
  320. fi
  321. fi
  322. if [[ $? -eq 0 ]]; then bash -c "updpkgsums && makepkg -Cf"; else exit 1; fi
  323. # After successful compilation...
  324. if [[ $(ls ./${pkgname}-*tar.xz 2>/dev/null | wc -l) -ne 0 ]]; then
  325. if [[ ! -v NO_INSTALL ]]; then
  326. yes | sudo pacman -U ${pkgname}-*.tar.xz
  327. fi
  328. mv ${pkgname}-*.tar.xz ${ARCH_BUILDROOT}/compiled_pkg/${datedir}/ && \
  329. echo -e "\nCompiled ${pkgname_friendly} is stored at '$(readlink -f ${ARCH_BUILDROOT}/compiled_pkg/${datedir}/)/'\n"
  330. for rml in ${cleanlist[*]}; do
  331. rm -rf "${ARCH_BUILDROOT}/${pkgdir}/${rml}"
  332. done
  333. else
  334. echo -e "\e[1mERROR:\e[0m Error occured during ${pkgname} compilation.\n"
  335. for rml in ${cleanlist[*]}; do
  336. rm -rf "${ARCH_BUILDROOT}/${pkgdir}/${rml}"
  337. done
  338. exit 1
  339. fi
  340. cd "${ARCH_BUILDROOT}"
  341. }
  342. ##########################################################
  343. # Update user's PlayOnLinux Wine prefixes if present
  344. function updatePOL() {
  345. # Check whether we will update user's PoL wine prefixes
  346. if [[ ! -v NO_POL ]]; then
  347. # Check existence of PoL default folder in user's homedir
  348. if [[ ! -d "$HOME/.PlayOnLinux" ]]; then
  349. echo -e "\e[1mWARNING:\e[0m Couldn't find PoL directories in $USER's homedir.\n"
  350. return 0
  351. fi
  352. fi
  353. if [[ ! -v NO_WINE ]]; then
  354. # If a new Wine Staging version was installed and 'System' version of Wine has been used in
  355. # PoL wineprefix configurations, update those existing PoL wineprefixes
  356. for wineprefix in $(find $HOME/.PlayOnLinux/wineprefix -mindepth 1 -maxdepth 1 -type d); do
  357. if [[ -d ${wineprefix}/dosdevices ]]; then
  358. # If VERSION string exists, skip updating that prefix.
  359. if [[ $(printf $(grep -ril "VERSION" ${wineprefix}/playonlinux.cfg &> /dev/null)$?) -ne 0 ]]; then
  360. WINEPREFIX=${wineprefix} wineboot -u
  361. fi
  362. fi
  363. done
  364. fi
  365. # TODO remove duplicate functionality
  366. if [[ ! -v NO_DXVK ]]; then
  367. for wineprefix in $(find $HOME/.PlayOnLinux/wineprefix -mindepth 1 -maxdepth 1 -type d); do
  368. if [[ -d ${wineprefix}/dosdevices ]]; then
  369. WINEPREFIX=${wineprefix} setup_dxvk
  370. fi
  371. done
  372. fi
  373. }
  374. ##########################################################
  375. # Clean these temporary folders & files
  376. # TODO Shall we remove git folders or keep them?
  377. dxvk_wine_cleanlist=('*.patch' '*.diff' 'pkg' 'src' '*-patches' '*.tar.xz')
  378. ##########################################################
  379. # Validate all buildtime files
  380. checkFiles
  381. # Check whether we build Wine or Wine Staging
  382. checkStaging
  383. # Check whether we have ccache installed
  384. ccacheCheck
  385. # Clean all previous trash we may have
  386. cleanUp
  387. # Prepare building environment: copy patches and create timestamped folder for compiled packages
  388. prepare_env
  389. #########################
  390. # Check Wine & DXVK dependencies, depending on whether these packages
  391. # are to be built
  392. echo -e "\e[1mINFO:\e[0m Checking dependencies for packages.\n"
  393. if [[ ! -v NO_WINE ]]; then
  394. checkDepends "0-wine-staging-git" "${wine_name}" _depends makedepends
  395. fi
  396. if [[ ! -v NO_DXVK ]]; then
  397. checkDepends "0-dxvk-git" "dxvk-git" depends makedepends
  398. fi
  399. check_alldeps
  400. #########################
  401. # Compile Wine & DXVK, depending on whether these packages
  402. # are to be built
  403. # Although the folder name is '0-wine-staging-git', we can still build vanilla Wine
  404. if [[ ! -v NO_WINE ]]; then
  405. build_pkg wine "${wine_name}" "0-wine-staging-git" "${dxvk_wine_cleanlist[*]}" gitcheck
  406. fi
  407. if [[ ! -v NO_DXVK ]]; then
  408. build_pkg dxvk DXVK "0-dxvk-git" "${dxvk_wine_cleanlist[*]}" gitcheck
  409. fi
  410. #########################
  411. # Update user's PlayonLinux wine prefixes if needed
  412. if [[ ! -v NO_POL ]]; then
  413. echo -e "\e[1mINFO:\e[0m Updating your PlayOnLinux Wine prefixes.\n"
  414. updatePOL
  415. fi