-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: Improve code for building a project
- Loading branch information
1 parent
9bff8af
commit e0fb44b
Showing
4 changed files
with
153 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.vscode | ||
.idea | ||
|
||
build | ||
example/build | ||
tests/build | ||
|
||
fireball |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package build | ||
|
||
import ( | ||
"fireball/core/codegen" | ||
"fireball/core/ir" | ||
"fireball/core/llvm" | ||
"fireball/core/workspace" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
func Build(project *workspace.Project, entrypoint *ir.Module, optimizationLevel uint8, outputName string) (string, error) { | ||
_ = os.Mkdir("build", 0750) | ||
|
||
// Emit project IR | ||
irPaths := make([]string, 0, len(project.Files)) | ||
|
||
for _, file := range project.Files { | ||
path := strings.ReplaceAll(file.Path, "/", "-") | ||
path = filepath.Join(project.Path, "build", path[:len(path)-3]+".ll") | ||
|
||
irFile, err := os.Create(path) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
module := codegen.Emit(file.AbsolutePath(), project.GetResolverFile(file.Ast), file.Ast) | ||
llvm.WriteText(module, irFile) | ||
|
||
_ = irFile.Close() | ||
|
||
irPaths = append(irPaths, path) | ||
} | ||
|
||
// Emit entrypoint IR | ||
entrypointPath := filepath.Join(project.Path, "build", "__entrypoint.ll") | ||
irPaths = append(irPaths, entrypointPath) | ||
|
||
entrypointFile, err := os.Create(entrypointPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
llvm.WriteText(entrypoint, entrypointFile) | ||
_ = entrypointFile.Close() | ||
|
||
// Compile | ||
c := Compiler{ | ||
OptimizationLevel: min(max(int(optimizationLevel), 0), 3), | ||
} | ||
|
||
for _, irPath := range irPaths { | ||
c.AddInput(irPath) | ||
} | ||
|
||
if runtime.GOOS == "darwin" { | ||
c.AddLibrary("System") | ||
} else { | ||
c.AddLibrary("m") | ||
c.AddLibrary("c") | ||
} | ||
|
||
for _, library := range project.Config.LinkLibraries { | ||
c.AddLibrary(library) | ||
} | ||
|
||
output := filepath.Join(project.Path, "build", outputName) | ||
|
||
err = c.Compile(output) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return output, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package build | ||
|
||
import ( | ||
"fireball/core/utils" | ||
"fireball/core/workspace" | ||
"fmt" | ||
"github.com/fatih/color" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func Report(project *workspace.Project) { | ||
reporter := consoleReporter{ | ||
error: color.New(color.FgRed), | ||
warning: color.New(color.FgYellow), | ||
} | ||
|
||
for _, file := range project.Files { | ||
for _, diagnostic := range file.Diagnostics() { | ||
reporter.Report(file, diagnostic) | ||
} | ||
} | ||
|
||
if reporter.errorCount > 0 { | ||
fmt.Println() | ||
_, _ = color.New(color.FgRed).Print("Build failed") | ||
|
||
if reporter.errorCount == 1 { | ||
fmt.Printf(", with %d error\n", reporter.errorCount) | ||
} else { | ||
fmt.Printf(", with %d errors\n", reporter.errorCount) | ||
} | ||
|
||
os.Exit(1) | ||
} | ||
|
||
if reporter.hadDiagnostic { | ||
fmt.Println() | ||
} | ||
} | ||
|
||
type consoleReporter struct { | ||
error *color.Color | ||
warning *color.Color | ||
|
||
hadDiagnostic bool | ||
errorCount int | ||
} | ||
|
||
func (c *consoleReporter) Report(file *workspace.File, diag utils.Diagnostic) { | ||
path, err := filepath.Rel(file.Project.Config.Src, file.Path) | ||
if err != nil { | ||
path = file.Path | ||
} | ||
|
||
msg := fmt.Sprintf("[%s:%d:%d] %s", path, diag.Range.Start.Line, diag.Range.Start.Column+1, diag.Message) | ||
|
||
if diag.Kind == utils.ErrorKind { | ||
_, _ = c.error.Fprint(os.Stderr, "ERROR ") | ||
_, _ = fmt.Fprintln(os.Stderr, msg) | ||
|
||
c.errorCount++ | ||
} else { | ||
_, _ = c.warning.Print("WARNING ") | ||
fmt.Println(msg) | ||
} | ||
|
||
c.hadDiagnostic = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters