-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
163 additions
and
113 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
// Context contains all the build system states that have to be remembered. | ||
type Context struct { | ||
// Name is the name of the project. | ||
Name string | ||
|
||
// SourceFiles contains paths to all the .c and .cpp files that have to be compiled. | ||
SourceFiles []string | ||
|
||
// ObjectPath is the intermediate folder where object files should be stored. | ||
ObjectPath string | ||
|
||
// Compiler is an abstract interface used for compiling and linking on multiple platforms. | ||
Compiler Compiler | ||
CompilerErrors int | ||
CompilerOptions *CompilerOptions | ||
CompilerWorkerChannel chan CompilerWorkerTask | ||
CompilerWorkerFinished chan int | ||
} | ||
|
||
// NewContext creates a new context with initial values. | ||
func NewContext() (*Context, error) { | ||
compiler, err := getCompiler() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Context{ | ||
Compiler: compiler, | ||
CompilerOptions: &CompilerOptions{}, | ||
|
||
SourceFiles: make([]string, 0), | ||
}, 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
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,82 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/codecat/go-libs/log" | ||
) | ||
|
||
// CompilerWorkerTask describes a task for the compiler worker | ||
type CompilerWorkerTask struct { | ||
path string | ||
outputDir string | ||
} | ||
|
||
func compileWorker(ctx *Context, num int) { | ||
for { | ||
// Get a task | ||
task, ok := <-ctx.CompilerWorkerChannel | ||
if !ok { | ||
break | ||
} | ||
|
||
// Log the file we're currently compiling | ||
fileForward := strings.Replace(task.path, "\\", "/", -1) | ||
log.Info("%s", fileForward) | ||
|
||
// Invoke the compiler | ||
err := ctx.Compiler.Compile(task.path, task.outputDir, ctx.CompilerOptions) | ||
if err != nil { | ||
log.Error("Failed to compile %s!\n%s", fileForward, err.Error()) | ||
ctx.CompilerErrors++ | ||
} | ||
} | ||
|
||
ctx.CompilerWorkerFinished <- num | ||
} | ||
|
||
func performCompilation(ctx *Context) { | ||
// Prepare worker channels | ||
ctx.CompilerWorkerChannel = make(chan CompilerWorkerTask) | ||
ctx.CompilerWorkerFinished = make(chan int) | ||
|
||
// Start compiler worker routines | ||
numWorkers := runtime.NumCPU() | ||
if len(ctx.SourceFiles) < numWorkers { | ||
numWorkers = len(ctx.SourceFiles) | ||
} | ||
for i := 0; i < numWorkers; i++ { | ||
go compileWorker(ctx, i) | ||
} | ||
|
||
// Compile all the source files | ||
for _, file := range ctx.SourceFiles { | ||
// The output dir will be a sub-folder in the object directory | ||
dir := filepath.Dir(file) | ||
outputDir := filepath.Join(ctx.ObjectPath, dir) | ||
|
||
err := os.MkdirAll(outputDir, 0777) | ||
if err != nil { | ||
log.Error("Unable to create output directory %s: %s", outputDir, err.Error()) | ||
ctx.CompilerErrors++ | ||
continue | ||
} | ||
|
||
// Send the task to an available worker | ||
ctx.CompilerWorkerChannel <- CompilerWorkerTask{ | ||
path: file, | ||
outputDir: outputDir, | ||
} | ||
} | ||
|
||
// Close the worker channel | ||
close(ctx.CompilerWorkerChannel) | ||
|
||
// Wait for all workers to finish compiling | ||
for i := 0; i < numWorkers; i++ { | ||
<-ctx.CompilerWorkerFinished | ||
} | ||
} |
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,21 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func performLinking(ctx *Context) (string, error) { | ||
// Get the link type | ||
linkType := LinkExe | ||
switch viper.GetString("type") { | ||
case "exe": | ||
linkType = LinkExe | ||
case "dll": | ||
linkType = LinkDll | ||
case "lib": | ||
linkType = LinkLib | ||
} | ||
|
||
// Invoke the linker | ||
return ctx.Compiler.Link(ctx.ObjectPath, ctx.Name, linkType, ctx.CompilerOptions) | ||
} |