Browse Source
main: compile C source files in packages
TODO: C++ etc. files
pull/89/head
Ayke van Laethem
6 years ago
No known key found for this signature in database
GPG Key ID: E97FF5335DFDFDED
3 changed files with
28 additions and
5 deletions
-
compiler/compiler.go
-
ir/ir.go
-
main.go
|
|
@ -149,6 +149,10 @@ func NewCompiler(pkgName string, config Config) (*Compiler, error) { |
|
|
|
return c, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (c *Compiler) Packages() []*loader.Package { |
|
|
|
return c.ir.LoaderProgram.Sorted() |
|
|
|
} |
|
|
|
|
|
|
|
// Return the LLVM module. Only valid after a successful compile.
|
|
|
|
func (c *Compiler) Module() llvm.Module { |
|
|
|
return c.mod |
|
|
|
|
|
@ -19,6 +19,7 @@ import ( |
|
|
|
// results.
|
|
|
|
type Program struct { |
|
|
|
Program *ssa.Program |
|
|
|
LoaderProgram *loader.Program |
|
|
|
mainPkg *ssa.Package |
|
|
|
Functions []*Function |
|
|
|
functionMap map[*ssa.Function]*Function |
|
|
@ -172,11 +173,12 @@ func NewProgram(lprogram *loader.Program, mainPath string) *Program { |
|
|
|
} |
|
|
|
|
|
|
|
p := &Program{ |
|
|
|
Program: program, |
|
|
|
mainPkg: mainPkg, |
|
|
|
functionMap: make(map[*ssa.Function]*Function), |
|
|
|
globalMap: make(map[*ssa.Global]*Global), |
|
|
|
comments: comments, |
|
|
|
Program: program, |
|
|
|
LoaderProgram: lprogram, |
|
|
|
mainPkg: mainPkg, |
|
|
|
functionMap: make(map[*ssa.Function]*Function), |
|
|
|
globalMap: make(map[*ssa.Global]*Global), |
|
|
|
comments: comments, |
|
|
|
} |
|
|
|
|
|
|
|
for _, pkg := range packageList { |
|
|
|
|
|
@ -192,6 +192,23 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act |
|
|
|
ldflags = append(ldflags, outpath) |
|
|
|
} |
|
|
|
|
|
|
|
// Compile C files in packages.
|
|
|
|
for i, pkg := range c.Packages() { |
|
|
|
for _, file := range pkg.CFiles { |
|
|
|
path := filepath.Join(pkg.Package.Dir, file) |
|
|
|
outpath := filepath.Join(dir, "pkg"+strconv.Itoa(i)+"-"+file+".o") |
|
|
|
cmd := exec.Command(spec.Compiler, append(spec.CFlags, "-c", "-o", outpath, path)...) |
|
|
|
cmd.Stdout = os.Stdout |
|
|
|
cmd.Stderr = os.Stderr |
|
|
|
cmd.Dir = sourceDir() |
|
|
|
err := cmd.Run() |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
ldflags = append(ldflags, outpath) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Link the object files together.
|
|
|
|
cmd := exec.Command(spec.Linker, ldflags...) |
|
|
|
cmd.Stdout = os.Stdout |
|
|
|