From d1d8f17e9e942d1c42862320f8ab9419336f5b29 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Mon, 29 Jul 2024 11:30:29 -0700 Subject: [PATCH 1/2] Move kotlin stdlib after javac jars A future CL is going to treat local jars separately from dependency jars. Rearrange the order of the kotlin standard library jars to come after the local jars produced by javac, which will better match the future ordering and ease ninja file comparisions. Bug: 308016794 Test: all soong tests pass Flag: EXEMPT refactor Change-Id: I9ed6a649350451bf1788077752db5222f50c0247 --- java/base.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/java/base.go b/java/base.go index 65a8b30c8..9c1d8fb8e 100644 --- a/java/base.go +++ b/java/base.go @@ -1211,6 +1211,7 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars, extraClasspath var kotlinJars android.Paths var kotlinHeaderJars android.Paths + var kotlinExtraJars android.Paths // Prepend extraClasspathJars to classpath so that the resource processor R.jar comes before // any dependencies so that it can override any non-final R classes from dependencies with the @@ -1321,17 +1322,8 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars, extraClasspath kotlinJars = append(kotlinJars, kotlinJarPath) kotlinHeaderJars = append(kotlinHeaderJars, kotlinHeaderJar) - - // Jar kotlin classes into the final jar after javac - if BoolDefault(j.properties.Static_kotlin_stdlib, true) { - kotlinJars = append(kotlinJars, deps.kotlinStdlib...) - kotlinJars = append(kotlinJars, deps.kotlinAnnotations...) - kotlinHeaderJars = append(kotlinHeaderJars, deps.kotlinStdlib...) - kotlinHeaderJars = append(kotlinHeaderJars, deps.kotlinAnnotations...) - } else { - flags.dexClasspath = append(flags.dexClasspath, deps.kotlinStdlib...) - flags.dexClasspath = append(flags.dexClasspath, deps.kotlinAnnotations...) - } + kotlinExtraJars = append(kotlinExtraJars, deps.kotlinStdlib...) + kotlinExtraJars = append(kotlinExtraJars, deps.kotlinAnnotations...) } jars := slices.Clone(kotlinJars) @@ -1349,7 +1341,11 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars, extraClasspath // allow for the use of annotation processors that do function correctly // with sharding enabled. See: b/77284273. } - extraJars := append(slices.Clone(kotlinHeaderJars), extraCombinedJars...) + extraJars := slices.Clone(kotlinHeaderJars) + if BoolDefault(j.properties.Static_kotlin_stdlib, true) { + extraJars = append(extraJars, kotlinExtraJars...) + } + extraJars = append(extraJars, extraCombinedJars...) var combinedHeaderJarFile android.Path headerJarFileWithoutDepsOrJarjar, combinedHeaderJarFile = j.compileJavaHeader(ctx, uniqueJavaFiles, srcJars, deps, flags, jarName, extraJars) @@ -1427,6 +1423,13 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars, extraClasspath } } + // Jar kotlin classes into the final jar after javac + if BoolDefault(j.properties.Static_kotlin_stdlib, true) { + jars = append(jars, kotlinExtraJars...) + } else { + flags.dexClasspath = append(flags.dexClasspath, kotlinExtraJars...) + } + jars = append(jars, extraCombinedJars...) j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles From 1f75cdf980cf2c2553c1329e0f2ac7c9d01c56de Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 31 Jul 2024 16:54:41 -0700 Subject: [PATCH 2/2] Use rsp for kotlin classpath Support long classpaths by passing the classpath in a file to the script that generates build.xml. Bug: 308016794 Test: builds Flag: EXEMPT refactor Change-Id: Ib238a83a26acba7ede8e55298397dbeb9a57a866 --- java/kotlin.go | 12 ++++++++++-- scripts/gen-kotlin-build-file.py | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/java/kotlin.go b/java/kotlin.go index aa2db0ecd..c28bc3f54 100644 --- a/java/kotlin.go +++ b/java/kotlin.go @@ -101,6 +101,10 @@ func kotlinCompile(ctx android.ModuleContext, outputFile, headerOutputFile andro commonSrcFilesArg = "--common_srcs " + commonSrcsList.String() } + classpathRspFile := android.PathForModuleOut(ctx, "kotlinc", "classpath.rsp") + android.WriteFileRule(ctx, classpathRspFile, strings.Join(flags.kotlincClasspath.Strings(), " ")) + deps = append(deps, classpathRspFile) + ctx.Build(pctx, android.BuildParams{ Rule: kotlinc, Description: "kotlinc", @@ -109,7 +113,7 @@ func kotlinCompile(ctx android.ModuleContext, outputFile, headerOutputFile andro Inputs: srcFiles, Implicits: deps, Args: map[string]string{ - "classpath": flags.kotlincClasspath.FormJavaClassPath(""), + "classpath": classpathRspFile.String(), "kotlincFlags": flags.kotlincFlags, "commonSrcFilesArg": commonSrcFilesArg, "srcJars": strings.Join(srcJars.Strings(), " "), @@ -205,6 +209,10 @@ func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile an kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName()) kotlinName = strings.ReplaceAll(kotlinName, "/", "__") + classpathRspFile := android.PathForModuleOut(ctx, "kapt", "classpath.rsp") + android.WriteFileRule(ctx, classpathRspFile, strings.Join(flags.kotlincClasspath.Strings(), "\n")) + deps = append(deps, classpathRspFile) + // First run kapt to generate .java stubs from .kt files kaptStubsJar := android.PathForModuleOut(ctx, "kapt", "stubs.jar") ctx.Build(pctx, android.BuildParams{ @@ -214,7 +222,7 @@ func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile an Inputs: srcFiles, Implicits: deps, Args: map[string]string{ - "classpath": flags.kotlincClasspath.FormJavaClassPath(""), + "classpath": classpathRspFile.String(), "kotlincFlags": flags.kotlincFlags, "commonSrcFilesArg": commonSrcFilesArg, "srcJars": strings.Join(srcJars.Strings(), " "), diff --git a/scripts/gen-kotlin-build-file.py b/scripts/gen-kotlin-build-file.py index 99afdca75..8b7876f64 100644 --- a/scripts/gen-kotlin-build-file.py +++ b/scripts/gen-kotlin-build-file.py @@ -37,7 +37,7 @@ def parse_args(): parser.add_argument('--out', dest='out', help='file to which the module.xml contents will be written.') parser.add_argument('--classpath', dest='classpath', action='append', default=[], - help='classpath to pass to kotlinc.') + help='file containing classpath to pass to kotlinc.') parser.add_argument('--name', dest='name', help='name of the module.') parser.add_argument('--out_dir', dest='out_dir', @@ -65,8 +65,8 @@ def main(): f.write(' \n' % (args.name, args.out_dir or '')) # Print classpath entries - for c in args.classpath: - for entry in c.split(':'): + for classpath_rsp_file in args.classpath: + for entry in NinjaRspFileReader(classpath_rsp_file): path = os.path.abspath(entry) f.write(' \n' % path)