Repository: pytorch
Issue: Can't build PyTorch 2.1 from source by GCC 13.2 on M1 MacOS
Labels: module: build,module: docs,triaged,module: macos

### 🐛 Describe the bug

I want to build Pytorch 2.1 from source via GCC13.2. I followed the installation instructions.

And, I set the environment variables as follows:

```shell
-- The CXX compiler identification is GNU 13.2.0
-- The C compiler identification is GNU 13.2.0

--   CMake version                     : 3.27.7
--   CMake command                     : /opt/homebrew/Cellar/cmake/3.27.7/bin/cmake
--   System                            : Darwin
--   C++ compiler                      : /opt/homebrew/bin/g++-13
--   C++ compiler version              : 13.2.0
--   CXX flags                         : -std=c++20 -march=native -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -Wnon-virtual-dtor
--   Build type                        : Release
```

Then the error emitted is as follows:

```shell
CMake Error at /opt/homebrew/Cellar/cmake/3.27.7/share/cmake/Modules/CMakeTestOBJCXXCompiler.cmake:63 (message):
  The Objective-C++ compiler

    "/opt/homebrew/bin/g++-13"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: '/Users/wyh/dev/pytorch_gcc/build/CMakeFiles/CMakeScratch/TryCompile-Yf0ZXD'

    Run Build Command(s): /opt/homebrew/bin/ninja -v cmTC_1e0d6
    [1/2] /opt/homebrew/bin/g++-13   -x objective-c++ -fopenmp -DUSE_MPS -fno-objc-arc  -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -fPIE -o CMakeFiles/cmTC_1e0d6.dir/testObjCXXCompiler.mm.o -c /Users/wyh/dev/pytorch_gcc/build/CMakeFiles/CMakeScratch/TryCompile-Yf0ZXD/testObjCXXCompiler.mm
    FAILED: CMakeFiles/cmTC_1e0d6.dir/testObjCXXCompiler.mm.o
    /opt/homebrew/bin/g++-13   -x objective-c++ -fopenmp -DUSE_MPS -fno-objc-arc  -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -fPIE -o CMakeFiles/cmTC_1e0d6.dir/testObjCXXCompiler.mm.o -c /Users/wyh/dev/pytorch_gcc/build/CMakeFiles/CMakeScratch/TryCompile-Yf0ZXD/testObjCXXCompiler.mm
    g++-13: error: unrecognized command-line option '-fno-objc-arc'; did you mean '-fno-objc-gc'?
    ninja: build stopped: subcommand failed.

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  caffe2/CMakeLists.txt:859 (enable_language)
```

I looked at the contents of caffe2/CMakeLIsts.txt:859 and I think turning off USE_MPS and USE_PYTORCH_METAL will fix the problem. Indeed, this error is no longer issued. 

The project then starts building, but again emits the following error:

```shell
FAILED: confu-deps/pthreadpool/CMakeFiles/pthreadpool.dir/src/portable-api.c.o
/opt/homebrew/bin/gcc-13 -DFXDIV_USE_INLINE_ASSEMBLY=0 -DPTHREADPOOL_USE_FASTPATH=0 -I/Users/wyh/dev/pytorch_gcc/third_party/pthreadpool/src -I/Users/wyh/dev/pytorch_gcc/third_party/pthreadpool/include -I/Users/wyh/dev/pytorch_gcc/third_party/FXdiv/include -isystem /Users/wyh/dev/pytorch_gcc/third_party/protobuf/src -O3 -DNDEBUG -std=c11 -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -fPIC -MD -MT confu-deps/pthreadpool/CMakeFiles/pthreadpool.dir/src/portable-api.c.o -MF confu-deps/pthreadpool/CMakeFiles/pthreadpool.dir/src/portable-api.c.o.d -o confu-deps/pthreadpool/CMakeFiles/pthreadpool.dir/src/portable-api.c.o -c /Users/wyh/dev/pytorch_gcc/third_party/pthreadpool/src/portable-api.c
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/dispatch/dispatch.h:67,
                 from /Users/wyh/dev/pytorch_gcc/third_party/pthreadpool/src/threadpool-object.h:18,
                 from /Users/wyh/dev/pytorch_gcc/third_party/pthreadpool/src/portable-api.c:20:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/dispatch/queue.h:103:1: error: unknown type name 'dispatch_queue_t'
  103 | DISPATCH_DECL_SUBCLASS(dispatch_queue_global, dispatch_queue);

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/dispatch/queue.h:173:1: error: unknown type name 'dispatch_queue_serial_t'
  173 | DISPATCH_DECL_SUBCLASS(dispatch_queue_main, dispatch_queue_serial);

In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/dispatch/dispatch.h:68:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/dispatch/queue.h:272:18: error: unknown type name 'dispatch_queue_t'; did you mean 'dispatch_time_t'?
  272 | dispatch_async_f(dispatch_queue_t queue,


... 


/Users/wyh/dev/pytorch_gcc/third_party/pthreadpool/src/threadpool-object.h:661:9: error: unknown type name 'dispatch_semaphore_t'
  661 |         dispatch_semaphore_t execution_semaphore;

[192/5382] Building CXX object third_party/protobuf/cmake/CMakeFiles/libprotoc.dir/__/src/google/protobuf/compiler/js/js_generator.cc.o
ninja: build stopped: subcommand failed.
```

I wanna ask if PyTorch 2.1 supports building from source on M1Mac using GCC 13.2？How can I do it successfully? (PS: I can successfully build this project using clang. But I need to use GCC.)

### Versions

```shell
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: macOS 14.1 (arm64)
GCC version: Could not collect
Clang version: 17.0.4
CMake version: version 3.27.7
Libc version: N/A

Python version: 3.11.6 | packaged by conda-forge | (main, Oct  3 2023, 10:37:07) [Clang 15.0.7 ] (64-bit runtime)
Python platform: macOS-14.1-arm64-arm-64bit
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:
Apple M1 Pro

Versions of relevant libraries:
[pip3] numpy==1.26.0
[conda] numpy                     1.26.0          py311hb8f3215_0    conda-forge
```

cc @malfet @seemethere @svekars @carljparker @albanD