-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathllcppsigfetch.go
More file actions
224 lines (206 loc) · 6.53 KB
/
llcppsigfetch.go
File metadata and controls
224 lines (206 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* 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"
"io"
"os"
"path/filepath"
"strings"
"unsafe"
"github.com/goplus/llcppg/_xtool/llcppsigfetch/dbg"
"github.com/goplus/llcppg/_xtool/llcppsigfetch/parse"
"github.com/goplus/llcppg/_xtool/llcppsymg/args"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/_xtool/llcppsymg/config"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llgo/c"
"github.com/goplus/llpkg/cjson"
)
func main() {
ags, remainArgs := args.ParseArgs(os.Args[1:], llcppg.LLCPPG_CFG, map[string]bool{
"--extract": true,
})
if ags.Help {
printUsage()
return
}
if ags.Verbose {
dbg.SetDebugAll()
}
extract := false
out := false
var extractFile string
isTemp := false
isCpp := true
otherArgs := []string{}
for i := 0; i < len(remainArgs); i++ {
arg := remainArgs[i]
switch {
case arg == "--extract":
extract = true
if i+1 < len(remainArgs) && !strings.HasPrefix(remainArgs[i+1], "-") {
extractFile = remainArgs[i+1]
i++
} else {
fmt.Fprintln(os.Stderr, "Error: --extract requires a valid file argument")
printUsage()
os.Exit(1)
}
case strings.HasPrefix(arg, "-out="):
out = args.BoolArg(arg, false)
case strings.HasPrefix(arg, "-temp="):
isTemp = args.BoolArg(arg, false)
case strings.HasPrefix(arg, "-cpp="):
isCpp = args.BoolArg(arg, true)
case strings.HasPrefix(arg, "-ClangResourceDir="):
// temp to avoid call clang in llcppsigfetch,will cause hang
parse.ClangResourceDir = args.StringArg(arg, "")
case strings.HasPrefix(arg, "-ClangSearchPath="):
// temp to avoid call clang in llcppsigfetch,will cause hang
parse.ClangSearchPath = strings.Split(args.StringArg(arg, ""), ",")
default:
otherArgs = append(otherArgs, arg)
}
}
if extract {
if ags.Verbose {
fmt.Fprintln(os.Stderr, "runExtract: extractFile:", extractFile)
fmt.Fprintln(os.Stderr, "isTemp:", isTemp)
fmt.Fprintln(os.Stderr, "isCpp:", isCpp)
fmt.Fprintln(os.Stderr, "out:", out)
fmt.Fprintln(os.Stderr, "otherArgs:", otherArgs)
}
runExtract(extractFile, isTemp, isCpp, out, otherArgs, ags.Verbose)
} else {
if ags.Verbose {
fmt.Fprintln(os.Stderr, "runFromConfig: config file:", ags.CfgFile)
fmt.Fprintln(os.Stderr, "use stdin:", ags.UseStdin)
fmt.Fprintln(os.Stderr, "output to file:", out)
}
runFromConfig(ags.CfgFile, ags.UseStdin, out, ags.Verbose)
}
}
func printUsage() {
fmt.Println("Usage:")
fmt.Println(" llcppsigfetch [-v] [-out=<bool>] [config_file]")
fmt.Println(" OR")
fmt.Println(" llcppsigfetch --extract <file> [-out=<bool>] [-temp=<bool>] [-cpp=<bool>] [-v] [args...]")
fmt.Println("")
fmt.Println("Options:")
fmt.Println(" [config_file]: Path to the configuration file (use '-' for stdin)")
fmt.Println(" If not provided, uses default 'llcppg.cfg'")
fmt.Println(" -out=<bool>: Optional. Set to 'true' to output results to a file,")
fmt.Println(" 'false' (default) to output to stdout")
fmt.Println(" This option can be used with both modes")
fmt.Println("")
fmt.Println(" --extract: Extract information from a single file")
fmt.Println(" <file>: Path to the file to process, or file content if -temp=true")
fmt.Println(" -temp=<bool>: Optional. Set to 'true' if <file> contains file content,")
fmt.Println(" 'false' (default) if it's a file path")
fmt.Println(" -cpp=<bool>: Optional. Set to 'true' if the language is C++ (default: true)")
fmt.Println(" If not present, <file> is a file path")
fmt.Println(" [args]: Optional additional arguments")
fmt.Println(" Default for C++: -x c++")
fmt.Println(" Default for C: -x c")
fmt.Println("")
fmt.Println(" --help, -h: Show this help message")
fmt.Println("")
fmt.Println("Note: The two usage modes are mutually exclusive. Use either [<config_file>] OR --extract, not both.")
}
func runFromConfig(cfgFile string, useStdin bool, outputToFile bool, verbose bool) {
var data []byte
var err error
if useStdin {
data, err = io.ReadAll(os.Stdin)
} else {
data, err = os.ReadFile(cfgFile)
}
if verbose {
if useStdin {
fmt.Fprintln(os.Stderr, "runFromConfig: read from stdin")
} else {
fmt.Fprintln(os.Stderr, "runFromConfig: read from file", cfgFile)
}
}
check(err)
conf, err := config.GetConf(data)
check(err)
defer conf.Delete()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to parse config file:", cfgFile)
os.Exit(1)
}
converter, err := parse.Do(&parse.ParseConfig{
Conf: conf.Config,
})
check(err)
info := converter.Output()
str := info.Print()
defer cjson.FreeCStr(unsafe.Pointer(str))
defer info.Delete()
outputResult(str, outputToFile)
}
func runExtract(content string, isTemp bool, isCpp bool, outToFile bool, otherArgs []string, verbose bool) {
var file string
cflags := otherArgs
if isTemp {
temp, err := os.Create(clangutils.TEMP_FILE)
if err != nil {
panic(err)
}
defer temp.Close()
defer os.Remove(file)
temp.Write([]byte(content))
file = temp.Name()
cflags = append(cflags, "-I"+filepath.Dir(file))
} else {
file = content
}
converter, err := parse.Do(&parse.ParseConfig{
Conf: &llcppg.Config{
Include: []string{file},
CFlags: strings.Join(cflags, ""),
},
})
check(err)
_, err = converter.Convert()
check(err)
result := converter.Output()
cstr := result.Print()
outputResult(cstr, outToFile)
cjson.FreeCStr(unsafe.Pointer(cstr))
result.Delete()
converter.Dispose()
}
func check(err error) {
if err != nil {
panic(err)
}
}
func outputResult(result *c.Char, outputToFile bool) {
if outputToFile {
outputFile := llcppg.LLCPPG_SIGFETCH
err := os.WriteFile(outputFile, []byte(c.GoString(result)), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing to output file: %v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "Results saved to %s\n", outputFile)
} else {
c.Printf(c.Str("%s"), result)
}
}