From dea660b21c9dff3122d975028a167c9998e3cc3f Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 25 Nov 2018 18:05:29 +0100 Subject: [PATCH] main: compile C source files in packages TODO: C++ etc. files --- compiler/compiler.go | 4 ++++ ir/ir.go | 12 +++++++----- main.go | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/compiler/compiler.go b/compiler/compiler.go index e8223727..8b5adab6 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.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 diff --git a/ir/ir.go b/ir/ir.go index 65558b8a..91822695 100644 --- a/ir/ir.go +++ b/ir/ir.go @@ -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 { diff --git a/main.go b/main.go index 77c76fb3..1bcd01c4 100644 --- a/main.go +++ b/main.go @@ -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