/
linux_backend.go
298 lines (254 loc) · 8.34 KB
/
linux_backend.go
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package core
import (
"os"
"path/filepath"
"strings"
"github.com/google/blueprint"
"github.com/ARM-software/bob-build/core/backend"
"github.com/ARM-software/bob-build/core/file"
"github.com/ARM-software/bob-build/core/tag"
"github.com/ARM-software/bob-build/core/toolchain"
"github.com/ARM-software/bob-build/internal/utils"
)
var (
pctx = blueprint.NewPackageContext("bob")
_ = pctx.VariableFunc("SrcDir", func(interface{}) (string, error) {
return getSourceDir(), nil
})
_ = pctx.VariableFunc("BuildDir", func(interface{}) (string, error) {
return getBuildDir(), nil
})
_ = pctx.VariableFunc("BobScriptsDir", func(interface{}) (string, error) {
return getBobScriptsDir(), nil
})
enableToc = getTocUsageFromEnvironment()
)
type linuxGenerator struct {
}
/* Compile time checks for interfaces that must be implemented by linuxGenerator */
var _ generatorBackend = (*linuxGenerator)(nil)
func getTocUsageFromEnvironment() bool {
enable := true // Default to using toc files
if str, ok := os.LookupEnv("BOB_ALWAYS_LINK_SHARED_LIBS"); ok {
// Disable according to the environment variable
//
// Be permissive in the values accepted to disable this
// feature. If someone is trying to set this variable, then by
// definition they are looking to disable it. Users who want
// the default behavior are unlikely to set it. So look for a
// few values which might be used to indicate "I'd like the
// default behavior", and take any other value to mean change
// behavior.
//
// This should reduce queries about what's the right setting
// to use to disable toc usage.
if !utils.Contains([]string{"n", "N", "0", ""}, str) {
enable = false
}
}
return enable
}
func addPhony(p phonyInterface, ctx blueprint.ModuleContext,
installDeps []string, optional bool) {
ctx.Build(pctx,
blueprint.BuildParams{
Rule: blueprint.Phony,
Inputs: installDeps,
Outputs: []string{p.shortName()},
Optional: optional,
})
}
type singleOutputModule interface {
blueprint.Module
outputName() string
outputFileName() string
}
type targetableModule interface {
singleOutputModule
getTarget() toolchain.TgtType
}
// Modules implementing sharedLibProducer create a shared library
type sharedLibProducer interface {
targetableModule
getTocName() string
}
// Modules implementing the linkableModule interface are linked
// by `ld` to produce a shared library or binary.
type linkableModule interface {
getVersionScript(ctx blueprint.ModuleContext) *string
// GetWholeStaticLibs(ctx blueprint.ModuleContext) []string
GetStaticLibs(ctx blueprint.ModuleContext) []string
}
// Full path for shared libraries, in a shared location to simplify linking.
// As long as the module is targetable, we can infer the library path.
func (g *linuxGenerator) getSharedLibLinkPath(t targetableModule) string {
// TODO: this should be part of core/backend
return filepath.Join(backend.Get().SharedLibsDir(t.getTarget()), t.outputFileName())
}
var _ = pctx.StaticVariable("toc", "${BobScriptsDir}/library_toc.py")
var tocRule = pctx.StaticRule("shared_library_toc",
blueprint.RuleParams{
Command: "$toc $in -o $out $tocflags",
CommandDeps: []string{"$toc"},
Description: "Generate toc $out",
Restat: true,
},
"tocflags")
func (g *linuxGenerator) addSharedLibToc(ctx blueprint.ModuleContext, soFile, tocFile string, tgt toolchain.TgtType) {
tc := backend.Get().GetToolchain(tgt)
tocFlags := tc.GetLibraryTocFlags()
ctx.Build(pctx,
blueprint.BuildParams{
Rule: tocRule,
Outputs: []string{tocFile},
Inputs: []string{soFile},
Optional: true,
Args: map[string]string{"tocflags": strings.Join(tocFlags, " ")},
})
}
func (*linuxGenerator) aliasActions(a *ModuleAlias, ctx blueprint.ModuleContext) {
srcs := []string{}
/* Only depend on enabled targets */
ctx.VisitDirectDepsIf(
func(p blueprint.Module) bool { return ctx.OtherModuleDependencyTag(p) == tag.AliasTag },
func(p blueprint.Module) {
if e, ok := p.(enableable); ok {
if !isEnabled(e) {
return
}
}
name := ctx.OtherModuleName(p)
if lib, ok := p.(phonyInterface); ok {
name = lib.shortName()
}
srcs = append(srcs, name)
})
ctx.Build(pctx,
blueprint.BuildParams{
Rule: blueprint.Phony,
Inputs: srcs,
Outputs: []string{a.Name()},
Optional: true,
})
}
var _ = pctx.StaticVariable("strip", "${BobScriptsDir}/strip.py")
var stripRule = pctx.StaticRule("strip",
blueprint.RuleParams{
Command: "$strip $args -o $out $in",
CommandDeps: []string{"$strip"},
Description: "strip $out",
}, "args")
var installRule = pctx.StaticRule("install",
blueprint.RuleParams{
Command: "rm -f $out; cp $in $out",
Description: "$out",
})
func (g *linuxGenerator) install(m interface{}, ctx blueprint.ModuleContext) []string {
ins := m.(installable)
props := ins.getInstallableProps()
installPath, ok := props.getInstallPath()
if !ok {
return []string{}
}
installPath = filepath.Join("${BuildDir}", installPath)
installedFiles := []string{}
rule := installRule
args := map[string]string{}
deps := []string{}
if props.Post_install_cmd != nil {
rulename := "install"
cmd := "rm -f $out; cp $in $out ; " + *props.Post_install_cmd
// Expand args immediately
cmd = strings.Replace(cmd, "${args}", strings.Join(props.Post_install_args, " "), -1)
if props.Post_install_tool != nil {
args["tool"] = *props.Post_install_tool
deps = append(deps, *props.Post_install_tool)
}
utils.StripUnusedArgs(args, cmd)
rule = ctx.Rule(pctx,
rulename,
blueprint.RuleParams{
Command: cmd,
Description: "$out",
},
utils.SortedKeys(args)...)
}
if provider, ok := m.(file.Provider); ok {
provider.OutFiles().ForEachIf(
func(fp file.Path) bool { return fp.IsType(file.TypeInstallable) },
func(fp file.Path) bool {
if fp.IsSymLink() {
symlink := filepath.Join(installPath, fp.UnScopedPath())
symlinkTgt := filepath.Join(installPath, fp.ExpandLink().UnScopedPath())
ctx.Build(pctx,
blueprint.BuildParams{
Rule: symlinkRule,
Outputs: []string{symlink},
Inputs: []string{symlinkTgt},
Args: map[string]string{"target": fp.ExpandLink().UnScopedPath()},
Optional: true,
})
installedFiles = append(installedFiles, symlink)
} else {
src := fp.BuildPath()
dest := filepath.Join(installPath, filepath.Base(src))
// Interpose strip target
if capable, ok := m.(BackendConfigurationProvider); ok {
if lib := capable.GetBackendConfiguration(ctx); lib != nil {
debugPath := lib.getDebugPath()
separateDebugInfo := debugPath != nil
debugPathPrefix := installPath //Default to install path
if separateDebugInfo && *debugPath != "" {
debugPathPrefix = filepath.Join("${BuildDir}", *debugPath)
}
if lib.strip() || separateDebugInfo {
tc := backend.Get().GetToolchain(lib.getTarget())
basename := filepath.Base(src)
strippedSrc := filepath.Join(lib.stripOutputDir(g), basename)
stArgs := tc.GetStripFlags()
if lib.strip() {
stArgs = append(stArgs, "--strip")
}
if separateDebugInfo {
// TODO: This should really be using file interface when enabled
dbgFile := filepath.Join(debugPathPrefix, basename+".dbg")
stArgs = append(stArgs, "--debug-file")
stArgs = append(stArgs, dbgFile)
}
stripArgs := map[string]string{
"args": strings.Join(stArgs, " "),
}
ctx.Build(pctx,
blueprint.BuildParams{
Rule: stripRule,
Outputs: []string{strippedSrc},
Inputs: []string{src},
Args: stripArgs,
Optional: true,
})
src = strippedSrc
}
}
}
ctx.Build(pctx,
blueprint.BuildParams{
Rule: rule,
Outputs: []string{dest},
Inputs: []string{src},
Args: args,
Implicits: deps,
Optional: true,
})
installedFiles = append(installedFiles, dest)
}
return true
})
}
return append(installedFiles, ins.getInstallDepPhonyNames(ctx)...)
}
func (g *linuxGenerator) resourceActions(r *ModuleResource, ctx blueprint.ModuleContext) {
installDeps := g.install(r, ctx)
addPhony(r, ctx, installDeps, false)
}
func (g *linuxGenerator) filegroupActions(m *ModuleFilegroup, ctx blueprint.ModuleContext) {
}