Skip to content

Commit e5fb398

Browse files
Optimize compile params (#11)
Default CGO_ENABLED to 0 regardless of go env CGO_ENABLED. Revise command line instructions. Add more examples to README.md. Use WinPlatform enum for wintun extraction function. Revise test-run.sh, making it more flexible.
1 parent c311f7e commit e5fb398

File tree

9 files changed

+132
-66
lines changed

9 files changed

+132
-66
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "xray-pack"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
edition = "2024"
55
description = "Script to build and package Xray-core."
66

README.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,18 @@ A Rust-based tool to build and package [Xray-core](https://github.com/XTLS/Xray-
1919
- [Rust](https://www.rust-lang.org/tools/install), if you want to build from source.
2020
- [Go](https://go.dev/doc/install) (must be in your `PATH`)
2121

22-
## Installation
22+
## Build & Installation
23+
24+
Type to the terminal
2325

2426
```bash
25-
cargo build --release
27+
CFLAGS="-O3 -march=native" CXXFLAGS="-O3 -march=native" RUSTFLAGS="-C target-cpu=native" cargo install --path .
2628
```
2729

28-
The executable will be in `target/release/Xray-pack.exe`.
30+
and the most optimized installation programme will be installed.
2931

3032
## Usage
3133

32-
Enable all features for x86_64 CPU and Linux system (Only use GOAMD=v4 if CPU supports AVX512 instructions):
33-
34-
```bash
35-
CGO_ENABLED=0 GOAMD64="v4" GOEXPERIMENT="greenteagc,jsonv2,newinliner" ./Xray-pack.exe -s -v --goos linux --goarch amd64
36-
```
37-
3834
### Proxy
3935

4036
The programme determines the proxy settings from the environment variables in the following order: `HTTPS_PROXY`, `https_proxy`, `ALL_PROXY` and `all_proxy`.
@@ -49,8 +45,8 @@ Options:
4945
-p, --source-path <PATH> Path to Xray-core source code [default: current directory]
5046
-o, --output-path <PATH> Output directory [default: dist]
5147
--xray-version <VER> Xray-core version/tag/branch [default: main]
52-
--goos <GOOS> Target OS for Go compiler [default: linux]
53-
--goarch <GOARCH> Target architecture for Go compiler [default: amd64]
48+
--goos <GOOS> Target OS for Go compiler. This will override `GOARCH` and `go env GOOS` values. [default: linux]
49+
--goarch <GOARCH> Target architecture for Go compiler. This will override `GOARCH` and `go env GOARCH` values. [default: amd64]
5450
--gcflags <FLAGS> Go compiler gcflags [default: all:-l=4]
5551
--ldflags <FLAGS> Go compiler ldflags [default: -X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=]
5652
--region <REGION> Region for geo files [default: china-mainland] [possible: china-mainland, russia, iran]
@@ -59,6 +55,26 @@ Options:
5955
-V, --version Print version
6056
```
6157

58+
### Examples
59+
60+
Enable all features for x86_64 CPU and Linux system (Only use GOAMD=v4 if CPU supports AVX512 instructions):
61+
62+
```bash
63+
CGO_ENABLED=0 GOAMD64="v4" GOEXPERIMENT="greenteagc,jsonv2,newinliner" ./Xray-pack.exe -s -v --goos linux --goarch amd64
64+
```
65+
66+
Optimize for most x86_64 CPU (with AVX2 support) and Windows system:
67+
68+
```bash
69+
CGO_ENABLED=0 GOAMD64="v3" ./xray-pack.exe -s -v --goos windows --goarch amd64
70+
```
71+
72+
Build for ARM64 MacOS, disabling inlining:
73+
74+
```bash
75+
CGO_ENABLED=0 ./xray-pack.exe -s -v --goos darwin --goarch arm64 --gcflags "all:-l"
76+
```
77+
6278
### Output
6379

6480
The packaged zip will be named:

README_zh.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,18 @@
1919
- [Rust](https://www.rust-lang.org/tools/install)(如果你想要从源码编译)
2020
- [Go](https://go.dev/doc/install)(需在 `PATH` 中)
2121

22-
## 安装
22+
## 编译&安装
23+
24+
命令行中输入
2325

2426
```bash
25-
cargo build --release
27+
CFLAGS="-O3 -march=native" CXXFLAGS="-O3 -march=native" RUSTFLAGS="-C target-cpu=native" cargo install --path .
2628
```
2729

28-
可执行文件位于 `target/release/xray-pack.exe`
30+
以安装最优化的安装程序
2931

3032
## 使用方法
3133

32-
为 x86_64 CPU 和 Linux 系统启用所有性能特性(若 CPU 支持 AVX512 指令集):
33-
34-
```bash
35-
CGO_ENABLED=0 GOAMD64="v4" GOEXPERIMENT="greenteagc,jsonv2,newinliner" ./xray-pack.exe -s -v --goos linux --goarch amd64
36-
```
37-
3834
### 代理
3935

4036
程序按以下顺序从环境变量中确定代理设置:`HTTPS_PROXY``https_proxy``ALL_PROXY``all_proxy`
@@ -49,8 +45,8 @@ CGO_ENABLED=0 GOAMD64="v4" GOEXPERIMENT="greenteagc,jsonv2,newinliner" ./xray-pa
4945
-p, --source-path <路径> xray-core 源码路径 [默认: 当前目录]
5046
-o, --output-path <路径> 输出目录 [默认: dist]
5147
--xray-version <版本> xray-core 版本/标签/分支 [默认: main]
52-
--goos <GOOS> Go 编译目标操作系统 [默认: linux]
53-
--goarch <GOARCH> Go 编译目标架构 [默认: amd64]
48+
--goos <GOOS> Go 编译目标操作系统。这将覆盖 `GOARCH` 和 `go env GOOS` 值。[默认: linux]
49+
--goarch <GOARCH> Go 编译目标架构。这将覆盖 `GOARCH` 和 `go env GOARCH` 值。[默认: amd64]
5450
--gcflags <参数> Go 编译 gcflags [默认: all:-l=4]
5551
--ldflags <参数> Go 编译 ldflags [默认: -X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=]
5652
--region <区域> Geo 数据区域 [默认: china-mainland] [可选: china-mainland, russia, iran]
@@ -59,6 +55,26 @@ CGO_ENABLED=0 GOAMD64="v4" GOEXPERIMENT="greenteagc,jsonv2,newinliner" ./xray-pa
5955
-V, --version 显示版本
6056
```
6157

58+
### 示例
59+
60+
为 x86_64 CPU 和 Linux 系统启用所有性能特性(若 CPU 支持 AVX512 指令集):
61+
62+
```bash
63+
CGO_ENABLED=0 GOAMD64="v4" GOEXPERIMENT="greenteagc,jsonv2,newinliner" ./xray-pack.exe -s -v --goos linux --goarch amd64
64+
```
65+
66+
为主流 x86_64 CPU 和 Windows 系统启用指令集优化(CPU 只支持到 AVX2 指令集):
67+
68+
```bash
69+
CGO_ENABLED=0 GOAMD64="v3" ./xray-pack.exe -s -v --goos windows --goarch amd64
70+
```
71+
72+
为 ARM64 CPU 和 macOS 编译,禁用内联优化:
73+
74+
```bash
75+
CGO_ENABLED=0 ./xray-pack.exe -s -v --goos darwin --goarch arm64 --gcflags "all:-l"
76+
```
77+
6278
### 输出内容
6379

6480
最终打包的 zip 文件命名为:

src/cli.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ There are several options to maximize the performance of Xray-core:
1919
+ `GOAMD64` `GO386` `GOARM` environment variables: Specify the microarchitecture of the CPU.
2020
+ `GOEXPERIMENT` environment variable: Specify the experimental features of the Go compiler.
2121
`jsonv2`, `newinliner`, `greenteagc`, `regabiargs`, `regabiwrappers` might improve performance.
22-
+ `CGO_ENABLED` environment variable: Specify whether to enable CGO. `CGO_ENABLED=0` is recommended for better performance.
22+
+ `CGO_ENABLED` environment variable: Specify whether to enable CGO. `CGO_ENABLED=0` is recommended for better performance. \
23+
Note that this programme will override `go env CGO_ENABLED` and set `CGO_ENABLED=0` unless you specify it manually.
2324
+ `--gcflags`: Specify the `-gcflags` for the Go compiler. Default to `all=-l=4`, maximizing inline optimization.\
2425
"
2526
)]
@@ -80,14 +81,14 @@ pub struct PathOptions {
8081
pub struct CompileOptions {
8182
#[arg(
8283
long,
83-
help = "Specify GOOS for the Go compiler. Default to `linux`",
84+
help = "Specify GOOS for the Go compiler. This will override `GOARCH` and `go env GOOS` values.",
8485
default_value = "linux"
8586
)]
8687
pub goos: String,
8788

8889
#[arg(
8990
long,
90-
help = "Specify GOARCH for the Go compiler. Default to `amd64`",
91+
help = "Specify GOARCH for the Go compiler. This will override `GOARCH` and `go env GOARCH` values.",
9192
default_value = "amd64"
9293
)]
9394
pub goarch: String,
@@ -106,12 +107,6 @@ pub struct CompileOptions {
106107

107108
#[derive(Debug, Parser)]
108109
pub struct DownloadOptions {
109-
// #[arg(
110-
// long,
111-
// help = "Specify platform for the Wintun driver. This is only required if the platform is Windows.",
112-
// default_value = "amd64"
113-
// )]
114-
// pub platform: WinPlatform,
115110
#[arg(
116111
long,
117112
help = "Specify region for geo files",
@@ -120,25 +115,6 @@ pub struct DownloadOptions {
120115
pub region: Region,
121116
}
122117

123-
// #[derive(Debug, Clone, Copy, ValueEnum)]
124-
// #[value(rename_all = "lowercase")]
125-
// pub enum WinPlatform {
126-
// X86,
127-
// Amd64,
128-
// Arm,
129-
// Arm64,
130-
// }
131-
// impl Display for WinPlatform {
132-
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133-
// match self {
134-
// WinPlatform::X86 => write!(f, "x86"),
135-
// WinPlatform::Amd64 => write!(f, "amd64"),
136-
// WinPlatform::Arm => write!(f, "arm"),
137-
// WinPlatform::Arm64 => write!(f, "arm64"),
138-
// }
139-
// }
140-
// }
141-
142118
/// Three main restricted regions.
143119
#[derive(Debug, Clone, Copy, ValueEnum)]
144120
pub enum Region {

src/compile.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ pub fn build_xray(commid: &str) -> PackResult<()> {
2222

2323
let mut cmd = Command::new("go");
2424
let mut ldflags = String::new();
25-
cmd.env("GOOS", &args.compile_options.goos)
26-
.env("GOARCH", &args.compile_options.goarch)
27-
.args([
25+
let build_args = {
26+
let mut vec = vec![
2827
"build",
2928
"-o",
3029
output_path.to_str().unwrap(),
@@ -38,9 +37,21 @@ pub fn build_xray(commid: &str) -> PackResult<()> {
3837
format!("-X github.com/xtls/xray-core/core.build={commid} -s -w -buildid=");
3938
&ldflags
4039
}),
41-
"-v",
4240
"./main",
43-
]);
41+
];
42+
if args.verbose {
43+
vec.push("-v")
44+
}
45+
vec
46+
};
47+
48+
cmd.env("GOOS", &args.compile_options.goos)
49+
.env("GOARCH", &args.compile_options.goarch)
50+
.env(
51+
"CGO_ENABLED",
52+
std::env::var("CGO_ENABLED").unwrap_or_else(|_| "0".to_string()),
53+
)
54+
.args(&build_args);
4455

4556
cmd.spawn()
4657
.map_err(|e| PackError::BuildFailed(e.to_string()))?

src/download/wintun.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
1+
use std::fmt::Display;
2+
13
use crate::{
24
TEMP_DIR,
35
download::download_file,
46
errors::{PackError, PackResult},
57
};
68

9+
pub enum WinPlatform {
10+
X86,
11+
Amd64,
12+
Arm,
13+
Arm64,
14+
}
15+
impl Display for WinPlatform {
16+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17+
match self {
18+
WinPlatform::X86 => write!(f, "x86"),
19+
WinPlatform::Amd64 => write!(f, "amd64"),
20+
WinPlatform::Arm => write!(f, "arm"),
21+
WinPlatform::Arm64 => write!(f, "arm64"),
22+
}
23+
}
24+
}
25+
impl From<&str> for WinPlatform {
26+
fn from(s: &str) -> Self {
27+
match s.to_lowercase().as_str() {
28+
"x86" => WinPlatform::X86,
29+
"amd64" => WinPlatform::Amd64,
30+
"arm" => WinPlatform::Arm,
31+
"arm64" => WinPlatform::Arm64,
32+
_ => panic!("Invalid platform: {}", s),
33+
}
34+
}
35+
}
36+
737
/// Download wintun
838
pub fn download_wintun() -> PackResult<()> {
939
let url = "https://www.wintun.net/builds/wintun-0.14.1.zip";
@@ -13,7 +43,7 @@ pub fn download_wintun() -> PackResult<()> {
1343
}
1444

1545
/// Extract .dll according to platform. Also copy the LICENSE file.
16-
pub fn extract_wintun(platform: impl AsRef<str>) -> PackResult<()> {
46+
pub fn extract_wintun(platform: WinPlatform) -> PackResult<()> {
1747
let zip_path = TEMP_DIR.join("wintun.zip");
1848
let extract_path = TEMP_DIR.join("wintun.dll");
1949

@@ -23,7 +53,7 @@ pub fn extract_wintun(platform: impl AsRef<str>) -> PackResult<()> {
2353
let reader =
2454
std::fs::File::open(&zip_path).map_err(|_| PackError::ReadFailed(zip_path.clone()))?;
2555
let mut zip = zip::ZipArchive::new(reader)?;
26-
let mut zip_file = zip.by_path(format!("wintun/bin/{}/wintun.dll", platform.as_ref()))?;
56+
let mut zip_file = zip.by_path(format!("wintun/bin/{}/wintun.dll", platform))?;
2757

2858
// create writer
2959
let mut writer = std::fs::File::create(&extract_path)

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{path::PathBuf, sync::LazyLock};
44
use clap::Parser;
55

66
use crate::download::geodat::download_geodat;
7-
use crate::download::wintun::{download_wintun, extract_wintun};
7+
use crate::download::wintun::{WinPlatform, download_wintun, extract_wintun};
88
use crate::errors::{PackError, PackResult};
99
use crate::package::package_all;
1010

@@ -76,9 +76,9 @@ fn main() -> PackResult<()> {
7676

7777
download_geodat(args.download_options.region)?;
7878

79-
if args.compile_options.goos == "windows" {
79+
if args.compile_options.goos.to_lowercase() == "windows" {
8080
download_wintun()?;
81-
extract_wintun(&args.compile_options.goarch)?;
81+
extract_wintun(WinPlatform::from(args.compile_options.goarch.as_str()))?;
8282
}
8383

8484
package_all()?;

test-run.sh

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
#!/usr/bin/bash
22

3-
cargo run --profile dev-rel -- \
4-
--from-source \
5-
--xray-version v26.1.18 \
6-
--verbose
3+
show_help() {
4+
echo "Usage: $0 [OPTIONS] [ARGS...]"
5+
echo ""
6+
echo "Run the Xray application with development profile."
7+
echo ""
8+
echo "Options:"
9+
echo " help Show this help message"
10+
echo ""
11+
echo "Arguments passed after options will be forwarded to the application."
12+
}
13+
14+
# Check if help was requested
15+
for arg in "$@"; do
16+
if [ "$arg" == "help" ]; then
17+
show_help
18+
exit 0
19+
fi
20+
done
21+
22+
# Run the cargo command with all arguments
23+
cargo run --profile dev-rel -- "$@"

0 commit comments

Comments
 (0)