forked from icexin/eggos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
373 lines (322 loc) · 8.13 KB
/
magefile.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var (
TOOLPREFIX = detectToolPrefix()
CC = TOOLPREFIX + "gcc"
LD = TOOLPREFIX + "ld"
CFLAGS = initCflags()
LDFLAGS = initLdflags()
)
var (
GOTAGS = "nes phy prometheus"
GOGCFLAGS = ""
)
var (
QEMU64 = "qemu-system-x86_64"
QEMU32 = "qemu-system-i386"
QEMU_OPT = initQemuOpt()
QEMU_DEBUG_OPT = initQemuDebugOpt()
)
var (
eggBin string
)
const (
goMajorVersionSupported = 1
maxGoMinorVersionSupported = 16
)
// Kernel target build the elf kernel for eggos, generate kernel.elf
func Kernel() error {
mg.Deps(Egg)
detectGoVersion()
return rundir("app", nil, eggBin, "build", "-o", "../kernel.elf",
"-gcflags", GOGCFLAGS,
"-tags", GOTAGS,
"./kmain")
}
func Boot64() error {
compileCfile("boot/boot64.S", "-m64")
compileCfile("boot/boot64main.c", "-m64")
ldflags := "-Ttext 0x3200000 -m elf_x86_64 -o boot64.elf boot64.o boot64main.o"
ldArgs := append([]string{}, LDFLAGS...)
ldArgs = append(ldArgs, strings.Fields(ldflags)...)
return sh.RunV(LD, ldArgs...)
}
// Multiboot target build Multiboot specification compatible elf format, generate multiboot.elf
func Multiboot() error {
mg.Deps(Boot64)
compileCfile("boot/multiboot.c", "-m32")
compileCfile("boot/multiboot_header.S", "-m32")
ldflags := "-Ttext 0x3300000 -m elf_i386 -o multiboot.elf multiboot.o multiboot_header.o -b binary boot64.elf"
ldArgs := append([]string{}, LDFLAGS...)
ldArgs = append(ldArgs, strings.Fields(ldflags)...)
err := sh.RunV(LD, ldArgs...)
if err != nil {
return err
}
return sh.Copy(
filepath.Join("cmd", "egg", "assets", "boot", "multiboot.elf"),
"multiboot.elf",
)
}
func Test() error {
mg.Deps(Egg)
envs := map[string]string{
"QEMU_OPTS": quoteArgs(QEMU_OPT),
}
return rundir("tests", envs, eggBin, "test")
}
func TestDebug() error {
mg.Deps(Egg)
envs := map[string]string{
"QEMU_OPTS": quoteArgs(QEMU_DEBUG_OPT),
}
return rundir("tests", envs, eggBin, "test")
}
// Qemu run multiboot.elf on qemu.
// If env QEMU_ACCEL is set,QEMU acceleration will be enabled.
// If env QEMU_GRAPHIC is set QEMU will run in graphic mode.
// Use Crtl+a c to switch console, and type `quit`
func Qemu() error {
mg.Deps(Kernel)
detectQemu()
return eggrun(QEMU_OPT, "kernel.elf")
}
// QemuDebug run multiboot.elf in debug mode.
// Monitor GDB connection on port 1234
func QemuDebug() error {
GOGCFLAGS += " -N -l"
mg.Deps(Kernel)
detectQemu()
return eggrun(QEMU_DEBUG_OPT, "kernel.elf")
}
// Iso generate eggos.iso, which can be used with qemu -cdrom option.
func Iso() error {
mg.Deps(Kernel)
return sh.RunV(eggBin, "pack", "-o", "eggos.iso", "-k", "kernel.elf")
}
// Graphic run eggos.iso on qemu, which vbe is enabled.
func Graphic() error {
detectQemu()
mg.Deps(Iso)
return eggrun(QEMU_OPT, "eggos.iso")
}
// GraphicDebug run eggos.iso on qemu in debug mode.
func GraphicDebug() error {
detectQemu()
GOGCFLAGS += " -N -l"
mg.Deps(Iso)
return eggrun(QEMU_DEBUG_OPT, "eggos.iso")
}
func Egg() error {
err := rundir("cmd", nil, "go", "build", "-o", "../egg", "./egg")
if err != nil {
return err
}
current, _ := os.Getwd()
eggBin = filepath.Join(current, "egg")
return nil
}
func Clean() {
rmGlob("*.o")
rmGlob("kernel.elf")
rmGlob("multiboot.elf")
rmGlob("qemu.log")
rmGlob("qemu.pcap")
rmGlob("eggos.iso")
rmGlob("egg")
rmGlob("boot64.elf")
rmGlob("bochs.log")
}
func detectToolPrefix() string {
prefix := os.Getenv("TOOLPREFIX")
if prefix != "" {
return prefix
}
if hasOutput("elf32-i386", "x86_64-elf-objdump", "-i") {
return "x86_64-elf-"
}
if hasOutput("elf32-i386", "i386-elf-objdump", "-i") {
return "i386-elf-"
}
if hasOutput("elf32-i386", "objdump", "-i") {
return ""
}
panic(`
*** Error: Couldn't find an i386-*-elf or x86_64-*-elf version of GCC/binutils
*** Is the directory with i386-elf-gcc or x86_64-elf-gcc in your PATH?
*** If your i386/x86_64-*-elf toolchain is installed with a command
*** prefix other than 'i386/x86_64-elf-', set your TOOLPREFIX
*** environment variable to that prefix and run 'make' again.
`)
}
var goVersionRegexp = regexp.MustCompile(`go(\d+)\.(\d+)\.?(\d?)`)
func goVersion() (string, int, int, error) {
versionBytes, err := cmdOutput(gobin(), "version")
if err != nil {
return "", 0, 0, err
}
version := strings.TrimSpace(string(versionBytes))
result := goVersionRegexp.FindStringSubmatch(version)
if len(result) < 3 {
return "", 0, 0, fmt.Errorf("use of unreleased go version `%s`, may not work", version)
}
major, _ := strconv.Atoi(result[1])
minor, _ := strconv.Atoi(result[2])
return version, major, minor, nil
}
func detectGoVersion() {
version, major, minor, err := goVersion()
if err != nil {
fmt.Printf("warning: %s\n", err)
return
}
if !(major == goMajorVersionSupported && minor <= maxGoMinorVersionSupported) {
fmt.Printf("warning: max supported go version go%d.%d.x, found go version `%s`, may not work\n",
goMajorVersionSupported, maxGoMinorVersionSupported, version,
)
return
}
}
func gobin() string {
goroot := os.Getenv("EGGOS_GOROOT")
if goroot != "" {
return filepath.Join(goroot, "bin", "go")
}
return "go"
}
func detectQemu() {
if !hasCommand(QEMU64) {
panic(QEMU64 + ` command not found`)
}
}
func accelArg() []string {
switch runtime.GOOS {
case "darwin":
return []string{"-M", "accel=hvf"}
default:
// fmt.Printf("accel method not found")
return nil
}
}
func initCflags() []string {
cflags := strings.Fields("-fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -Werror -fno-omit-frame-pointer -I. -nostdinc")
if hasOutput("-fno-stack-protector", CC, "--help") {
cflags = append(cflags, "-fno-stack-protector")
}
if hasOutput("[^f]no-pie", CC, "-dumpspecs") {
cflags = append(cflags, "-fno-pie", "-no-pie")
}
if hasOutput("[^f]nopie", CC, "-dumpspecs") {
cflags = append(cflags, "-fno-pie", "-nopie")
}
return cflags
}
func initLdflags() []string {
ldflags := strings.Fields("-N -e _start")
return ldflags
}
func initQemuOpt() []string {
var opts []string
if os.Getenv("QEMU_ACCEL") != "" {
opts = append(opts, accelArg()...)
}
if os.Getenv("QEMU_GRAPHIC") == "" {
opts = append(opts, "-nographic")
}
return opts
}
func initQemuDebugOpt() []string {
opts := `
-d int -D qemu.log
-object filter-dump,id=f1,netdev=eth0,file=qemu.pcap
-s -S
`
ret := append([]string{}, initQemuOpt()...)
ret = append(ret, strings.Fields(opts)...)
return ret
}
func compileCfile(file string, extFlags ...string) {
args := append([]string{}, CFLAGS...)
args = append(args, extFlags...)
args = append(args, "-c", file)
err := sh.RunV(CC, args...)
if err != nil {
panic(err)
}
}
func rundir(dir string, envs map[string]string, cmd string, args ...string) error {
current, _ := os.Getwd()
os.Chdir(dir)
defer os.Chdir(current)
return sh.RunWithV(envs, cmd, args...)
}
func eggrun(qemuArgs []string, flags ...string) error {
qemuOpts := quoteArgs(qemuArgs)
var args []string
args = append(args, "run")
args = append(args, "-p", "8080:80")
args = append(args, flags...)
envs := map[string]string{
"QEMU_OPTS": qemuOpts,
}
return sh.RunWithV(envs, eggBin, args...)
}
func cmdOutput(cmd string, args ...string) ([]byte, error) {
return exec.Command(cmd, args...).CombinedOutput()
}
// quote string which has spaces with ""
func quoteArgs(args []string) string {
var ret []string
for _, s := range args {
if strings.Index(s, " ") != -1 {
ret = append(ret, strconv.Quote(s))
} else {
ret = append(ret, s)
}
}
return strings.Join(ret, " ")
}
func hasCommand(cmd string) bool {
_, err := exec.LookPath(cmd)
if err != nil {
return false
}
return true
}
func hasOutput(regstr, cmd string, args ...string) bool {
out, err := cmdOutput(cmd, args...)
if err != nil {
return false
}
match, err := regexp.Match(regstr, []byte(out))
if err != nil {
return false
}
return match
}
func rmGlob(patten string) error {
match, err := filepath.Glob(patten)
if err != nil {
return err
}
for _, file := range match {
err = os.Remove(file)
if err != nil {
return err
}
}
return nil
}