aboutsummaryrefslogtreecommitdiff
path: root/build/cpp/make_cpp.go
blob: 75721ec3d0cfdee6eb207140a0a2c97369c120ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (C) 2015 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 cpp

import (
	"path/filepath"

	"android.googlesource.com/platform/tools/gpu/build"
	"android.googlesource.com/platform/tools/gpu/log"
	"android.googlesource.com/platform/tools/gpu/maker"
)

// Add steps to the "maker" build system for compiling, linking of c++.

var sourcePatterns = []string{"*.cpp", "*.c", "*.cc", "*.mm", "*.asm"}

func makeEntity(f build.File) maker.Entity {
	return maker.File(f.Absolute())
}

func makeEntities(fs build.FileSet) []interface{} {
	var es []interface{}
	for _, f := range fs {
		es = append(es, makeEntity(f))
	}
	return es
}

func makeStep(name string, output build.File, source build.FileSet, always bool, a func(*maker.Step) error) *maker.Step {
	s := maker.NewStep(a)
	e := makeEntity(output)
	s.Creates(e)
	s.DependsOn(maker.DirOf(e))
	s.DependsOn(makeEntities(source)...)
	// This is a bit sad, but codergen does not know what its outputs are.
	s.DependsOn("code")
	if always {
		s.AlwaysRun()
	}
	if name != "" {
		maker.List("cc:" + name).DependsOn(e)
	}
	maker.List("cc").DependsOn(e)
	return s
}

func logger(env build.Environment, name string) log.Logger {
	rootLogger := env.Logger
	if env.Verbose {
		rootLogger.Infof("Building %s", name)
	}
	return rootLogger.Enter(name)
}

// Add make steps to compile all the specified source files.
// Returns fileset of the resulting object files.
// Note we do not get a dependency on the compiler.
func MakeCompile(sources build.FileSet, cfg Config, env build.Environment) build.FileSet {
	ext := cfg.Toolchain.ObjExt(cfg)
	if cfg.OutputExt != nil {
		ext = *cfg.OutputExt
	}

	sources = sources.Append(cfg.AdditionalSources...)
	objects := make([]build.File, len(sources))
	for i, source := range sources {
		i, source, env := i, source, env
		object := IntermediatePath(source, ext, cfg, env)
		objects[i] = object
		prev := maker.Creator(makeEntity(object))
		if prev != nil {
			// skip any objects which have existing build steps
			continue
		}
		s := makeStep("", object, build.Files(source), env.ForceBuild,
			func(*maker.Step) error {
				env.Logger = env.Logger.Enter(object.Name())
				env.Logger.Enter("C++.Compile")
				return cfg.Toolchain.Compiler(source, object, cfg, env)
			})
		depsFor := cfg.Toolchain.DepsFor
		if depsFor == nil {
			// If the toolchain can't check dependencies, then we have to build
			s.AlwaysRun()
			continue
		}
		deps, valid := depsFor(object, cfg, env)
		if !valid {
			s.AlwaysRun()
			continue
		}
		s.DependsOn(makeEntities(deps)...)
	}
	return objects
}

// Add make steps to construct a static library. The inputs may be a
// combination of source and / or object files. Source file inputs will
// generate compilation steps. Returns the output library name.
// Note we do not get a dependency on the archiver.
func MakeStaticLibrary(inputs build.FileSet, cfg Config, env build.Environment) build.File {
	objects := MakeCompile(inputs.Filter(sourcePatterns...), cfg, env)
	objects = objects.Append(inputs.Filter("*" + cfg.Toolchain.ObjExt(cfg))...)

	name := cfg.Toolchain.LibName(cfg)
	output := env.Intermediates.Join(Triplet(cfg), name).ChangeExt(cfg.Toolchain.LibExt(cfg))
	if cfg.OutputExt != nil {
		output = output.ChangeExt(*cfg.OutputExt)
	}

	makeStep("", output, objects, env.ForceBuild,
		func(*maker.Step) error {
			env.Logger = logger(env, cfg.Name).Enter("C++.StaticLibrary")
			return cfg.Toolchain.Archiver(objects, output, cfg, env)
		})
	return output
}

// Add make steps to construct a dynamic library. The inputs can be a
// combination of source files, object files and / or library files.
// Source file inputs will generate compilation steps.
// Returns the output library name.
// Note we do not get a dependency on the linker.
func MakeDynamicLibrary(inputs build.FileSet, cfg Config, env build.Environment) build.File {
	objects := MakeCompile(inputs.Filter(sourcePatterns...), cfg, env)
	objects = objects.Append(inputs.Filter("*" + cfg.Toolchain.ObjExt(cfg))...)
	libs := inputs.Filter("*" + cfg.Toolchain.LibExt(cfg))

	libraries := build.FileSet{}
	for _, library := range libs {
		dir, base := filepath.Split(library.Absolute())
		libraries = libraries.Append(build.File(base))
		cfg.LibrarySearchPaths = cfg.LibrarySearchPaths.Append(build.File(dir))
	}

	cfg.Libraries = append(libraries, cfg.Libraries...)

	output := cfg.OutputDir.Join(cfg.Toolchain.DllName(cfg))
	if cfg.OutputExt != nil {
		output = output.ChangeExt(*cfg.OutputExt)
	}

	deps := libs.Append(objects...)
	makeStep(cfg.Name, output, deps, env.ForceBuild,
		func(*maker.Step) error {
			env.Logger = logger(env, cfg.Name).Enter("C++.DynamicLibrary")
			return cfg.Toolchain.DllLinker(objects, output, cfg, env)
		})
	return output
}

// Add make steps to construct an executable. The inputs can be a combination
// of source files, object files and / or library files. Source file inputs
// will generate compilation steps. Returns the output library name.
// Note we do not get a dependency on the linker.
func MakeExecutable(inputs build.FileSet, cfg Config, env build.Environment) build.File {
	objects := MakeCompile(inputs.Filter(sourcePatterns...), cfg, env)
	objects = objects.Append(inputs.Filter("*" + cfg.Toolchain.ObjExt(cfg))...)
	libs := inputs.Filter("*" + cfg.Toolchain.LibExt(cfg))

	libraries := build.FileSet{}
	for _, library := range libs {
		dir, base := filepath.Split(library.Absolute())
		libraries = libraries.Append(build.File(base))
		cfg.LibrarySearchPaths = cfg.LibrarySearchPaths.Append(build.File(dir))
	}

	cfg.Libraries = append(libraries, cfg.Libraries...)

	output := cfg.OutputDir.Join(cfg.Toolchain.ExeName(cfg))
	if cfg.OutputExt != nil {
		output = output.ChangeExt(*cfg.OutputExt)
	}

	deps := libs.Append(objects...)
	makeStep(cfg.Name, output, deps, env.ForceBuild,
		func(*maker.Step) error {
			env.Logger = logger(env, cfg.Name).Enter("C++.Executable")
			return cfg.Toolchain.ExeLinker(objects, output, cfg, env)
		})
	return output
}

// Add a make step to run a test. The test only runs on the host OS.
// The test is automatically included in the "test" target.
func MakeRunTest(test build.File, cfg Config) {
	// We only run tests on the host OS.
	if cfg.OS == maker.HostOS {
		phony := maker.Virtual("cc:" + test.Name() + ":run")
		maker.Command(makeEntity(test)).Creates(phony)
		maker.List("cc_test").DependsOn(phony)
	}
}

// Add a make step to copy a file from "src" to "dest". The copy only
// happens on the host OS.
func MakeCopy(src build.File, dest build.File, cfg Config, env build.Environment) {
	logger := env.Logger
	if cfg.OS == maker.HostOS {
		makeStep(cfg.Name, dest, build.Files(src), env.ForceBuild,
			func(*maker.Step) error {
				logger.Infof("Copying %s -> %s", src, dest)
				if err := src.CopyTo(dest); err != nil {
					logger.Errorf("Copy failed: %v", err)
					// Error delibrately suppressed
				}
				return nil
			}).AlwaysRun()
	}
}