1// Copyright 2016 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 cc
16
17import (
18	"path/filepath"
19
20	"android/soong/android"
21)
22
23// This file handles installing files into their final location
24
25type InstallerProperties struct {
26	// install to a subdirectory of the default install path for the module
27	Relative_install_path *string `android:"arch_variant"`
28
29	// Install output directly in {partition}/, not in any subdir.  This is only intended for use by
30	// init_first_stage.
31	Install_in_root *bool `android:"arch_variant"`
32
33	// Install output directly in {partition}/xbin
34	Install_in_xbin *bool `android:"arch_variant"`
35}
36
37type installLocation int
38
39const (
40	InstallInSystem       installLocation = 0
41	InstallInData                         = iota
42	InstallInSanitizerDir                 = iota
43)
44
45func NewBaseInstaller(dir, dir64 string, location installLocation) *baseInstaller {
46	return &baseInstaller{
47		dir:      dir,
48		dir64:    dir64,
49		location: location,
50	}
51}
52
53type baseInstaller struct {
54	Properties InstallerProperties
55
56	dir      string
57	dir64    string
58	subDir   string
59	relative string
60	location installLocation
61
62	installDeps android.InstallPaths
63
64	path android.InstallPath
65}
66
67var _ installer = (*baseInstaller)(nil)
68
69func (installer *baseInstaller) installerProps() []interface{} {
70	return []interface{}{&installer.Properties}
71}
72
73func (installer *baseInstaller) installDir(ctx ModuleContext) android.InstallPath {
74	dir := installer.dir
75	if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
76		dir = installer.dir64
77	}
78
79	if installer.installInRoot() {
80		dir = ""
81	} else if installer.installInXbin() {
82		dir = "xbin"
83	}
84
85	if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
86		dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
87	} else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
88		dir = filepath.Join(dir, ctx.Arch().ArchType.String())
89	}
90	if installer.location == InstallInData && ctx.InVendorOrProduct() {
91		if ctx.inProduct() {
92			dir = filepath.Join(dir, "product")
93		} else {
94			dir = filepath.Join(dir, "vendor")
95		}
96	}
97	return android.PathForModuleInstall(ctx, dir, installer.subDir,
98		installer.relativeInstallPath(), installer.relative)
99}
100
101func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
102	installer.path = ctx.InstallFile(installer.installDir(ctx), file.Base(), file, installer.installDeps...)
103}
104
105func (installer *baseInstaller) installTestData(ctx ModuleContext, data []android.DataPath) {
106	installedData := ctx.InstallTestData(installer.installDir(ctx), data)
107	installer.installDeps = append(installer.installDeps, installedData...)
108}
109
110func (installer *baseInstaller) everInstallable() bool {
111	// Most cc modules are installable.
112	return true
113}
114
115func (installer *baseInstaller) inData() bool {
116	return installer.location == InstallInData
117}
118
119func (installer *baseInstaller) inSanitizerDir() bool {
120	return installer.location == InstallInSanitizerDir
121}
122
123func (installer *baseInstaller) hostToolPath() android.OptionalPath {
124	return android.OptionalPath{}
125}
126
127func (installer *baseInstaller) relativeInstallPath() string {
128	return String(installer.Properties.Relative_install_path)
129}
130
131func (installer *baseInstaller) makeUninstallable(mod *Module) {
132	mod.ModuleBase.MakeUninstallable()
133}
134
135func (installer *baseInstaller) installInRoot() bool {
136	return Bool(installer.Properties.Install_in_root)
137}
138
139func (installer *baseInstaller) installInXbin() bool {
140	return Bool(installer.Properties.Install_in_xbin)
141}
142