返回 CodeWhale
ohos-env.ps1
根目录 / scripts / 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", "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:LIBCLANG_PATH = [System.IO.Path]::GetDirectoryName($libclang)
56 Set-Item -Path "Env:BINDGEN_EXTRA_CLANG_ARGS_$target" -Value $commonFlags
57
58 $separator = [char]0x1f
59 $env:CARGO_ENCODED_RUSTFLAGS = @(
60 "-Clink-arg=-target",
61 "-Clink-arg=aarch64-linux-ohos",
62 "-Clink-arg=--sysroot=$sysroot",
63 "-Clink-arg=-D__MUSL__"
64 ) -join $separator
65
66 Write-Host "Configured OpenHarmony Cargo environment for $targetUpper from $sdk"
67
67 lines Plain Text