Repository: pytorch
Issue: PyTorch with non-shared build (building a single shared lib) is unsupported
Labels: module: build,triaged,module: static linking

### 🐛 Describe the bug

Building with `BUILD_SHARED_LIBS=0` to produce a single `libtorch.so` appears to be broken when using Python/torch rather than C++/libtorch.

Full build command:
```
BUILD_SHARED_LIBS=0 TORCH_CUDA_ARCH_LIST="9.0" CUDA_HOME="/usr/local/cuda"     CMAKE_PREFIX_PATH="$(dirname $(which conda))/../" USE_SYSTEM_NCCL=1 USE_MAGMA=1 USE_OPENCV=1 BUILD_CAFFE2=0 BUILD_CAFFE2_OPS=0 BUILD_TEST=0 USE_CUSPARSELT=1 USE_FBGEMM=0 USE_KINETO=0 USE_METAL=0 USE_NATIVE_ARCH=1 USE_NNPACK=0 USE_QNNPACK=0 USE_VALGRIND=0 USE_XNNPACK=0 USE_ITT=0 USE_C10D_UCC=0 USE_GLOO=0 USE_C10D_MPI=0 ONNX_ML=0 USE_TENSORPIPE=0 BUILD_NVFUSER=0 BUILD_FUNCTORCH=1 BUILD_SPLIT_CUDA=0 python3 setup.py develop
```

The following output is observed:
```
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/workspace/pytorch/torch/__init__.py", line 235, in <module>
    _load_global_deps()
  File "/workspace/pytorch/torch/__init__.py", line 194, in _load_global_deps
    raise err
  File "/workspace/pytorch/torch/__init__.py", line 175, in _load_global_deps
    ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL)
  File "/usr/lib/python3.10/ctypes/__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /workspace/pytorch/torch/lib/libtorch_global_deps.so: cannot open shared object file: No such file or directory
```
Running with ` TORCH_USE_RTLD_GLOBAL=1` to bypass the above failure seems to indicate that something is being loaded twice:
```
>>> import torch
terminate called after throwing an instance of 'c10::Error'
  what():  Tried to register multiple backend fallbacks for the same dispatch key Conjugate; previous registration registered at /workspace/pytorch/aten/src/ATen/ConjugateFallback.cpp:17, new registration registered at /workspace/pytorch/aten/src/ATen/ConjugateFallback.cpp:17
Exception raised from registerFallback at /workspace/pytorch/aten/src/ATen/core/dispatch/Dispatcher.cpp:401 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) + 0x98 (0xfffe1caf24d8 in /workspace/pytorch/torch/lib/libshm.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) + 0xf8 (0xfffe16cbdd28 in /workspace/pytorch/torch/lib/libshm.so)
frame #2: c10::Dispatcher::registerFallback(c10::DispatchKey, c10::KernelFunction, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) + 0x558 (0xfffe27e16a88 in /workspace/pytorch/torch/lib/libtorch_python.so)
frame #3: torch::Library::_fallback(torch::CppFunction&&) & + 0x1c4 (0xfffe27e53e54 in /workspace/pytorch/torch/lib/libtorch_python.so)
frame #4: <unknown function> + 0x21b8790 (0xfffe27bd8790 in /workspace/pytorch/torch/lib/libtorch_python.so)
frame #5: <unknown function> + 0x21bc00c (0xfffe27bdc00c in /workspace/pytorch/torch/lib/libtorch_python.so)
frame #6: <unknown function> + 0x16824d4 (0xfffe270a24d4 in /workspace/pytorch/torch/lib/libtorch_python.so)
frame #7: <unknown function> + 0x5624 (0xfffe37405624 in /lib/ld-linux-aarch64.so.1)
frame #8: <unknown function> + 0x572c (0xfffe3740572c in /lib/ld-linux-aarch64.so.1)
frame #9: _dl_catch_exception + 0xd0 (0xfffe3722d1f0 in /usr/lib/aarch64-linux-gnu/libc.so.6)
frame #10: <unknown function> + 0xbf5c (0xfffe3740bf5c in /lib/ld-linux-aarch64.so.1)
frame #11: _dl_catch_exception + 0x78 (0xfffe3722d198 in /usr/lib/aarch64-linux-gnu/libc.so.6)
frame #12: <unknown function> + 0xc2fc (0xfffe3740c2fc in /lib/ld-linux-aarch64.so.1)
frame #13: <unknown function> + 0x796e4 (0xfffe371796e4 in /usr/lib/aarch64-linux-gnu/libc.so.6)
frame #14: _dl_catch_exception + 0x78 (0xfffe3722d198 in /usr/lib/aarch64-linux-gnu/libc.so.6)
frame #15: _dl_catch_error + 0x40 (0xfffe3722d260 in /usr/lib/aarch64-linux-gnu/libc.so.6)
frame #16: <unknown function> + 0x791c0 (0xfffe371791c0 in /usr/lib/aarch64-linux-gnu/libc.so.6)
frame #17: dlopen + 0x54 (0xfffe37179784 in /usr/lib/aarch64-linux-gnu/libc.so.6)
<omitting python frames>

Aborted (core dumped)
```

### Versions

(upstream source build)
```
Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (aarch64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.27.5
Libc version: glibc-2.35

Python version: 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.2.0-1008-nvidia-64k-aarch64-with-glibc2.35
Is CUDA available: N/A
CUDA runtime version: 12.2.140
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: GPU 0: NVIDIA Graphics Device
Nvidia driver version: 535.115
```

cc @malfet @seemethere