Files
llgo/chore/nmindex/nmindex.go

111 lines
2.1 KiB
Go
Raw Normal View History

2024-04-20 00:44:36 +08:00
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"os"
"github.com/goplus/llgo/x/env/llvm"
"github.com/goplus/llgo/x/nm"
)
func main() {
if len(os.Args) < 2 {
fmt.Fprint(os.Stderr, `Usage:
nmindex <command> [arguments]
The commands are:
mk Create index file
q Query a symbol
`)
return
}
cmd := os.Args[1]
switch cmd {
case "mk":
makeIndex()
case "q":
2024-04-20 11:58:26 +08:00
if len(os.Args) < 3 {
fmt.Fprint(os.Stderr, "Usage: nmindex q <symbol>\n")
return
}
query(os.Args[2])
2024-04-20 00:44:36 +08:00
default:
fmt.Fprintf(os.Stderr, "unknown command: %s\n", cmd)
}
}
func makeIndex() {
env := llvm.New()
2024-04-20 11:58:26 +08:00
idxDir := indexDir()
2024-04-20 00:44:36 +08:00
os.MkdirAll(idxDir, 0755)
b := nm.NewIndexBuilder(env.Nm())
libDirs := []string{
usrLib(false),
usrLib(true),
stdLib("LLGO_STDROOT"),
stdLib("LLGO_USRROOT"),
}
2024-04-20 11:58:26 +08:00
err := b.Index(libDirs, idxDir, func(path string) {
2024-04-20 00:44:36 +08:00
fmt.Println("==>", path)
})
check(err)
}
2024-04-20 11:58:26 +08:00
func query(q string) {
files, err := nm.Query(indexDir(), q)
check(err)
for _, f := range files {
fmt.Printf("%s:\n", f.ArFile)
for _, item := range f.Items {
fmt.Printf(" %c %s %s\n", item.Type, item.Symbol, item.ObjFile)
}
}
}
func indexDir() string {
home, err := os.UserHomeDir()
check(err)
return home + "/.llgo/nmindex"
2024-04-20 00:44:36 +08:00
}
func stdLib(where string) string {
dir := os.Getenv(where)
if dir != "" {
dir += "/lib"
}
return dir
}
func usrLib(local bool) string {
if local {
return "/usr/local/lib"
}
return "/usr/lib"
}
2024-04-20 00:44:36 +08:00
func check(err error) {
if err != nil {
panic(err)
}
}