這篇是延續《Windows 的 Qt SDK Dockerfile [202002]》,來記錄一下到底要怎樣建置出一個可以拿來建置 Visual Studio + Qt VS Tools 專案的 Docker 容器。
Visual Studio 的 Docker 處理方法,基本上是延續之前《Visual C++ 2017 的 Docker 建置環境》、只做版本的更新。
以要安裝 Visual Studio 2019 Build Tools 的話,主要就是要在: Dockerfile 中加入:
# Download the Build Tools 2019 bootstrapper. ADD https://aka.ms/vs/16/release/vs_buildtools.exe C:\TEMP\vs_buildtools.exe ADD https://aka.ms/vs/16/release/channel C:\TEMP\VisualStudio.chman # Install Build Tools excluding workloads and components with known issues. RUN C:\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache `
--channelUri C:\TEMP\VisualStudio.chman `
--installChannelUri C:\TEMP\VisualStudio.chman `
--installPath C:\BuildTools `
--add Microsoft.VisualStudio.Workload.VCTools `
--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 `
--add Microsoft.VisualStudio.Component.VC.ATLMFC `
--add Microsoft.VisualStudio.Component.Windows10SDK.18362
不過,光是這樣其實還沒辦法直接透過 msbuild 去建置使用 Qt VS Tools(連結)所建立的專案。
因為實際上,Qt VS Tools 所建立的專案在建置的時候,還會需要額外的「build rules」的檔案;這些檔案是用來告訴 Visual Studio 要怎麼去處理 Qt 的特殊檔案用的(moc、qrc 等等)。
在有安裝 Qt VS Tools 的電腦上,這些檔案會放在
C:\Users\Heresy\AppData\Local\QtMsBuild
這個目錄下,同時會在系統裡面加入一個名為「QtMsBuild」的環境變數,告訴 Visual Studio 要到哪邊找到這些檔案。
所以如果要讓建立出來的 Docker 容器也可以正確地建置 Qt VS Tools 的專案的話,就需要把這些檔案、以及環境變數、也複製到 Docker 容器中。
這邊最簡單的方法,就是把「QtMsBuild」這整個資料夾,都複製出來、然後放到 Docker 容器裡面,之後再手動建立對應的環境變數。
例如 Heresy 這邊的方法,就是:
# Qt build rules and env setting ADD .\QtMsBuild c:\QtMsBuild ENV QTDIR C:\Qt\5.13.1\msvc2017_64 ENV QtMsBuild c:\QtMsBuild
如此一來,應該就可以讓 VisualStudio + Qt VS Tools 可以正常運作了。
整個 Dockerfile 的內容大致上會是:
# escape=` # Use the latest Windows Server Core image with .NET Framework 4.8. FROM mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-1909 # Restore the default Windows shell for correct batch processing. SHELL ["cmd", "/S", "/C"] # Download the Build Tools 2019 bootstrapper. ADD https://aka.ms/vs/16/release/vs_buildtools.exe C:\TEMP\vs_buildtools.exe ADD https://aka.ms/vs/16/release/channel C:\TEMP\VisualStudio.chman # Install Build Tools excluding workloads and components with known issues. RUN C:\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache `
--channelUri C:\TEMP\VisualStudio.chman `
--installChannelUri C:\TEMP\VisualStudio.chman `
--installPath C:\BuildTools `
--add Microsoft.VisualStudio.Workload.VCTools `
--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 `
--add Microsoft.VisualStudio.Component.VC.ATLMFC `
--add Microsoft.VisualStudio.Component.Windows10SDK.18362 # Install Qt 5 ADD http://download.qt.io/official_releases/online_installers/qt-unified-windows-x86-online.exe C:\TEMP\qt.exe ADD qt-install.qs C:\TEMP\qt-install.qs ADD qtaccount.ini C:\Users\ContainerAdministrator\AppData\Roaming\Qt\qtaccount.ini RUN C:\TEMP\qt.exe -v --script C:\TEMP\qt-install.qs # Qt build rules and env setting ADD .\QtMsBuild c:\QtMsBuild ENV QTDIR C:\Qt\5.13.1\msvc2017_64 ENV QtMsBuild c:\QtMsBuild # clean download files RUN del C:\TEMP\* /q # Start developer command prompt with any other commands specified. ENTRYPOINT c:\BuildTools\VC\Auxiliary\Build\vcvarsall.bat x64 &&
建置的指令則可以寫成:
docker build -m 16g -t vsqt .
完整的檔案可以參考:https://github.com/KHeresy/QtSDK-WindowsDocker/tree/withVS2019
[…] 在使用舊版的 Qt VS Tools 專案的時候,基本上只需要設定「QTDIR」和「QtMsBuild」這兩個環境變數,個別去指到 Qt SDK 的安裝路徑、以及 QT VS Tools 的 build rule 所在路徑(預設是在「%LOCALAPPDATA%QtMsBuild」)就可以了。(參考) […]
讚讚