返回 CodeWhale
ohos-env.ps1
根目录 / scripts / ohos / ohos-env.ps1
1 $ErrorActionPreference = "Stop"
2
3 function Stop-OhosEnv {
4 param([string]$Message)
5
6 [Console]::Error.WriteLine("error: $Message")
7 throw "OpenHarmony Cargo environment setup failed."
8 }
9
10 if ([string]::IsNullOrWhiteSpace($env:OHOS_NATIVE_SDK)) {
11 Stop-OhosEnv "set OHOS_NATIVE_SDK to the OpenHarmony native SDK directory."
12 }
13
14 if (-not (Test-Path -LiteralPath $env:OHOS_NATIVE_SDK -PathType Container -ErrorAction SilentlyContinue)) {
15 Stop-OhosEnv "OHOS_NATIVE_SDK does not exist: $env:OHOS_NATIVE_SDK"
16 }
17
18 $sdk = (Resolve-Path -LiteralPath $env:OHOS_NATIVE_SDK -ErrorAction Stop).Path
19 $env:OHOS_NATIVE_SDK = $sdk
20 $clang = [System.IO.Path]::Combine($sdk, "llvm", "bin", "clang.exe")
21 $clangxx = [System.IO.Path]::Combine($sdk, "llvm", "bin", "clang++.exe")
22 $ar = [System.IO.Path]::Combine($sdk, "llvm", "bin", "llvm-ar.exe")
23 $libclang = [System.IO.Path]::Combine($sdk, "llvm", "bin", "libclang.dll")
24 $sysroot = [System.IO.Path]::Combine($sdk, "sysroot")
25 $cmakeToolchain = [System.IO.Path]::Combine($sdk, "build", "cmake", "ohos.toolchain.cmake")
26 $linker = [System.IO.Path]::Combine($PSScriptRoot, "ohos-clang.cmd")
27
28 $requiredFiles = @($clang, $clangxx, $ar, $libclang, $cmakeToolchain)
29 foreach ($path in $requiredFiles) {
30 if (-not (Test-Path -LiteralPath $path -PathType Leaf -ErrorAction SilentlyContinue)) {
31 Stop-OhosEnv "required OpenHarmony SDK file is missing: $path"
32 }
33 }
34
35 if (-not (Test-Path -LiteralPath $linker -PathType Leaf -ErrorAction SilentlyContinue)) {
36 Stop-OhosEnv "required repository linker wrapper is missing: $linker"
37 }
38
39 if (-not (Test-Path -LiteralPath $sysroot -PathType Container -ErrorAction SilentlyContinue)) {
40 Stop-OhosEnv "required OpenHarmony SDK sysroot is missing: $sysroot"
41 }
42
43 $target = "aarch64_unknown_linux_ohos"
44 $targetUpper = "AARCH64_UNKNOWN_LINUX_OHOS"
45 $commonFlags = "-target aarch64-linux-ohos --sysroot=`"$sysroot`" -D__MUSL__"
46
47 $env:CARGO_TARGET_AARCH64_UNKNOWN_LINUX_OHOS_LINKER = $linker
48 $env:AR_aarch64_unknown_linux_ohos = $ar
49 $env:CC_aarch64_unknown_linux_ohos = $clang
50 $env:CXX_aarch64_unknown_linux_ohos = $clangxx
51 $env:CC_SHELL_ESCAPED_FLAGS = "1"
52 Set-Item -Path "Env:CFLAGS_$target" -Value $commonFlags
53 Set-Item -Path "Env:CXXFLAGS_$target" -Value $commonFlags
54 Set-Item -Path "Env:CMAKE_TOOLCHAIN_FILE_$target" -Value $cmakeToolchain
55 $env:CMAKE_GENERATOR = "Ninja"
56 if (-not (Get-Command ninja -ErrorAction SilentlyContinue)) {
57 $sdkNinja = [System.IO.Path]::Combine($sdk, "build-tools", "cmake", "bin")
58 if (-not (Test-Path -LiteralPath ([System.IO.Path]::Combine($sdkNinja, "ninja.exe")) -PathType Leaf)) {
59 Stop-OhosEnv "ninja was not found on PATH and the OpenHarmony SDK does not bundle build-tools/cmake/bin/ninja.exe; install ninja so CMake-based crates such as aws-lc-sys can build for $target."
60 }
61 $env:PATH = "$sdkNinja;$env:PATH"
62 }
63 $env:LIBCLANG_PATH = [System.IO.Path]::GetDirectoryName($libclang)
64 Set-Item -Path "Env:BINDGEN_EXTRA_CLANG_ARGS_$target" -Value $commonFlags
65
66 $separator = [char]0x1f
67 $env:CARGO_ENCODED_RUSTFLAGS = @(
68 "-Clink-arg=-target",
69 "-Clink-arg=aarch64-linux-ohos",
70 "-Clink-arg=--sysroot=$sysroot",
71 "-Clink-arg=-D__MUSL__"
72 ) -join $separator
73
74 Write-Host "Configured OpenHarmony Cargo environment for $targetUpper from $sdk"
75
75 lines Plain Text