forked from rkt/rkt
/
cmake.go
234 lines (202 loc) · 5.6 KB
/
cmake.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
package proj2aci
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/schema"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/schema/types"
"github.com/coreos/rkt/Godeps/_workspace/src/golang.org/x/tools/go/vcs"
)
type CmakeConfiguration struct {
CommonConfiguration
BinDir string
ReuseSrcDir string
CmakeParams []string
}
type CmakePaths struct {
CommonPaths
src string
build string
install string
binDir string
}
type CmakeCustomizations struct {
Configuration CmakeConfiguration
paths CmakePaths
fullBinPath string
}
func (custom *CmakeCustomizations) Name() string {
return "cmake"
}
func (custom *CmakeCustomizations) GetCommonConfiguration() *CommonConfiguration {
return &custom.Configuration.CommonConfiguration
}
func (custom *CmakeCustomizations) ValidateConfiguration() error {
if !DirExists(custom.Configuration.ReuseSrcDir) {
return fmt.Errorf("Invalid src dir to reuse")
}
return nil
}
func (custom *CmakeCustomizations) GetCommonPaths() *CommonPaths {
return &custom.paths.CommonPaths
}
func (custom *CmakeCustomizations) SetupPaths() error {
setupReusableDir(&custom.paths.src, custom.Configuration.ReuseSrcDir, filepath.Join(custom.paths.TmpDir, "src"))
custom.paths.build = filepath.Join(custom.paths.TmpDir, "build")
custom.paths.install = filepath.Join(custom.paths.TmpDir, "install")
return nil
}
func setupReusableDir(path *string, reusePath, stockPath string) {
if path == nil {
panic("path in setupReusableDir cannot be nil")
}
if reusePath != "" {
*path = reusePath
} else {
*path = stockPath
}
}
func (custom *CmakeCustomizations) GetDirectoriesToMake() []string {
dirs := []string{
custom.paths.build,
custom.paths.install,
}
// not creating custom.paths.src, because go.vcs requires the
// src directory to be nonexistent
return dirs
}
func (custom *CmakeCustomizations) PrepareProject() error {
if custom.Configuration.ReuseSrcDir == "" {
if err := custom.createRepo(); err != nil {
return err
}
}
Info("Running cmake")
if err := custom.runCmake(); err != nil {
return err
}
Info("Running make")
if err := custom.runMake(); err != nil {
return err
}
Info("Running make install")
if err := custom.runMakeInstall(); err != nil {
return err
}
return nil
}
func (custom *CmakeCustomizations) createRepo() error {
Info(fmt.Sprintf("Downloading %s", custom.Configuration.Project))
repo, err := vcs.RepoRootForImportPath(custom.Configuration.Project, false)
if err != nil {
return err
}
return repo.VCS.Create(custom.paths.src, repo.Repo)
}
func (custom *CmakeCustomizations) runCmake() error {
args := []string{"cmake"}
args = append(args, custom.Configuration.CmakeParams...)
args = append(args, custom.paths.src)
return RunCmd(args, nil, custom.paths.build)
}
func (custom *CmakeCustomizations) runMake() error {
args := []string{
"make",
fmt.Sprintf("-j%d", runtime.NumCPU()),
}
return RunCmd(args, nil, custom.paths.build)
}
func (custom *CmakeCustomizations) runMakeInstall() error {
args := []string{
"make",
"install",
}
env := append(os.Environ(), "DESTDIR="+custom.paths.install)
return RunCmd(args, env, custom.paths.build)
}
func (custom *CmakeCustomizations) GetPlaceholderMapping() map[string]string {
return map[string]string{
"<SRCPATH>": custom.paths.src,
"<BUILDPATH>": custom.paths.build,
"<INSTALLPATH>": custom.paths.install,
}
}
func (custom *CmakeCustomizations) GetAssets(aciBinDir string) ([]string, error) {
binaryName, err := custom.GetBinaryName()
if err != nil {
return nil, err
}
rootBinary := filepath.Join(aciBinDir, binaryName)
return []string{GetAssetString(rootBinary, custom.fullBinPath)}, nil
}
func (custom *CmakeCustomizations) getBinDir() (string, error) {
if custom.Configuration.BinDir != "" {
return filepath.Join(custom.paths.install, custom.Configuration.BinDir), nil
}
dirs := []string{
"/usr/local/sbin",
"/usr/local/bin",
"/usr/sbin",
"/usr/bin",
"/sbin",
"/bin",
}
for _, dir := range dirs {
path := filepath.Join(custom.paths.install, dir)
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
continue
}
return "", err
}
return path, nil
}
return "", fmt.Errorf("Could not find any bin directory")
}
func (custom *CmakeCustomizations) GetImageName() (*types.ACIdentifier, error) {
imageName := custom.Configuration.Project
if filepath.Base(imageName) == "..." {
imageName = filepath.Dir(imageName)
if custom.Configuration.UseBinary != "" {
imageName += "-" + custom.Configuration.UseBinary
}
}
return types.NewACIdentifier(strings.ToLower(imageName))
}
func (custom *CmakeCustomizations) GetBinaryName() (string, error) {
if err := custom.findFullBinPath(); err != nil {
return "", err
}
return filepath.Base(custom.fullBinPath), nil
}
func (custom *CmakeCustomizations) findFullBinPath() error {
if custom.fullBinPath != "" {
return nil
}
binDir, err := custom.getBinDir()
if err != nil {
return err
}
binary, err := GetBinaryName(binDir, custom.Configuration.UseBinary)
if err != nil {
return err
}
custom.fullBinPath = filepath.Join(binDir, binary)
return nil
}
func (custom *CmakeCustomizations) GetRepoPath() (string, error) {
return custom.paths.src, nil
}
func (custom *CmakeCustomizations) GetImageFileName() (string, error) {
base := filepath.Base(custom.Configuration.Project)
if base == "..." {
base = filepath.Base(filepath.Dir(custom.Configuration.Project))
if custom.Configuration.UseBinary != "" {
base += "-" + custom.Configuration.UseBinary
}
}
return base + schema.ACIExtension, nil
}