Allow build linux target with toolchain: clang

This commit is contained in:
halx99 2024-01-10 01:57:56 +08:00
parent a7231b53d3
commit 1adb2031dd
1 changed files with 91 additions and 54 deletions

View File

@ -266,26 +266,32 @@ if (!$TARGET_OS) {
} }
# define some useful global vars # define some useful global vars
$Global:is_wasm = $TARGET_OS -eq 'wasm' $Global:is_wasm = $TARGET_OS -eq 'wasm'
$Global:is_winrt = ($target_os -eq 'winrt') $Global:Is_win32 = $TARGET_OS -eq 'win32'
$Global:is_win_family = $is_winrt -or ($target_os -eq 'win32') $Global:is_winrt = $TARGET_OS -eq 'winrt'
$Global:is_apple_family = !!(@{'osx' = $true; 'ios' = $true; 'tvos' = $true }[$TARGET_OS]) $Global:is_mac = $TARGET_OS -eq 'osx'
$Global:is_linux = $TARGET_OS -eq 'linux'
$Global:is_android = $TARGET_OS -eq 'android'
$Global:is_ios = $TARGET_OS -eq 'ios'
$Global:is_tvos = $TARGET_OS -eq 'tvos'
$Global:is_watchos = $TARGET_OS -eq 'watchos'
$Global:is_win_family = $Global:is_winrt -or $Global:is_win32
$Global:is_apple_family = $Global:is_mac -or $Global:is_ios -or $Global:is_tvos
$Global:is_gh_act = "$env:GITHUB_ACTIONS" -eq 'true' $Global:is_gh_act = "$env:GITHUB_ACTIONS" -eq 'true'
if (!$is_wasm) { if (!$is_wasm) {
$TARGET_ARCH = $options.a $TARGET_CPU = $options.a
if (!$TARGET_ARCH) { if (!$TARGET_CPU) {
$TARGET_ARCH = @{'ios' = 'arm64'; 'tvos' = 'arm64'; 'watchos' = 'arm64'; 'android' = 'arm64'; }[$TARGET_OS] $TARGET_CPU = @{'ios' = 'arm64'; 'tvos' = 'arm64'; 'watchos' = 'arm64'; 'android' = 'arm64'; }[$TARGET_OS]
if (!$TARGET_ARCH) { if (!$TARGET_CPU) {
$TARGET_ARCH = $hostArch $TARGET_CPU = $hostArch
} }
$options.a = $TARGET_ARCH $options.a = $TARGET_CPU
} elseif($TARGET_ARCH -eq 'arm') { } elseif($TARGET_CPU -eq 'arm') {
$TARGET_ARCH = $options.a = 'armv7' $TARGET_CPU = $options.a = 'armv7'
} }
} }
else { else {
$TARGET_ARCH = $options.a = '*' $TARGET_CPU = $options.a = '*'
} }
if (!$setupOnly) { if (!$setupOnly) {
@ -300,12 +306,12 @@ $toolchains = @{
'win32' = 'msvc'; 'win32' = 'msvc';
'winrt' = 'msvc'; 'winrt' = 'msvc';
'linux' = 'gcc'; 'linux' = 'gcc';
'android' = 'ndk'; 'android' = 'clang'; # xcode clang
'osx' = 'xcode'; 'osx' = 'clang'; # xcode clang
'ios' = 'xcode'; 'ios' = 'clang'; # xcode clang
'tvos' = 'xcode'; 'tvos' = 'clang'; # xcode clang
'watchos' = 'xcode'; 'watchos' = 'clang'; # xcode clang
'wasm' = 'emcc'; # wasm llvm-emcc 'wasm' = 'clang'; # emcc clang
} }
if (!$TOOLCHAIN) { if (!$TOOLCHAIN) {
$TOOLCHAIN = $toolchains[$TARGET_OS] $TOOLCHAIN = $toolchains[$TARGET_OS]
@ -323,6 +329,9 @@ if (!$TOOLCHAIN_VER) {
$TOOLCHAIN_NAME = $TOOLCHAIN $TOOLCHAIN_NAME = $TOOLCHAIN
} }
$Global:is_clang = $TOOLCHAIN_NAME -eq 'clang'
$Global:is_msvc = $TOOLCHAIN_NAME -eq 'msvc'
$external_prefix = if ($options.prefix) { $options.prefix } else { Join-Path $HOME '.1kiss' } $external_prefix = if ($options.prefix) { $options.prefix } else { Join-Path $HOME '.1kiss' }
if (!$b1k.isdir($external_prefix)) { if (!$b1k.isdir($external_prefix)) {
$b1k.mkdirs($external_prefix) $b1k.mkdirs($external_prefix)
@ -1056,14 +1065,14 @@ function setup_devenv($vs_path = $null) {
if (!$vs_path) { $vs_path = find_vs } if (!$vs_path) { $vs_path = find_vs }
if ($vs_path) { if ($vs_path) {
Import-Module "$vs_path\Common7\Tools\Microsoft.VisualStudio.DevShell.dll" Import-Module "$vs_path\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
Enter-VsDevShell -VsInstanceId a55efc1d -SkipAutomaticLocation -DevCmdArguments "-arch=$target_arch -host_arch=x64 -no_logo" Enter-VsDevShell -VsInstanceId a55efc1d -SkipAutomaticLocation -DevCmdArguments "-arch=$target_cpu -host_arch=x64 -no_logo"
# msvc14x support # msvc14x support
$use_msvcr14x = $null $use_msvcr14x = $null
if ([bool]::TryParse($env:use_msvcr14x, [ref]$use_msvcr14x) -and $use_msvcr14x) { if ([bool]::TryParse($env:use_msvcr14x, [ref]$use_msvcr14x) -and $use_msvcr14x) {
if ("$env:LIB".IndexOf('msvcr14x') -eq -1) { if ("$env:LIB".IndexOf('msvcr14x') -eq -1) {
$msvcr14x_root = $env:msvcr14x_ROOT $msvcr14x_root = $env:msvcr14x_ROOT
$env:Platform = $target_arch $env:Platform = $target_cpu
Invoke-Expression -Command "$msvcr14x_root\msvcr14x_nmake.ps1" Invoke-Expression -Command "$msvcr14x_root\msvcr14x_nmake.ps1"
} }
@ -1090,9 +1099,13 @@ function setup_gclient() {
# git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git $gclient_dir # git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git $gclient_dir
$gclient_dir = Join-Path $external_prefix 'depot_tools' $gclient_dir = Join-Path $external_prefix 'depot_tools'
if(!(Test-Path $gclient_dir -PathType Container)) { if(!(Test-Path $gclient_dir -PathType Container)) {
$b1k.mkdirs($gclient_dir) if ($IsWin) {
Invoke-WebRequest -Uri "https://storage.googleapis.com/chrome-infra/depot_tools.zip" -OutFile "${gclient_dir}.zip" $b1k.mkdirs($gclient_dir)
Expand-Archive -Path "${gclient_dir}.zip" -DestinationPath $gclient_dir Invoke-WebRequest -Uri "https://storage.googleapis.com/chrome-infra/depot_tools.zip" -OutFile "${gclient_dir}.zip"
Expand-Archive -Path "${gclient_dir}.zip" -DestinationPath $gclient_dir
} else {
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git $gclient_dir
}
} }
if ($env:PATH.IndexOf($gclient_dir) -eq -1) { if ($env:PATH.IndexOf($gclient_dir) -eq -1) {
@ -1110,7 +1123,7 @@ function preprocess_win([string[]]$inputOptions) {
$outputOptions += "-DCMAKE_SYSTEM_VERSION=$($options.sdk)" $outputOptions += "-DCMAKE_SYSTEM_VERSION=$($options.sdk)"
} }
if ($TOOLCHAIN_NAME -eq 'msvc') { if ($Global:is_msvc) {
# Generate vs2019 on github ci # Generate vs2019 on github ci
# Determine arch name # Determine arch name
$arch = if ($options.a -eq 'x86') { 'Win32' } else { $options.a } $arch = if ($options.a -eq 'x86') { 'Win32' } else { $options.a }
@ -1153,7 +1166,7 @@ function preprocess_win([string[]]$inputOptions) {
$outputOptions += '-DBUILD_SHARED_LIBS=TRUE' $outputOptions += '-DBUILD_SHARED_LIBS=TRUE'
} }
} }
elseif ($TOOLCHAIN_NAME -eq 'clang') { elseif ($Global:is_clang) {
$outputOptions += '-DCMAKE_C_COMPILER=clang', '-DCMAKE_CXX_COMPILER=clang++' $outputOptions += '-DCMAKE_C_COMPILER=clang', '-DCMAKE_CXX_COMPILER=clang++'
$Script:cmake_generator = 'Ninja Multi-Config' $Script:cmake_generator = 'Ninja Multi-Config'
} }
@ -1166,6 +1179,9 @@ function preprocess_win([string[]]$inputOptions) {
function preprocess_linux([string[]]$inputOptions) { function preprocess_linux([string[]]$inputOptions) {
$outputOptions = $inputOptions $outputOptions = $inputOptions
if ($Global:is_clang) {
$outputOptions += '-DCMAKE_C_COMPILER=clang', '-DCMAKE_CXX_COMPILER=clang++'
}
return $outputOptions return $outputOptions
} }
@ -1236,10 +1252,10 @@ function preprocess_ios([string[]]$inputOptions) {
if (!$cmake_toolchain_file) { if (!$cmake_toolchain_file) {
$cmake_toolchain_file = Join-Path $myRoot 'ios.cmake' $cmake_toolchain_file = Join-Path $myRoot 'ios.cmake'
$outputOptions += "-DCMAKE_TOOLCHAIN_FILE=$cmake_toolchain_file", "-DARCHS=$arch" $outputOptions += "-DCMAKE_TOOLCHAIN_FILE=$cmake_toolchain_file", "-DARCHS=$arch"
if ($TARGET_OS -eq 'tvos') { if ($Global:is_tvos) {
$outputOptions += '-DPLAT=tvOS' $outputOptions += '-DPLAT=tvOS'
} }
elseif ($TARGET_OS -eq 'watchos') { elseif ($Global:is_watchos) {
$outputOptions += '-DPLAT=watchOS' $outputOptions += '-DPLAT=watchOS'
} }
} }
@ -1253,7 +1269,7 @@ function preprocess_wasm([string[]]$inputOptions) {
function validHostAndToolchain() { function validHostAndToolchain() {
$appleTable = @{ $appleTable = @{
'host' = @{'macos' = $True }; 'host' = @{'macos' = $True };
'toolchain' = @{'xcode' = $True; }; 'toolchain' = @{'clang' = $True; };
}; };
$validTable = @{ $validTable = @{
'win32' = @{ 'win32' = @{
@ -1266,11 +1282,11 @@ function validHostAndToolchain() {
}; };
'linux' = @{ 'linux' = @{
'host' = @{'linux' = $True }; 'host' = @{'linux' = $True };
'toolchain' = @{'gcc' = $True; }; 'toolchain' = @{'gcc' = $True; 'clang' = $True };
}; };
'android' = @{ 'android' = @{
'host' = @{'windows' = $True; 'linux' = $True; 'macos' = $True }; 'host' = @{'windows' = $True; 'linux' = $True; 'macos' = $True };
'toolchain' = @{'ndk' = $True; }; 'toolchain' = @{'clang' = $True; };
}; };
'osx' = $appleTable; 'osx' = $appleTable;
'ios' = $appleTable; 'ios' = $appleTable;
@ -1278,7 +1294,7 @@ function validHostAndToolchain() {
'watchos' = $appleTable; 'watchos' = $appleTable;
'wasm' = @{ 'wasm' = @{
'host' = @{'windows' = $True; 'linux' = $True; 'macos' = $True }; 'host' = @{'windows' = $True; 'linux' = $True; 'macos' = $True };
'toolchain' = @{'emcc' = $True; }; 'toolchain' = @{'clang' = $True; };
}; };
} }
$validInfo = $validTable[$TARGET_OS] $validInfo = $validTable[$TARGET_OS]
@ -1312,20 +1328,20 @@ $null = setup_glslcc
$cmake_prog = setup_cmake $cmake_prog = setup_cmake
if ($TARGET_OS -eq 'win32' -or $TARGET_OS -eq 'winrt') { if ($Global:is_win_family) {
$nuget_prog = setup_nuget $nuget_prog = setup_nuget
} }
if ($TARGET_OS -eq 'win32') { if ($Global:is_win32) {
$nsis_prog = setup_nsis $nsis_prog = setup_nsis
if ($TOOLCHAIN_NAME -eq 'gcc' -or $TOOLCHAIN_NAME -eq 'clang') { if (!$Global:is_msvc) {
$ninja_prog = setup_ninja $ninja_prog = setup_ninja
} }
if ($TOOLCHAIN_NAME -eq 'clang') { if ($Global:is_clang) {
$null = setup_llvm $null = setup_llvm
} }
} }
elseif ($TARGET_OS -eq 'android') { elseif ($Global:is_android) {
$ninja_prog = setup_ninja $ninja_prog = setup_ninja
$null = setup_jdk # setup android sdk cmdlinetools require jdk $null = setup_jdk # setup android sdk cmdlinetools require jdk
$sdk_root, $ndk_root = setup_android_sdk $sdk_root, $ndk_root = setup_android_sdk
@ -1355,12 +1371,12 @@ elseif ($TARGET_OS -eq 'android') {
$clang_prog, $clang_ver = find_prog -name 'clang' $clang_prog, $clang_ver = find_prog -name 'clang'
} }
} }
elseif ($TARGET_OS -eq 'wasm') { elseif ($Global:is_wasm) {
$ninja_prog = setup_ninja $ninja_prog = setup_ninja
. setup_emsdk . setup_emsdk
} }
$is_host_target = ($TARGET_OS -eq 'win32') -or ($TARGET_OS -eq 'linux') -or ($TARGET_OS -eq 'osx') $is_host_target = $Global:is_win32 -or $Global:is_linux -or $Global:is_mac
if (!$setupOnly) { if (!$setupOnly) {
$stored_cwd = $(Get-Location).Path $stored_cwd = $(Get-Location).Path
@ -1439,7 +1455,7 @@ if (!$setupOnly) {
$CONFIG_ALL_OPTIONS += "-DCMAKE_BUILD_TYPE=$optimize_flag" $CONFIG_ALL_OPTIONS += "-DCMAKE_BUILD_TYPE=$optimize_flag"
} }
if ($using_ninja -and $TARGET_OS -eq 'android') { if ($using_ninja -and $Global:is_android) {
$CONFIG_ALL_OPTIONS += "-DCMAKE_MAKE_PROGRAM=$ninja_prog" $CONFIG_ALL_OPTIONS += "-DCMAKE_MAKE_PROGRAM=$ninja_prog"
} }
} }
@ -1468,12 +1484,12 @@ if (!$setupOnly) {
$prefix = $category $prefix = $category
} }
if ($is_host_target) { if ($is_host_target) {
$out_dir = "${prefix}_${TARGET_ARCH}" $out_dir = "${prefix}_${TARGET_CPU}"
} }
else { else {
$out_dir = "${prefix}_${TARGET_OS}" $out_dir = "${prefix}_${TARGET_OS}"
if ($TARGET_ARCH -ne '*') { if ($TARGET_CPU -ne '*') {
$out_dir += "_$TARGET_ARCH" $out_dir += "_$TARGET_CPU"
} }
} }
return $b1k.realpath($out_dir) return $b1k.realpath($out_dir)
@ -1508,7 +1524,7 @@ if (!$setupOnly) {
$b1k.println("CONFIG_ALL_OPTIONS=$CONFIG_ALL_OPTIONS, Count={0}" -f $CONFIG_ALL_OPTIONS.Count) $b1k.println("CONFIG_ALL_OPTIONS=$CONFIG_ALL_OPTIONS, Count={0}" -f $CONFIG_ALL_OPTIONS.Count)
if (($TARGET_OS -eq 'android') -and $is_gradlew) { if ($Global:is_android -and $is_gradlew) {
$storedLocation = (Get-Location).Path $storedLocation = (Get-Location).Path
$build_tool = (Get-Command $options.xt).Source $build_tool = (Get-Command $options.xt).Source
$build_tool_dir = Split-Path $build_tool -Parent $build_tool_dir = Split-Path $build_tool -Parent
@ -1572,7 +1588,7 @@ if (!$setupOnly) {
# step4. build # step4. build
# apply additional build options # apply additional build options
$BUILD_ALL_OPTIONS += "--parallel" $BUILD_ALL_OPTIONS += "--parallel"
if ($TARGET_OS -eq 'linux') { if ($Global:is_linux) {
$BUILD_ALL_OPTIONS += "$(nproc)" $BUILD_ALL_OPTIONS += "$(nproc)"
} }
if (($cmake_generator -eq 'Xcode') -and ($BUILD_ALL_OPTIONS.IndexOf('--verbose') -eq -1)) { if (($cmake_generator -eq 'Xcode') -and ($BUILD_ALL_OPTIONS.IndexOf('--verbose') -eq -1)) {
@ -1588,26 +1604,47 @@ if (!$setupOnly) {
buildDir = $BUILD_DIR buildDir = $BUILD_DIR
targetOS = $TARGET_OS targetOS = $TARGET_OS
hostArch = $hostArch hostArch = $hostArch
isHostArch = $TARGET_ARCH -eq $hostArch isHostArch = $TARGET_CPU -eq $hostArch
isHostTarget = $is_host_target isHostTarget = $is_host_target
compilerID = $TOOLCHAIN_NAME compilerID = $TOOLCHAIN_NAME
} }
} else { } else {
# google gn build system: only support vs2022 currently # google gn build system: only support vs2022 currently
$CONFIG_ALL_OPTIONS = $options.xc $CONFIG_ALL_OPTIONS = $options.xc
$is_winrt = $TARGET_OS -eq 'winrt' $stored_env_path = $null
if ($is_winrt) {
if ($Global:is_winrt) {
$CONFIG_ALL_OPTIONS += 'target_os=\"winuwp\"' $CONFIG_ALL_OPTIONS += 'target_os=\"winuwp\"'
} elseif($Global:is_ios) {
$CONFIG_ALL_OPTIONS += 'target_os=\"ios\"'
if ($TARGET_CPU -eq 'x64') {
$CONFIG_ALL_OPTIONS += 'target_environment=\"simulator\"'
}
} elseif($Global:is_android) {
$CONFIG_ALL_OPTIONS += 'target_os=\"android\"'
$stored_env_path = $env:PATH
active_ndk_toolchain
} }
Write-Output ("CONFIG_ALL_OPTIONS=$CONFIG_ALL_OPTIONS, Count={0}" -f $CONFIG_ALL_OPTIONS.Count) Write-Output ("CONFIG_ALL_OPTIONS=$CONFIG_ALL_OPTIONS, Count={0}" -f $CONFIG_ALL_OPTIONS.Count)
$cmdStr="gn gen out/release --sln=angle-release --ide=vs2022 ""--args=target_cpu=\""$TARGET_ARCH\"" $CONFIG_ALL_OPTIONS""" $cmd_args = @('gen', 'out/release')
Write-Output "Executing command: {$cmdStr}" if ($Global:is_win_family) {
cmd /c $cmdStr $cmd_args += '--sln=angle-release'
$cmd_args += '--ide=vs2022'
}
$translated_target_cpu = if ($TARGET_CPU -ne 'armv7') { $TARGET_CPU } else { 'arm' }
$cmd_args += "--args=target_cpu=\`"$translated_target_cpu\`" $CONFIG_ALL_OPTIONS"
Write-Output "Executing command: gn {$cmd_args}"
gn $cmd_args
# build # build
$cmdStr="autoninja -C out\release --verbose $(${options}.t)" autoninja -C out/release --verbose $options.t
Write-Output "Executing command: {$cmdStr}"
cmd /c $cmdStr # restore env:PATH
if ($stored_env_path) {
$env:PATH = $stored_env_path
}
} }
Set-Location $stored_cwd Set-Location $stored_cwd