genbindings: replace LinuxOnly with generic cpp/go requirements

This commit is contained in:
mappu 2025-04-15 20:49:35 +12:00
parent dc4c83998a
commit 57fee82f35
4 changed files with 20 additions and 17 deletions

View File

@ -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 {

View File

@ -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",

View File

@ -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")
}
`)

View File

@ -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"
}