-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpackage.go
97 lines (77 loc) · 2.24 KB
/
package.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
package main
import (
"os/exec"
"runtime"
"strings"
"github.com/mattn/go-shellwords"
"github.com/spf13/viper"
)
// Package contains basic information about a library.
type Package struct {
Name string
}
func addPackage(options *CompilerOptions, name string) *Package {
if ret := addPackageLocal(options, name); ret != nil {
return ret
}
//TODO: Implement global packages
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
if ret := addPackagePkgconfig(options, name); ret != nil {
return ret
}
}
//TODO: Implement vcpkg
//if runtime.GOOS == "windows" {
//}
return nil
}
func addPackageLocal(options *CompilerOptions, name string) *Package {
packageInfo := viper.GetStringMap("package." + name)
if len(packageInfo) == 0 {
return nil
}
return configurePackageFromConfig(options, packageInfo, name)
}
func addPackagePkgconfig(options *CompilerOptions, name string) *Package {
// pkg-config must be installed for this to work
_, err := exec.LookPath("pkg-config")
if err != nil {
return nil
}
cmdCflags := exec.Command("pkg-config", name, "--cflags")
outputCflags, err := cmdCflags.CombinedOutput()
if err != nil {
return nil
}
cmdLibs := exec.Command("pkg-config", name, "--libs")
outputLibs, err := cmdLibs.CombinedOutput()
if err != nil {
return nil
}
parseCflags, _ := shellwords.Parse(strings.Trim(string(outputCflags), "\r\n"))
parseLibs, _ := shellwords.Parse(strings.Trim(string(outputLibs), "\r\n"))
options.CompilerFlagsCXX = append(options.CompilerFlagsCXX, parseCflags...)
options.LinkerFlags = append(options.LinkerFlags, parseLibs...)
return &Package{
Name: name,
}
}
func configurePackageFromConfig(options *CompilerOptions, pkg map[string]interface{}, name string) *Package {
maybeUnpack(&options.IncludeDirectories, pkg["includes"])
maybeUnpack(&options.LinkDirectories, pkg["linkdirs"])
maybeUnpack(&options.LinkLibraries, pkg["links"])
maybeUnpack(&options.Defines, pkg["defines"])
maybeUnpack(&options.CompilerFlagsCXX, pkg["cflags"])
maybeUnpack(&options.LinkerFlags, pkg["lflags"])
return &Package{
Name: name,
}
}
func maybeUnpack(dest *[]string, src interface{}) {
if src == nil {
return
}
for _, val := range src.([]interface{}) {
(*dest) = append(*dest, val.(string))
}
}