From 1adb2031dd227eb49e58c27c21be91a4336d1a9a Mon Sep 17 00:00:00 2001 From: halx99 Date: Wed, 10 Jan 2024 01:57:56 +0800 Subject: [PATCH] Allow build linux target with toolchain: clang --- 1k/build.ps1 | 145 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 91 insertions(+), 54 deletions(-) diff --git a/1k/build.ps1 b/1k/build.ps1 index 4dc2b2f656..cbb9317120 100644 --- a/1k/build.ps1 +++ b/1k/build.ps1 @@ -266,26 +266,32 @@ if (!$TARGET_OS) { } # define some useful global vars $Global:is_wasm = $TARGET_OS -eq 'wasm' -$Global:is_winrt = ($target_os -eq 'winrt') -$Global:is_win_family = $is_winrt -or ($target_os -eq 'win32') -$Global:is_apple_family = !!(@{'osx' = $true; 'ios' = $true; 'tvos' = $true }[$TARGET_OS]) - +$Global:Is_win32 = $TARGET_OS -eq 'win32' +$Global:is_winrt = $TARGET_OS -eq 'winrt' +$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' if (!$is_wasm) { - $TARGET_ARCH = $options.a - if (!$TARGET_ARCH) { - $TARGET_ARCH = @{'ios' = 'arm64'; 'tvos' = 'arm64'; 'watchos' = 'arm64'; 'android' = 'arm64'; }[$TARGET_OS] - if (!$TARGET_ARCH) { - $TARGET_ARCH = $hostArch + $TARGET_CPU = $options.a + if (!$TARGET_CPU) { + $TARGET_CPU = @{'ios' = 'arm64'; 'tvos' = 'arm64'; 'watchos' = 'arm64'; 'android' = 'arm64'; }[$TARGET_OS] + if (!$TARGET_CPU) { + $TARGET_CPU = $hostArch } - $options.a = $TARGET_ARCH - } elseif($TARGET_ARCH -eq 'arm') { - $TARGET_ARCH = $options.a = 'armv7' + $options.a = $TARGET_CPU + } elseif($TARGET_CPU -eq 'arm') { + $TARGET_CPU = $options.a = 'armv7' } } else { - $TARGET_ARCH = $options.a = '*' + $TARGET_CPU = $options.a = '*' } if (!$setupOnly) { @@ -300,12 +306,12 @@ $toolchains = @{ 'win32' = 'msvc'; 'winrt' = 'msvc'; 'linux' = 'gcc'; - 'android' = 'ndk'; - 'osx' = 'xcode'; - 'ios' = 'xcode'; - 'tvos' = 'xcode'; - 'watchos' = 'xcode'; - 'wasm' = 'emcc'; # wasm llvm-emcc + 'android' = 'clang'; # xcode clang + 'osx' = 'clang'; # xcode clang + 'ios' = 'clang'; # xcode clang + 'tvos' = 'clang'; # xcode clang + 'watchos' = 'clang'; # xcode clang + 'wasm' = 'clang'; # emcc clang } if (!$TOOLCHAIN) { $TOOLCHAIN = $toolchains[$TARGET_OS] @@ -323,6 +329,9 @@ if (!$TOOLCHAIN_VER) { $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' } if (!$b1k.isdir($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) { 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 $use_msvcr14x = $null if ([bool]::TryParse($env:use_msvcr14x, [ref]$use_msvcr14x) -and $use_msvcr14x) { if ("$env:LIB".IndexOf('msvcr14x') -eq -1) { $msvcr14x_root = $env:msvcr14x_ROOT - $env:Platform = $target_arch + $env:Platform = $target_cpu 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 $gclient_dir = Join-Path $external_prefix 'depot_tools' if(!(Test-Path $gclient_dir -PathType Container)) { - $b1k.mkdirs($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 + if ($IsWin) { + $b1k.mkdirs($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) { @@ -1110,7 +1123,7 @@ function preprocess_win([string[]]$inputOptions) { $outputOptions += "-DCMAKE_SYSTEM_VERSION=$($options.sdk)" } - if ($TOOLCHAIN_NAME -eq 'msvc') { + if ($Global:is_msvc) { # Generate vs2019 on github ci # Determine arch name $arch = if ($options.a -eq 'x86') { 'Win32' } else { $options.a } @@ -1153,7 +1166,7 @@ function preprocess_win([string[]]$inputOptions) { $outputOptions += '-DBUILD_SHARED_LIBS=TRUE' } } - elseif ($TOOLCHAIN_NAME -eq 'clang') { + elseif ($Global:is_clang) { $outputOptions += '-DCMAKE_C_COMPILER=clang', '-DCMAKE_CXX_COMPILER=clang++' $Script:cmake_generator = 'Ninja Multi-Config' } @@ -1166,6 +1179,9 @@ function preprocess_win([string[]]$inputOptions) { function preprocess_linux([string[]]$inputOptions) { $outputOptions = $inputOptions + if ($Global:is_clang) { + $outputOptions += '-DCMAKE_C_COMPILER=clang', '-DCMAKE_CXX_COMPILER=clang++' + } return $outputOptions } @@ -1236,10 +1252,10 @@ function preprocess_ios([string[]]$inputOptions) { if (!$cmake_toolchain_file) { $cmake_toolchain_file = Join-Path $myRoot 'ios.cmake' $outputOptions += "-DCMAKE_TOOLCHAIN_FILE=$cmake_toolchain_file", "-DARCHS=$arch" - if ($TARGET_OS -eq 'tvos') { + if ($Global:is_tvos) { $outputOptions += '-DPLAT=tvOS' } - elseif ($TARGET_OS -eq 'watchos') { + elseif ($Global:is_watchos) { $outputOptions += '-DPLAT=watchOS' } } @@ -1253,7 +1269,7 @@ function preprocess_wasm([string[]]$inputOptions) { function validHostAndToolchain() { $appleTable = @{ 'host' = @{'macos' = $True }; - 'toolchain' = @{'xcode' = $True; }; + 'toolchain' = @{'clang' = $True; }; }; $validTable = @{ 'win32' = @{ @@ -1266,11 +1282,11 @@ function validHostAndToolchain() { }; 'linux' = @{ 'host' = @{'linux' = $True }; - 'toolchain' = @{'gcc' = $True; }; + 'toolchain' = @{'gcc' = $True; 'clang' = $True }; }; 'android' = @{ 'host' = @{'windows' = $True; 'linux' = $True; 'macos' = $True }; - 'toolchain' = @{'ndk' = $True; }; + 'toolchain' = @{'clang' = $True; }; }; 'osx' = $appleTable; 'ios' = $appleTable; @@ -1278,7 +1294,7 @@ function validHostAndToolchain() { 'watchos' = $appleTable; 'wasm' = @{ 'host' = @{'windows' = $True; 'linux' = $True; 'macos' = $True }; - 'toolchain' = @{'emcc' = $True; }; + 'toolchain' = @{'clang' = $True; }; }; } $validInfo = $validTable[$TARGET_OS] @@ -1312,20 +1328,20 @@ $null = setup_glslcc $cmake_prog = setup_cmake -if ($TARGET_OS -eq 'win32' -or $TARGET_OS -eq 'winrt') { +if ($Global:is_win_family) { $nuget_prog = setup_nuget } -if ($TARGET_OS -eq 'win32') { +if ($Global:is_win32) { $nsis_prog = setup_nsis - if ($TOOLCHAIN_NAME -eq 'gcc' -or $TOOLCHAIN_NAME -eq 'clang') { + if (!$Global:is_msvc) { $ninja_prog = setup_ninja } - if ($TOOLCHAIN_NAME -eq 'clang') { + if ($Global:is_clang) { $null = setup_llvm } } -elseif ($TARGET_OS -eq 'android') { +elseif ($Global:is_android) { $ninja_prog = setup_ninja $null = setup_jdk # setup android sdk cmdlinetools require jdk $sdk_root, $ndk_root = setup_android_sdk @@ -1355,12 +1371,12 @@ elseif ($TARGET_OS -eq 'android') { $clang_prog, $clang_ver = find_prog -name 'clang' } } -elseif ($TARGET_OS -eq 'wasm') { +elseif ($Global:is_wasm) { $ninja_prog = setup_ninja . 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) { $stored_cwd = $(Get-Location).Path @@ -1439,7 +1455,7 @@ if (!$setupOnly) { $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" } } @@ -1468,12 +1484,12 @@ if (!$setupOnly) { $prefix = $category } if ($is_host_target) { - $out_dir = "${prefix}_${TARGET_ARCH}" + $out_dir = "${prefix}_${TARGET_CPU}" } else { $out_dir = "${prefix}_${TARGET_OS}" - if ($TARGET_ARCH -ne '*') { - $out_dir += "_$TARGET_ARCH" + if ($TARGET_CPU -ne '*') { + $out_dir += "_$TARGET_CPU" } } 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) - if (($TARGET_OS -eq 'android') -and $is_gradlew) { + if ($Global:is_android -and $is_gradlew) { $storedLocation = (Get-Location).Path $build_tool = (Get-Command $options.xt).Source $build_tool_dir = Split-Path $build_tool -Parent @@ -1572,7 +1588,7 @@ if (!$setupOnly) { # step4. build # apply additional build options $BUILD_ALL_OPTIONS += "--parallel" - if ($TARGET_OS -eq 'linux') { + if ($Global:is_linux) { $BUILD_ALL_OPTIONS += "$(nproc)" } if (($cmake_generator -eq 'Xcode') -and ($BUILD_ALL_OPTIONS.IndexOf('--verbose') -eq -1)) { @@ -1588,26 +1604,47 @@ if (!$setupOnly) { buildDir = $BUILD_DIR targetOS = $TARGET_OS hostArch = $hostArch - isHostArch = $TARGET_ARCH -eq $hostArch + isHostArch = $TARGET_CPU -eq $hostArch isHostTarget = $is_host_target compilerID = $TOOLCHAIN_NAME } } else { # google gn build system: only support vs2022 currently $CONFIG_ALL_OPTIONS = $options.xc - $is_winrt = $TARGET_OS -eq 'winrt' - if ($is_winrt) { + $stored_env_path = $null + + if ($Global:is_winrt) { $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) - $cmdStr="gn gen out/release --sln=angle-release --ide=vs2022 ""--args=target_cpu=\""$TARGET_ARCH\"" $CONFIG_ALL_OPTIONS""" - Write-Output "Executing command: {$cmdStr}" - cmd /c $cmdStr + $cmd_args = @('gen', 'out/release') + if ($Global:is_win_family) { + $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 - $cmdStr="autoninja -C out\release --verbose $(${options}.t)" - Write-Output "Executing command: {$cmdStr}" - cmd /c $cmdStr + autoninja -C out/release --verbose $options.t + + # restore env:PATH + if ($stored_env_path) { + $env:PATH = $stored_env_path + } } Set-Location $stored_cwd