1 Star 0 Fork 0

wmdng/vake

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
node.go 4.05 KB
一键复制 编辑 原始数据 按行查看 历史
wmdng 提交于 2024-06-12 14:38 . add ccopts to Object struct
package bond
import (
"fmt"
"path/filepath"
"strings"
"gitee.com/wmdng/vake/ninja"
)
type Node interface {
GetType() string
GetExport() []string
RunGenerate(nj *ninja.Ninja) error
}
type NFile struct {
ext string
fds []string
opt *Option
}
/*
c : c lang souce
asm : assembler souce
library : ar, achive
object : elf, relocatable file
program : elf, executable file
*/
func (nf *NFile) GetType() string {
return nf.ext
}
func (nf *NFile) GetExport() []string {
return nf.fds
}
func (nf *NFile) RunGenerate(nj *ninja.Ninja) error {
return nil
}
type NObject struct {
dir string
fds []Node
opt *Option
}
func (no *NObject) conv2obj(sfn string) string {
bbb := filepath.Base(sfn)
ext := filepath.Ext(bbb)
bbb = strings.TrimSuffix(bbb, ext)
return filepath.Join(no.dir, bbb+".o")
}
func (nf *NObject) GetType() string {
return "object"
}
func (no *NObject) GetExport() []string {
var rult []string
for _, nf := range no.fds {
/**/
fps := nf.GetExport()
/* replace ext */
for i := 0; i < len(fps); i++ {
/**/
rult = append(rult, no.conv2obj(fps[i]))
}
}
/**/
return rult
}
func (no *NObject) RunGenerate(nj *ninja.Ninja) error {
var incpath string = ""
var ccopts string = ""
/**/
sary, err := no.opt.ExpectStrArray("cpppath", false)
if err == nil {
incpath = "-I" + strings.Join(sary, " -I")
}
/**/
opts, err := no.opt.ExpectStrArray("ccopts", false)
if err == nil {
ccopts = strings.Join(opts, " ")
}
/**/
for _, nf := range no.fds {
err := nf.RunGenerate(nj)
if err != nil {
return err
}
/**/
ext := nf.GetType()
fps := nf.GetExport()
for i := 0; i < len(fps); i++ {
switch ext {
case "c":
bd := ninja.NewBuild([]string{no.conv2obj(fps[i])}, []string{fps[i]}, "cc")
if len(incpath) > 0 {
bd.AddVar("ccinc", incpath)
}
if len(ccopts) > 0 {
bd.AddVar("ccopt", ccopts)
}
nj.AddBuild(bd)
case "asm":
bd := ninja.NewBuild([]string{no.conv2obj(fps[i])}, []string{fps[i]}, "as")
if len(incpath) > 0 {
bd.AddVar("asinc", incpath)
}
if len(ccopts) > 0 {
bd.AddVar("asopt", ccopts)
}
nj.AddBuild(bd)
default:
return fmt.Errorf("object, ext not support, %s", ext)
}
}
}
return nil
}
type NArchive struct {
dst string
fds []Node
opt *Option
}
func (na *NArchive) GetType() string {
return "library"
}
func (np *NArchive) GetExport() []string {
return []string{np.dst}
}
func (np *NArchive) RunGenerate(nj *ninja.Ninja) error {
var ins []string
for _, nf := range np.fds {
err := nf.RunGenerate(nj)
if err != nil {
return err
}
/**/
fps := nf.GetExport()
ins = append(ins, fps...)
}
/**/
bd := ninja.NewBuild([]string{np.dst}, ins, "ar")
nj.AddBuild(bd)
return nil
}
type NProgram struct {
dst string
fds []Node
opt *Option
}
func (nf *NProgram) GetType() string {
return "program"
}
func (np *NProgram) GetExport() []string {
return []string{np.dst}
}
func (np *NProgram) RunGenerate(nj *ninja.Ninja) error {
var ins []string
for _, nf := range np.fds {
err := nf.RunGenerate(nj)
if err != nil {
return err
}
/**/
fps := nf.GetExport()
ins = append(ins, fps...)
}
/**/
bd := ninja.NewBuild([]string{np.dst}, ins, "lk")
/**/
sct, err := np.opt.ExpectString("script", false)
if err == nil {
bd.AddVar("sctarg", "--script="+sct)
}
/**/
lbs, err := np.opt.ExpectStrArray("libs", false)
if err == nil {
bd.AddVar("libarg", strings.Join(lbs, " "))
}
nj.AddBuild(bd)
return nil
}
type NBianry struct {
dst string
fds []Node
opt *Option
}
func (nf *NBianry) GetType() string {
return "binary"
}
func (np *NBianry) GetExport() []string {
return []string{np.dst}
}
func (np *NBianry) RunGenerate(nj *ninja.Ninja) error {
var ins []string
for _, nf := range np.fds {
err := nf.RunGenerate(nj)
if err != nil {
return err
}
/**/
fps := nf.GetExport()
ins = append(ins, fps...)
}
if len(ins) > 1 {
return fmt.Errorf("input file must only-one")
}
/**/
bd := ninja.NewBuild([]string{np.dst}, ins, "oc")
nj.AddBuild(bd)
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/wmdng/vake.git
git@gitee.com:wmdng/vake.git
wmdng
vake
vake
dca90b215a59

搜索帮助