|
| 1 | +function Assert-QtavOhosWindowsHost { |
| 2 | + if ($env:OS -ne 'Windows_NT') { |
| 3 | + throw 'This OHOS PowerShell entry point can run only on Windows.' |
| 4 | + } |
| 5 | + if (-not [Environment]::Is64BitOperatingSystem) { |
| 6 | + throw 'The OHOS build requires 64-bit Windows.' |
| 7 | + } |
| 8 | + if (-not [Environment]::Is64BitProcess) { |
| 9 | + throw 'Run the OHOS build from a 64-bit PowerShell process.' |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +function Resolve-QtavOhosSdkRoot { |
| 14 | + param( |
| 15 | + [string]$SdkRoot |
| 16 | + ) |
| 17 | + |
| 18 | + $Candidates = @() |
| 19 | + if ($SdkRoot) { |
| 20 | + $Candidates += $SdkRoot |
| 21 | + } |
| 22 | + if ($env:OHOS_SDK_ROOT) { |
| 23 | + $Candidates += $env:OHOS_SDK_ROOT |
| 24 | + } |
| 25 | + if ($env:OHOS_NDK) { |
| 26 | + $Candidates += Split-Path -Parent $env:OHOS_NDK |
| 27 | + } |
| 28 | + if ($env:DEVECO_SDK_HOME) { |
| 29 | + $Candidates += Join-Path $env:DEVECO_SDK_HOME 'default/openharmony' |
| 30 | + } |
| 31 | + if (${env:ProgramFiles}) { |
| 32 | + $Candidates += Join-Path ${env:ProgramFiles} ` |
| 33 | + 'Huawei/DevEco Studio/sdk/default/openharmony' |
| 34 | + } |
| 35 | + |
| 36 | + foreach ($Candidate in $Candidates | Select-Object -Unique) { |
| 37 | + if (-not $Candidate) { |
| 38 | + continue |
| 39 | + } |
| 40 | + $Toolchain = Join-Path $Candidate 'native/build/cmake/ohos.toolchain.cmake' |
| 41 | + if (Test-Path -LiteralPath $Toolchain -PathType Leaf) { |
| 42 | + return (Resolve-Path -LiteralPath $Candidate).Path |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + throw @' |
| 47 | +The OpenHarmony native SDK was not found. Pass -SdkRoot with the directory |
| 48 | +that contains native/, or set OHOS_SDK_ROOT or DEVECO_SDK_HOME. |
| 49 | +'@ |
| 50 | +} |
| 51 | + |
| 52 | +function Use-QtavOhosSdkWithoutSpaces { |
| 53 | + param( |
| 54 | + [Parameter(Mandatory)] |
| 55 | + [string]$SdkRoot, |
| 56 | + |
| 57 | + [Parameter(Mandatory)] |
| 58 | + [string]$AliasRoot |
| 59 | + ) |
| 60 | + |
| 61 | + if ($SdkRoot -notmatch '\s') { |
| 62 | + return $SdkRoot |
| 63 | + } |
| 64 | + |
| 65 | + $AliasRoot = [IO.Path]::GetFullPath($AliasRoot) |
| 66 | + $AliasParent = Split-Path -Parent $AliasRoot |
| 67 | + New-Item -ItemType Directory -Path $AliasParent -Force | Out-Null |
| 68 | + |
| 69 | + if (Test-Path -LiteralPath $AliasRoot) { |
| 70 | + $Alias = Get-Item -LiteralPath $AliasRoot -Force |
| 71 | + if (-not ($Alias.Attributes -band [IO.FileAttributes]::ReparsePoint)) { |
| 72 | + throw "OHOS SDK alias exists but is not a junction: $AliasRoot" |
| 73 | + } |
| 74 | + $AliasTarget = @($Alias.Target) | Select-Object -First 1 |
| 75 | + if (-not $AliasTarget) { |
| 76 | + throw "Could not resolve the OHOS SDK junction target: $AliasRoot" |
| 77 | + } |
| 78 | + $ResolvedAliasTarget = [IO.Path]::GetFullPath($AliasTarget).TrimEnd('\') |
| 79 | + $ResolvedSdkRoot = [IO.Path]::GetFullPath($SdkRoot).TrimEnd('\') |
| 80 | + if (-not $ResolvedAliasTarget.Equals( |
| 81 | + $ResolvedSdkRoot, |
| 82 | + [StringComparison]::OrdinalIgnoreCase)) { |
| 83 | + throw @" |
| 84 | +OHOS SDK alias already points to a different SDK: |
| 85 | + alias: $AliasRoot |
| 86 | + target: $ResolvedAliasTarget |
| 87 | +Remove the alias or choose a different -WorkRoot before changing SDKs. |
| 88 | +"@ |
| 89 | + } |
| 90 | + } else { |
| 91 | + New-Item -ItemType Junction -Path $AliasRoot -Target $SdkRoot | Out-Null |
| 92 | + } |
| 93 | + |
| 94 | + $AliasedToolchain = Join-Path $AliasRoot ` |
| 95 | + 'native/build/cmake/ohos.toolchain.cmake' |
| 96 | + if (-not (Test-Path -LiteralPath $AliasedToolchain -PathType Leaf)) { |
| 97 | + throw "The aliased OHOS toolchain is not readable: $AliasedToolchain" |
| 98 | + } |
| 99 | + return $AliasRoot |
| 100 | +} |
| 101 | + |
| 102 | +function Enable-QtavOhosWindowsBuildTools { |
| 103 | + param( |
| 104 | + [Parameter(Mandatory)] |
| 105 | + [string]$SdkRoot |
| 106 | + ) |
| 107 | + |
| 108 | + $BuildTools = Join-Path $SdkRoot 'native/build-tools/cmake/bin' |
| 109 | + $CMake = Join-Path $BuildTools 'cmake.exe' |
| 110 | + $Ninja = Join-Path $BuildTools 'ninja.exe' |
| 111 | + if (-not (Test-Path -LiteralPath $CMake -PathType Leaf)) { |
| 112 | + $CMakeCommand = Get-Command cmake.exe -CommandType Application ` |
| 113 | + -ErrorAction SilentlyContinue | Select-Object -First 1 |
| 114 | + if (-not $CMakeCommand) { |
| 115 | + throw 'cmake.exe was not found in the OHOS SDK or on PATH.' |
| 116 | + } |
| 117 | + $CMake = $CMakeCommand.Source |
| 118 | + } |
| 119 | + if (-not (Test-Path -LiteralPath $Ninja -PathType Leaf)) { |
| 120 | + $NinjaCommand = Get-Command ninja.exe -CommandType Application ` |
| 121 | + -ErrorAction SilentlyContinue | Select-Object -First 1 |
| 122 | + if (-not $NinjaCommand) { |
| 123 | + throw 'ninja.exe was not found in the OHOS SDK or on PATH.' |
| 124 | + } |
| 125 | + $Ninja = $NinjaCommand.Source |
| 126 | + } |
| 127 | + |
| 128 | + $ToolDirectories = @( |
| 129 | + (Split-Path -Parent $CMake) |
| 130 | + (Split-Path -Parent $Ninja) |
| 131 | + (Join-Path $SdkRoot 'native/llvm/bin') |
| 132 | + ) | Select-Object -Unique |
| 133 | + $PathEntries = $env:PATH -split [IO.Path]::PathSeparator |
| 134 | + foreach ($ToolDirectory in $ToolDirectories) { |
| 135 | + if ($PathEntries -notcontains $ToolDirectory) { |
| 136 | + $env:PATH = "$ToolDirectory$([IO.Path]::PathSeparator)$env:PATH" |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return [pscustomobject]@{ |
| 141 | + CMake = $CMake |
| 142 | + Ninja = $Ninja |
| 143 | + } |
| 144 | +} |
0 commit comments