-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
compiler_linux.go
202 lines (166 loc) · 4.15 KB
/
compiler_linux.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
//go:build linux
package main
import (
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/codecat/go-libs/log"
)
type linuxCompiler struct {
toolset string
}
func (ci linuxCompiler) Compile(path, objDir string, options *CompilerOptions) error {
fileext := filepath.Ext(path)
filename := strings.TrimSuffix(filepath.Base(path), fileext)
args := make([]string, 0)
args = append(args, "-c")
args = append(args, "-o", filepath.Join(objDir, filename+".o"))
args = append(args, "-std=c++17")
// Set warnings flags
if options.Strict {
args = append(args, "-Wall")
args = append(args, "-Wextra")
args = append(args, "-Werror")
}
// Set debug flag
if options.Debug {
args = append(args, "-g")
}
// Add optimization flags
if options.Optimization == OptimizeSize {
args = append(args, "-Os")
} else if options.Optimization == OptimizeSpeed {
args = append(args, "-O3")
}
// Add C++ standard flag
if fileext != ".c" {
switch options.CPPStandard {
case CPPStandardLatest:
args = append(args, "-std=c++23")
case CPPStandard20:
args = append(args, "-std=c++20")
case CPPStandard17:
args = append(args, "-std=c++17")
case CPPStandard14:
args = append(args, "-std=c++14")
}
}
// Add C standard flag
if fileext == ".c" {
switch options.CStandard {
case CStandardLatest:
args = append(args, "-std=c2x")
case CStandard17:
args = append(args, "-std=c17")
case CStandard11:
args = append(args, "-std=c11")
}
}
// Add include directories
for _, dir := range options.IncludeDirectories {
args = append(args, "-I"+dir)
}
// Add precompiler definitions
for _, define := range options.Defines {
args = append(args, "-D"+define)
}
// Add additional compiler flags for C/C++
for _, flag := range options.CompilerFlagsCXX {
args = append(args, flag)
}
// Add additional compiler flags for C++
if fileext != ".c" {
for _, flag := range options.CompilerFlagsCPP {
args = append(args, flag)
}
}
// Add additional compiler flags for C
if fileext == ".c" {
for _, flag := range options.CompilerFlagsC {
args = append(args, flag)
}
}
args = append(args, path)
cmd := exec.Command(ci.toolset, args...)
if options.Verbose {
log.Trace("%s", strings.Join(cmd.Args, " "))
}
outputBytes, err := cmd.CombinedOutput()
if err != nil {
output := strings.Trim(string(outputBytes), "\r\n")
return errors.New(output)
}
return nil
}
func (ci linuxCompiler) Link(objDir, outPath string, outType LinkType, options *CompilerOptions) (string, error) {
args := make([]string, 0)
exeName := ci.toolset
switch outType {
case LinkExe:
case LinkDll:
outPath += ".so"
args = append(args, "-shared")
case LinkLib:
exeName = "ar"
outPath += ".a"
}
if outType == LinkLib {
// r = insert with replacement
// c = create new archie
// s = write an index
args = append(args, "rcs")
args = append(args, outPath)
} else {
args = append(args, "-o", outPath)
if ci.toolset == "gcc" {
args = append(args, "-static-libgcc")
args = append(args, "-static-libstdc++")
}
if options.Static {
args = append(args, "-static")
}
// Add additional library paths
for _, dir := range options.LinkDirectories {
args = append(args, "-L"+dir)
}
}
filepath.Walk(objDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || !strings.HasSuffix(path, ".o") {
return nil
}
args = append(args, path)
return nil
})
if outType != LinkLib {
// Link to some common standard libraries
args = append(args, "-lstdc++")
// Add libraries to link
for _, link := range options.LinkLibraries {
args = append(args, "-l"+link)
}
// Add additional linker flags
for _, flag := range options.LinkerFlags {
args = append(args, flag)
}
}
cmd := exec.Command(exeName, args...)
if options.Verbose {
log.Trace("%s", strings.Join(cmd.Args, " "))
}
outputBytes, err := cmd.CombinedOutput()
if err != nil {
output := strings.Trim(string(outputBytes), "\r\n")
return "", errors.New(output)
}
return outPath, nil
}
func (ci linuxCompiler) Clean(name string) {
os.Remove(name)
os.Remove(name + ".so")
os.Remove(name + ".a")
}