1// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file generates the final rules for compiling all Java.  All properties related to
18// compiling should have been translated into javaBuilderFlags or another argument to the Transform*
19// functions.
20
21import (
22	"strings"
23
24	"github.com/google/blueprint"
25
26	"android/soong/common"
27)
28
29var (
30	aaptCreateResourceJavaFile = pctx.StaticRule("aaptCreateResourceJavaFile",
31		blueprint.RuleParams{
32			Command: `rm -rf "$javaDir" && mkdir -p "$javaDir" && ` +
33				`$aaptCmd package -m $aaptFlags -P $publicResourcesFile -G $proguardOptionsFile ` +
34				`-J $javaDir || ( rm -rf "$javaDir/*"; exit 41 ) && ` +
35				`find $javaDir -name "*.java" > $javaFileList`,
36			CommandDeps: []string{"$aaptCmd"},
37			Description: "aapt create R.java $out",
38		},
39		"aaptFlags", "publicResourcesFile", "proguardOptionsFile", "javaDir", "javaFileList")
40
41	aaptCreateAssetsPackage = pctx.StaticRule("aaptCreateAssetsPackage",
42		blueprint.RuleParams{
43			Command:     `rm -f $out && $aaptCmd package $aaptFlags -F $out`,
44			CommandDeps: []string{"$aaptCmd"},
45			Description: "aapt export package $out",
46		},
47		"aaptFlags", "publicResourcesFile", "proguardOptionsFile", "javaDir", "javaFileList")
48
49	aaptAddResources = pctx.StaticRule("aaptAddResources",
50		blueprint.RuleParams{
51			// TODO: add-jni-shared-libs-to-package
52			Command:     `cp -f $in $out.tmp && $aaptCmd package -u $aaptFlags -F $out.tmp && mv $out.tmp $out`,
53			CommandDeps: []string{"$aaptCmd"},
54			Description: "aapt package $out",
55		},
56		"aaptFlags")
57
58	signapk = pctx.StaticRule("signapk",
59		blueprint.RuleParams{
60			Command:     `java -jar $signapkCmd $certificates $in $out`,
61			CommandDeps: []string{"$signapkCmd"},
62			Description: "signapk $out",
63		},
64		"certificates")
65
66	androidManifestMerger = pctx.StaticRule("androidManifestMerger",
67		blueprint.RuleParams{
68			Command: "java -classpath $androidManifestMergerCmd com.android.manifmerger.Main merge " +
69				"--main $in --libs $libsManifests --out $out",
70			CommandDeps: []string{"$androidManifestMergerCmd"},
71			Description: "merge manifest files $out",
72		},
73		"libsManifests")
74)
75
76func init() {
77	pctx.SourcePathVariable("androidManifestMergerCmd", "prebuilts/devtools/tools/lib/manifest-merger.jar")
78	pctx.HostBinToolVariable("aaptCmd", "aapt")
79	pctx.HostJavaToolVariable("signapkCmd", "signapk.jar")
80}
81
82func CreateResourceJavaFiles(ctx common.AndroidModuleContext, flags []string,
83	deps common.Paths) (common.Path, common.Path, common.Path) {
84	javaDir := common.PathForModuleGen(ctx, "R")
85	javaFileList := common.PathForModuleOut(ctx, "R.filelist")
86	publicResourcesFile := common.PathForModuleOut(ctx, "public_resources.xml")
87	proguardOptionsFile := common.PathForModuleOut(ctx, "proguard.options")
88
89	ctx.ModuleBuild(pctx, common.ModuleBuildParams{
90		Rule:      aaptCreateResourceJavaFile,
91		Outputs:   common.WritablePaths{publicResourcesFile, proguardOptionsFile, javaFileList},
92		Implicits: deps,
93		Args: map[string]string{
94			"aaptFlags":           strings.Join(flags, " "),
95			"publicResourcesFile": publicResourcesFile.String(),
96			"proguardOptionsFile": proguardOptionsFile.String(),
97			"javaDir":             javaDir.String(),
98			"javaFileList":        javaFileList.String(),
99		},
100	})
101
102	return publicResourcesFile, proguardOptionsFile, javaFileList
103}
104
105func CreateExportPackage(ctx common.AndroidModuleContext, flags []string, deps common.Paths) common.ModuleOutPath {
106	outputFile := common.PathForModuleOut(ctx, "package-export.apk")
107
108	ctx.ModuleBuild(pctx, common.ModuleBuildParams{
109		Rule:      aaptCreateAssetsPackage,
110		Output:    outputFile,
111		Implicits: deps,
112		Args: map[string]string{
113			"aaptFlags": strings.Join(flags, " "),
114		},
115	})
116
117	return outputFile
118}
119
120func CreateAppPackage(ctx common.AndroidModuleContext, flags []string, jarFile common.Path,
121	certificates []string) common.Path {
122
123	resourceApk := common.PathForModuleOut(ctx, "resources.apk")
124
125	ctx.ModuleBuild(pctx, common.ModuleBuildParams{
126		Rule:   aaptAddResources,
127		Output: resourceApk,
128		Input:  jarFile,
129		Args: map[string]string{
130			"aaptFlags": strings.Join(flags, " "),
131		},
132	})
133
134	outputFile := common.PathForModuleOut(ctx, "package.apk")
135
136	var certificateArgs []string
137	for _, c := range certificates {
138		certificateArgs = append(certificateArgs, c+".x509.pem", c+".pk8")
139	}
140
141	ctx.ModuleBuild(pctx, common.ModuleBuildParams{
142		Rule:   signapk,
143		Output: outputFile,
144		Input:  resourceApk,
145		Args: map[string]string{
146			"certificates": strings.Join(certificateArgs, " "),
147		},
148	})
149
150	return outputFile
151}
152