xref: /aosp_15_r20/external/executorch/third-party/link_torch.sh (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1*523fa7a6SAndroid Build Coastguard Worker#!/bin/bash
2*523fa7a6SAndroid Build Coastguard Worker# Copyright (c) Meta Platforms, Inc. and affiliates.
3*523fa7a6SAndroid Build Coastguard Worker# All rights reserved.
4*523fa7a6SAndroid Build Coastguard Worker#
5*523fa7a6SAndroid Build Coastguard Worker# This source code is licensed under the BSD-style license found in the
6*523fa7a6SAndroid Build Coastguard Worker# LICENSE file in the root directory of this source tree.
7*523fa7a6SAndroid Build Coastguard Worker
8*523fa7a6SAndroid Build Coastguard Worker# Links the path of `libtorch.so` or include library installed in python
9*523fa7a6SAndroid Build Coastguard Worker# library, to the output of buck, if pytorch is properly installed.
10*523fa7a6SAndroid Build Coastguard Worker# This util can be used by buck2 build.
11*523fa7a6SAndroid Build Coastguard Worker
12*523fa7a6SAndroid Build Coastguard Workerset -e
13*523fa7a6SAndroid Build Coastguard Worker
14*523fa7a6SAndroid Build Coastguard Worker# Named argument:
15*523fa7a6SAndroid Build Coastguard Worker# -o: output directory/file
16*523fa7a6SAndroid Build Coastguard Worker# -f: a list of files/directories that we want to link to the output directory, separated by comma ",".
17*523fa7a6SAndroid Build Coastguard Worker# These paths need to be in relative path format to the python library path.
18*523fa7a6SAndroid Build Coastguard Workerwhile getopts ":o:f:" opt; do
19*523fa7a6SAndroid Build Coastguard Worker  case $opt in
20*523fa7a6SAndroid Build Coastguard Worker    o) OUT="$OPTARG"
21*523fa7a6SAndroid Build Coastguard Worker    ;;
22*523fa7a6SAndroid Build Coastguard Worker    f) FILES="$OPTARG"
23*523fa7a6SAndroid Build Coastguard Worker    ;;
24*523fa7a6SAndroid Build Coastguard Worker    \?) echo "Invalid option -$OPTARG" >&2
25*523fa7a6SAndroid Build Coastguard Worker    exit 1
26*523fa7a6SAndroid Build Coastguard Worker    ;;
27*523fa7a6SAndroid Build Coastguard Worker  esac
28*523fa7a6SAndroid Build Coastguard Worker
29*523fa7a6SAndroid Build Coastguard Worker  case $OPTARG in
30*523fa7a6SAndroid Build Coastguard Worker    -*) echo "Option $opt needs to be one of -o, -f."
31*523fa7a6SAndroid Build Coastguard Worker    exit 1
32*523fa7a6SAndroid Build Coastguard Worker    ;;
33*523fa7a6SAndroid Build Coastguard Worker  esac
34*523fa7a6SAndroid Build Coastguard Workerdone
35*523fa7a6SAndroid Build Coastguard Worker
36*523fa7a6SAndroid Build Coastguard WorkerLIB=$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
37*523fa7a6SAndroid Build Coastguard Worker
38*523fa7a6SAndroid Build Coastguard Worker# delimiter ,
39*523fa7a6SAndroid Build Coastguard Workerexport IFS=","
40*523fa7a6SAndroid Build Coastguard Worker
41*523fa7a6SAndroid Build Coastguard Workerfor SUBPATH in $FILES; do
42*523fa7a6SAndroid Build Coastguard Worker  if [[ -f "$LIB/$SUBPATH" ]] || [[ -d "$LIB/$SUBPATH" ]];
43*523fa7a6SAndroid Build Coastguard Worker  then
44*523fa7a6SAndroid Build Coastguard Worker      ln -s "$LIB/$SUBPATH" "$OUT"
45*523fa7a6SAndroid Build Coastguard Worker  else
46*523fa7a6SAndroid Build Coastguard Worker      # NB: If a path doesn't exist, it's ok to skip it here. This is to handle the case
47*523fa7a6SAndroid Build Coastguard Worker      # of optional PyTorch dependencies like libgomp. They are part of PyTorch nightly
48*523fa7a6SAndroid Build Coastguard Worker      # wheel (from CentOS), but are not needed when building PyTorch from source
49*523fa7a6SAndroid Build Coastguard Worker      # (from system libgomp)
50*523fa7a6SAndroid Build Coastguard Worker      echo "Warning: $LIB/$SUBPATH doesn't exist, skipping..."
51*523fa7a6SAndroid Build Coastguard Worker  fi
52*523fa7a6SAndroid Build Coastguard Workerdone
53