xref: /aosp_15_r20/external/grpc-grpc-java/netty/shaded/build.gradle (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1*e07d83d3SAndroid Build Coastguard Workerimport com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
2*e07d83d3SAndroid Build Coastguard Workerimport com.github.jengelman.gradle.plugins.shadow.transformers.CacheableTransformer
3*e07d83d3SAndroid Build Coastguard Workerimport com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
4*e07d83d3SAndroid Build Coastguard Workerimport org.gradle.api.file.FileTreeElement
5*e07d83d3SAndroid Build Coastguard Workerimport shadow.org.apache.tools.zip.ZipOutputStream
6*e07d83d3SAndroid Build Coastguard Workerimport shadow.org.apache.tools.zip.ZipEntry
7*e07d83d3SAndroid Build Coastguard Worker
8*e07d83d3SAndroid Build Coastguard Workerplugins {
9*e07d83d3SAndroid Build Coastguard Worker    id "java"
10*e07d83d3SAndroid Build Coastguard Worker    id "maven-publish"
11*e07d83d3SAndroid Build Coastguard Worker
12*e07d83d3SAndroid Build Coastguard Worker    id "com.github.johnrengelman.shadow"
13*e07d83d3SAndroid Build Coastguard Worker    id "ru.vyarus.animalsniffer"
14*e07d83d3SAndroid Build Coastguard Worker}
15*e07d83d3SAndroid Build Coastguard Worker
16*e07d83d3SAndroid Build Coastguard Workerdescription = "gRPC: Netty Shaded"
17*e07d83d3SAndroid Build Coastguard Worker
18*e07d83d3SAndroid Build Coastguard WorkersourceSets { testShadow {} }
19*e07d83d3SAndroid Build Coastguard Worker
20*e07d83d3SAndroid Build Coastguard Workerdependencies {
21*e07d83d3SAndroid Build Coastguard Worker    implementation project(':grpc-netty')
22*e07d83d3SAndroid Build Coastguard Worker    runtimeOnly libraries.netty.tcnative,
23*e07d83d3SAndroid Build Coastguard Worker            libraries.netty.tcnative.classes
24*e07d83d3SAndroid Build Coastguard Worker    runtimeOnly (libraries.netty.tcnative) {
25*e07d83d3SAndroid Build Coastguard Worker        artifact {
26*e07d83d3SAndroid Build Coastguard Worker            classifier = "linux-x86_64"
27*e07d83d3SAndroid Build Coastguard Worker        }
28*e07d83d3SAndroid Build Coastguard Worker    }
29*e07d83d3SAndroid Build Coastguard Worker    runtimeOnly (libraries.netty.tcnative) {
30*e07d83d3SAndroid Build Coastguard Worker        artifact {
31*e07d83d3SAndroid Build Coastguard Worker            classifier = "linux-aarch_64"
32*e07d83d3SAndroid Build Coastguard Worker        }
33*e07d83d3SAndroid Build Coastguard Worker    }
34*e07d83d3SAndroid Build Coastguard Worker    runtimeOnly (libraries.netty.tcnative) {
35*e07d83d3SAndroid Build Coastguard Worker        artifact {
36*e07d83d3SAndroid Build Coastguard Worker            classifier = "osx-x86_64"
37*e07d83d3SAndroid Build Coastguard Worker        }
38*e07d83d3SAndroid Build Coastguard Worker    }
39*e07d83d3SAndroid Build Coastguard Worker    runtimeOnly (libraries.netty.tcnative) {
40*e07d83d3SAndroid Build Coastguard Worker        artifact {
41*e07d83d3SAndroid Build Coastguard Worker            classifier = "osx-aarch_64"
42*e07d83d3SAndroid Build Coastguard Worker        }
43*e07d83d3SAndroid Build Coastguard Worker    }
44*e07d83d3SAndroid Build Coastguard Worker    runtimeOnly (libraries.netty.tcnative) {
45*e07d83d3SAndroid Build Coastguard Worker        artifact {
46*e07d83d3SAndroid Build Coastguard Worker            classifier = "windows-x86_64"
47*e07d83d3SAndroid Build Coastguard Worker        }
48*e07d83d3SAndroid Build Coastguard Worker    }
49*e07d83d3SAndroid Build Coastguard Worker    runtimeOnly (libraries.netty.transport.epoll) {
50*e07d83d3SAndroid Build Coastguard Worker        artifact {
51*e07d83d3SAndroid Build Coastguard Worker            classifier = "linux-x86_64"
52*e07d83d3SAndroid Build Coastguard Worker        }
53*e07d83d3SAndroid Build Coastguard Worker    }
54*e07d83d3SAndroid Build Coastguard Worker    runtimeOnly (libraries.netty.transport.epoll) {
55*e07d83d3SAndroid Build Coastguard Worker        artifact {
56*e07d83d3SAndroid Build Coastguard Worker            classifier = "linux-aarch_64"
57*e07d83d3SAndroid Build Coastguard Worker        }
58*e07d83d3SAndroid Build Coastguard Worker    }
59*e07d83d3SAndroid Build Coastguard Worker    testShadowImplementation files(shadowJar),
60*e07d83d3SAndroid Build Coastguard Worker            project(':grpc-testing-proto'),
61*e07d83d3SAndroid Build Coastguard Worker            project(':grpc-testing'),
62*e07d83d3SAndroid Build Coastguard Worker            libraries.truth
63*e07d83d3SAndroid Build Coastguard Worker    shadow project(':grpc-netty').configurations.runtimeClasspath.allDependencies.matching {
64*e07d83d3SAndroid Build Coastguard Worker        it.group != 'io.netty'
65*e07d83d3SAndroid Build Coastguard Worker    }
66*e07d83d3SAndroid Build Coastguard Worker    signature libraries.signature.java
67*e07d83d3SAndroid Build Coastguard Worker    signature libraries.signature.android
68*e07d83d3SAndroid Build Coastguard Worker}
69*e07d83d3SAndroid Build Coastguard Worker
70*e07d83d3SAndroid Build Coastguard Workertasks.named("jar").configure {
71*e07d83d3SAndroid Build Coastguard Worker    // Must use a different archiveClassifier to avoid conflicting with shadowJar
72*e07d83d3SAndroid Build Coastguard Worker    archiveClassifier = 'original'
73*e07d83d3SAndroid Build Coastguard Worker}
74*e07d83d3SAndroid Build Coastguard Worker
75*e07d83d3SAndroid Build Coastguard Workertasks.named("shadowJar").configure {
76*e07d83d3SAndroid Build Coastguard Worker    archiveClassifier = null
77*e07d83d3SAndroid Build Coastguard Worker    dependencies {
78*e07d83d3SAndroid Build Coastguard Worker        include(project(':grpc-netty'))
79*e07d83d3SAndroid Build Coastguard Worker        include(dependency('io.netty:'))
80*e07d83d3SAndroid Build Coastguard Worker    }
81*e07d83d3SAndroid Build Coastguard Worker    exclude 'META-INF/maven/**'
82*e07d83d3SAndroid Build Coastguard Worker    relocate 'io.grpc.netty', 'io.grpc.netty.shaded.io.grpc.netty'
83*e07d83d3SAndroid Build Coastguard Worker    relocate 'io.netty', 'io.grpc.netty.shaded.io.netty'
84*e07d83d3SAndroid Build Coastguard Worker    // We have to be careful with these replacements as they must not match any
85*e07d83d3SAndroid Build Coastguard Worker    // string in NativeLibraryLoader, else they cause corruption. Note that
86*e07d83d3SAndroid Build Coastguard Worker    // this includes concatenation of string literals and constants.
87*e07d83d3SAndroid Build Coastguard Worker    relocate 'META-INF/native/libnetty', 'META-INF/native/libio_grpc_netty_shaded_netty'
88*e07d83d3SAndroid Build Coastguard Worker    relocate 'META-INF/native/netty', 'META-INF/native/io_grpc_netty_shaded_netty'
89*e07d83d3SAndroid Build Coastguard Worker    transform(NettyResourceTransformer.class)
90*e07d83d3SAndroid Build Coastguard Worker    mergeServiceFiles()
91*e07d83d3SAndroid Build Coastguard Worker}
92*e07d83d3SAndroid Build Coastguard Worker
93*e07d83d3SAndroid Build Coastguard Workerpublishing {
94*e07d83d3SAndroid Build Coastguard Worker    publications {
95*e07d83d3SAndroid Build Coastguard Worker        maven(MavenPublication) {
96*e07d83d3SAndroid Build Coastguard Worker            project.shadow.component(it)
97*e07d83d3SAndroid Build Coastguard Worker
98*e07d83d3SAndroid Build Coastguard Worker            // Empty jars are not published via withJavadocJar() and withSourcesJar()
99*e07d83d3SAndroid Build Coastguard Worker            artifact javadocJar
100*e07d83d3SAndroid Build Coastguard Worker            artifact sourcesJar
101*e07d83d3SAndroid Build Coastguard Worker
102*e07d83d3SAndroid Build Coastguard Worker            // Avoid confusing error message "class file for
103*e07d83d3SAndroid Build Coastguard Worker            // io.grpc.internal.AbstractServerImplBuilder not found"
104*e07d83d3SAndroid Build Coastguard Worker            // (https://github.com/grpc/grpc-java/issues/5881). This can be
105*e07d83d3SAndroid Build Coastguard Worker            // removed after https://github.com/grpc/grpc-java/issues/7211 is
106*e07d83d3SAndroid Build Coastguard Worker            // resolved.
107*e07d83d3SAndroid Build Coastguard Worker            pom.withXml {
108*e07d83d3SAndroid Build Coastguard Worker                asNode().dependencies.'*'.findAll() { dep ->
109*e07d83d3SAndroid Build Coastguard Worker                    dep.artifactId.text() == 'grpc-core'
110*e07d83d3SAndroid Build Coastguard Worker                }.each() { core ->
111*e07d83d3SAndroid Build Coastguard Worker                    core.scope*.value = "compile"
112*e07d83d3SAndroid Build Coastguard Worker                }
113*e07d83d3SAndroid Build Coastguard Worker            }
114*e07d83d3SAndroid Build Coastguard Worker        }
115*e07d83d3SAndroid Build Coastguard Worker    }
116*e07d83d3SAndroid Build Coastguard Worker}
117*e07d83d3SAndroid Build Coastguard Worker
118*e07d83d3SAndroid Build Coastguard Workertasks.register("testShadow", Test) {
119*e07d83d3SAndroid Build Coastguard Worker    testClassesDirs = sourceSets.testShadow.output.classesDirs
120*e07d83d3SAndroid Build Coastguard Worker    classpath = sourceSets.testShadow.runtimeClasspath
121*e07d83d3SAndroid Build Coastguard Worker}
122*e07d83d3SAndroid Build Coastguard Workertasks.named("compileTestShadowJava").configure {
123*e07d83d3SAndroid Build Coastguard Worker    options.compilerArgs = compileTestJava.options.compilerArgs
124*e07d83d3SAndroid Build Coastguard Worker    options.encoding = compileTestJava.options.encoding
125*e07d83d3SAndroid Build Coastguard Worker}
126*e07d83d3SAndroid Build Coastguard Worker
127*e07d83d3SAndroid Build Coastguard Workertasks.named("test").configure {
128*e07d83d3SAndroid Build Coastguard Worker    dependsOn tasks.named("testShadow")
129*e07d83d3SAndroid Build Coastguard Worker}
130*e07d83d3SAndroid Build Coastguard Worker
131*e07d83d3SAndroid Build Coastguard Worker/**
132*e07d83d3SAndroid Build Coastguard Worker * A Transformer which updates the Netty JAR META-INF/ resources to accurately
133*e07d83d3SAndroid Build Coastguard Worker * reference shaded class names.
134*e07d83d3SAndroid Build Coastguard Worker */
135*e07d83d3SAndroid Build Coastguard Worker@CacheableTransformer
136*e07d83d3SAndroid Build Coastguard Workerclass NettyResourceTransformer implements Transformer {
137*e07d83d3SAndroid Build Coastguard Worker
138*e07d83d3SAndroid Build Coastguard Worker    // A map of resource file paths to be modified
139*e07d83d3SAndroid Build Coastguard Worker    private Map<String, String> resources = [:]
140*e07d83d3SAndroid Build Coastguard Worker
141*e07d83d3SAndroid Build Coastguard Worker    @Override
142*e07d83d3SAndroid Build Coastguard Worker    boolean canTransformResource(FileTreeElement fileTreeElement) {
143*e07d83d3SAndroid Build Coastguard Worker        fileTreeElement.name.startsWith("META-INF/native-image/io.netty")
144*e07d83d3SAndroid Build Coastguard Worker    }
145*e07d83d3SAndroid Build Coastguard Worker
146*e07d83d3SAndroid Build Coastguard Worker    @Override
147*e07d83d3SAndroid Build Coastguard Worker    void transform(TransformerContext context) {
148*e07d83d3SAndroid Build Coastguard Worker        String updatedPath = context.path.replace("io.netty", "io.grpc.netty.shaded.io.netty")
149*e07d83d3SAndroid Build Coastguard Worker        String updatedContent = context.is.getText().replace("io.netty", "io.grpc.netty.shaded.io.netty")
150*e07d83d3SAndroid Build Coastguard Worker        resources.put(updatedPath, updatedContent)
151*e07d83d3SAndroid Build Coastguard Worker    }
152*e07d83d3SAndroid Build Coastguard Worker
153*e07d83d3SAndroid Build Coastguard Worker    @Override
154*e07d83d3SAndroid Build Coastguard Worker    boolean hasTransformedResource() {
155*e07d83d3SAndroid Build Coastguard Worker        resources.size() > 0
156*e07d83d3SAndroid Build Coastguard Worker    }
157*e07d83d3SAndroid Build Coastguard Worker
158*e07d83d3SAndroid Build Coastguard Worker    @Override
159*e07d83d3SAndroid Build Coastguard Worker    void modifyOutputStream(ZipOutputStream outputStream, boolean preserveFileTimestamps) {
160*e07d83d3SAndroid Build Coastguard Worker        for (resourceEntry in resources) {
161*e07d83d3SAndroid Build Coastguard Worker            ZipEntry entry = new ZipEntry(resourceEntry.key)
162*e07d83d3SAndroid Build Coastguard Worker            entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
163*e07d83d3SAndroid Build Coastguard Worker
164*e07d83d3SAndroid Build Coastguard Worker            outputStream.putNextEntry(entry)
165*e07d83d3SAndroid Build Coastguard Worker            outputStream.write(resourceEntry.value.getBytes())
166*e07d83d3SAndroid Build Coastguard Worker            outputStream.closeEntry()
167*e07d83d3SAndroid Build Coastguard Worker        }
168*e07d83d3SAndroid Build Coastguard Worker    }
169*e07d83d3SAndroid Build Coastguard Worker}
170