xref: /aosp_15_r20/external/pytorch/scripts/read_conda_versions.sh (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1# Simple script used to easily search all packages in conda for their
2# dependency requirements
3# TODO also search through output of ldd
4# TODO update conda info syntax for different channels
5
6if [ -z "$CONDA_ROOT" ]; then
7  # TODO create our own environment
8  echo "Please set CONDA_ROOT so that I know where to search for conda libraries"
9  echo "I expect CONDA_ROOT to be the path to the current conda environment."
10  echo "Also FYI I will probably mess up the current conda environment."
11  exit 1
12fi
13
14if [ -z "$1" ]; then
15  echo "Please give me a package name to search for"
16  exit 1
17fi
18PKG_NAME="$1"
19
20if [ -n "$2" ]; then
21  echo "Searching in channel $2"
22  CONDA_CHANNEL="$2"
23fi
24
25# These are the packages of interest to search the dependencies for
26# TODO use this
27PACKAGES_OF_INTEREST=( libgcc-ng libprotobuf numpy )
28
29# We will run `conda install` and `conda uninstall` a lot, but we don't want
30# this very noisy output to clutter the user experience
31VERBOSE_LOG='read_conda_versions.log'
32echo "Conda install/uninstall log for $PKG_NAME" > $VERBOSE_LOG
33
34
35
36#
37# Build up the name of the installed library to call `nm` on
38#
39PKG_INSTALLED_LIB="$PKG_NAME"
40
41# opencv installs a bunch of libraries. We'll just check libopencv_core
42if [[ $PKG_NAME == opencv ]]; then
43  PKG_INSTALLED_LIB="${PKG_INSTALLED_LIB}_core"
44fi
45
46# Most packages prepend a 'lib' to the package name, but libprotobuf is an
47# exception
48if [[ $PKG_NAME != lib* ]]; then
49  PKG_INSTALLED_LIB="lib${PKG_INSTALLED_LIB}"
50fi
51
52# The shared library suffix differs on macOS an Linux
53if [[ "$(uname)" == Darwin ]]; then
54  PKG_INSTALLED_LIB="${PKG_INSTALLED_LIB}.dylib"
55else
56  PKG_INSTALLED_LIB="${PKG_INSTALLED_LIB}.so"
57fi
58echo "Determined the library name of $PKG_NAME to be $PKG_INSTALLED_LIB"
59echo "Determined the library name of $PKG_NAME to be $PKG_INSTALLED_LIB" >> $VERBOSE_LOG
60
61
62
63#
64# Get all available packages with conda-search
65#
66
67# Split the output from conda search into an array, one line per package (plus
68# the header)
69conda_search_packages=()
70while read -r line; do conda_search_packages+=("$line"); done <<< "$(conda search $PKG_NAME $CONDA_CHANNEL)"
71
72### Typical `conda search` output looks like
73###   Loading channels: done
74###   Name                       Version                   Build  Channel
75###   protobuf                   2.6.1                    py27_0  defaults
76###                              2.6.1                    py27_1  defaults
77###                              3.2.0                    py27_0  defaults
78###                              3.2.0                    py35_0  defaults
79###                              3.2.0                    py36_0  defaults
80###                              3.4.1            py27h66c1d77_0  defaults
81###                              3.4.1            py35h9d33684_0  defaults
82###                              3.4.1            py36h314970b_0  defaults
83###                              3.5.1            py27h0a44026_0  defaults
84###                              3.5.1            py35h0a44026_0  defaults
85###                              3.5.1            py36h0a44026_0  defaults
86##
87### Typical `conda info` output looks like
88###   protobuf 3.5.1 py36h0a44026_0
89###     -----------------------------
90###   file name   : protobuf-3.5.1-py36h0a44026_0.tar.bz2
91###   name        : protobuf
92###   version     : 3.5.1
93###   build string: py36h0a44026_0
94###   build number: 0
95###   channel     : https://repo.continuum.io/pkgs/main/osx-64
96###   size        : 589 KB
97###   arch        : None
98###   constrains  : ()
99###   license     : New BSD License
100###   license_family: BSD
101###   md5         : 7dbdb06612e21c42fbb8a62354e13e10
102###   platform    : None
103###   subdir      : osx-64
104###   timestamp   : 1519951502766
105###   url         : https://repo.continuum.io/pkgs/main/osx-64/protobuf-3.5.1-py36h0a44026_0.tar.bz2
106###   dependencies:
107###       libcxx >=4.0.1
108###       libprotobuf >=3.5.1,<3.6.0a0
109###       python >=3.6,<3.7.0a0
110###       six
111
112# Echo what packages we'll look through.
113echo "Processing these packages:"
114for pkg in "${conda_search_packages[@]:2}"; do
115  echo "  $pkg"
116done
117
118
119
120#
121# Look up each package in conda info, then install it and search the exported
122# symbols for signs of cxx11
123#
124for pkg in "${conda_search_packages[@]:2}"; do
125  echo "Processing $pkg" >> $VERBOSE_LOG
126
127  # Split each line into an array and build the package specification
128  # <package_name (1st line only)>  maj.min.patch  build_string  channel_name
129  line_parts=( $pkg )
130  if [[ ${line_parts[0]} == $PKG_NAME ]]; then
131    # First line of output
132    PKG_VERSION="${line_parts[1]}"
133    PKG_BUILD_STR="${line_parts[2]}"
134  else
135    PKG_VERSION="${line_parts[0]}"
136    PKG_BUILD_STR="${line_parts[1]}"
137  fi
138  PKG_SPEC="$PKG_NAME=$PKG_VERSION=$PKG_BUILD_STR"
139
140  # Output current pkg spec
141  echo
142  echo "${PKG_SPEC}:"
143  echo "Determined that the package spec is $PKG_SPEC" >> $VERBOSE_LOG
144
145  # Split the output of conda_info into an array of lines
146  pkg_dependencies=()
147  while read -r line; do pkg_dependencies+=("$line"); done <<< "$(conda info "$PKG_SPEC" $CONDA_CHANNEL)"
148
149  # List all the listed dependencies in `conda info`
150  if [ "${#pkg_dependencies[@]}" -gt 19 ]; then
151    echo "  Listed dependencies:"
152    echo "  Listed dependencies:" >> $VERBOSE_LOG
153    for pkg_dependency in "${pkg_dependencies[@]:20}"; do
154      echo "    $pkg_dependency"
155      echo "    $pkg_dependency" >> $VERBOSE_LOG
156    done
157  else
158    echo "  No listed dependencies in conda-info" >> $VERBOSE_LOG
159  fi
160
161  # But sometimes (a lot of the time) the gcc with which a package was built
162  # against is not listed in dependencies. So we try to figure it out manually
163  # We install this exact package, and then grep the exported symbols for signs
164  # of cxx11
165  echo "Calling conda-uninstall on $PKG_NAME" >> $VERBOSE_LOG
166  echo "conda uninstall -y $PKG_NAME --quiet" >> $VERBOSE_LOG
167  conda uninstall -y "$PKG_NAME" --quiet >> $VERBOSE_LOG 2>&1
168
169  echo "Calling conda-install on $PKG_SPEC" >> $VERBOSE_LOG
170  echo "conda install -y $PKG_SPEC --quiet --no-deps $CONDA_CHANNEL" >> $VERBOSE_LOG
171  conda install -y "$PKG_SPEC" --quiet --no-deps $CONDA_CHANNEL >> $VERBOSE_LOG 2>&1
172  if [ $? -eq 0 ]; then
173    # Only grep the exported symbols if the library was installed correctly
174
175    MENTIONS_CXX11="$(nm "$CONDA_ROOT/lib/$PKG_INSTALLED_LIB" | grep cxx11 | wc -l)"
176    if [ $MENTIONS_CXX11 -gt 0 ]; then
177      echo "  This package is built against the recent gcc ABI ($MENTIONS_CXX11 mentions of cxx11)"
178      echo "$CONDA_ROOT/lib/$PKG_INSTALLED_LIB mentions cxx11 $MENTIONS_CXX11 times" >> $VERBOSE_LOG
179    fi
180  else
181    echo "Error installing $PKG_SPEC , continuing"
182    echo "Error installing $PKG_SPEC , continuing" >> $VERBOSE_LOG
183  fi
184done
185