From 57fee82f3515e68ef13d36fa424ba921f3263246 Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 15 Apr 2025 20:49:35 +1200 Subject: [PATCH] genbindings: replace LinuxOnly with generic cpp/go requirements --- cmd/genbindings/config-allowlist.go | 6 ++++-- cmd/genbindings/emitcabi.go | 20 ++++++++++---------- cmd/genbindings/emitgo.go | 8 ++++---- cmd/genbindings/intermediate.go | 3 ++- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/cmd/genbindings/config-allowlist.go b/cmd/genbindings/config-allowlist.go index 4afba288..4fa13ca8 100644 --- a/cmd/genbindings/config-allowlist.go +++ b/cmd/genbindings/config-allowlist.go @@ -638,13 +638,15 @@ func ApplyQuirks(packageName, className string, mm *CppMethod) { if mm.ReturnType.GetQtCppType().ParameterType == "Q_PID" { // int64 on Linux, _PROCESS_INFORMATION* on Windows - mm.LinuxOnly = true + mm.RequireCpp = addr("defined(Q_OS_LINUX)") + mm.RequireGOOS = addr("linux") } if mm.ReturnType.GetQtCppType().ParameterType == "QSocketDescriptor::DescriptorType" || (len(mm.Parameters) > 0 && mm.Parameters[0].GetQtCppType().ParameterType == "QSocketDescriptor::DescriptorType") { // uintptr_t-compatible on Linux, void* on Windows - mm.LinuxOnly = true + mm.RequireCpp = addr("defined(Q_OS_LINUX)") + mm.RequireGOOS = addr("linux") } if className == "QArrayData" && mm.MethodName == "needsDetach" && mm.IsConst { diff --git a/cmd/genbindings/emitcabi.go b/cmd/genbindings/emitcabi.go index 1e3214fd..e3ceb992 100644 --- a/cmd/genbindings/emitcabi.go +++ b/cmd/genbindings/emitcabi.go @@ -1156,11 +1156,9 @@ extern "C" { cabiClassName(c.ClassName) + "* " + cabiNewName(c, i) + "(" + emitParametersCabiConstructor(&c, &ctor) + ") {\n", ) - if ctor.LinuxOnly { + if ctor.RequireCpp != nil { ret.WriteString( - "#ifndef Q_OS_LINUX\n" + - "\treturn nullptr;\n" + - "#else\n", + "#if " + *ctor.RequireCpp + "\n", ) } @@ -1169,9 +1167,11 @@ extern "C" { "\treturn new " + cppClassName + "(" + forwarding + ");\n", ) - if ctor.LinuxOnly { + if ctor.RequireCpp != nil { ret.WriteString( - "#endif\n", + "#else\n" + + "\treturn nullptr;\n" + + "#endif\n", ) } @@ -1232,15 +1232,15 @@ extern "C" { callTarget = "(*self " + operator + " " + forwarding + ")" } - if m.LinuxOnly { + if m.RequireCpp != nil { ret.WriteString(fmt.Sprintf( "%s %s_%s(%s) {\n"+ - "#ifdef Q_OS_LINUX\n"+ + "#if "+*m.RequireCpp+"\n"+ "%s"+ "%s"+ "#else\n"+ - "\t%s _ret_invalidOS;\n"+ - "\treturn _ret_invalidOS;\n"+ + "\t%s _ret_unavailable;\n"+ + "\treturn _ret_unavailable;\n"+ "#endif\n"+ "}\n"+ "\n", diff --git a/cmd/genbindings/emitgo.go b/cmd/genbindings/emitgo.go index d69059f9..db3ef600 100644 --- a/cmd/genbindings/emitgo.go +++ b/cmd/genbindings/emitgo.go @@ -899,10 +899,10 @@ import "C" `, ) - if ctor.LinuxOnly { + if ctor.RequireGOOS != nil { gfs.imports["runtime"] = struct{}{} ret.WriteString(` - if runtime.GOOS != "linux" { + if runtime.GOOS != "` + *ctor.RequireGOOS + `" { panic("Unsupported OS") } `) @@ -941,10 +941,10 @@ import "C" ret.WriteString(` func ` + receiverAndMethod + `(` + gfs.emitParametersGo(m.Parameters) + `) ` + returnTypeDecl + ` {`) - if m.LinuxOnly { + if m.RequireGOOS != nil { gfs.imports["runtime"] = struct{}{} ret.WriteString(` - if runtime.GOOS != "linux" { + if runtime.GOOS != "` + *m.RequireGOOS + `" { panic("Unsupported OS") } `) diff --git a/cmd/genbindings/intermediate.go b/cmd/genbindings/intermediate.go index 85073e26..6dd862ea 100644 --- a/cmd/genbindings/intermediate.go +++ b/cmd/genbindings/intermediate.go @@ -254,7 +254,8 @@ type CppMethod struct { HiddenParams []CppParameter // Populated if there is an overload with more parameters // Special quirks - LinuxOnly bool + RequireGOOS *string // constructs a `if runtime.GOOS = {foo}` block in the Go side, no effect on CABI / C++ sides + RequireCpp *string // constructs a `#if {foo}` preprocessor block in the C++ side, no effect on Go / CABI sides BecomesNonConstInVersion *string // "6,7" }