This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
libtool.go
124 lines (100 loc) · 2.18 KB
/
libtool.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
// Copyright 2017 The 99c Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
var (
libToolConfigs = map[string]libToolConfig{}
)
type libToolConfig map[string]interface{}
func newLibToolConfig(r io.Reader) (libToolConfig, error) {
s := bufio.NewScanner(r)
c := libToolConfig{}
for s.Scan() {
l := strings.TrimSpace(s.Text())
if l == "" || strings.HasPrefix(l, "#") {
continue
}
a := strings.SplitN(l, "=", 2)
if len(a) != 2 {
return nil, fmt.Errorf("invalid libtool config line: %s", l)
}
nm := strings.TrimSpace(a[0])
val := strings.TrimSpace(a[1])
if _, ok := c[nm]; ok {
return nil, fmt.Errorf("duplicate libtool config item: %s", l)
}
if strings.HasPrefix(val, "'") {
if !strings.HasSuffix(val, "'") {
return nil, fmt.Errorf("invalid libtool config value: %s", l)
}
c[nm] = val[1 : len(val)-1]
continue
}
if val == "yes" {
c[nm] = true
continue
}
if val == "no" {
c[nm] = false
continue
}
n, err := strconv.ParseUint(val, 10, 31)
if err == nil {
c[nm] = int(n)
continue
}
return nil, fmt.Errorf("invalid libtool config line: %s", l)
}
if err := s.Err(); err != nil {
return nil, err
}
return c, nil
}
func newLibToolConfigFile(fn string) (libToolConfig, error) {
if x, ok := libToolConfigs[fn]; ok {
return x, nil
}
f, err := os.Open(fn)
if err != nil {
return nil, err
}
defer f.Close()
c, err := newLibToolConfig(f)
if err != nil {
return nil, err
}
libToolConfigs[fn] = c
return c, nil
}
func (c libToolConfig) dependencyLibs() ([]string, error) {
var r []string
rq, ok := c["dependency_libs"]
if !ok {
return nil, nil
}
s, ok := rq.(string)
if !ok {
return nil, fmt.Errorf("invalid dependency_libs value: %T(%#v)", rq, rq)
}
s = strings.TrimSpace(s)
a := strings.Split(s, " ")
for _, v := range a {
v = strings.TrimSpace(v)
if v == "" {
continue
}
if v == "-l" || !strings.HasPrefix(v, "-l") {
return nil, fmt.Errorf("invalid dependency_libs value: %v", s)
}
r = append(r, v[2:])
}
return r, nil
}