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

Use the right compiler flag on the C++ static library linked inside Swift executable #1009

Open lacasseio opened 5 years ago

lacasseio commented 5 years ago

Expected Behavior

Starting with Swift 4.1, executables are compiled with -pie flag. The C++ static library will need to be compiled with -fPIC on Linux. On macOS, clang already compiles with -fPIC flag.

Current Behavior

Linux doesn't default with to the position independent code flags.

Context

See https://github.com/gradle/gradle/pull/9188

Steps to Reproduce (for bugs)

Your Environment

lacasseio commented 5 years ago

The discussion around this issue is we currently mixed languages using 2 separated projects. Either we allow mixing languages in the same project or we have a way to automatically create variants or we model the flags better. All three solution aren't mutually exclusive.

adammurdoch commented 5 years ago

Mixing languages in the same project only solves the problem for people who happen to be implementing both parts. It doesn't work for situations where different teams are responsible for the different pieces. It could be considered a temporary work around, but it's not really any better than asking people to simply add the required flags to the static variant of the C++ library.

I think there are 2 separate pieces here. Firstly, dependency resolution should understand the difference between position independent and position dependent variants, and choose the correct variant of the library or fail if there is no such variant. Secondly, we should change the convention for libraries so that there is by default a position independent variant available for static linkage on Linux (but not the other platforms). And later, if people ask for it, a way to define which combinations of position independent and position dependent they would like to produce for a library.

lacasseio commented 5 years ago

I agree it's a bucket of things that needs to be considered in a more detailed plan that came out of the PR for fixing tests. Thanks for the input, hoping to get there soon.

zosrothko commented 5 years ago

Another issue about the compiler flags on Windows at least on the new cpp-plugin with mixing C and C++ sources in a same project. Currently, the cpp-plugin adds implicitly the /TP compiler flag even for compiler C code. This does not work as some C code could old C code with a Kernighan & Ritchie syntax for the parameters type. One user's case is Poco/Foundation/src, where some old style 'pcre' C source code are along with Poco C++ source code.

lacasseio commented 5 years ago

@zosrothko The mixing of C and C++ is a separated issue. It's expected in the scenario you describe as it's using the C++ task instead of the C task.