Skip to content

Commit

Permalink
[build] 改进Windows环境下的构建流程
Browse files Browse the repository at this point in the history
- 将`cmd`替换为`powershell`以提升脚本的现代性和兼容性
- 引入`vswhere`自动定位Visual Studio,移除了硬编码的`vcvarsall.bat`路径
- 新增`setVsDev.ps1`脚本来自动化开发环境的设置,包括Visual Studio的定位和开发命令的执行
- 通过引入参数化的`setVsDev.ps1`脚本,增强了构建流程的灵活性,允许用户根据需要选择Visual Studio的版本和架构
- 保留了与旧流程的兼容性,确保现有工作流可以无缝迁移到新的构建系统
- 优化了`.github/workflows/cmake.yml`和`.github/workflows/qmake.yml`文件,以适应新的构建环境设置
- 在`scripts/windows/`目录下添加了新的PowerShell脚本`setVsDev.ps1`,用于更灵活地设置和进入Visual Studio开发环境
  • Loading branch information
RealChuan committed May 13, 2024
1 parent 1743a08 commit a2e91fa
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 23 deletions.
35 changes: 17 additions & 18 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,37 @@ jobs:
with:
os_name: ${{ matrix.os }}

- name: Configure msvc for amd64
if: startsWith(matrix.os, 'windows')
uses: ilammy/msvc-dev-cmd@v1
with:
arch: amd64
- name: Configure windows
- name: Configure and build windows
if: startsWith(matrix.os, 'windows')
shell: pwsh
run: |
.\scripts\windows\setVsDev.ps1
cmake `
-S . `
-B ./build `
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} `
-G "${{ matrix.generators }}"
cmake --build ./build --config ${{ matrix.build_type }}
- name: Configure and build ubuntu
if: startsWith(matrix.os, 'ubuntu')
shell: bash
run: |
cmake \
-S . \
-B ./build \
-DCMAKE_C_COMPILER=cl \
-DCMAKE_CXX_COMPILER=cl \
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
-G "${{ matrix.generators }}" \
-DCMAKE_INSTALL_PREFIX:PATH=instdir
- name: Configure macos or ubuntu
if: startsWith(matrix.os, 'macos') || startsWith(matrix.os, 'ubuntu')
-G "${{ matrix.generators }}"
cmake --build ./build --config ${{ matrix.build_type }}
- name: Configure and build macos
if: startsWith(matrix.os, 'macos')
shell: bash
run: |
cmake \
-S . \
-B ./build \
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
-DCMAKE_OSX_ARCHITECTURES="${{ matrix.arch }}" \
-G "${{ matrix.generators }}" \
-DCMAKE_INSTALL_PREFIX:PATH=instdir
- name: Build
shell: bash
run: |
-G "${{ matrix.generators }}"
cmake --build ./build --config ${{ matrix.build_type }}
- name: Deploy windows
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/delete_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ on:
delete_run_by_conclusion_pattern:
description: 'Remove runs based on conclusion: action_required, cancelled, failure, skipped, success'
required: true
default: "Unsuccessful"
default: "Unsuccessful: action_required,cancelled,failure,skipped"
type: choice
options:
- "ALL"
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/qmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ jobs:
- name: msvc-build
if: startsWith(matrix.os, 'windows')
shell: cmd
shell: pwsh
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
qmake ./../.
.\..\jom\jom.exe
..\scripts\windows\setVsDev.ps1 -VersionRange "[16.0,17.0)" -Arch "x64"
& qmake ./../.
& .\..\jom\jom.exe
working-directory: build
- name: ubuntu-build
if: startsWith(matrix.os, 'ubuntu')
Expand Down
62 changes: 62 additions & 0 deletions scripts/windows/setVsDev.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
param(
[Parameter(Mandatory = $false)]
[string]$VersionRange,
[Parameter(Mandatory = $false)]
[string]$Arch
)


$vswhereArgs = @()

if ($VersionRange) {
$vswhereArgs += "-version"
$vswhereArgs += $VersionRange
}
else {
$vswhereArgs += "-latest"
}


$vswhereArgs += "-property"
$vswhereArgs += "installationPath"

if ([string]::IsNullOrEmpty($Arch)) {
$Arch = "x64"
}

Write-Host "Architecture: $Arch"
Write-Host "VSWhere Args: $vswhereArgs"

# 使用 vswhere 获取 Visual Studio 的安装路径
$vswherePath = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
$vsInstallPath = & $vswherePath $vswhereArgs | ForEach-Object { $_ }
# Output the results
Write-Host "Visual Studio installation paths:"
$vsInstallPath

if ($null -ne $vsInstallPath) {
$vsDevShell = Join-Path $vsInstallPath "Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
if (-not (Test-Path $vsDevShell)) {
Write-Host "Failed to find Visual Studio DevShell DLL: $vsDevShell"
exit 1
}
Import-Module $vsDevShell
Enter-VsDevShell -VsInstallPath $vsInstallPath -DevCmdArguments "-arch=$Arch -host_arch=$Arch" -SkipAutomaticLocation

if ($LASTEXITCODE -eq 0) {
Write-Host "Development environment set up successfully."
}
else {
Write-Host "Failed to set up the development environment."
}
}
else {
Write-Host "Using Custom Visual Studio installation path."
$vsInstallPath = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise"
if (-not (Test-Path $vsInstallPath)) {
$vsInstallPath = "C:\Program Files (x86)\Microsoft Visual Studio\2022\Enterprise"
}
$vsDevShell = Join-Path $vsInstallPath "Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
Import-Module $vsDevShell
Enter-VsDevShell -VsInstallPath $vsInstallPath -DevCmdArguments "-arch=x64 -host_arch=x64" -SkipAutomaticLocation
}

0 comments on commit a2e91fa

Please sign in to comment.