Skip to content

Commit

Permalink
perf: Optimize the build process on the Windows platform.
Browse files Browse the repository at this point in the history
Optimize the build process on the Windows platform.
  • Loading branch information
AdachiAndShimamura committed Mar 18, 2024
1 parent ef12a2c commit 02ff181
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 154 deletions.
49 changes: 1 addition & 48 deletions BuildOnWindows.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
## 1.拉取子模块代码
git submodule update --init --recursive


## 2.环境准备
### webrtc编译环境(webrtc版本差异会导致所依赖的msvc工具差异):
安装visual studio2022及组件\
Expand All @@ -14,7 +13,7 @@ git submodule update --init --recursive
* 适用于v143生成工具的C++ ATL
* 适用于Windows的C++ Clang工具

命令形式: \
通过vs_installer在命令行安装: \
`$ PATH_TO_INSTALLER.EXE
--add Microsoft.VisualStudio.Workload.NativeDesktop
--add Microsoft.VisualStudio.Component.VC.ATLMFC
Expand All @@ -31,51 +30,5 @@ winget install Microsoft.DotNet.DesktopRuntime.8 \
winget install --id=Kitware.CMake -e



### 本项目编译所需环境(以下示例仅供参考,以实际路径为准):
* 将 Clang 和 MSVC 路径配置到Path环境变量\
MSVC: C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64 \
Clang: C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\bin \
* 配置INCLUDE和LIB环境,分别包含msvc工具的include/lib目录,和Windows SDK的include/lib目录下的所有子目录\
INCLUDE: \
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include \
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt \
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um \
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt \
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared \
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\cppwinrt \
LIB: \
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\lib\x64 \
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64 \
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64 \
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt_enclave\x64 \
* 安装powershell 7(Windows powershell存在部分命令无法正确执行的问题,需要新版的powershell 7)

在powershell中配置环境变量(根据自己的实际版本进行修正):

$clangPath = "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\bin" \
$msvcPath = "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64"

$env:Path += ";$clangPath;$msvcPath"

$includePaths = @(
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include",
"C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt",
"C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um",
"C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt",
"C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared",
"C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\cppwinrt"
)

$libPaths = @(
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\lib\x64",
"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64",
"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64",
"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt_enclave\x64"
)

$env:INCLUDE = ($includePaths -join ";") \
$env:LIB = ($libPaths -join ";")

## 3.编译
在powershell 7中执行根目录下的build.ps1构建脚本
14 changes: 8 additions & 6 deletions bin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ fn main() {
"{}-linux-gnu-gcc",
env::var("CARGO_CFG_TARGET_ARCH").unwrap()
))
.arg("--print-file-name")
.arg("libstdc++.a")
.output()
.unwrap();
.arg("--print-file-name")
.arg("libstdc++.a")
.output()
.unwrap();
let mut path = PathBuf::from(String::from_utf8_lossy(&output.stdout).into_owned());
path.pop();
println!("cargo:rustc-link-search=native={}", path.to_str().unwrap());
Expand All @@ -56,8 +56,10 @@ fn main() {
println!("cargo:rustc-link-lib=framework=CoreMedia");
println!("cargo:rustc-link-lib=framework=AVFoundation");
}
"windows"=>{
//do nothing
"windows" => {
println!("cargo:rerun-if-changed=libcs/release/windows");
println!("cargo:rustc-link-search=libcs/release/windows");
println!("cargo:rustc-link-lib=static=gt");
}
os => {
panic!("Unsupported OS: {}", os)
Expand Down
16 changes: 16 additions & 0 deletions bin/src/cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ pub fn run_client(client_args: ClientArgs) {
};
let (args, go_str) = convert_to_go_slices(&args);
unsafe {
#[cfg(target_os = "windows")]
{
_rt0_amd64_windows_lib();
}

RunClient(args);
}
}
Expand All @@ -77,6 +82,17 @@ pub fn run_server(server_args: ServerArgs) {
};
let (args, go_str) = convert_to_go_slices(&args);
unsafe {
#[cfg(target_os = "windows")]
{
_rt0_amd64_windows_lib();
}

RunServer(args);
}
}


#[cfg(target_os = "windows")]
extern "C" {
fn _rt0_amd64_windows_lib();
}
29 changes: 0 additions & 29 deletions bin/src/cs_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,39 +188,10 @@ fn bindgen_test_layout_GoSlice() {
}


#[cfg(not(target_os = "windows"))]
extern "C" {
pub fn RunServer(args: GoSlice);
}

#[cfg(not(target_os = "windows"))]
extern "C" {
pub fn RunClient(args: GoSlice);
}

#[cfg(target_os = "windows")]
const DLL_DATA: &'static [u8] = include_bytes!("../../libcs/msvc-build/target/gt.dll");

#[cfg(target_os = "windows")]
pub fn InitGtDll() {
let mut dll_file = File::create( temp_dir().join("gt.dll")).expect("Failed to create DLL file");
dll_file.write_all(DLL_DATA).expect("Failed to write DLL data to file");
}

#[cfg(target_os = "windows")]
fn RunServer(args: GoSlice) {
unsafe {
let lib = libloading::Library::new( temp_dir().join("gt.dll")).unwrap();
let func: libloading::Symbol<unsafe extern fn(GoSlice)> = lib.get(b"RunServer").unwrap();
func(args);
}
}

#[cfg(target_os = "windows")]
fn RunClient(args: GoSlice) {
unsafe {
let lib = libloading::Library::new( temp_dir().join("gt.dll")).unwrap();
let func: libloading::Symbol<unsafe extern fn(GoSlice)> = lib.get(b"RunClient").unwrap();
func(args);
}
}
5 changes: 0 additions & 5 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ enum Commands {
}

fn main() {
#[cfg(target_os = "windows")]
{
cs::InitGtDll();
}

env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let cli = Cli::parse();
if let Some(signal) = cli.signal {
Expand Down
44 changes: 18 additions & 26 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@ $WEBRTC_DIR="$WORK_DIR/libcs/dep/_google-webrtc"
$MSQUIC_DIR="$WORK_DIR/libcs/dep/_msquic"
$WEBRTC_OUT_DIR="$WEBRTC_DIR/src/out/release/obj"
$MSQUIC_OUT_DIR="$MSQUIC_DIR/build/windows/x64_schannel/obj/Release"
$MSVC_BUILD_DIR="$WORK_DIR/libcs/msvc-build"

$env:CC="clang"
$env:CXX="clang++"
$env:CXXFLAGS="-I$WEBRTC_DIR/src -I$WEBRTC_DIR/src/third_party/abseil-cpp -I$MSQUIC_DIR/src/inc -std=c++17 -DWEBRTC_WIN -DQUIC_API_ENABLE_PREVIEW_FEATURES -DNOMINMAX"
$env:CGO_LDFLAGS="-L$MSQUIC_DIR/build/windows/x64_schannel/obj/Release -L$WEBRTC_DIR/src/out/release/obj -lmsquic.lib -lwebrtc.lib"
$env:CARGO_CFG_TARGET_OS="windows"
$env:RUSTFLAGS="-L $MSQUIC_OUT_DIR -l msquic -L $WEBRTC_OUT_DIR -l webrtc"

$env:DEPOT_TOOLS_WIN_TOOLCHAIN="0"
$env:GYP_GENERATORS="msvs-ninja,ninja"
$env:GYP_MSVS_OVERRIDE_PATH="C:\Program Files\Microsoft Visual Studio\2022\Community"
$env:GYP_MSVS_VERSION="2022"

# 检查 Pscx 模块是否已安装
if (-not (Get-Module -Name Pscx -ListAvailable)) {
Write-Host "安装Pscx PowerShell插件"
Install-Module -Name Pscx -AllowPrerelease -Force
}

# 检查 VSSetup 模块是否已安装
if (-not (Get-Module -Name VSSetup -ListAvailable)) {
Write-Host "安装VSSetup PowerShell插件"
Install-Module -Name VSSetup -AllowPrerelease -Force
}
Import-VisualStudioVars 2022 amd64

Set-Location $WORK_DIR
function complie_webrtc{
Set-Location "$WEBRTC_DIR/src"
Expand Down Expand Up @@ -51,11 +64,11 @@ function complie_msquic{
}


function release_gt_dylib{
function release_gt_lib{
Set-Location "$WORK_DIR/libcs"
Write-Host "开始编译gt server/client"
go build -tags release -trimpath -ldflags "-s -w" -buildmode=c-archive -o release/gt.lib ./lib/export
if (Test-Path -Path "./release/gt.lib")
go build -tags release -trimpath -ldflags "-s -w" -buildmode=c-archive -o release/windows/gt.lib ./lib/export
if (Test-Path -Path "./release/windows/gt.lib")
{
Write-Host "gt server/client编译完成"
}
Expand All @@ -65,27 +78,6 @@ function release_gt_dylib{
Set-Location $WORK_DIR
exit
}
Set-Location ./msvc-build

$directory = "$WORK_DIR/libcs/msvc-build/target"
if (-not (Test-Path -Path $directory -PathType Container)) {
New-Item -Path $directory -ItemType Directory -Force
Write-Host "目录已创建:$directory"
} else {
Write-Host "目录已存在:$directory"
}
Write-Host "开始编译发布gt server/client动态库"
cl /LD /MT /Fe:$MSVC_BUILD_DIR/gt.dll gt.cpp /link /DEF:gt.def "../release/gt.lib" "$MSQUIC_OUT_DIR/msquic.lib" "$WEBRTC_OUT_DIR/webrtc.lib" ntdll.lib
if (Test-Path -Path "$MSVC_BUILD_DIR/gt.dll")
{
Write-Host "动态库编译完成"
}
else
{
Write-Host "动态库编译失败"
Set-Location $WORK_DIR
exit
}
}

function release_gt_exe{
Expand All @@ -102,5 +94,5 @@ if (!(Test-Path -Path "$WEBRTC_OUT_DIR/webrtc.lib")){
if (!(Test-Path -Path "$MSQUIC_OUT_DIR/msquic.lib")){
complie_msquic
}
release_gt_dylib
release_gt_lib
release_gt_exe
17 changes: 0 additions & 17 deletions libcs/msvc-build/cgo.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions libcs/msvc-build/gt.cpp

This file was deleted.

4 changes: 0 additions & 4 deletions libcs/msvc-build/gt.def

This file was deleted.

0 comments on commit 02ff181

Please sign in to comment.