xref: /aosp_15_r20/external/executorch/third-party/link_torch.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
8# Links the path of `libtorch.so` or include library installed in python
9# library, to the output of buck, if pytorch is properly installed.
10# This util can be used by buck2 build.
11
12set -e
13
14# Named argument:
15# -o: output directory/file
16# -f: a list of files/directories that we want to link to the output directory, separated by comma ",".
17# These paths need to be in relative path format to the python library path.
18while getopts ":o:f:" opt; do
19  case $opt in
20    o) OUT="$OPTARG"
21    ;;
22    f) FILES="$OPTARG"
23    ;;
24    \?) echo "Invalid option -$OPTARG" >&2
25    exit 1
26    ;;
27  esac
28
29  case $OPTARG in
30    -*) echo "Option $opt needs to be one of -o, -f."
31    exit 1
32    ;;
33  esac
34done
35
36LIB=$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
37
38# delimiter ,
39export IFS=","
40
41for SUBPATH in $FILES; do
42  if [[ -f "$LIB/$SUBPATH" ]] || [[ -d "$LIB/$SUBPATH" ]];
43  then
44      ln -s "$LIB/$SUBPATH" "$OUT"
45  else
46      # NB: If a path doesn't exist, it's ok to skip it here. This is to handle the case
47      # of optional PyTorch dependencies like libgomp. They are part of PyTorch nightly
48      # wheel (from CentOS), but are not needed when building PyTorch from source
49      # (from system libgomp)
50      echo "Warning: $LIB/$SUBPATH doesn't exist, skipping..."
51  fi
52done
53