This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.go
253 lines (218 loc) · 6 KB
/
lib.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
// Copyright 2018 Schibsted
package buildbuild
import (
"path"
"strings"
)
type LibDescriptor interface {
IsDummyLib() bool
LibName() string
PiclibName() string
NameAsLib() (name string, islib bool)
NameAsPiclib() (name string, islib bool)
LibDeps() []string
Linker() string
}
type LibDesc struct {
LinkDesc
LinkSet bool
}
func (tmpl *LibDesc) NewFromTemplate(bd, tname string, flavors []string) Descriptor {
return &LibDesc{
LinkDesc: *tmpl.LinkDesc.NewFromTemplate(bd, tname, flavors),
LinkSet: tmpl.LinkSet,
}
}
func (l *LibDesc) Parse(ops *GlobalOps, realsrcdir string, args map[string][]string) Descriptor {
desc := l.GenericParse(l, ops, realsrcdir, args, LinkerExtra("includes", "incprefix"))
l.LinkerParse(realsrcdir, args)
l.Includes = append(l.Includes, args["includes"]...)
destInc := "dest_inc"
if len(args["incprefix"]) > 0 {
destInc = path.Join("dest_inc", args["incprefix"][0])
}
for _, inc := range args["includes"] {
l.AddTarget(inc, "install_header", []string{inc}, destInc, l.Srcdir, nil, nil)
}
if ops.Libs == nil {
ops.Libs = make(map[string]LibDescriptor)
}
ops.Libs[l.TargetName] = l
return desc
}
func (l *LibDesc) Finalize(ops *GlobalOps) {
libname := l.TargetName
l.FinalizeCC(ops)
// We can have dummy libs without any sources (for include dependencies).
if l.LinkSet || len(l.Objs) > 0 {
objs := l.SuffixedObjs(".o", nil)
picobjs := l.SuffixedObjs(".pic_o", nil)
if l.LinkSet {
l.AddTarget("lib"+libname+".o", "partiallink", objs, l.Destdir, "", nil, l.TargetOptions)
l.AddTarget("lib"+libname+"_pic.o", "partiallink", picobjs, l.Destdir, "", nil, l.TargetOptions)
} else {
l.AddTarget("lib"+libname+".a", "ar", objs, l.Destdir, "", nil, l.TargetOptions)
l.AddTarget("lib"+libname+"_pic.a", "ar", picobjs, l.Destdir, "", nil, l.TargetOptions)
}
}
l.Deps["depend_includes_"+libname] = l.ResolveIncdeps(ops)
l.AddTarget("depend_includes_"+libname, "phony", l.Includes, "builddir", l.Srcdir, nil, nil)
l.FinalizeAnalyse(ops)
l.GeneralDesc.Finalize(ops)
}
func (l *LibDesc) IsDummyLib() bool {
return len(l.Objs) == 0
}
func (l *LibDesc) LibName() string {
if l.LinkSet {
return "lib" + l.TargetName + ".o"
}
return "lib" + l.TargetName + ".a"
}
func (l *LibDesc) PiclibName() string {
if l.LinkSet {
return "lib" + l.TargetName + "_pic.o"
}
return "lib" + l.TargetName + "_pic.a"
}
func (l *LibDesc) NameAsLib() (string, bool) {
if l.LinkSet {
return l.LibName(), false
}
return "-l" + l.TargetName, true
}
func (l *LibDesc) NameAsPiclib() (string, bool) {
if l.LinkSet {
return l.PiclibName(), false
}
return "-l" + l.TargetName + "_pic", true
}
func (l *LibDesc) LibDeps() []string {
return l.Libs
}
func (l *LibDesc) Linker() string {
return l.Link
}
var LibTemplate = LibDesc{
LinkDesc: LinkDesc{
GeneralDesc: GeneralDesc{
Destdir: "dest_lib",
TargetOptions: map[string]bool{"lib": true},
},
Picrules: true,
},
LinkSet: false,
}
var LinkersetLibTemplate = LibDesc{
LinkDesc: LinkDesc{
GeneralDesc: GeneralDesc{
Destdir: "dest_lib",
TargetOptions: map[string]bool{"lib": true},
},
Picrules: true,
},
LinkSet: true,
}
// Topological sort of library dependencies. Implemented as
// a depth-first descent of the dependencies. The return value
// is in dependency order. This is only really necessary for
// resolving link order static libraries, but it also allows
// us to generate a deduplicated list of depedencies.
func (ops *GlobalOps) ResolveLibs(libs []string) []string {
var ret []string
resolved := make(map[string]bool)
var resolver func(libs []string)
// Recursive resolver function
resolver = func(libs []string) {
for _, l := range libs {
if resolved[l] {
continue
}
resolved[l] = true
if desc := ops.Libs[l]; desc != nil {
resolver(desc.LibDeps())
}
ret = append(ret, l)
}
}
resolver(libs)
return ret
}
// Resolve non-dummy libs that we build.
// Dummy libs are libs that don't have objs.
func (ops *GlobalOps) ResolveLibsOur(libs []string) []LibDescriptor {
var ret []LibDescriptor
libs = ops.ResolveLibs(libs)
for i := len(libs) - 1; i >= 0; i-- {
if desc := ops.Libs[libs[i]]; desc != nil && !desc.IsDummyLib() {
ret = append(ret, desc)
}
}
return ret
}
// Resolves all external libs
func (ops *GlobalOps) ResolveLibsExternal(libs []string) []string {
var ret []string
libs = ops.ResolveLibs(libs)
for i := len(libs) - 1; i >= 0; i-- {
if ops.Libs[libs[i]] == nil {
if strings.ContainsAny(libs[i], "/$") {
ret = append(ret, libs[i])
} else {
ret = append(ret, "-l"+libs[i])
}
}
}
return ret
}
// The following three functions resolve arguments to link with various libraries.
func (ops *GlobalOps) ResolveLibsOurStatic(libs []string) []string {
var ret []string
for _, lib := range ops.ResolveLibsOur(libs) {
ret = append(ret, "$libdir/"+lib.LibName())
}
return ret
}
func (ops *GlobalOps) ResolveLibsOurPic(libs []string) []string {
var ret []string
for _, lib := range ops.ResolveLibsOur(libs) {
ret = append(ret, "$libdir/"+lib.PiclibName())
}
return ret
}
func (ops *GlobalOps) ResolveLibsOurStaticAsLib(libs []string) ([]string, []string) {
var objs, llibs []string
for _, lib := range ops.ResolveLibsOur(libs) {
name, islib := lib.NameAsLib()
if islib {
llibs = append(llibs, name)
} else {
objs = append(objs, "$libdir/"+name)
}
}
return objs, llibs
}
func (ops *GlobalOps) ResolveLibsOurPicAsLib(libs []string) ([]string, []string) {
var objs, llibs []string
for _, lib := range ops.ResolveLibsOur(libs) {
name, islib := lib.NameAsPiclib()
if islib {
llibs = append(llibs, name)
} else {
objs = append(objs, "$libdir/"+name)
}
}
return objs, llibs
}
func (ops *GlobalOps) ResolveLibsLinker(link string, libs []string) string {
for _, lib := range ops.ResolveLibsOur(libs) {
llink := lib.Linker()
if llink != "link" && llink != "" && llink != link {
if link != "link" && link != "" {
panic("Multiple custom linkers, got " + link + " and " + llink)
}
link = llink
}
}
return link
}