|
16 | 16 | package preprocessor
|
17 | 17 |
|
18 | 18 | import (
|
| 19 | + "bytes" |
19 | 20 | "context"
|
20 | 21 | "errors"
|
21 | 22 | "fmt"
|
@@ -77,10 +78,42 @@ func GCC(
|
77 | 78 | if err != nil {
|
78 | 79 | return Result{}, err
|
79 | 80 | }
|
80 |
| - stdout, stderr, err := proc.RunAndCaptureOutput(ctx) |
81 | 81 |
|
82 |
| - // Append gcc arguments to stdout |
83 |
| - stdout = append([]byte(fmt.Sprintln(strings.Join(args, " "))), stdout...) |
| 82 | + stdout := bytes.NewBuffer(nil) |
| 83 | + stderr := bytes.NewBuffer(nil) |
84 | 84 |
|
85 |
| - return Result{args: proc.GetArgs(), stdout: stdout, stderr: stderr}, err |
| 85 | + ctx, cancel := context.WithCancel(ctx) |
| 86 | + defer cancel() |
| 87 | + count := 0 |
| 88 | + stderrLimited := writerFunc(func(p []byte) (int, error) { |
| 89 | + // Limit the size of the stderr buffer to 100KB |
| 90 | + n, err := stderr.Write(p) |
| 91 | + count += n |
| 92 | + if count > 100*1024 { |
| 93 | + cancel() |
| 94 | + fmt.Fprintln(stderr, i18n.Tr("Compiler error output has been truncated.")) |
| 95 | + } |
| 96 | + return n, err |
| 97 | + }) |
| 98 | + |
| 99 | + proc.RedirectStdoutTo(stdout) |
| 100 | + proc.RedirectStderrTo(stderrLimited) |
| 101 | + |
| 102 | + // Append gcc arguments to stdout before running the command |
| 103 | + fmt.Fprintln(stdout, strings.Join(args, " ")) |
| 104 | + |
| 105 | + if err := proc.Start(); err != nil { |
| 106 | + return Result{}, err |
| 107 | + } |
| 108 | + |
| 109 | + // Wait for the process to finish |
| 110 | + err = proc.WaitWithinContext(ctx) |
| 111 | + |
| 112 | + return Result{args: proc.GetArgs(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err |
| 113 | +} |
| 114 | + |
| 115 | +type writerFunc func(p []byte) (n int, err error) |
| 116 | + |
| 117 | +func (f writerFunc) Write(p []byte) (n int, err error) { |
| 118 | + return f(p) |
86 | 119 | }
|
0 commit comments