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.

588 lines
18 KiB

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