Home > Software engineering >  Gradle fails building NativeLibrarySpec and/or NativeExecutableSpec on Apple CPU (M1)
Gradle fails building NativeLibrarySpec and/or NativeExecutableSpec on Apple CPU (M1)

Time:07-11

On Mac M1 if you try to build project with Native Component Module it would fail with the following error:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'hellonative'.
> Exception thrown while executing model rule: NativeComponentModelPlugin.Rules#createBinaries(TargetedNativeComponentInternal, PlatformResolvers, BuildTypeContainer, FlavorContainer, ServiceRegistry)
   > Invalid NativePlatform: osx_arm-v8

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 401ms

Example build.gradle could look as simple as:

plugins {                                                                       
  id 'cpp'                                                                    
}                                                                               
                                                                               
model {                                                                         
  components {                                                                
    hello(NativeExecutableSpec) {                                                                               
      sources {                                                           
        cpp {                                                           
          source {                                                     
            srcDir "src/cpp"                                           
            include "hello.cc"                                         
          }                                                            
        }                                                               
      }                                                                   
    }                                                                       
  }                                                                           
} 

Is there a way to fix this?

CodePudding user response:

Unfortunately, the current version of Gradle (7.4.2 as of now) cannot build native components on Mac M1.

There's a PR to fix this proposed for 7.6RC (https://github.com/gradle/gradle/pull/20310) but it isn't even merged yet.

Some insights can be found here: https://issueantenna.com/repo/gradle/gradle-native/issues/1096

The easiest way to fix this is to add target platform to the build.gradle and force clang to think target platform is Intel CPU:

plugins {                                                                       
  id 'cpp'                                                                    
}                                                                               
                                                                               
model {                                                                         
  components {                                                                
    hello(NativeExecutableSpec) {
      // Just add this line
      targetPlatform "osx_x86-64"                                                                             
      sources {                                                           
        cpp {                                                           
          source {                                                     
            srcDir "src/cpp"                                           
            include "hello.cc"                                         
          }                                                            
        }                                                               
      }                                                                   
    }                                                                       
  }                                                                           
} 

It does not change the Gradle behaviour for Intel CPUs and will actually build native ARM executables as no -arch flag is passed to Clang.

  • Related