Skip to content

Commit

Permalink
cxgo: Allow disabling libs and remapping includes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Jul 2, 2023
1 parent f6a1096 commit 52ac65b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
27 changes: 16 additions & 11 deletions cmd/cxgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,18 @@ type File struct {
}

type Config struct {
VCS string `yaml:"vcs"`
Branch string `yaml:"branch"`
Root string `yaml:"root"`
Out string `yaml:"out"`
Package string `yaml:"package"`
Include []string `yaml:"include"`
SysInclude []string `yaml:"sys_include"`
Hooks bool `yaml:"hooks"`
Define []cxgo.Define `yaml:"define"`
Predef string `yaml:"predef"`
SubPackage bool `yaml:"subpackage"`
VCS string `yaml:"vcs"`
Branch string `yaml:"branch"`
Root string `yaml:"root"`
Out string `yaml:"out"`
Package string `yaml:"package"`
Include []string `yaml:"include"`
SysInclude []string `yaml:"sys_include"`
IncludeMap map[string]string `yaml:"include_map"`
Hooks bool `yaml:"hooks"`
Define []cxgo.Define `yaml:"define"`
Predef string `yaml:"predef"`
SubPackage bool `yaml:"subpackage"`

IntSize int `yaml:"int_size"`
PtrSize int `yaml:"ptr_size"`
Expand All @@ -140,6 +141,7 @@ type Config struct {
UnexportedFields bool `yaml:"unexported_fields"`
IntReformat bool `yaml:"int_reformat"`
KeepFree bool `yaml:"keep_free"`
NoLibs bool `yaml:"no_libs"`

SrcFiles []*SrcFile `yaml:"src_files"`
FilePref string `yaml:"file_pref"`
Expand Down Expand Up @@ -272,12 +274,15 @@ func run(cmd *cobra.Command, args []string) error {
Idents: ilist,
Include: c.Include,
SysInclude: c.SysInclude,
IncludeMap: c.IncludeMap,
FixImplicitReturns: c.ImplicitReturns,
IgnoreIncludeDir: c.IgnoreIncludeDir,
UnexportedFields: c.UnexportedFields,
IntReformat: c.IntReformat,
KeepFree: c.KeepFree,
}
env.NoLibs = c.NoLibs
env.Map = c.IncludeMap
if f.MaxDecls > 0 {
fc.MaxDecls = f.MaxDecls
}
Expand Down
14 changes: 13 additions & 1 deletion libs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ func NewEnv(conf types.Config) *Env {

type Env struct {
*types.Env
NoLibs bool // completely disable library lookups
Map map[string]string // when searching for library name, consult the map first and search that name instead
libs map[string]*Library
imports map[string]string
macros map[string]bool
}

func (c *Env) Clone() *Env {
c2 := &Env{Env: c.Env}
c2 := &Env{Env: c.Env, NoLibs: c.NoLibs}
c2.libs = make(map[string]*Library)
for k, v := range c.libs {
c2.libs[k] = v
Expand All @@ -41,6 +43,10 @@ func (c *Env) Clone() *Env {
for k, v := range c.macros {
c2.macros[k] = v
}
c2.Map = make(map[string]string)
for k, v := range c.Map {
c2.Map[k] = v
}
return c2
}

Expand All @@ -56,6 +62,12 @@ func (c *Env) ResolveImport(name string) string {
//
// Typically, the GetLibrary function should be used instead, because it will load the library automatically, if needed.
func (c *Env) LookupLibrary(name string) *Library {
if c.NoLibs && name != BuiltinH {
return nil
}
if v, ok := c.Map[name]; ok {
name = v
}
l, ok := c.libs[name]
if !ok {
panic(errors.New("cannot find library: " + name))
Expand Down
12 changes: 12 additions & 0 deletions libs/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ var defPathReplacer = strings.NewReplacer(

// GetLibrary finds or initializes the library, given a C include filename.
func (c *Env) GetLibrary(name string) (*Library, bool) {
if c.NoLibs && name != BuiltinH {
return nil, false
}
if v, ok := c.Map[name]; ok {
name = v
}
l, ok := c.libs[name]
if ok {
return l, true
Expand Down Expand Up @@ -225,6 +231,9 @@ func (c *Env) NewLibrary(path string) (*Library, bool) {
}

func (c *Env) TypeByName(name string) (types.Type, bool) {
if c.NoLibs && name != BuiltinH {
return nil, false
}
for _, l := range c.libs {
if t, ok := l.Types[name]; ok {
return t, true
Expand All @@ -234,6 +243,9 @@ func (c *Env) TypeByName(name string) (types.Type, bool) {
}

func (c *Env) LibIdentByName(name string) (*Library, *types.Ident, bool) {
if c.NoLibs && name != BuiltinH {
return nil, nil, false
}
for _, l := range c.libs {
if id, ok := l.Idents[name]; ok {
return l, id, true
Expand Down
1 change: 1 addition & 0 deletions translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
GoFilePref string
Include []string
SysInclude []string
IncludeMap map[string]string
MaxDecls int
Predef string
Define []Define
Expand Down

0 comments on commit 52ac65b

Please sign in to comment.