gradle / gradle-native

The home of Gradle's support for natively compiled languages
https://blog.gradle.org/introducing-the-new-cpp-plugins
Apache License 2.0
91 stars 8 forks source link

Gradle Native plugin cross-compile is broken #1031

Open dinocore1 opened 4 years ago

dinocore1 commented 4 years ago

Native library plugin do not allow building for diffrent hosts

Building a native library using the 'cpp-library' plugin does not allow targeting builds for hosts other than the current host. This is a very common senario when using a cross-compiler. For example using mingw on Linux targeting Windows builds. Consider the following example being run on Ubuntu machine with mingw installed.

plugins {
  id 'cpp-library'
}

model {
  toolChains {
    gcc(Gcc) {
      target('linux_x86')
      target('windows_x86') {
        CppCompiler.executable = 'mingw-g++'
        CCompiler.executable = 'mingw-gcc'
      }
    }
  }
}

library {
  targetMachines = [
    machines.windows.x86,
    machines.linux.x86
  ]
}

Expected Behavior

running gradle tasks should produce two sets of assemble and build tasks with naming convention following: assembleLinuxX86 and assembleWindowsX86

Current Behavior

Gradle creates assemble task targeting the local host machine only. It is impossible to cross-compile builds targeting Operating Systems other than the host.

Context

I work mainly with C/C++ developing cross-platform software. We target Windows, Linux, Mac, and Android. I really like to be able to use Maven for native builds just like its done in the Java ecosystem.

Steps to Reproduce

example.zip

Your Environment

https://gradle.com/s/vhbk6go52j5uo

lacasseio commented 4 years ago

I moved the issue to the native organization.

lhunath commented 4 years ago

I'm unsure what the purpose of targetMachines is if not to do this. The documentation seems to suggest that building for non-host targets is exactly what targetMachines is supposed to do, but it doesn't?

By default, Gradle will attempt to create a C++ binary variant for the host operating system and architecture. It is possible to override this by specifying the set of TargetMachine on the application or library script block:

I guess my question is - does targetMachines have any effect currently, if so, what, and is there any way to set up the currently missing pieces using an official gradle release (currently- v5.6.2) (eg. manually create variants)?

dinocore1 commented 4 years ago

setting targetMachines does not currently have any effect. I think I have tracked down the source of the problem to CppLibraryPlugin.java#L137.

It seems that tryToBuildOnHost(variantIdentity) returns false for cross-compile targets and thus does not add the shared or static libraries to the build.

I have fixed it with my pull request

lhunath commented 4 years ago

I should point out that setting targetMachines does have an effect in that gradle will build multiple host architectures if there are multiple architectures of the host's platform in the targetMachines list.

zrehak commented 2 years ago

Are there any updates/milestones when the C++ cross-compilation will be fixed in New C++ Plugin? Mind that the old C++ plugin is deprecated in the latest Gradle version.

hu-chia commented 3 months ago

any update?