Skip to content

Commit

Permalink
Change crosscc names and more crosscc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Edoardo Giaimo authored and ptia committed Jul 6, 2021
1 parent cdcd2e6 commit 02a3e8e
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 234 deletions.
96 changes: 96 additions & 0 deletions RULES/crosscc/crosscc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package crosscc

import (
"fmt"

"dbt-rules/RULES/cc"
"dbt-rules/RULES/core"

"libsupcxx/RULES/crosscc/crosslib"
"libsupcxx/RULES/platform"
"libsupcxx/libruncxx"
libsupcxx "libsupcxx/libsupcxx/src"
)

// CrossLibrary and CrossBinary are cross-compilation rules. Each CrossLibrary / CrossBinary instance defines rules
// for multiple platforms (given in the Platforms field, defaults to all available platforms).
// When the CrossLibrary / CrossBinary is built, it is compiled for all its platforms (internally, each
// goes inside its own subdirectory). Other rules can access a specific CrossLibrary / CrossBinary build
// with the ForPlatform(platform.Platform) function. XLibraries / XBinares can depend on XLibraries,
// so that the dependency is built for the same platform as the CrossLibrary / CrossBinary that uses it.
//
// CrossBinary describes a full executable, so it is also linked with the boot libraries

type CrossLibrary = crosslib.CrossLibrary

type CrossBinary struct {
Out core.OutPath
Srcs []core.Path
CompilerFlags []string
XDeps []CrossLibrary
Script core.Path
Platforms []platform.Platform
}

func (xBin CrossBinary) platforms() []platform.Platform {
if xBin.Platforms == nil {
return platform.AllPlatforms()
}
return xBin.Platforms
}

func (xBin CrossBinary) ForPlatform(p platform.Platform) cc.Binary {
if xBin.Platforms != nil && !platform.ContainsPlatform(xBin.Platforms, p) {
core.Fatal("CrossBinary %s does not support platform %s", xBin.Out, p.Name)
}
return xBin.forPlatform(p)
}

func (xBin CrossBinary) forPlatform(p platform.Platform) cc.Binary {
xDeps := []CrossLibrary{libruncxx.XLib, libsupcxx.XLib}
xDeps = append(xDeps, xBin.XDeps...)

deps := []cc.Dep{p.BootLast}
deps = append(deps, crosslib.AllForPlatform(xDeps, p)...)
deps = append(deps, p.BootFirst)

return cc.Binary{
Out: crosslib.OutPathForPlatform(xBin.Out, p),
Srcs: xBin.Srcs,
Deps: deps,
CompilerFlags: xBin.CompilerFlags,
Script: p.LinkerScript,
Toolchain: p.GccToolchain,
}
}

func hexOutForElf(elfOut core.OutPath) core.OutPath {
return elfOut.WithExt("hex")
}

func (xBin CrossBinary) Build(ctx core.Context) {
for _, p := range xBin.platforms() {
ccBin := xBin.forPlatform(p)
ccBin.Build(ctx)
ctx.AddBuildStep(core.BuildStep{
Out: hexOutForElf(ccBin.Out),
In: ccBin.Out,
Cmd: fmt.Sprintf(
"%q -O binary %q %q",
p.GccToolchain.Objcopy,
ccBin.Out,
hexOutForElf(ccBin.Out),
),
Descr: fmt.Sprintf("Creating raw binary %s", hexOutForElf(ccBin.Out)),
})
}
}

func (xBin CrossBinary) Outputs() []core.Path {
outputs := []core.Path{}
for _, p := range xBin.platforms() {
elfOut := crosslib.OutPathForPlatform(xBin.Out, p)
outputs = append(outputs, elfOut, hexOutForElf(elfOut))
}
return outputs
}
69 changes: 69 additions & 0 deletions RULES/crosscc/crosslib/crosslib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package crosslib

import (
"path"

"dbt-rules/RULES/cc"
"dbt-rules/RULES/core"

"libsupcxx/RULES/platform"
"libsupcxx/libsupcxx/include"
)

// This package is only meant to be used within libsupcxx, outside code should use
// libsupcxx/RULES/libsupcxx. See there for info about CrossLibrary.
type CrossLibrary struct {
Out core.OutPath
Srcs []core.Path
Includes []core.Path
CompilerFlags []string
XDeps []CrossLibrary
Shared bool
AlwaysLink bool
Platforms []platform.Platform
}

func OutPathForPlatform(base core.OutPath, p platform.Platform) core.OutPath {
return base.WithPrefix("/" + path.Base(base.Absolute()) + "-" + p.Name + "/")
}

func (xLib CrossLibrary) platforms() []platform.Platform {
if xLib.Platforms == nil {
return platform.AllPlatforms()
}
return xLib.Platforms
}

func AllForPlatform(xLibs []CrossLibrary, p platform.Platform) []cc.Dep {
libs := make([]cc.Dep, len(xLibs))
for i, xLib := range xLibs {
libs[i] = xLib.ForPlatform(p)
}
return libs
}

func (xLib CrossLibrary) ForPlatform(p platform.Platform) cc.Library {
if xLib.Platforms != nil && !platform.ContainsPlatform(xLib.Platforms, p) {
core.Fatal("CrossLibrary %s does not support platform %s", xLib.Out, p.Name)
}
return xLib.forPlatform(p)
}

func (xLib CrossLibrary) forPlatform(p platform.Platform) cc.Library {
return cc.Library{
Out: OutPathForPlatform(xLib.Out, p),
Srcs: xLib.Srcs,
Includes: append(xLib.Includes, include.Headers...),
CompilerFlags: xLib.CompilerFlags,
Deps: AllForPlatform(xLib.XDeps, p),
Shared: xLib.Shared,
AlwaysLink: xLib.AlwaysLink,
Toolchain: p.GccToolchain,
}
}

func (xLib CrossLibrary) Build(ctx core.Context) {
for _, p := range xLib.platforms() {
xLib.forPlatform(p).Build(ctx)
}
}
98 changes: 0 additions & 98 deletions RULES/libsupcxx/libsupcxx.go

This file was deleted.

76 changes: 0 additions & 76 deletions RULES/libsupcxx/xlib/xlib.go

This file was deleted.

37 changes: 37 additions & 0 deletions RULES/platform/platform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package platform

import (
"dbt-rules/RULES/cc"
"dbt-rules/RULES/core"
)

type Platform struct {
Name string
BootFirst cc.Library
BootLast cc.Library
LinkerScript core.Path
GccToolchain cc.GccToolchain
}

var platforms = []Platform{}

func ContainsPlatform(ps []Platform, p Platform) bool {
for _, el := range ps {
if el.Name == p.Name {
return true
}
}
return false
}

func (p Platform) Register() Platform {
if ContainsPlatform(platforms, p) {
core.Fatal("A platform with name %s has already been registered", p.Name)
}
platforms = append(platforms, p)
return p
}

func AllPlatforms() []Platform {
return platforms
}
Loading

0 comments on commit 02a3e8e

Please sign in to comment.