forked from xlab/c-for-go
/
gen_files.go
209 lines (186 loc) · 4.87 KB
/
gen_files.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
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
package generator
import (
"fmt"
"io"
"strings"
"time"
)
func genLabel(noTimestamps bool) string {
// These headers comply with a recognized format for generated code, so that
// static analysis tools may recognize the code as generated and skip
// processing of these files.
//
// For more information, see:
// https://github.com/golang/go/issues/13560#issuecomment-276866852.
if noTimestamps {
tpl := "WARNING: This file has automatically been generated \nCode generated by https://git.io/c-for-go. DO NOT EDIT."
return tpl
}
tpl := "WARNING: This file has automatically been generated on %s.\nCode generated by https://git.io/c-for-go. DO NOT EDIT."
return fmt.Sprintf(tpl, time.Now().Format(time.RFC1123))
}
func (gen *Generator) WriteDoc(wr io.Writer) bool {
var hasDoc bool
if len(gen.cfg.PackageLicense) > 0 {
writeTextBlock(wr, gen.cfg.PackageLicense)
writeSpace(wr, 1)
hasDoc = true
}
writeTextBlock(wr, genLabel(gen.noTimestamps))
writeSpace(wr, 1)
if len(gen.cfg.PackageDescription) > 0 {
writeLongTextBlock(wr, gen.cfg.PackageDescription)
hasDoc = true
}
writePackageName(wr, gen.pkg)
writeSpace(wr, 1)
return hasDoc
}
func (gen *Generator) WriteIncludes(wr io.Writer) {
writeStartComment(wr)
writePkgConfig(wr, gen.cfg.PkgConfigOpts)
for _, group := range gen.cfg.FlagGroups {
writeFlagGroup(wr, group)
}
for _, path := range gen.cfg.SysIncludes {
writeSysInclude(wr, path)
}
for _, path := range gen.cfg.Includes {
writeInclude(wr, path)
}
writeCStdIncludes(wr, gen.cfg.SysIncludes)
fmt.Fprintln(wr, `#include "cgo_helpers.h"`)
writeEndComment(wr)
fmt.Fprintln(wr, `import "C"`)
writeSpace(wr, 1)
}
func hasLib(paths []string, lib string) bool {
for i := range paths {
if paths[i] == lib {
return true
}
}
return false
}
func (gen *Generator) writeGoHelpersHeader(wr io.Writer) {
writeTextBlock(wr, gen.cfg.PackageLicense)
writeSpace(wr, 1)
writeTextBlock(wr, genLabel(gen.noTimestamps))
writeSpace(wr, 1)
writePackageName(wr, gen.pkg)
writeSpace(wr, 1)
gen.WriteIncludes(wr)
}
func (gen *Generator) writeCHHelpersHeader(wr io.Writer) {
if len(gen.cfg.PackageLicense) > 0 {
writeTextBlock(wr, gen.cfg.PackageLicense)
writeSpace(wr, 1)
}
writeTextBlock(wr, genLabel(gen.noTimestamps))
writeSpace(wr, 1)
for _, path := range gen.cfg.SysIncludes {
writeSysInclude(wr, path)
}
for _, path := range gen.cfg.Includes {
writeInclude(wr, path)
}
writeCStdIncludes(wr, gen.cfg.SysIncludes)
writeCHPragma(wr)
writeSpace(wr, 1)
}
func (gen *Generator) writeCCHelpersHeader(wr io.Writer) {
if len(gen.cfg.PackageLicense) > 0 {
writeTextBlock(wr, gen.cfg.PackageLicense)
writeSpace(wr, 1)
}
writeTextBlock(wr, genLabel(gen.noTimestamps))
writeSpace(wr, 1)
writeCGOIncludes(wr)
writeSpace(wr, 1)
}
func writeCGOIncludes(wr io.Writer) {
fmt.Fprintln(wr, `#include "_cgo_export.h"`)
fmt.Fprintln(wr, `#include "cgo_helpers.h"`)
}
func writeCHPragma(wr io.Writer) {
fmt.Fprintln(wr, "#pragma once")
}
func writeCStdIncludes(wr io.Writer, sysIncludes []string) {
if !hasLib(sysIncludes, "stdlib.h") {
fmt.Fprintln(wr, "#include <stdlib.h>")
}
// if !hasLib(sysIncludes, "stdbool.h") {
// fmt.Fprintln(wr, "#include <stdbool.h>")
// }
}
func (gen *Generator) WritePackageHeader(wr io.Writer) {
writeTextBlock(wr, gen.cfg.PackageLicense)
writeSpace(wr, 1)
writeTextBlock(wr, genLabel(gen.noTimestamps))
writeSpace(wr, 1)
writePackageName(wr, gen.pkg)
writeSpace(wr, 1)
}
func writeFlagGroup(wr io.Writer, group TraitFlagGroup) {
if len(group.Name) == 0 {
return
}
if len(group.Flags) == 0 {
return
}
if len(group.Traits) == 0 {
fmt.Fprintf(wr, "#cgo %s: %s\n", group.Name, strings.Join(group.Flags, " "))
return
}
traits := strings.Join(group.Traits, " ")
fmt.Fprintf(wr, "#cgo %s %s: %s\n", traits, group.Name, strings.Join(group.Flags, " "))
}
func writeSysInclude(wr io.Writer, path string) {
fmt.Fprintf(wr, "#include <%s>\n", path)
}
func writeInclude(wr io.Writer, path string) {
fmt.Fprintf(wr, "#include \"%s\"\n", path)
}
func writePkgConfig(wr io.Writer, opts []string) {
if len(opts) == 0 {
return
}
fmt.Fprintf(wr, "#cgo pkg-config: %s\n", strings.Join(opts, " "))
}
func writeStartComment(wr io.Writer) {
fmt.Fprintln(wr, "/*")
}
func writeEndComment(wr io.Writer) {
fmt.Fprintln(wr, "*/")
}
func writePackageName(wr io.Writer, name string) {
if len(name) == 0 {
name = "main"
}
fmt.Fprintf(wr, "package %s\n", name)
}
func writeLongTextBlock(wr io.Writer, text string) {
if len(text) == 0 {
return
}
writeStartComment(wr)
fmt.Fprint(wr, text)
writeSpace(wr, 1)
writeEndComment(wr)
}
func writeTextBlock(wr io.Writer, text string) {
if len(text) == 0 {
return
}
lines := strings.Split(text, "\n")
for _, line := range lines {
fmt.Fprintf(wr, "// %s\n", line)
}
}
func writeSourceBlock(wr io.Writer, src string) {
if len(src) == 0 {
return
}
fmt.Fprint(wr, src)
writeSpace(wr, 1)
}