// Copyright (C) 2016 The Android Open Source Project
//
// 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 art

import (
	"path/filepath"
	"strings"

	"android/soong/android"
	"android/soong/cc/config"
)

var (
	pctx = android.NewPackageContext("android/soong/art")

	// Copy the following prebuilts to the testcases directory.
	// The original prebuilts directory is not accessible when running tests remotely.
	prebuiltToolsForTests = []string{
		"bin/clang",
		"bin/clang-real",
		"bin/llvm-addr2line",
		"bin/llvm-dwarfdump",
		"bin/llvm-objdump",
		"lib/libc++.so",
	}
)

func init() {
	android.RegisterMakeVarsProvider(pctx, makeVarsProvider)
	pctx.Import("android/soong/cc/config")
}

func makeVarsProvider(ctx android.MakeVarsContext) {
	ctx.Strict("LIBART_IMG_HOST_BASE_ADDRESS", ctx.Config().LibartImgHostBaseAddress())
	ctx.Strict("LIBART_IMG_TARGET_BASE_ADDRESS", ctx.Config().LibartImgDeviceBaseAddress())

	testMap := make(map[string][]string)
	testcasesContent := make(map[string]string)
	ctx.VisitAllModuleProxies(func(m android.ModuleProxy) {
		if provider, ok := android.OtherModuleProvider(ctx, m, testInstallInfoProvider); ok {
			for k, v := range provider.Testcases {
				if oldSrc, ok := testcasesContent[k]; ok {
					ctx.ModuleErrorf(m, "Conflicting sources for %s: %s and %s", k, oldSrc, v)
					return
				}
				testcasesContent[k] = v
			}
			for k, v := range provider.TestMap {
				testMap[k] = append(testMap[k], v...)
			}
		}
	})

	for _, name := range android.SortedKeys(testMap) {
		ctx.Strict("ART_TEST_LIST_"+name, strings.Join(android.FirstUniqueStrings(testMap[name]), " "))
	}

	// Create list of copy commands to install the content of the testcases directory.
	copy_cmds := []string{}
	for _, key := range android.SortedKeys(testcasesContent) {
		copy_cmds = append(copy_cmds, testcasesContent[key]+":"+key)
	}
	ctx.Strict("ART_TESTCASES_CONTENT", strings.Join(copy_cmds, " "))

	// Add prebuilt tools.
	clang_path := filepath.Join(config.ClangDefaultBase, ctx.Config().PrebuiltOS(), config.ClangVersion(ctx))
	copy_cmds = []string{}
	for _, tool := range prebuiltToolsForTests {
		src := filepath.Join(clang_path, "/", tool)
		copy_cmds = append(copy_cmds, src+":"+src)
	}
	ctx.Strict("ART_TESTCASES_PREBUILT_CONTENT", strings.Join(copy_cmds, " "))
}
