Dexpreopt soong modules inside soong

Port the dexpreopt logic from Make to the dexpreopt package in Soong,
and use it to dexpreopt Soong modules.  The same package is also
compiled into the dexpreopt_gen binary to generate dexpreopt scripts
for Make modules.

This relands Ib67e2febf9ed921f06e8a86b9ec945c80dff35eb and
I462182638bd57b1367b5bfb0718e975c11ae66f7, along with multiple fixes
to depsfile generation in dexpreopt_gen that caused .odex files for
modules in defined make to be missing dependencies on boot.art, and
a fix to not dexpreopt and strip tests.

Bug: 119412419
Bug: 120273280
Test: no differences to dexpreopt outputs on aosp_sailfish system/,
      only expected changes to dexpreopt outputs on system_other
      (.vdex files for privileged Soong modules no longer incorrectly
      contain .dex contents).
Test: OUT_DIR=$PWD/out m
Test: NINJA_ARGS="-t deps out/target/product/sailfish/obj/APPS/Contacts_intermediates/dexpreopt.zip" m
Change-Id: I6bb2c971cee65d2338839753aa0d84939f335b1b
diff --git a/dexpreopt/dexpreopt_test.go b/dexpreopt/dexpreopt_test.go
new file mode 100644
index 0000000..5265248
--- /dev/null
+++ b/dexpreopt/dexpreopt_test.go
@@ -0,0 +1,208 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package dexpreopt
+
+import (
+	"reflect"
+	"strings"
+	"testing"
+)
+
+var testGlobalConfig = GlobalConfig{
+	DefaultNoStripping:                 false,
+	DisablePreoptModules:               nil,
+	OnlyPreoptBootImageAndSystemServer: false,
+	HasSystemOther:                     false,
+	PatternsOnSystemOther:              nil,
+	DisableGenerateProfile:             false,
+	BootJars:                           nil,
+	SystemServerJars:                   nil,
+	SystemServerApps:                   nil,
+	SpeedApps:                          nil,
+	PreoptFlags:                        nil,
+	DefaultCompilerFilter:              "",
+	SystemServerCompilerFilter:         "",
+	GenerateDMFiles:                    false,
+	NoDebugInfo:                        false,
+	AlwaysSystemServerDebugInfo:        false,
+	NeverSystemServerDebugInfo:         false,
+	AlwaysOtherDebugInfo:               false,
+	NeverOtherDebugInfo:                false,
+	MissingUsesLibraries:               nil,
+	IsEng:                              false,
+	SanitizeLite:                       false,
+	DefaultAppImages:                   false,
+	Dex2oatXmx:                         "",
+	Dex2oatXms:                         "",
+	EmptyDirectory:                     "",
+	DefaultDexPreoptImageLocation:      nil,
+	CpuVariant:                         nil,
+	InstructionSetFeatures:             nil,
+	Tools: Tools{
+		Profman:             "profman",
+		Dex2oat:             "dex2oat",
+		Aapt:                "aapt",
+		SoongZip:            "soong_zip",
+		Zip2zip:             "zip2zip",
+		VerifyUsesLibraries: "verify_uses_libraries.sh",
+		ConstructContext:    "construct_context.sh",
+	},
+}
+
+var testModuleConfig = ModuleConfig{
+	Name:                   "",
+	DexLocation:            "",
+	BuildPath:              "",
+	DexPath:                "",
+	PreferIntegrity:        false,
+	UncompressedDex:        false,
+	HasApkLibraries:        false,
+	PreoptFlags:            nil,
+	ProfileClassListing:    "",
+	ProfileIsTextListing:   false,
+	EnforceUsesLibraries:   false,
+	OptionalUsesLibraries:  nil,
+	UsesLibraries:          nil,
+	LibraryPaths:           nil,
+	Archs:                  nil,
+	DexPreoptImageLocation: "",
+	PreoptExtractedApk:     false,
+	NoCreateAppImage:       false,
+	ForceCreateAppImage:    false,
+	PresignedPrebuilt:      false,
+	StripInputPath:         "",
+	StripOutputPath:        "",
+}
+
+func TestDexPreopt(t *testing.T) {
+	global, module := testGlobalConfig, testModuleConfig
+
+	module.Name = "test"
+	module.DexLocation = "/system/app/test/test.apk"
+	module.BuildPath = "out/test/test.apk"
+	module.Archs = []string{"arm"}
+
+	rule, err := GenerateDexpreoptRule(global, module)
+	if err != nil {
+		t.Error(err)
+	}
+
+	wantInstalls := []Install{
+		{"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
+		{"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
+	}
+
+	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
+		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
+	}
+}
+
+func TestDexPreoptSystemOther(t *testing.T) {
+	global, module := testGlobalConfig, testModuleConfig
+
+	global.HasSystemOther = true
+	global.PatternsOnSystemOther = []string{"app/%"}
+
+	module.Name = "test"
+	module.DexLocation = "/system/app/test/test.apk"
+	module.BuildPath = "out/test/test.apk"
+	module.Archs = []string{"arm"}
+
+	rule, err := GenerateDexpreoptRule(global, module)
+	if err != nil {
+		t.Error(err)
+	}
+
+	wantInstalls := []Install{
+		{"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"},
+		{"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"},
+	}
+
+	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
+		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
+	}
+}
+
+func TestDexPreoptProfile(t *testing.T) {
+	global, module := testGlobalConfig, testModuleConfig
+
+	module.Name = "test"
+	module.DexLocation = "/system/app/test/test.apk"
+	module.BuildPath = "out/test/test.apk"
+	module.ProfileClassListing = "profile"
+	module.Archs = []string{"arm"}
+
+	rule, err := GenerateDexpreoptRule(global, module)
+	if err != nil {
+		t.Error(err)
+	}
+
+	wantInstalls := []Install{
+		{"out/test/profile.prof", "/system/app/test/test.apk.prof"},
+		{"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"},
+		{"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
+		{"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
+	}
+
+	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
+		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
+	}
+}
+
+func TestStripDex(t *testing.T) {
+	global, module := testGlobalConfig, testModuleConfig
+
+	module.Name = "test"
+	module.DexLocation = "/system/app/test/test.apk"
+	module.BuildPath = "out/test/test.apk"
+	module.Archs = []string{"arm"}
+	module.StripInputPath = "$1"
+	module.StripOutputPath = "$2"
+
+	rule, err := GenerateStripRule(global, module)
+	if err != nil {
+		t.Error(err)
+	}
+
+	want := `zip2zip -i $1 -o $2 -x "classes*.dex"`
+	if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
+		t.Errorf("\nwant commands[0] to have:\n   %v\ngot:\n   %v", want, rule.Commands()[0])
+	}
+}
+
+func TestNoStripDex(t *testing.T) {
+	global, module := testGlobalConfig, testModuleConfig
+
+	global.DefaultNoStripping = true
+
+	module.Name = "test"
+	module.DexLocation = "/system/app/test/test.apk"
+	module.BuildPath = "out/test/test.apk"
+	module.Archs = []string{"arm"}
+	module.StripInputPath = "$1"
+	module.StripOutputPath = "$2"
+
+	rule, err := GenerateStripRule(global, module)
+	if err != nil {
+		t.Error(err)
+	}
+
+	wantCommands := []string{
+		"cp -f $1 $2",
+	}
+	if !reflect.DeepEqual(rule.Commands(), wantCommands) {
+		t.Errorf("\nwant commands:\n   %v\ngot:\n   %v", wantCommands, rule.Commands())
+	}
+}