| 1 |
/* |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
or more contributor license agreements. See the NOTICE file |
| 4 |
distributed with this work for additional information |
| 5 |
regarding copyright ownership. The ASF licenses this file |
| 6 |
to you under the Apache License, Version 2.0 (the |
| 7 |
"License"); you may not use this file except in compliance |
| 8 |
with the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, |
| 13 |
software distributed under the License is distributed on an |
| 14 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
KIND, either express or implied. See the License for the |
| 16 |
specific language governing permissions and limitations |
| 17 |
under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
// Code generator to generate a thin Go wrapper API around the C proton API. |
| 21 |
// |
| 22 |
// Not run automatically, generated sources are checked in. To update the |
| 23 |
// generated sources run `go run genwrap.go` in this directory. |
| 24 |
// |
| 25 |
// WARNING: generating code from the wrong proton header file versions |
| 26 |
// will break compatibility guarantees. This program will attempt to detect |
| 27 |
// such errors. If you are deliberately changing the compatibility requirements |
| 28 |
// update the variable minVersion below. |
| 29 |
// |
| 30 |
|
| 31 |
package main |
| 32 |
|
| 33 |
import ( |
| 34 |
"flag" |
| 35 |
"fmt" |
| 36 |
"io" |
| 37 |
"io/ioutil" |
| 38 |
"os" |
| 39 |
"os/exec" |
| 40 |
"path" |
| 41 |
"regexp" |
| 42 |
"strings" |
| 43 |
"text/template" |
| 44 |
) |
| 45 |
|
| 46 |
// Note last code generation was from 0.27 source, but we should still |
| 47 |
// be binary compatible back to 0.10. Only change was the type name |
| 48 |
// for pn_proton_tick() from pn_timestamp_t to int64_t, identical type. |
| 49 |
|
| 50 |
var minVersion = "0.27" // The proton-c header version last used to generate code |
| 51 |
|
| 52 |
var include = flag.String("include", "../c/include", "Directory containing proton/*.h include files") |
| 53 |
|
| 54 |
var versionH = regexp.MustCompile("(?s:PN_VERSION_MAJOR ([0-9]+).*PN_VERSION_MINOR ([0-9]+))") |
| 55 |
var versionTxt = regexp.MustCompile("^[0-9]+\\.[0-9]+") |
| 56 |
|
| 57 |
func main() { |
| 58 |
flag.Parse() |
| 59 |
genVersion() |
| 60 |
genWrappers() |
| 61 |
} |
| 62 |
|
| 63 |
func getVersion() string { |
| 64 |
_, err := ioutil.ReadFile(path.Join(*include, "proton/version.h.in")) |
| 65 |
if err == nil { |
| 66 |
// We are using the headers in git sources, get the VERSION.txt |
| 67 |
vt, err := ioutil.ReadFile(path.Join(*include, "../../VERSION.txt")) |
| 68 |
panicIf(err) |
| 69 |
return versionTxt.FindString(string(vt)) |
| 70 |
} |
| 71 |
vh, err := ioutil.ReadFile(path.Join(*include, "proton/version.h")) |
| 72 |
if err == nil { |
| 73 |
// We are using installed headers |
| 74 |
return strings.Join(versionH.FindStringSubmatch(string(vh))[1:], ".") |
| 75 |
} |
| 76 |
panic(err) |
| 77 |
} |
| 78 |
|
| 79 |
func genVersion() { |
| 80 |
version := getVersion() |
| 81 |
if minVersion != version { |
| 82 |
panic(fmt.Errorf("Found proton-c version %v, expected %v. Update minVersion in genwrap.go if you want to increase the minimum required proton-c version.", version, minVersion)) |
| 83 |
} |
| 84 |
out, err := os.Create("pkg/amqp/version.go") |
| 85 |
panicIf(err) |
| 86 |
defer out.Close() |
| 87 |
splitVersion := strings.Split(minVersion, ".") |
| 88 |
fmt.Fprintf(out, copyright+` |
| 89 |
|
| 90 |
package amqp |
| 91 |
|
| 92 |
// Version check for proton library. |
| 93 |
// Done here because this is the lowest-level dependency for all the proton Go packages. |
| 94 |
|
| 95 |
// #include <proton/version.h> |
| 96 |
// #if PN_VERSION_MAJOR == %s && PN_VERSION_MINOR < %s |
| 97 |
// #error module github.com/apache/qpid-proton requires Proton-C library version 0.10 or greater |
| 98 |
// #endif |
| 99 |
import "C" |
| 100 |
`, splitVersion[0], splitVersion[1]) |
| 101 |
} |
| 102 |
|
| 103 |
func genWrappers() { |
| 104 |
outPath := "pkg/proton/wrappers_gen.go" |
| 105 |
out, err := os.Create(outPath) |
| 106 |
panicIf(err) |
| 107 |
defer out.Close() |
| 108 |
|
| 109 |
apis := []string{"session", "link", "delivery", "disposition", "condition", "terminus", "connection", "transport", "sasl"} |
| 110 |
fmt.Fprintln(out, copyright) |
| 111 |
fmt.Fprint(out, ` |
| 112 |
package proton |
| 113 |
|
| 114 |
import ( |
| 115 |
"time" |
| 116 |
"unsafe" |
| 117 |
) |
| 118 |
|
| 119 |
// #include <proton/condition.h> |
| 120 |
// #include <proton/error.h> |
| 121 |
// #include <proton/event.h> |
| 122 |
// #include <proton/types.h> |
| 123 |
// #include <stdlib.h> |
| 124 |
import "C" |
| 125 |
|
| 126 |
`) |
| 127 |
for _, api := range apis { |
| 128 |
fmt.Fprintf(out, "// #include <proton/%s.h>\n", api) |
| 129 |
} |
| 130 |
fmt.Fprintln(out, `import "C"`) |
| 131 |
|
| 132 |
event(out) |
| 133 |
|
| 134 |
for _, api := range apis { |
| 135 |
fmt.Fprintf(out, "// Wrappers for declarations in %s.h\n\n", api) |
| 136 |
header := readHeader(api) |
| 137 |
enums := findEnums(header) |
| 138 |
for _, e := range enums { |
| 139 |
genEnum(out, e.Name, e.Values) |
| 140 |
} |
| 141 |
apiWrapFns(api, header, out) |
| 142 |
} |
| 143 |
out.Close() |
| 144 |
if err := exec.Command("gofmt", "-w", outPath).Run(); err != nil { |
| 145 |
fmt.Fprintf(os.Stderr, "gofmt: %s", err) |
| 146 |
os.Exit(1) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// Identify acronyms that should be uppercase not Mixedcase |
| 151 |
var acronym = regexp.MustCompile("(?i)SASL|AMQP") |
| 152 |
|
| 153 |
func mixedCase(s string) string { |
| 154 |
result := "" |
| 155 |
for _, w := range strings.Split(s, "_") { |
| 156 |
if acronym.MatchString(w) { |
| 157 |
w = strings.ToUpper(w) |
| 158 |
} else { |
| 159 |
w = strings.ToUpper(w[0:1]) + strings.ToLower(w[1:]) |
| 160 |
} |
| 161 |
result = result + w |
| 162 |
} |
| 163 |
return result |
| 164 |
} |
| 165 |
|
| 166 |
func mixedCaseTrim(s, prefix string) string { |
| 167 |
return mixedCase(strings.TrimPrefix(s, prefix)) |
| 168 |
} |
| 169 |
|
| 170 |
var templateFuncs = template.FuncMap{"mixedCase": mixedCase, "mixedCaseTrim": mixedCaseTrim} |
| 171 |
|
| 172 |
func doTemplate(out io.Writer, data interface{}, tmpl string) { |
| 173 |
panicIf(template.Must(template.New("").Funcs(templateFuncs).Parse(tmpl)).Execute(out, data)) |
| 174 |
} |
| 175 |
|
| 176 |
type enumType struct { |
| 177 |
Name string |
| 178 |
Values []string |
| 179 |
} |
| 180 |
|
| 181 |
// Find enums in a header file return map of enum name to values. |
| 182 |
func findEnums(header string) (enums []enumType) { |
| 183 |
for _, enum := range enumDefRe.FindAllStringSubmatch(header, -1) { |
| 184 |
enums = append(enums, enumType{enum[2], enumValRe.FindAllString(enum[1], -1)}) |
| 185 |
} |
| 186 |
return enums |
| 187 |
} |
| 188 |
|
| 189 |
// Types that are integral, not wrappers. Enums are added automatically. |
| 190 |
var simpleType = map[string]bool{ |
| 191 |
"State": true, // integral typedef |
| 192 |
} |
| 193 |
|
| 194 |
func genEnum(out io.Writer, name string, values []string) { |
| 195 |
simpleType[mixedCase(name)] = true |
| 196 |
doTemplate(out, []interface{}{name, values}, ` |
| 197 |
{{$enumName := index . 0}}{{$values := index . 1}} |
| 198 |
type {{mixedCase $enumName}} C.pn_{{$enumName}}_t |
| 199 |
const ({{range $values}} |
| 200 |
{{mixedCaseTrim . "PN_"}} {{mixedCase $enumName}} = C.{{.}} {{end}} |
| 201 |
) |
| 202 |
|
| 203 |
func (e {{mixedCase $enumName}}) String() string { |
| 204 |
switch e { |
| 205 |
{{range $values}} |
| 206 |
case C.{{.}}: return "{{mixedCaseTrim . "PN_"}}" {{end}} |
| 207 |
} |
| 208 |
return "unknown" |
| 209 |
} |
| 210 |
`) |
| 211 |
} |
| 212 |
|
| 213 |
func panicIf(err error) { |
| 214 |
if err != nil { |
| 215 |
panic(err) |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func readHeader(name string) string { |
| 220 |
s, err := ioutil.ReadFile(path.Join(*include, "proton", name+".h")) |
| 221 |
panicIf(err) |
| 222 |
return string(s) |
| 223 |
} |
| 224 |
|
| 225 |
var copyright string = `/* |
| 226 |
Licensed to the Apache Software Foundation (ASF) under one |
| 227 |
or more contributor license agreements. See the NOTICE file |
| 228 |
distributed with this work for additional information |
| 229 |
regarding copyright ownership. The ASF licenses this file |
| 230 |
to you under the Apache License, Version 2.0 (the |
| 231 |
"License"); you may not use this file except in compliance |
| 232 |
with the License. You may obtain a copy of the License at |
| 233 |
|
| 234 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 235 |
|
| 236 |
Unless required by applicable law or agreed to in writing, |
| 237 |
software distributed under the License is distributed on an |
| 238 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 239 |
KIND, either express or implied. See the License for the |
| 240 |
specific language governing permissions and limitations |
| 241 |
under the License. |
| 242 |
*/ |
| 243 |
|
| 244 |
// |
| 245 |
// NOTE: DO NOT EDIT. This file was generated by genwrap.go from the proton header files. |
| 246 |
// Update the generator and re-run if you need to modify this code. |
| 247 |
// |
| 248 |
` |
| 249 |
|
| 250 |
type eventType struct { |
| 251 |
// C, function and interface names for the event |
| 252 |
Name, Cname, Fname, Iname string |
| 253 |
} |
| 254 |
|
| 255 |
func newEventType(cName string) eventType { |
| 256 |
var etype eventType |
| 257 |
etype.Cname = cName |
| 258 |
etype.Name = mixedCaseTrim(cName, "PN_") |
| 259 |
etype.Fname = "On" + etype.Name |
| 260 |
etype.Iname = etype.Fname + "Interface" |
| 261 |
return etype |
| 262 |
} |
| 263 |
|
| 264 |
var ( |
| 265 |
enumDefRe = regexp.MustCompile("typedef enum {([^}]*)} pn_([a-z_]+)_t;") |
| 266 |
enumValRe = regexp.MustCompile("PN_[A-Z_]+") |
| 267 |
skipEventRe = regexp.MustCompile("EVENT_NONE|REACTOR|SELECTABLE|TIMER") |
| 268 |
skipFnRe = regexp.MustCompile("attach|context|class|collect|link_recv|link_send|transport_.*logf$|transport_.*trace|transport_head|transport_tail|transport_push|connection_set_password|link_get_drain") |
| 269 |
) |
| 270 |
|
| 271 |
// Generate event wrappers. |
| 272 |
func event(out io.Writer) { |
| 273 |
event_h := readHeader("event") |
| 274 |
|
| 275 |
// Event is implemented by hand in wrappers.go |
| 276 |
|
| 277 |
// Get all the pn_event_type_t enum values |
| 278 |
var etypes []eventType |
| 279 |
enums := findEnums(event_h) |
| 280 |
for _, e := range enums[0].Values { |
| 281 |
if skipEventRe.FindStringSubmatch(e) == nil { |
| 282 |
etypes = append(etypes, newEventType(e)) |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
doTemplate(out, etypes, ` |
| 287 |
type EventType int |
| 288 |
const ({{range .}} |
| 289 |
E{{.Name}} EventType = C.{{.Cname}}{{end}} |
| 290 |
) |
| 291 |
`) |
| 292 |
|
| 293 |
doTemplate(out, etypes, ` |
| 294 |
func (e EventType) String() string { |
| 295 |
switch e { |
| 296 |
{{range .}} |
| 297 |
case C.{{.Cname}}: return "{{.Name}}"{{end}} |
| 298 |
} |
| 299 |
return "Unknown" |
| 300 |
} |
| 301 |
`) |
| 302 |
} |
| 303 |
|
| 304 |
type genType struct { |
| 305 |
Ctype, Gotype string |
| 306 |
ToGo func(value string) string |
| 307 |
ToC func(value string) string |
| 308 |
Assign func(value string) string |
| 309 |
} |
| 310 |
|
| 311 |
func (g genType) printBody(out io.Writer, value string) { |
| 312 |
if g.Gotype != "" { |
| 313 |
fmt.Fprintf(out, "return %s", g.ToGo(value)) |
| 314 |
} else { |
| 315 |
fmt.Fprintf(out, "%s", value) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
func (g genType) goLiteral(value string) string { |
| 320 |
return fmt.Sprintf("%s{%s}", g.Gotype, value) |
| 321 |
} |
| 322 |
|
| 323 |
func (g genType) goConvert(value string) string { |
| 324 |
switch g.Gotype { |
| 325 |
case "string": |
| 326 |
return fmt.Sprintf("C.GoString(%s)", value) |
| 327 |
case "Event": |
| 328 |
return fmt.Sprintf("makeEvent(%s)", value) |
| 329 |
default: |
| 330 |
return fmt.Sprintf("%s(%s)", g.Gotype, value) |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
func mapType(ctype string) (g genType) { |
| 335 |
g.Ctype = "C." + strings.Trim(ctype, " \n") |
| 336 |
|
| 337 |
// Special-case mappings for C types, default: is the general wrapper type case. |
| 338 |
switch g.Ctype { |
| 339 |
case "C.void": |
| 340 |
g.Gotype = "" |
| 341 |
case "C.size_t": |
| 342 |
g.Gotype = "uint" |
| 343 |
case "C.int": |
| 344 |
g.Gotype = "int" |
| 345 |
case "C.void *": |
| 346 |
g.Gotype = "unsafe.Pointer" |
| 347 |
g.Ctype = "unsafe.Pointer" |
| 348 |
case "C.bool": |
| 349 |
g.Gotype = "bool" |
| 350 |
case "C.ssize_t": |
| 351 |
g.Gotype = "int" |
| 352 |
case "C.int64_t": |
| 353 |
g.Gotype = "int64" |
| 354 |
case "C.int32_t": |
| 355 |
g.Gotype = "int16" |
| 356 |
case "C.int16_t": |
| 357 |
g.Gotype = "int32" |
| 358 |
case "C.uint64_t": |
| 359 |
g.Gotype = "uint64" |
| 360 |
case "C.uint32_t": |
| 361 |
g.Gotype = "uint16" |
| 362 |
case "C.uint16_t": |
| 363 |
g.Gotype = "uint32" |
| 364 |
case "C.const char *": |
| 365 |
fallthrough |
| 366 |
case "C.char *": |
| 367 |
g.Gotype = "string" |
| 368 |
g.Ctype = "C.CString" |
| 369 |
g.ToC = func(v string) string { return fmt.Sprintf("%sC", v) } |
| 370 |
g.Assign = func(v string) string { |
| 371 |
return fmt.Sprintf("%sC := C.CString(%s)\n defer C.free(unsafe.Pointer(%sC))\n", v, v, v) |
| 372 |
} |
| 373 |
case "C.pn_seconds_t": |
| 374 |
g.Gotype = "time.Duration" |
| 375 |
g.ToGo = func(v string) string { return fmt.Sprintf("(time.Duration(%s) * time.Second)", v) } |
| 376 |
g.ToC = func(v string) string { return fmt.Sprintf("C.pn_seconds_t(%s/time.Second)", v) } |
| 377 |
case "C.pn_millis_t": |
| 378 |
g.Gotype = "time.Duration" |
| 379 |
g.ToGo = func(v string) string { return fmt.Sprintf("(time.Duration(%s) * time.Millisecond)", v) } |
| 380 |
g.ToC = func(v string) string { return fmt.Sprintf("C.pn_millis_t(%s/time.Millisecond)", v) } |
| 381 |
case "C.pn_timestamp_t": |
| 382 |
g.Gotype = "time.Time" |
| 383 |
g.ToC = func(v string) string { return fmt.Sprintf("pnTime(%s)", v) } |
| 384 |
g.ToGo = func(v string) string { return fmt.Sprintf("goTime(%s)", v) } |
| 385 |
case "C.pn_error_t *": |
| 386 |
g.Gotype = "error" |
| 387 |
g.ToGo = func(v string) string { return fmt.Sprintf("PnError(%s)", v) } |
| 388 |
default: |
| 389 |
pnId := regexp.MustCompile(" *pn_([a-z_]+)_t *\\*? *") |
| 390 |
match := pnId.FindStringSubmatch(g.Ctype) |
| 391 |
if match == nil { |
| 392 |
panic(fmt.Errorf("unknown C type %#v", g.Ctype)) |
| 393 |
} |
| 394 |
g.Gotype = mixedCase(match[1]) |
| 395 |
if !simpleType[g.Gotype] { |
| 396 |
g.ToGo = g.goLiteral |
| 397 |
g.ToC = func(v string) string { return v + ".pn" } |
| 398 |
} |
| 399 |
} |
| 400 |
if g.ToGo == nil { |
| 401 |
g.ToGo = g.goConvert // Use conversion by default. |
| 402 |
} |
| 403 |
if g.ToC == nil { |
| 404 |
g.ToC = func(v string) string { return fmt.Sprintf("%s(%s)", g.Ctype, v) } |
| 405 |
} |
| 406 |
return |
| 407 |
} |
| 408 |
|
| 409 |
type genArg struct { |
| 410 |
Name string |
| 411 |
genType |
| 412 |
} |
| 413 |
|
| 414 |
var typeNameRe = regexp.MustCompile("^(.*( |\\*))([^ *]+)$") |
| 415 |
|
| 416 |
func splitArgs(argstr string) []genArg { |
| 417 |
argstr = strings.Trim(argstr, " \n") |
| 418 |
if argstr == "" { |
| 419 |
return []genArg{} |
| 420 |
} |
| 421 |
args := make([]genArg, 0) |
| 422 |
for _, item := range strings.Split(argstr, ",") { |
| 423 |
item = strings.Trim(item, " \n") |
| 424 |
typeName := typeNameRe.FindStringSubmatch(item) |
| 425 |
if typeName == nil { |
| 426 |
panic(fmt.Errorf("Can't split argument type/name %#v", item)) |
| 427 |
} |
| 428 |
cType := strings.Trim(typeName[1], " \n") |
| 429 |
name := strings.Trim(typeName[3], " \n") |
| 430 |
if name == "type" { |
| 431 |
name = "type_" |
| 432 |
} |
| 433 |
args = append(args, genArg{name, mapType(cType)}) |
| 434 |
} |
| 435 |
return args |
| 436 |
} |
| 437 |
|
| 438 |
func goArgs(args []genArg) string { |
| 439 |
l := "" |
| 440 |
for i, arg := range args { |
| 441 |
if i != 0 { |
| 442 |
l += ", " |
| 443 |
} |
| 444 |
l += arg.Name + " " + arg.Gotype |
| 445 |
} |
| 446 |
return l |
| 447 |
} |
| 448 |
|
| 449 |
func cArgs(args []genArg) string { |
| 450 |
l := "" |
| 451 |
for _, arg := range args { |
| 452 |
l += fmt.Sprintf(", %s", arg.ToC(arg.Name)) |
| 453 |
} |
| 454 |
return l |
| 455 |
} |
| 456 |
|
| 457 |
func cAssigns(args []genArg) string { |
| 458 |
l := "\n" |
| 459 |
for _, arg := range args { |
| 460 |
if arg.Assign != nil { |
| 461 |
l += fmt.Sprintf("%s\n", arg.Assign(arg.Name)) |
| 462 |
} |
| 463 |
} |
| 464 |
return l |
| 465 |
} |
| 466 |
|
| 467 |
// Return the go name of the function or "" to skip the function. |
| 468 |
func goFnName(api, fname string) string { |
| 469 |
// Skip class, context and attachment functions. |
| 470 |
if skipFnRe.FindStringSubmatch(api+"_"+fname) != nil { |
| 471 |
return "" |
| 472 |
} |
| 473 |
return mixedCaseTrim(fname, "get_") |
| 474 |
} |
| 475 |
|
| 476 |
func apiWrapFns(api, header string, out io.Writer) { |
| 477 |
defer func() { |
| 478 |
if err := recover(); err != nil { |
| 479 |
panic(fmt.Sprintf("in %s.h: %s", api, err)) |
| 480 |
} |
| 481 |
}() |
| 482 |
fmt.Fprintf(out, "type %s struct{pn *C.pn_%s_t}\n", mixedCase(api), api) |
| 483 |
fmt.Fprintf(out, "func (%c %s) IsNil() bool { return %c.pn == nil }\n", api[0], mixedCase(api), api[0]) |
| 484 |
fmt.Fprintf(out, "func (%c %s) CPtr() unsafe.Pointer { return unsafe.Pointer(%c.pn) }\n", api[0], mixedCase(api), api[0]) |
| 485 |
fn := regexp.MustCompile(fmt.Sprintf(`PN_EXTERN ([a-z0-9_ ]+ *\*?) *pn_%s_([a-z_]+)\(pn_%s_t *\*[a-z_]+ *,? *([^)]*)\)`, api, api)) |
| 486 |
for _, m := range fn.FindAllStringSubmatch(header, -1) { |
| 487 |
rtype, fname, argstr := mapType(m[1]), m[2], m[3] |
| 488 |
gname := goFnName(api, fname) |
| 489 |
if gname == "" { // Skip |
| 490 |
continue |
| 491 |
} |
| 492 |
args := splitArgs(argstr) |
| 493 |
fmt.Fprintf(out, "func (%c %s) %s", api[0], mixedCase(api), gname) |
| 494 |
fmt.Fprintf(out, "(%s) %s { ", goArgs(args), rtype.Gotype) |
| 495 |
fmt.Fprint(out, cAssigns(args)) |
| 496 |
rtype.printBody(out, fmt.Sprintf("C.pn_%s_%s(%c.pn%s)", api, fname, api[0], cArgs(args))) |
| 497 |
fmt.Fprintf(out, "}\n") |
| 498 |
} |
| 499 |
} |