xref: /aosp_15_r20/external/executorch/kernels/portable/find_op_header.sh (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1#!/bin/bash
2# Copyright (c) Meta Platforms, Inc. and affiliates.
3# All rights reserved.
4#
5# This source code is licensed under the BSD-style license found in the
6# LICENSE file in the root directory of this source tree.
7
8set -o errexit
9set -o nounset
10set -o pipefail
11
12# The directory containing this shell script, regardless of the CWD when it
13# was invoked.
14SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
15readonly SCRIPT_DIR
16
17# The fbsource root that contains this script, even if it was run from xplat.
18FBSOURCE_DIR="$(cd -- "${SCRIPT_DIR}" && hg root)"
19readonly FBSOURCE_DIR
20
21# The buck target that generates the header file.
22readonly GENRULE_TARGET='fbcode//executorch/kernels/portable:generated_lib_combined'
23
24# Prints the path to the generated NativeFunctions.h to stdout.
25print_header_path() {
26  # buck2 will produce a line like
27  # fbcode//executorch/kernels/portable:generated_lib_combined[NativeFunctions.h] buck-out/v2/gen/fbcode/d839c731f5505c62/executorch/codegen/__generated_lib_combined__/out/NativeFunctions.h
28  # The sed command chops off everything before the space character.
29  # The relative path is relative to fbsource, so we print that first.
30  echo -n "${FBSOURCE_DIR}/"
31  (
32    cd "${FBSOURCE_DIR}/fbcode"
33    buck2 build --show-output \
34        "${GENRULE_TARGET}[NativeFunctions.h]" 2>&1 \
35        | grep '/NativeFunctions.h' \
36        | head -1 \
37        | sed -e 's/.* //'
38  )
39}
40
41main() {
42  echo "===== Generating header files ====="
43  (
44    cd "${FBSOURCE_DIR}/fbcode"
45    buck2 build "${GENRULE_TARGET}"
46  )
47  echo ""
48  echo "Header file: $(print_header_path)"
49}
50
51main "$@"
52