Repository: pytorch
Issue: Issue with Protoc while building PyTorch for RISC-V
Labels: module: build,triaged

### 🐛 Describe the bug

I am trying to build PyTorch using the PR #99468 on x86_64 for riscv64. I have installed protoc for risc-v using apt and before starting the setup.py script I exported: ` CMAKE_TOOLCHAIN_FILE=/home/nemanja/Desktop/riscv-toolchain.cmake`, `USE_KINETO=0`, `USE_SYSTEM_SLEEF=ON`, `BUILD_CUSTOM_PROTOBUF=OFF`. My toolchain file looks like this:
```
#riscv-toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR riscv64)

set(CMAKE_C_COMPILER "/usr/bin/riscv64-linux-gnu-gcc")
set(CMAKE_CXX_COMPILER "/usr/bin/riscv64-linux-gnu-g++")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=rv64imafdc -mabi=lp64d")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=rv64imafdc -mabi=lp64d -std=c++17")
```

### Error

`[1410/1850] Linking CXX shared library lib/libtorch_cpu.so
FAILED: lib/libtorch_cpu.so 
: && /usr/bin/riscv64-linux-gnu-g++ -fPIC -march=rv64imafdc -mabi=lp64d -march=rv64imafdc -mabi=lp64d -D_GLIBCXX_USE_CXX11_ABI=0 -fabi-version=11 -fvisibility-inlines-hidden -DNDEBUG -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -O2 -fPIC -Wall -Wextra -Werror=return-type -Werror=non-virtual-dtor -Werror=bool-operation -Wnarrowing -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-unused-parameter -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic -Wno-error=old-style-cast -Wno-invalid-partial-specialization -Wno-unused-private-field -Wno-aligned-allocation-unavailable -Wno-missing-braces -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Werror=cast-function-type -Wno-stringop-overflow -O3 -DNDEBUG -DNDEBUG  -Wl,--no-as-needed -rdynamic -shared -Wl,-soname,libtorch_cpu.so -o lib/libtorch_cpu.so caffe2/CMakeFiles/torch_cpu.dir/__/torch/csrc/api/src/serialize/output-archive.cpp.o  -Wl,-rpath,/home/nemanja/Desktop/pytorch/build/lib:/usr/local/lib:  /usr/local/lib/libprotobuf.a  lib/libc10.so  lib/libcpuinfo.a  lib/libtensorpipe.a  lib/libgloo.a  lib/libfoxi_loader.a  -lrt  -lgcc_s  -lgcc  -ldl  -lrt  -lm  lib/libcpuinfo.a  -lsleef  -Wl,--whole-archive,"/home/nemanja/Desktop/pytorch/build/lib/libcaffe2_protos.a" -Wl,--no-whole-archive  lib/libclog.a  /usr/lib/gcc-cross/riscv64-linux-gnu/9/libgomp.so  /usr/riscv64-linux-gnu/lib/libpthread.so  /usr/lib/gcc-cross/riscv64-linux-gnu/9/libatomic.so  lib/libtensorpipe_uv.a  -lpthread  -lrt  -ldl  -Wl,--whole-archive,"/home/nemanja/Desktop/pytorch/build/lib/libonnx.a" -Wl,--no-whole-archive  lib/libonnx_proto.a  /usr/local/lib/libprotobuf.a  -lpthread  /usr/local/lib/libz.so  /usr/local/lib/libabsl_log_internal_check_op.a  /usr/local/lib/libabsl_leak_check.a  /usr/local/lib/libabsl_die_if_null.a  /usr/local/lib/libabsl_log_internal_conditions.a  /usr/local/lib/libabsl_log_internal_message.a  /usr/local/lib/libabsl_log_internal_nullguard.a  /usr/local/lib/libabsl_examine_stack.a  /usr/local/lib/libabsl_log_internal_format.a  /usr/local/lib/libabsl_log_internal_proto.a  /usr/local/lib/libabsl_log_internal_log_sink_set.a  /usr/local/lib/libabsl_log_sink.a  /usr/local/lib/libabsl_log_entry.a  /usr/local/lib/libabsl_flags.a  /usr/local/lib/libabsl_flags_internal.a  /usr/local/lib/libabsl_flags_marshalling.a  /usr/local/lib/libabsl_flags_reflection.a  /usr/local/lib/libabsl_flags_config.a  /usr/local/lib/libabsl_flags_program_name.a  /usr/local/lib/libabsl_flags_private_handle_accessor.a  /usr/local/lib/libabsl_flags_commandlineflag.a  /usr/local/lib/libabsl_flags_commandlineflag_internal.a  /usr/local/lib/libabsl_log_initialize.a  /usr/local/lib/libabsl_log_globals.a  /usr/local/lib/libabsl_log_internal_globals.a  /usr/local/lib/libabsl_raw_hash_set.a  /usr/local/lib/libabsl_hash.a  /usr/local/lib/libabsl_city.a  /usr/local/lib/libabsl_low_level_hash.a  /usr/local/lib/libabsl_hashtablez_sampler.a  /usr/local/lib/libabsl_statusor.a  /usr/local/lib/libabsl_status.a  /usr/local/lib/libabsl_cord.a  /usr/local/lib/libabsl_cordz_info.a  /usr/local/lib/libabsl_cord_internal.a  /usr/local/lib/libabsl_cordz_functions.a  /usr/local/lib/libabsl_exponential_biased.a  /usr/local/lib/libabsl_cordz_handle.a  /usr/local/lib/libabsl_crc_cord_state.a  /usr/local/lib/libabsl_crc32c.a  /usr/local/lib/libabsl_crc_internal.a  /usr/local/lib/libabsl_crc_cpu_detect.a  /usr/local/lib/libabsl_bad_optional_access.a  /usr/local/lib/libabsl_str_format_internal.a  /usr/local/lib/libabsl_strerror.a  /usr/local/lib/libabsl_synchronization.a  /usr/local/lib/libabsl_stacktrace.a  /usr/local/lib/libabsl_symbolize.a  /usr/local/lib/libabsl_debugging_internal.a  /usr/local/lib/libabsl_demangle_internal.a  /usr/local/lib/libabsl_graphcycles_internal.a  /usr/local/lib/libabsl_kernel_timeout_internal.a  /usr/local/lib/libabsl_malloc_internal.a  /usr/local/lib/libabsl_time.a  /usr/local/lib/libabsl_civil_time.a  /usr/local/lib/libabsl_time_zone.a  /usr/local/lib/libabsl_bad_variant_access.a  /usr/local/lib/libutf8_validity.a  /usr/local/lib/libabsl_strings.a  /usr/local/lib/libabsl_int128.a  /usr/local/lib/libabsl_string_view.a  /usr/local/lib/libabsl_throw_delegate.a  /usr/local/lib/libabsl_strings_internal.a  /usr/local/lib/libabsl_base.a  -pthread  /usr/local/lib/libabsl_raw_logging_internal.a  /usr/local/lib/libabsl_log_severity.a  /usr/local/lib/libabsl_spinlock_wait.a  -lrt && :
/usr/lib/gcc-cross/riscv64-linux-gnu/9/../../../../riscv64-linux-gnu/bin/ld: /usr/local/lib/libprotobuf.a(arena.cc.o): Relocations in generic ELF (EM: 62)
/usr/lib/gcc-cross/riscv64-linux-gnu/9/../../../../riscv64-linux-gnu/bin/ld: /usr/local/lib/libprotobuf.a(arena.cc.o): Relocations in generic ELF (EM: 62)
/usr/lib/gcc-cross/riscv64-linux-gnu/9/../../../../riscv64-linux-gnu/bin/ld: /usr/local/lib/libprotobuf.a(arena.cc.o): Relocations in generic ELF (EM: 62)
/usr/lib/gcc-cross/riscv64-linux-gnu/9/../../../../riscv64-linux-gnu/bin/ld: /usr/local/lib/libprotobuf.a(arena.cc.o): Relocations in generic ELF (EM: 62)
/usr/lib/gcc-cross/riscv64-linux-gnu/9/../../../../riscv64-linux-gnu/bin/ld: /usr/local/lib/libprotobuf.a(arena.cc.o): Relocations in generic ELF (EM: 62)
/usr/lib/gcc-cross/riscv64-linux-gnu/9/../../../../riscv64-linux-gnu/bin/ld: /usr/local/lib/libprotobuf.a(arena.cc.o): Relocations in generic ELF (EM: 62)
/usr/lib/gcc-cross/riscv64-linux-gnu/9/../../../../riscv64-linux-gnu/bin/ld: /usr/local/lib/libprotobuf.a(arena.cc.o): Relocations in generic ELF (EM: 62)
/usr/lib/gcc-cross/riscv64-linux-gnu/9/../../../../riscv64-linux-gnu/bin/ld: /usr/local/lib/libprotobuf.a(arena.cc.o): Relocations in generic ELF (EM: 62)
/usr/lib/gcc-cross/riscv64-linux-gnu/9/../../../../riscv64-linux-gnu/bin/ld: /usr/local/lib/libprotobuf.a: error adding symbols: file in wrong format
collect2: error: ld returned 1 exit status
[1415/1850] Building CXX object caffe2/CMakeFiles/MaybeOwned_test.dir/__/aten/src/ATen/test/MaybeOwned_test.cpp.o
ninja: build stopped: subcommand failed.
`

When I try and build PyTorch without  `BUILD_CUSTOM_PROTOBUF=OFF`, i get this error:

`[ 23%] Built target libprotoc
[ 23%] Building CXX object third_party/protobuf/cmake/CMakeFiles/protoc.dir/__/src/google/protobuf/compiler/main.cc.o
[ 25%] Linking CXX executable ../../../bin/protoc
[ 25%] Built target protoc
[ 25%] Running C++/Python protocol buffer compiler on /home/nemanja/Desktop/pytorch/caffe2/proto/torch.proto
[ 25%] Running C++/Python protocol buffer compiler on /home/nemanja/Desktop/pytorch/caffe2/proto/caffe2.proto
/bin/sh: 1: protobuf::protoc: not found
make[2]: *** [caffe2/proto/CMakeFiles/Caffe2_PROTO.dir/build.make:76: caffe2/proto/torch.pb.cc] Error 127
make[2]: *** Waiting for unfinished jobs....
/bin/sh: 1: protobuf::protoc: not found
make[2]: *** [caffe2/proto/CMakeFiles/Caffe2_PROTO.dir/build.make:90: caffe2/proto/caffe2.pb.cc] Error 127
[ 25%] Running C++ protocol buffer compiler on /home/nemanja/Desktop/pytorch/build/third_party/onnx/onnx/onnx_onnx_torch-ml.proto
make[1]: *** [CMakeFiles/Makefile2:6034: caffe2/proto/CMakeFiles/Caffe2_PROTO.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
/bin/sh: 1: ../../bin/protoc-3.13.0.0: Exec format error
make[2]: *** [third_party/onnx/CMakeFiles/gen_onnx_proto.dir/build.make:74: third_party/onnx/onnx/onnx_onnx_torch-ml.pb.cc] Error 2
make[1]: *** [CMakeFiles/Makefile2:2446: third_party/onnx/CMakeFiles/gen_onnx_proto.dir/all] Error 2
[ 25%] Linking CXX executable ../bin/vec_test_all_types_DEFAULT
[ 25%] Built target vec_test_all_types_DEFAULT
make: *** [Makefile:146: all] Error 2
`


### Versions

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 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Clang version: Could not collect
CMake version: version 3.28.1
Libc version: glibc-2.31

Python version: 3.11.7 (main, Dec 12 2023, 09:51:44) [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.15.0-91-generic-x86_64-with-glibc2.31
Is CUDA available: N/A
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Byte Order:                         Little Endian
Address sizes:                      39 bits physical, 48 bits virtual
CPU(s):                             6
On-line CPU(s) list:                0-5
Thread(s) per core:                 1
Core(s) per socket:                 6
Socket(s):                          1
NUMA node(s):                       1
Vendor ID:                          GenuineIntel
CPU family:                         6
Model:                              140
Model name:                         11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
Stepping:                           1
CPU MHz:                            1689.599
BogoMIPS:                           3379.19
Hypervisor vendor:                  KVM
Virtualization type:                full
L1d cache:                          288 KiB
L1i cache:                          192 KiB
L2 cache:                           7,5 MiB
L3 cache:                           72 MiB
NUMA node0 CPU(s):                  0-5
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Mitigation; Enhanced IBRS
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Vulnerable
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Enhanced IBRS, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 movbe popcnt aes rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ibrs_enhanced fsgsbase bmi1 bmi2 invpcid rdseed clflushopt md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] numpy==1.26.2
[conda] Could not collect

- riscv64-linux-gnu-g++ (Ubuntu 9.4.0-1ubuntu1 20.04) 9.4.0
- riscv64-linux-gnu-gcc (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0
- cmake version 3.28.1
- libprotoc 3.21.12

cc @malfet @seemethere