xref: /aosp_15_r20/external/crosvm/jail/seccomp/generate_constants.sh (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1#!/usr/bin/env bash
2# Copyright 2022 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Run this script to re-generate the seccomp/*/constants.json files for
7# each architecture.
8
9set -ex
10cd "$(dirname "${BASH_SOURCE[0]}")/.."
11
12MINIJAIL_DIR=$(realpath "third_party/minijail")
13SECCOMP_DIR=$(realpath seccomp)
14
15export SRC="$MINIJAIL_DIR"
16
17# Create temporary directory for build artifacts and make sure it's cleaned up.
18TMP_DIR="$(mktemp -d)"
19cleanup() {
20    rm -rf "$TMP_DIR"
21}
22trap cleanup EXIT
23
24# Create bindings for each platform
25for arch in "x86_64" "arm" "aarch64" "riscv64"; do
26    BUILD_DIR="$TMP_DIR/$arch"
27    mkdir -p "$BUILD_DIR"
28    cd "$BUILD_DIR"
29
30    # Pick the right cross-compiler
31    if [ "$arch" = "x86_64" ]; then
32        export CC="gcc"
33        TARGET="x86_64-unknown-linux-gnu"
34    elif [ "$arch" = "arm" ]; then
35        export CC="arm-linux-gnueabihf-gcc"
36        TARGET="armv7-unknown-linux-gnueabihf"
37    elif [ "$arch" = "aarch64" ]; then
38        export CC="aarch64-linux-gnu-gcc"
39        TARGET="aarch64-unknown-linux-gnu"
40    elif [ "$arch" = "riscv64" ]; then
41        export CC="riscv64-unknown-linux-gnu-gcc"
42        TARGET="riscv64-unknown-linux-gnu"
43    fi
44
45    "$MINIJAIL_DIR/gen_constants.sh" "libconstants.gen.c"
46    "$MINIJAIL_DIR/gen_syscalls.sh" "libsyscalls.gen.c"
47
48    clang \
49        -target "$TARGET" \
50        -S \
51        -emit-llvm \
52        -I "$MINIJAIL_DIR" \
53        "libconstants.gen.c" \
54        "libsyscalls.gen.c"
55
56    "$MINIJAIL_DIR/tools/generate_constants_json.py" \
57        --output "$SECCOMP_DIR/$arch/constants.json" \
58        "libconstants.gen.ll" \
59        "libsyscalls.gen.ll"
60done
61