github.com/goplus/llgo@v0.8.3/chore/nmindex/nmindex.go (about)

     1  /*
     2   * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  
    23  	"github.com/goplus/llgo/xtool/env/llvm"
    24  	"github.com/goplus/llgo/xtool/nm"
    25  )
    26  
    27  func main() {
    28  	if len(os.Args) < 2 {
    29  		fmt.Fprint(os.Stderr, `Usage:
    30  	nmindex <command> [arguments]
    31  
    32  The commands are:
    33  
    34  	mk		Create index file
    35  	q		Query a symbol
    36  
    37  `)
    38  		return
    39  	}
    40  
    41  	cmd := os.Args[1]
    42  	switch cmd {
    43  	case "mk":
    44  		makeIndex()
    45  	case "q":
    46  		if len(os.Args) < 3 {
    47  			fmt.Fprint(os.Stderr, "Usage: nmindex q <symbol>\n")
    48  			return
    49  		}
    50  		query(os.Args[2])
    51  	default:
    52  		fmt.Fprintf(os.Stderr, "unknown command: %s\n", cmd)
    53  	}
    54  }
    55  
    56  func makeIndex() {
    57  	env := llvm.New()
    58  	idxDir := indexDir()
    59  	os.MkdirAll(idxDir, 0755)
    60  
    61  	b := nm.NewIndexBuilder(env.Nm())
    62  	libDirs := []string{
    63  		usrLib(false),
    64  		usrLib(true),
    65  		stdLib("LLGO_STDROOT"),
    66  		stdLib("LLGO_USRROOT"),
    67  		pythonLib(),
    68  	}
    69  	err := b.Index(libDirs, idxDir, func(path string) {
    70  		fmt.Println("==>", path)
    71  	})
    72  	check(err)
    73  }
    74  
    75  func query(q string) {
    76  	if len(q) > 0 {
    77  		if c := q[0]; c != '*' && c != '_' {
    78  			q = "_" + q
    79  		}
    80  	}
    81  	files, err := nm.Query(indexDir(), q)
    82  	check(err)
    83  	for _, f := range files {
    84  		fmt.Printf("%s:\n", f.ArFile)
    85  		for _, item := range f.Items {
    86  			fmt.Printf("  %c %s %s\n", item.Type, item.Symbol, item.ObjFile)
    87  		}
    88  	}
    89  }
    90  
    91  func indexDir() string {
    92  	home, err := os.UserHomeDir()
    93  	check(err)
    94  	return home + "/.llgo/nmindex"
    95  }
    96  
    97  func stdLib(where string) string {
    98  	dir := os.Getenv(where)
    99  	if dir != "" {
   100  		dir += "/lib"
   101  	}
   102  	return dir
   103  }
   104  
   105  func usrLib(local bool) string {
   106  	if local {
   107  		return "/usr/local/lib"
   108  	}
   109  	return "/usr/lib"
   110  }
   111  
   112  func pythonLib() string {
   113  	return os.Getenv("LLGO_PYTHON_ROOT")
   114  }
   115  
   116  func check(err error) {
   117  	if err != nil {
   118  		panic(err)
   119  	}
   120  }