Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cargo config auto-gen #51

Merged
merged 20 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ package-lock.json
core/src/ten_manager/target/
core/src/ten_rust/src/schema/bindings.rs
core/src/ten_rust/target/
/.cargo

# private modules
packages/private_apps
Expand Down
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
"editor.wordWrapColumn": 80
},
"[python]": {
"editor.tabSize": 4
"editor.tabSize": 4,
"editor.defaultFormatter": "ms-python.black-formatter"
},
"black-formatter.args": [
"--line-length",
"80"
],
"debug.allowBreakpointsEverywhere": true,
"doxdocgen.c.commentPrefix": "/// ",
"doxdocgen.c.firstLine": "///",
Expand Down
144 changes: 134 additions & 10 deletions build/common/rust/rust.gni
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,31 @@ template("rust_target") {
_rustflags = invoker._rustflags
}

if (is_debug) {
if (ten_rust_enable_asan) {
if (enable_sanitizer) {
_rustflags = "${_rustflags} -Zsanitizer=address"
}
if (!ten_rust_enable_gen_cargo_config && enable_sanitizer &&
ten_rust_enable_asan) {
asan_args = [
"--action",
"print",
"--compiler",
]
if (is_clang) {
asan_args += [ "clang" ]
} else {
asan_args += [ "gcc" ]
}

asan_args += [
"--target-os",
target_os,
"--target-arch",
target_cpu,
]

flags = exec_script("//build/common/rust/rust_gen_cargo_config.py",
asan_args,
"trim string")

_rustflags = "${_rustflags} ${flags}"
}

if (_rustflags != "") {
Expand Down Expand Up @@ -209,12 +228,31 @@ template("rust_test") {
_rustflags = invoker._rustflags
}

if (is_debug) {
if (ten_rust_enable_asan) {
if (enable_sanitizer) {
_rustflags = "${_rustflags} -Zsanitizer=address"
}
if (!ten_rust_enable_gen_cargo_config && enable_sanitizer &&
ten_rust_enable_asan) {
asan_args = [
"--action",
"print",
"--compiler",
]
if (is_clang) {
asan_args += [ "clang" ]
} else {
asan_args += [ "gcc" ]
}

asan_args += [
"--target-os",
target_os,
"--target-arch",
target_cpu,
]

flags = exec_script("//build/common/rust/rust_gen_cargo_config.py",
asan_args,
"trim string")

_rustflags = "${_rustflags} ${flags}"
}

if (_rustflags != "") {
Expand Down Expand Up @@ -353,3 +391,89 @@ template("rust_cbindgen") {
outputs = [ _output ]
}
}

template("rust_gen_cargo_config") {
assert(defined(invoker.project_root), "project_root is not defined")

_project_root = rebase_path(invoker.project_root)
_target_name = target_name
_target_path = target_gen_dir

action("${_target_name}") {
script = "//build/common/rust/rust_gen_cargo_config.py"
_output = "${_target_path}/${_target_name}_gen_cargo_config"

args = [
"--project-root",
_project_root,
"--tg-timestamp-proxy-file",
rebase_path(_output),
]

args += [ "--compiler" ]
if (is_clang) {
args += [ "clang" ]
} else {
args += [ "gcc" ]
}

if (is_win) {
if (target_cpu == "x86") {
target = "i686-pc-windows-msvc"
} else if (target_cpu == "x64") {
target = "x86_64-pc-windows-msvc"
} else if (target_cpu == "arm64") {
target = "aarch64-pc-windows-msvc"
}
} else if (is_linux) {
if (target_cpu == "arm64") {
target = "aarch64-unknown-linux-gnu"
} else if (target_cpu == "arm") {
target = "armv7-unknown-linux-gnueabi"
} else if (target_cpu == "x86") {
target = "i686-unknown-linux-gnu"
} else if (target_cpu == "x64") {
target = "x86_64-unknown-linux-gnu"
}
} else if (is_mac) {
if (target_cpu == "arm64") {
target = "aarch64-apple-darwin"
} else if (target_cpu == "x86") {
target = "i686-apple-darwin"
} else if (target_cpu == "x64") {
target = "x86_64-apple-darwin"
}
}

args += [
"--target",
target,
"--target-os",
target_os,
"--target-arch",
target_cpu,
]

if (enable_sanitizer && ten_rust_enable_gen_cargo_config) {
args += [
"--action",
"gen",
]
} else {
args += [
"--action",
"delete",
]
}

forward_variables_from(invoker,
[
"deps",
"public_deps",
"data_deps",
"public_configs",
])

outputs = [ _output ]
}
}
179 changes: 179 additions & 0 deletions build/common/rust/rust_gen_cargo_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#
# Copyright © 2024 Agora
# This file is part of TEN Framework, an open source project.
# Licensed under the Apache License, Version 2.0, with certain conditions.
# Refer to the "LICENSE" file in the root directory for more information.
#
import argparse
import json
import shutil
import sys
import os
from build.scripts import timestamp_proxy, detect_asan_lib

halajohn marked this conversation as resolved.
Show resolved Hide resolved

GCC_ASAN_FLAGS = [
"-C",
"linker=gcc",
"-Z",
"external-clangrt",
"-Z",
"sanitizer=address",
"-l",
"asan",
]


# It is better to use lld as the linker in clang. In Rust, you can specify it
# using ["-C", "link-arg=-fuse-ld=lld"]. However, lld needs to be in the PATH.
# On some CI machines, lld is not in the PATH, so we won't use lld here.
CLANG_ASAN_FLAGS = [
"-C",
"linker=clang",
"-Z",
"external-clangrt",
"-Z",
"sanitizer=address",
"-C",
"link-args=-fsanitize=address",
]

ASAN_CONFIG = """[target.{build_target}]
rustflags = {asan_flags}

[build]
target = "{build_target}"
"""


class ArgumentInfo(argparse.Namespace):
def __init__(self):
super().__init__()
self.project_root: str
self.build_type: str
self.compiler: str
self.target: str
self.target_os: str
self.target_arch: str
self.tg_timestamp_proxy_file: str | None = None
self.enable_asan: bool
self.action: str


# There is only clang compiler on Mac, and the asan runtime in clang only has
# dynamic library. However, the ldflag '-shared-libsan' does not support in
# cargo + clang on Mac, cargo will raise a warning and ignore the flag, ex:
# clang: warning: argument unused during compilation: '-shared-libasan'. This
# might be a bug in cargo. So we need to use the following flag instead.
def special_link_args_on_mac(arch: str) -> str:
asan_lib = detect_asan_lib.detect_mac_asan_lib(arch)
return f"link-arg=-Wl,{asan_lib}"


def gen_cargo_config(args: ArgumentInfo):
if not os.path.exists(args.project_root):
raise Exception(f"Project root {args.project_root} does not exist.")

cargo_dir = os.path.join(args.project_root, ".cargo")
if not os.path.exists(cargo_dir):
os.mkdir(cargo_dir)

cargo_config = os.path.join(cargo_dir, "config.toml")
if os.path.exists(cargo_config):
os.remove(cargo_config)

flags = []
if args.compiler == "gcc":
flags = GCC_ASAN_FLAGS
else:
flags = CLANG_ASAN_FLAGS

if args.target_os == "mac":
flags.extend(["-C", special_link_args_on_mac(args.target_arch)])

config_content = ASAN_CONFIG.format(
build_target=args.target, asan_flags=json.dumps(flags)
)

with open(cargo_config, "w") as f:
f.write(config_content)


def delete_cargo_config(root: str):
cargo_dir = os.path.join(root, ".cargo")
if os.path.exists(cargo_dir):
shutil.rmtree(cargo_dir)


if __name__ == "__main__":
parser = argparse.ArgumentParser()

parser.add_argument(
"--action", type=str, required=True, help="gen|delete|print"
)
parser.add_argument("--project-root", type=str, required=False)
parser.add_argument("--compiler", type=str, required=True)
parser.add_argument("--target", type=str, required=False)
parser.add_argument("--target-os", type=str, required=True)
parser.add_argument("--target-arch", type=str, required=True)
parser.add_argument(
"--tg-timestamp-proxy-file", type=str, default="", required=False
)
parser.add_argument(
"--enable-asan", action=argparse.BooleanOptionalAction, default=True
)

arg_info = ArgumentInfo()
args = parser.parse_args(namespace=arg_info)

returncode = 0
if args.action == "gen":
try:
gen_cargo_config(args)

# Success to gen cargo config, update the stamp file to represent
# this fact.
timestamp_proxy.touch_timestamp_proxy_file(
args.tg_timestamp_proxy_file
)
except Exception as exc:
returncode = 1
timestamp_proxy.remove_timestamp_proxy_file(
args.tg_timestamp_proxy_file
)
print(exc)

finally:
sys.exit(-1 if returncode != 0 else 0)

elif args.action == "delete":
try:
delete_cargo_config(args.project_root)

# Success to delete cargo config, update the stamp file to represent
# this fact.
timestamp_proxy.touch_timestamp_proxy_file(
args.tg_timestamp_proxy_file
)
except Exception as exc:
returncode = 1
timestamp_proxy.remove_timestamp_proxy_file(
args.tg_timestamp_proxy_file
)
print(exc)

finally:
sys.exit(-1 if returncode != 0 else 0)

else:
flags = [
CLANG_ASAN_FLAGS[i] + CLANG_ASAN_FLAGS[i + 1]
for i in range(0, len(CLANG_ASAN_FLAGS) - 1, 2)
]

if args.target_os == "mac":
asan_flag = special_link_args_on_mac(args.target_arch)
flags.append(f"-C{asan_flag}")

print(" ".join(flags))
sys.exit(0)
Loading
Loading