Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/host_tools/cargo_build.py
1956 views
1
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Functionality for a shared binary build and release path for all tests."""
4
5
import os
6
import platform
7
8
import framework.utils as utils
9
from framework import defs
10
11
from framework.defs import (
12
FC_BINARY_NAME, FC_WORKSPACE_DIR, FC_WORKSPACE_TARGET_DIR,
13
JAILER_BINARY_NAME
14
)
15
16
CARGO_BUILD_REL_PATH = 'firecracker_binaries'
17
"""Keep a single build path across all build tests."""
18
19
CARGO_RELEASE_REL_PATH = os.path.join(
20
CARGO_BUILD_REL_PATH, 'release'
21
)
22
"""Keep a single Firecracker release binary path across all test types."""
23
24
25
DEFAULT_BUILD_TARGET = '{}-unknown-linux-musl'.format(platform.machine())
26
RELEASE_BINARIES_REL_PATH = '{}/release/'.format(DEFAULT_BUILD_TARGET)
27
28
CARGO_UNITTEST_REL_PATH = os.path.join(CARGO_BUILD_REL_PATH, "test")
29
30
31
def cargo_build(path, extra_args='', src_dir='', extra_env=''):
32
"""Trigger build depending on flags provided."""
33
cmd = 'CARGO_TARGET_DIR={} {} cargo build {}'.format(
34
path,
35
extra_env,
36
extra_args
37
)
38
if src_dir:
39
cmd = 'cd {} && {}'.format(src_dir, cmd)
40
41
utils.run_cmd(cmd)
42
43
44
def cargo_test(path, extra_args=''):
45
"""Trigger unit tests depending on flags provided."""
46
path = os.path.join(path, CARGO_UNITTEST_REL_PATH)
47
cmd = 'CARGO_TARGET_DIR={} RUST_TEST_THREADS=1 RUST_BACKTRACE=1 ' \
48
'RUSTFLAGS="{}" cargo test {} --all --no-fail-fast'.format(
49
path, get_rustflags(), extra_args)
50
utils.run_cmd(cmd)
51
52
53
def get_firecracker_binaries():
54
"""Build the Firecracker and Jailer binaries if they don't exist.
55
56
Returns the location of the firecracker related binaries eventually after
57
building them in case they do not exist at the specified root_path.
58
"""
59
target = DEFAULT_BUILD_TARGET
60
out_dir = "{target_dir}/{target}/release".format(
61
target_dir=FC_WORKSPACE_TARGET_DIR, target=target
62
)
63
fc_bin_path = "{}/{}".format(out_dir, FC_BINARY_NAME)
64
jailer_bin_path = "{}/{}".format(out_dir, JAILER_BINARY_NAME)
65
66
if getattr(get_firecracker_binaries, 'binaries_built', False):
67
return fc_bin_path, jailer_bin_path
68
69
cd_cmd = "cd {}".format(FC_WORKSPACE_DIR)
70
flags = 'RUSTFLAGS="{}"'.format(get_rustflags())
71
cargo_default_cmd = "cargo build --release --target {}".format(
72
target
73
)
74
cargo_jailer_cmd = "cargo build -p jailer --release --target {}".format(
75
target
76
)
77
cmd = "{0} && {1} {2} && {1} {3}".format(
78
cd_cmd,
79
flags,
80
cargo_default_cmd,
81
cargo_jailer_cmd
82
)
83
84
utils.run_cmd(cmd)
85
86
utils.run_cmd(
87
"strip --strip-debug {} {}"
88
.format(fc_bin_path, jailer_bin_path)
89
)
90
91
setattr(get_firecracker_binaries, 'binaries_built', True)
92
93
return fc_bin_path, jailer_bin_path
94
95
96
def get_rustflags():
97
"""Get the relevant rustflags for building/unit testing."""
98
rustflags = "-D warnings"
99
if platform.machine() == "aarch64":
100
rustflags += " -C link-arg=-lgcc -C link-arg=-lfdt "
101
return rustflags
102
103
104
def run_seccompiler_bin(bpf_path,
105
json_path=defs.SECCOMP_JSON_DIR,
106
basic=False):
107
"""
108
Run seccompiler-bin.
109
110
:param bpf_path: path to the output file
111
:param json_path: optional path to json file
112
"""
113
cargo_target = '{}-unknown-linux-musl'.format(platform.machine())
114
115
# If no custom json filter, use the default one for the current target.
116
if json_path == defs.SECCOMP_JSON_DIR:
117
json_path = json_path / "{}.json".format(cargo_target)
118
119
cmd = 'cargo run -p seccompiler --target-dir {} --target {} --\
120
--input-file {} --target-arch {} --output-file {}'.format(
121
defs.SECCOMPILER_TARGET_DIR,
122
cargo_target,
123
json_path,
124
platform.machine(),
125
bpf_path
126
)
127
128
if basic:
129
cmd += ' --basic'
130
131
rc, _, _ = utils.run_cmd(cmd)
132
133
assert rc == 0
134
135