Skip to content

Commit

Permalink
calc relpath
Browse files Browse the repository at this point in the history
  • Loading branch information
tsingbx committed Oct 19, 2024
1 parent 42035e6 commit 8ecf856
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ _tinygo/
_output/
build.dir/
.vscode/
.devcontainer/

*.cfg
*.json

# Test binary, built with `go test -c`
*.test
Expand Down
36 changes: 30 additions & 6 deletions chore/llcppcfg/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ func expandString(str string) string {
})
}

func expandCflags(str string, fn func(s string) bool) []string {
func expandCflags(str string, fn func(s string) bool) (includes []string, flags string) {
list := strings.FieldsFunc(str, func(r rune) bool {
return unicode.IsSpace(r)
})
contains := make(map[string]string, 0)
results := make([]string, 0)
for _, l := range list {
trimStr := strings.TrimPrefix(l, "-I")
trimStr += "/"
Expand All @@ -68,13 +67,34 @@ func expandCflags(str string, fn func(s string) bool) []string {
}
_, ok := contains[path]
if !ok {
results = append(results, d.Name())
contains[path] = d.Name()
relPath, err := filepath.Rel(trimStr, path)
if err == nil {
contains[path] = relPath
} else {
contains[path] = d.Name()
}
}
return nil
})
}
return results

includeMap := make(map[string]struct{})
for path, relPath := range contains {
includeDir, found := strings.CutSuffix(path, relPath)
if found {
includeMap[includeDir] = struct{}{}
}
includes = append(includes, relPath)
}
var flagsBuilder strings.Builder
for include := range includeMap {
if flagsBuilder.Len() > 0 {
flagsBuilder.WriteRune(' ')
}
flagsBuilder.WriteString("-I" + include)
}
flags = flagsBuilder.String()
return
}

func NewLLCppConfig(name string, isCpp bool) *LLCppConfig {
Expand All @@ -89,10 +109,14 @@ func NewLLCppConfig(name string, isCpp bool) *LLCppConfig {
cfg.Include = []string{}
Cflags := fmt.Sprintf("${pkg-config --cflags %s}", name)
cflags := expandString(Cflags)
cfg.Include = expandCflags(cflags, func(s string) bool {
retIncludes, retCflags := expandCflags(cflags, func(s string) bool {
ext := filepath.Ext(s)
return ext == ".h" || ext == ".hpp"
})
cfg.Include = retIncludes
if len(retCflags) > 0 {
cflags = retCflags
}
// expand Cflags and Libs
cfg.Cflags = strings.TrimLeft(strings.TrimRight(cflags, " \t\r\n"), " \t\r\n")
Libs := fmt.Sprintf("${pkg-config --libs %s}", name)
Expand Down
2 changes: 1 addition & 1 deletion chore/llcppcfg/llcppcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ usage: llcppcfg [-cpp|-help] libname`)
if err != nil {
log.Fatal(err)
}
log.Println("Config file has been generated at ", outFile, "!")
fmt.Printf("\nllcppg.cfg has been generated!\n")
}

0 comments on commit 8ecf856

Please sign in to comment.