From da874c95ed1220c1b60ba71c0a9ba562d7693325 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 15 Feb 2025 17:10:03 +1300 Subject: [PATCH] genbindings: prevent using qt6 qsharedmemory on android --- cmd/genbindings/config-allowlist.go | 29 +++++++++++++++++++++++++++++ cmd/genbindings/emitcabi.go | 16 ++++++++++++++++ cmd/genbindings/emitgo.go | 7 +++++++ 3 files changed, 52 insertions(+) diff --git a/cmd/genbindings/config-allowlist.go b/cmd/genbindings/config-allowlist.go index 91ae0072..60fce8f7 100644 --- a/cmd/genbindings/config-allowlist.go +++ b/cmd/genbindings/config-allowlist.go @@ -105,6 +105,35 @@ func Widgets_AllowHeader(fullpath string) bool { return true } +type AllowedPlatformInfo interface { + GoBuildTag() string + CxxIf() string +} + +type AndroidBlockedPlatform struct{} + +func (abp AndroidBlockedPlatform) GoBuildTag() string { + return `!android` +} + +func (abp AndroidBlockedPlatform) CxxIf() string { + return `! defined(Q_OS_ANDROID)` +} + +func HeaderPlatformRestriction(fullpath string) AllowedPlatformInfo { + fname := filepath.Base(fullpath) + + if fname == `qsharedmemory.h` { + // Not implemented on Android nor iOS + // Qt 5: Classes are present but do not work + // Qt 6: Class definition is not present and our generated subclass fails to compile + return AndroidBlockedPlatform{} + } + + // No platform restriction + return nil +} + func ImportHeaderForClass(className string) bool { if className[0] != 'Q' { return false diff --git a/cmd/genbindings/emitcabi.go b/cmd/genbindings/emitcabi.go index 7014f7e8..1cabff09 100644 --- a/cmd/genbindings/emitcabi.go +++ b/cmd/genbindings/emitcabi.go @@ -931,6 +931,16 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) { ret.WriteString(`#include <` + filename + ">\n") ret.WriteString(`#include "gen_` + filename + "\"\n") + // Perform any platform checks + // n.b. The Q_OS_ variable is defined usually indirectly from another Qt + // header, so it should be checked only after all the other includes, + // although that seems suboptimal + + platformRestriction := HeaderPlatformRestriction(filename) + if platformRestriction != nil { + ret.WriteString(`#if ` + platformRestriction.CxxIf() + "\n\n") + } + // Write prototypes for functions that the host language bindings should export // for virtual function overrides @@ -1410,5 +1420,11 @@ extern "C" { } } + // + + if platformRestriction != nil { + ret.WriteString(`#endif //` + platformRestriction.CxxIf() + "\n\n") + } + return ret.String(), nil } diff --git a/cmd/genbindings/emitgo.go b/cmd/genbindings/emitgo.go index 53a16dab..57866b35 100644 --- a/cmd/genbindings/emitgo.go +++ b/cmd/genbindings/emitgo.go @@ -669,6 +669,13 @@ func (gfs *goFileState) emitCabiToGo(assignExpr string, rt CppParameter, rvalue func emitGo(src *CppParsedHeader, headerName string, packageName string) (string, string, error) { ret := strings.Builder{} + + platformRestriction := HeaderPlatformRestriction(headerName) + if platformRestriction != nil { + ret.WriteString(`//go:build ` + platformRestriction.GoBuildTag() + "\n" + + `// +build ` + platformRestriction.GoBuildTag() + "\n\n") + } + ret.WriteString(`package ` + path.Base(packageName) + ` /*