Gradle失败了Apple CPU(M1)上的NativelibrarySpec和/或本机ExecutableSpec

发布于 2025-02-13 18:04:00 字数 2140 浏览 0 评论 0原文

在Mac M1上,如果您尝试使用本机组件模块构建项目,则会因以下错误而失败:

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

示例build.gradle看起来很简单:

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

有没有办法解决此问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光礼记 2025-02-20 18:04:00

不幸的是,当前版本的Gradle(截至目前为7.4.2)无法在Mac M1上构建本机组件。

有一个解决此问题的PR( https://github.com/gradle.com/gradle/gradle/gradle/gradle/gradle /拉/20310 ),但甚至还没有合并。

可以在此处找到一些见解: https:// https://issueantenna.com/repo/repo/gradle /gradle-native/eskity/1096

解决此问题的最简单方法是将目标平台添加到build.gradle和强迫Clang认为目标平台是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"                                         
          }                                                            
        }                                                               
      }                                                                   
    }                                                                       
  }                                                                           
} 

它不会更改Intel CPU的Gradle行为,并且实际上将构建本机ARM可执行文件,为no -ark flag被传给了Clang。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文