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.

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