Repository: pytorch
Issue: `onnxifi_load` symbol undefined in `libtorch_cpu.a` library built from source
Labels: triaged

## 🐛 Bug

When building `libtorch_cpu.a` library from source manually, the generated `libtorch_cpu.a` contains undefined symbol `onnxifi_load` and thus would break some certain usage.

## To Reproduce

Steps to reproduce the behavior:

1. Build libtorch using the following command:

	```shell
	mkdir -p ~/libtorch_build && cd ~/libtorch_build
	cmake \
		-DCMAKE_INSTALL_PREFIX=$HOME/libtorch_install -DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_C_COMPILER=gcc-9 -DCMAKE_CXX_COMPILER=g++-9 \
		-DPYTHON_EXECUTABLE=$(which python) -DBUILD_BINARY=ON -DBUILD_DOCS=ON -DBUILD_PYTHON=OFF -DBUILD_TEST=OFF -DINSTALL_TEST=OFF -DBUILD_SHARED_LIBS=OFF\
		-DUSE_CUDA=OFF -DUSE_CUDNN=OFF -DUSE_LEVELDB=OFF -DUSE_NCCL=OFF -DUSE_NNPACK=OFF -DUSE_NVRTC=OFF \
		-DUSE_OBSERVERS=ON -DUSE_ZMQ=OFF -DUSE_DISTRIBUTED=ON -DUSE_MPI=OFF -DUSE_TENSORPIPE=OFF -DUSE_TBB=ON \
		~/pytorch
	make -j16
	make install
	```

2. Try to consume this libtorch library using the following command:

	```shell
	touch ~/test1.cpp
	g++-9 -o ~/test1.o ~/test1.cpp
	g++-9 -fPIC -shared -o ~/test1.so ~/test1.o \
		-Wl,--whole-archive,~/libtorch_install/install/lib/libc10.a \
		-Wl,--whole-archive,~/libtorch_install/install/lib/libtorch_cpu.a \
		-Wl,--whole-archive,~/libtorch_install/install/lib/libpytorch_qnnpack.a \
		-Wl,--whole-archive,~/libtorch_install/install/lib/libXNNPACK.a \
		-Wl,--whole-archive,~/libtorch_install/install/lib/libpthreadpool.a \
		-Wl,--whole-archive,~/libtorch_install/install/lib/libcpuinfo.a \
		-Wl,--whole-archive,~/libtorch_install/install/lib/libclog.a \
		-Wl,--no-whole-archive \
		~/libtorch_install/install/lib/libtorch.a \
		~/libtorch_install/install/lib/libc10.a \
		~/libtorch_install/install/lib/libtorch_cpu.a \
		~/libtorch_install/install/lib/libpytorch_qnnpack.a \
		~/libtorch_install/install/lib/libXNNPACK.a \
		~/libtorch_install/install/lib/libpthreadpool.a \
		~/libtorch_install/install/lib/libcpuinfo.a \
		~/libtorch_install/install/lib/libclog.a
	```

3. The commands in step 2 will report the following error:

	```
	/usr/bin/ld: /home/gnaggnoyil/libtorch_install/lib/libtorch_cpu.a(onnxifi_init.cc.o): in function `std::call_once<caffe2::onnx::initOnnxifiLibrary()::{lambda()#1}>(std::once_flag&, caffe2::onnx::initOnnxifiLibrary()::{lambda()#1}&&)::{lambda()#2}::_FUN()':
	onnxifi_init.cc:(.text+0x27): undefined reference to `onnxifi_load'
	/usr/bin/ld: test1.so: hidden symbol `onnxifi_load' isn't defined
	/usr/bin/ld: final link failed: bad value
	collect2: error: ld returned 1 exit status
	```

## Expected behavior

The commands mentioned in step 2 shall success and produce `~/test1.so` without any errors.

## Environment

 - PyTorch Version (e.g., 1.0): current master(commit 91c80d) and tag/v1.6.0
 - OS (e.g., Linux): Archlinux 20200816
 - How you installed PyTorch (`conda`, `pip`, source): source
 - Build command you used (if compiling from source): steps mentioned in reproduction steps
 - Python version: 3.8, installed through `pacman -S python`
 - CUDA/cuDNN version: CUDA 11.0, cuDNN 8.0.0, installed through `pacman -S cuda cudnn`
 - GPU models and configuration: GPU 0: GeForce GTX 1080

The compiler I'm using is `g++-9` installed through `pacman -S gcc-9`, as Archlinux's current main compiler(gcc 10.1) is having trouble building libtorch static library.

No conda environment exists in the environment.

## Additional context

Note that CMake explicitly specifies that when building a `STATIC` library target, all of its dependencies will NOT be linked to the generated static library file.

(I remembered there'are discussions in CMake's gitlab repo's issue section about why such decision was made yet I could not recall the exact issue No.)

The command `nm -C ~/libtorch_install/lib/libtorch_cpu.a | grep onnxifi_load` gives the following results:

<pre>                U <font color="#FF5555"><b>onnxifi_load</b></font></pre>

I also checked the verbose output of `make install` command of the above libtorch build and found there aren't any onnx or foxi library being installed, yet there are `libfoxi_loader.a`, `libonnx_proto.a` and `libonnx.a` exists in the build dir.

I tried to remove the `EXCLUDE_ALL` options from `add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/onnx EXCLUDE_FROM_ALL)` and `add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/foxi EXCLUDE_FROM_ALL)` in `cmake/Dependencies.cmake`, however that makes foxi fail to build, reporting a conflict declaration of `onnxInitGraph` as described in https://github.com/houseroad/foxi/pull/15#issuecomment-671689079

cc @houseroad @spandantiwari @lara-hdr @BowenBao @neginraoof