forked from karalabe/hid
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gen.go
161 lines (140 loc) · 3.03 KB
/
gen.go
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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
)
func main() {
hidapiIncludes := make(map[string]string)
loadIncludes(hidapiIncludes, "./hidapi/hidapi")
// hidapi/mac/hid.c -> hidapi_mac.h
inlineTo(
"hidapi/mac/hid.c",
hidapiIncludes,
"hidapi_mac.h",
)
// hidapi/windows/hid.c -> hidapi_windows.h
inlineTo(
"hidapi/windows/hid.c",
hidapiIncludes,
"hidapi_windows.h",
)
fmt.Println("hidapi_linux.h:")
outF, err := os.Create("hidapi_linux.h")
if err != nil {
panic(err)
}
defer outF.Close()
out := bufio.NewWriter(outF)
defer out.Flush()
/*
// -I./hidapi/hidapi -I./libusb/libusb
#include <poll.h>
#include "os/threads_posix.c"
#include "os/poll_posix.c"
#include "os/linux_usbfs.c"
#include "os/linux_netlink.c"
#include "core.c"
#include "descriptor.c"
#include "hotplug.c"
#include "io.c"
#include "strerror.c"
#include "sync.c"
#include "hidapi/libusb/hid.c"
*/
linuxIncludes := make(map[string]string)
loadIncludes(linuxIncludes, "./hidapi/hidapi")
loadIncludes(linuxIncludes, "./libusb/libusb")
seen := make(map[string]bool)
out.WriteString("#include <poll.h>\n")
for _, f := range []string{
"os/threads_posix.c",
"os/poll_posix.c",
"os/linux_usbfs.c",
"os/linux_netlink.c",
"core.c",
"descriptor.c",
"hotplug.c",
"io.c",
"strerror.c",
"sync.c",
} {
inline(
"libusb/libusb/"+f,
linuxIncludes,
out,
seen,
)
}
inline(
"hidapi/libusb/hid.c",
linuxIncludes,
out,
seen,
)
}
func loadIncludes(m map[string]string, dir string) {
d, err := os.Open(dir)
if err != nil {
panic(err)
}
names, err := d.Readdirnames(0)
if err != nil {
panic(err)
}
for _, name := range names {
m[name] = filepath.Join(dir, name)
}
}
func inlineTo(src string, includes map[string]string, out string) error {
fmt.Printf("%s:\n", src)
outF, err := os.Create(out)
if err != nil {
return err
}
defer outF.Close()
return inline(src, includes, bufio.NewWriter(outF), map[string]bool{})
}
var reInclude = regexp.MustCompile(`^ *#include *([<"])([^">]+)[">]`)
func inline(src string, includes map[string]string, w *bufio.Writer, seen map[string]bool) error {
if seen[src] {
fmt.Println(" SKIP ", src)
fmt.Fprintf(w, "// SKIP #include %q\n", src)
return nil
}
fmt.Println(" ", src)
seen[src] = true
fmt.Fprintf(w, "#line 1 %q\n", src)
dir := filepath.Dir(src)
srcF, err := os.Open(src)
if err != nil {
return err
}
scanner := bufio.NewScanner(srcF)
linenum := 0
for scanner.Scan() {
line := scanner.Text()
linenum++
matches := reInclude.FindStringSubmatch(line)
var target string
if matches != nil {
target = includes[matches[2]]
if target == "" && matches[1][0] == '"' {
target = filepath.Join(dir, matches[2])
}
if target != "" {
if err = inline(target, includes, w, seen); err != nil {
return fmt.Errorf("%s:%d: %w", src, linenum, err)
}
fmt.Fprintf(w, "#line %d %q\n", linenum+1, src)
continue
}
}
w.WriteString(line)
w.WriteByte('\n')
}
w.Flush()
return scanner.Err()
}