From 562590fa967d82b345b8e2fadad665a8e512d252 Mon Sep 17 00:00:00 2001 From: mappu Date: Wed, 16 Oct 2024 18:05:07 +1300 Subject: [PATCH] libmiqt: move binding helpers to standalone package, support const_char --- cmd/genbindings/emitcabi.go | 3 ++- cmd/genbindings/emitgo.go | 15 ++++++++++----- cmd/genbindings/main.go | 1 + qt/binding.cpp => libmiqt/libmiqt.cpp | 5 +---- qt/binding.h => libmiqt/libmiqt.h | 7 +++++-- qt/binding.go => libmiqt/strings.go | 8 ++++---- 6 files changed, 23 insertions(+), 16 deletions(-) rename qt/binding.cpp => libmiqt/libmiqt.cpp (78%) rename qt/binding.h => libmiqt/libmiqt.h (79%) rename qt/binding.go => libmiqt/strings.go (71%) diff --git a/cmd/genbindings/emitcabi.go b/cmd/genbindings/emitcabi.go index 03541721..3fa0fb10 100644 --- a/cmd/genbindings/emitcabi.go +++ b/cmd/genbindings/emitcabi.go @@ -480,6 +480,7 @@ func emitBindingHeader(src *CppParsedHeader, filename string) (string, error) { includeGuard := "GEN_" + strings.ToUpper(strings.Replace(filename, `.`, `_`, -1)) + bindingInclude := "../libmiqt/libmiqt.h" ret.WriteString(`#ifndef ` + includeGuard + ` #define ` + includeGuard + ` @@ -489,7 +490,7 @@ func emitBindingHeader(src *CppParsedHeader, filename string) (string, error) { #pragma GCC diagnostic ignored "-Wdeprecated-declarations" -#include "binding.h" +#include "` + bindingInclude + `" #ifdef __cplusplus extern "C" { diff --git a/cmd/genbindings/emitgo.go b/cmd/genbindings/emitgo.go index 79096ab0..1f5367cd 100644 --- a/cmd/genbindings/emitgo.go +++ b/cmd/genbindings/emitgo.go @@ -134,7 +134,8 @@ func (p CppParameter) parameterTypeCgo() string { } tmp := strings.Replace(p.RenderTypeCabi(), `*`, "", -1) - if strings.HasPrefix(tmp, "const ") { + + if strings.HasPrefix(tmp, "const ") && tmp != "const char" { // Special typedef to make this work for const char* signal parameters tmp = tmp[6:] // Constness doesn't survive the CABI boundary } if strings.HasPrefix(tmp, "unsigned ") { @@ -234,7 +235,8 @@ func (gfs *goFileState) emitParameterGo2CABIForwarding(p CppParameter) (preamble // Go: convert string -> miqt_string* // CABI: convert miqt_string* -> real QString - preamble += nameprefix + "_ms := miqt_strdupg(" + p.ParameterName + ")\n" + gfs.imports["libmiqt"] = struct{}{} + preamble += nameprefix + "_ms := libmiqt.Strdupg(" + p.ParameterName + ")\n" preamble += "defer C.free(" + nameprefix + "_ms)\n" rvalue = "(*C.struct_miqt_string)(" + nameprefix + "_ms)" @@ -660,14 +662,17 @@ import "C" if len(gfs.imports) > 0 { allImports := make([]string, 0, len(gfs.imports)) for k, _ := range gfs.imports { - allImports = append(allImports, `"`+k+`"`) + if k == "libmiqt" { + allImports = append(allImports, `"`+BaseModule+`/libmiqt"`) + } else { + allImports = append(allImports, `"`+k+`"`) + } } + sort.Strings(allImports) goSrc = strings.Replace(goSrc, `%%_IMPORTLIBS_%%`, "import (\n\t"+strings.Join(allImports, "\n\t")+"\n)", 1) - } else { goSrc = strings.Replace(goSrc, `%%_IMPORTLIBS_%%`, "", 1) - } // Run gofmt over the result diff --git a/cmd/genbindings/main.go b/cmd/genbindings/main.go index d02a6a81..a7cbe3bf 100644 --- a/cmd/genbindings/main.go +++ b/cmd/genbindings/main.go @@ -15,6 +15,7 @@ import ( const ( ClangSubprocessCount = 3 + BaseModule = "github.com/mappu/miqt" ) func cacheFilePath(inputHeader string) string { diff --git a/qt/binding.cpp b/libmiqt/libmiqt.cpp similarity index 78% rename from qt/binding.cpp rename to libmiqt/libmiqt.cpp index 15464ff2..5b786b34 100644 --- a/qt/binding.cpp +++ b/libmiqt/libmiqt.cpp @@ -1,7 +1,4 @@ -#include -#include - -#include "binding.h" +#include "libmiqt.h" struct miqt_string* miqt_strdup(const char* src, size_t len) { struct miqt_string* ret = (struct miqt_string*)( malloc(len + sizeof(size_t)) ); diff --git a/qt/binding.h b/libmiqt/libmiqt.h similarity index 79% rename from qt/binding.h rename to libmiqt/libmiqt.h index b9a087fa..73bbcf3b 100644 --- a/qt/binding.h +++ b/libmiqt/libmiqt.h @@ -1,6 +1,9 @@ #ifndef MIQT_BINDING_H #define MIQT_BINDING_H +#include +#include + #ifdef __cplusplus extern "C" { #endif @@ -15,10 +18,10 @@ struct miqt_array { void* data; // Separate, second allocation }; -// miqt_strdup allocates a miqt_string and copies C data into it. -// The function is defined in C++. struct miqt_string* miqt_strdup(const char* src, size_t len); +typedef const char const_char; + #ifdef __cplusplus } #endif diff --git a/qt/binding.go b/libmiqt/strings.go similarity index 71% rename from qt/binding.go rename to libmiqt/strings.go index 18bb70d2..5f6f5ddf 100644 --- a/qt/binding.go +++ b/libmiqt/strings.go @@ -1,10 +1,10 @@ -package qt +package libmiqt // SPDX-License-Identifier: MIT /* -#include "binding.h" +#include "libmiqt.h" struct miqt_string* miqt_strdupg(_GoString_ gs) { return miqt_strdup(_GoStringPtr(gs), _GoStringLen(gs)); @@ -17,9 +17,9 @@ import ( "unsafe" ) -// miqt_strdupg will strdup a Go string into a miqt_string*. +// Strdupg will strdup a Go string into a miqt_string*. // It is typed as returning an unsafe.Pointer because Cgo types cannot be shared // across Go file boundaries. -func miqt_strdupg(s string) unsafe.Pointer { +func Strdupg(s string) unsafe.Pointer { return unsafe.Pointer(C.miqt_strdupg(s)) }