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

Configuring SwiftCompile for a static swift-library does not work #1090

Open bric3 opened 3 years ago

bric3 commented 3 years ago

Following the official documentation (7.2) on swift project on tweaking compiler options, I expected that adding withType<SwiftCompile>().configureEach {} would work but it didn't

Expected Behavior

The configure all work as documented / expected.

tasks.withType<SwiftCompile>().configureEach {
  optimized.set(false)
  debuggable.set(false)
}

Current Behavior

I noticed this on the optimized and debuggable flags in particular, turning them off (with false) isn't applied:

$  ./gradlew :lib:assembleRelease --info | grep swiftc
Starting process 'command '/usr/bin/swiftc''. Working directory: /usr/bin Command: /usr/bin/swiftc --version
Successfully started process 'command '/usr/bin/swiftc''
Starting process 'command '/usr/bin/swiftc''. Working directory: /Users/brice.dutheil/opensource/sandbox/to-be-removed/lib/build/obj/main/release Command: /usr/bin/swiftc -sdk /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -parse-as-library /Users/brice.dutheil/opensource/sandbox/to-be-removed/lib/src/main/swift/Hello.swift -module-name Lib -emit-module-path /Users/brice.dutheil/opensource/sandbox/to-be-removed/lib/build/modules/main/release/Lib.swiftmodule -incremental -emit-dependencies -emit-object -g -O -swift-version 5 -output-file-map /Users/brice.dutheil/opensource/sandbox/to-be-removed/lib/build/obj/main/release/output-file-map.json
Successfully started process 'command '/usr/bin/swiftc''

Notice the -g (debug symbols) and -O (optimizations) flags.

Context

When switching shell build scripts using a simpler commands like this

swiftc something.swift -emit-library -o libsomething.dylib

to gradle I noticed awkward code behavior due to optimization turned on.

Steps to Reproduce

This can be reproduced using the a simple gradle init (or via the linked project.zip)

gradle init ``` Select type of project to generate: 1: basic 2: application 3: library 4: Gradle plugin Enter selection (default: basic) [1..4] 3 Select implementation language: 1: C++ 2: Groovy 3: Java 4: Kotlin 5: Scala 6: Swift Enter selection (default: Java) [1..6] 6 Select build script DSL: 1: Groovy 2: Kotlin Enter selection (default: Groovy) [1..2] 2 Project name (default: to-be-removed): > Task :init Get more help with your project: https://docs.gradle.org/7.2/samples/sample_building_swift_libraries.html BUILD SUCCESSFUL in 15s 2 actionable tasks: 2 executed ```

And adding this to lib/build.gradle.kts

+ tasks.withType(SwiftCompile::class.java).configureEach {
+   optimized.set(false)
+   debuggable.set(false)
+ }

Then run

$ ./gradlew :lib:assembleRelease --info | grep swiftc

Eventually use the outputs.upToDateWhen { false } to force the compilation to re-run.

Workaround

At this point the workaround is to define these flags on the specific variant tasks.

  library {
    // ...
+   binaries.configureEach(SwiftSharedLibrary::class.java) {
+     compileTask.get().run {
+       optimized.set(false)
+       debuggable.set(false)
+     }
+   }
  }

Your Environment

Build scan URL: https://scans.gradle.com/s/jlyikcvpo6rto

$ ./gradlew --version

------------------------------------------------------------
Gradle 7.2
------------------------------------------------------------

Build time:   2021-08-17 09:59:03 UTC
Revision:     a773786b58bb28710e3dc96c4d1a7063628952ad

Kotlin:       1.5.21
Groovy:       3.0.8
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          17 (Amazon.com Inc. 17+35-LTS)
OS:           Mac OS X 11.6 x86_64

$ swiftc --version
Apple Swift version 5.5 (swiftlang-1300.0.31.1 clang-1300.0.29.1)
Target: x86_64-apple-darwin20.6.0
bric3 commented 3 years ago

Ah sorry this issue should be moved over https://github.com/gradle/gradle-native/issues