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
92 stars 8 forks source link

Clang on Windows uses non-windows flag -fPIC #936

Open nablajs opened 5 years ago

nablajs commented 5 years ago

Using the Android Clang toolchain for a non-Linux build system compile (vanilla gradle + 'cpp' plugin) on Windows adds unsupported '-fPIC' command line argument.

Expected Behavior

No errors when invoking clang->g++ because of invalid arguments

Perhaps this check should check the building platform, not the target one?

See also: llvm patch (2016)

Current Behavior

An error message is produced when invoking the C++ compiler:

clang++.exe: error: unsupported option '-fPIC' for target 'x86_64-w64-windows-gnu'

Context

The goal here is to:

Steps to Reproduce (for bugs)

On Windows 10 with Visual Studio 2017 and Android NDK (r18b?) ...

Build android target as described in build.gradle below:

.\gradlew myJniAndroidDebugSharedLibrary 

build.gradle parts:

model {
    repositories {
        libs(PrebuiltLibraries) {
            myLib {
                headers.srcDir new File(myLibIncludeDir.toString())
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = new File(myLibPath.toString())
                }
            }
            jni {
                headers.srcDirs = [
                        new File(Paths.get(javaHome, "include").toString()),
                        new File(Paths.get(javaHome, "include", "win32").toString())
                ]
            }
        }
    }
    components {
        myJni(NativeLibrarySpec) {
            sources {
                cpp.lib library: 'myLib', linkage: 'api'
                cpp.lib library: 'jni', linkage: 'api'
            }
            targetPlatform 'android'
            targetPlatform 'win64'
        }
    }
    buildTypes {
        debug
        release
    }
    toolChains {
        visualCpp(VisualCpp) {
        }
        clang(Clang) {
            target('android') {
                path Paths.get(System.getenv("ANDROID_HOME"), "ndk-bundle", "toolchains",
                        "llvm", "prebuilt" , "windows-x86_64" , "bin").toString()
                cCompiler.executable = "clang.exe"
                cppCompiler.executable = "clang++.exe"
                assembler.executable = "llvm-as.exe"
                linker.executable = "clang++.exe"
            }
        }
    }
    // DOES NOT WORK TO CLEAR OUT COMPILER ARGS
    binaries {
        withType(NativeLibrarySpec) {
            if (toolchain in Clang) {
                cppCompiler.args ''
            }
        }
    }
    platforms {
        android {
            architecture 'arm'
            operatingSystem 'android'
        }
        win64 {
            architecture 'x86_64'
            operatingSystem 'windows'
        }
    }
}

Your Environment

Gradle details:

Build time:   2018-06-04 10:39:58 UTC
Revision:     9e1261240e412cbf61a5e3a5ab734f232b2f887d

Groovy:       2.4.12
Ant:          Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM:          1.8.0_181 (Oracle Corporation 25.181-b13)
OS:           Windows 10 10.0 amd64

Output of gradlew components:

Binaries
    Shared library 'myJni:android:debug:sharedLibrary'
        build using task: :myJniAndroidDebugSharedLibrary
        build type: build type 'debug'
        flavor: flavor 'default'
        target platform: platform 'android'
        tool chain: Tool chain 'clang' (Clang)
        shared library file: build\libs\myJni\shared\android\debug\libmyJni.so
devopsone commented 5 years ago

I'm facing the same problem with latest stable version (r19c) and Beta 3 (r20). I hope the next NDK release contains a working CLANG version to fix this issue, which currently is a blocker for me.

lacasseio commented 4 years ago

Unfortunately, Clang on Windows is not tested so there will be issues like this. The best way to work around this issue is to change the arguments using:

toolChains {
    clang(Clang) {
        target("android") {
            cppCompiler.withArguments { it.remove('-fPIC') }
        }
    }
}