From a683a17759f6c128057c20aedbdacb7755f19168 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:32:24 +0200 Subject: [PATCH 01/19] Initial go build steps --- Makefile.am | 4 ++++ bindings/go/fix.sh | 1 + bindings/go/go.mod | 3 +++ bindings/go/include.am | 34 ++++++++++++++++++++++++++++++++++ bindings/go/test_libmega.go | 27 +++++++++++++++++++++++++++ configure.ac | 31 ++++++++++++++++++++++++++++--- 6 files changed, 97 insertions(+), 3 deletions(-) create mode 100755 bindings/go/fix.sh create mode 100644 bindings/go/go.mod create mode 100644 bindings/go/include.am create mode 100644 bindings/go/test_libmega.go diff --git a/Makefile.am b/Makefile.am index e5e2dfb958..cee397827a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -63,6 +63,10 @@ if BUILD_PYTHON include bindings/python/include.am endif +if BUILD_GO +include bindings/go/include.am +endif + if BUILD_PHP include bindings/php/include.am endif diff --git a/bindings/go/fix.sh b/bindings/go/fix.sh new file mode 100755 index 0000000000..3b2a8bef01 --- /dev/null +++ b/bindings/go/fix.sh @@ -0,0 +1 @@ +sed -i 's,#include "megaapi.h",#include "../../../include/megaapi.h",' megasdk/mega_wrap.cpp diff --git a/bindings/go/go.mod b/bindings/go/go.mod new file mode 100644 index 0000000000..a5a340d505 --- /dev/null +++ b/bindings/go/go.mod @@ -0,0 +1,3 @@ +module megasdk + +go 1.20 diff --git a/bindings/go/include.am b/bindings/go/include.am new file mode 100644 index 0000000000..49e9666f77 --- /dev/null +++ b/bindings/go/include.am @@ -0,0 +1,34 @@ +# Output directory for Go bindings +GOBINDIR = bindings/go/megasdk + +# SWIG command for generating Go bindings +SWIG_GO = swig + +# Go package name +GOPACKAGE = mega + +# SWIG flags for Go bindings +SWIG_GO_FLAGS = -go -cgo -intgosize 64 -c++ + +# SWIG generated Go source file +GO_SOURCE = $(GOBINDIR)/mega_wrap.cpp + +# Clean Go bindings files +CLEANFILES += $(GO_SOURCE) + +# Sources for SWIG Go wrapper +nodist_bindings_go__mega_la_SOURCES = $(GO_SOURCE) +bindings_go__mega_la_SOURCES = $(top_srcdir)/bindings/megaapi.i + +bindings_go__mega_la_CPPFLAGS = -I$(top_srcdir)/include +bindings_go__mega_la_LDFLAGS = +bindings_go__mega_la_LIBADD = $(top_builddir)/src/libmega.la + +$(GO_SOURCE): $(top_srcdir)/bindings/megaapi.i + $(SWIG_GO) $(SWIG_GO_FLAGS) -o $@ -outdir $(GOBINDIR) -package $(GOPACKAGE) -I$(top_srcdir)/include $< + +.PHONY: go-bindings +go-bindings: $(GO_SOURCE) + +# Ensure that the Go bindings are built when requested +all-local: go-bindings diff --git a/bindings/go/test_libmega.go b/bindings/go/test_libmega.go new file mode 100644 index 0000000000..4abf40d57b --- /dev/null +++ b/bindings/go/test_libmega.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + mega "megasdk/megasdk" +) + +func main() { + // Your code goes here + fmt.Println("Hello, World!") + a := mega.NewMegaApi("ox8xnQZL", nil, nil, "Go CRUD example") + user, pass := getAuth() + a.Login(user, pass) + fmt.Println("Email: %s", a.GetMyEmail()) +} + +func getAuth() (username string, password string) { + fmt.Print("Enter your username: ") + fmt.Scan(&username) + + fmt.Print("Enter your password: ") + fmt.Scan(&password) + + fmt.Printf("Username: %s\n", username) + fmt.Printf("Password: %s\n", password) + return +} diff --git a/configure.ac b/configure.ac index fb23e4ff59..c3c935039a 100644 --- a/configure.ac +++ b/configure.ac @@ -293,13 +293,13 @@ fi AC_ARG_ENABLE(sync, AS_HELP_STRING([--enable-sync], [include sync subsystem [default=yes]]), [enable_sync=$enableval - if test "x$enable_sync" = "xyes" && (test "x$enable_java" = "xyes" || test "x$enable_python" = "xyes" || test "x$enable_php" = "xyes"); then + if test "x$enable_sync" = "xyes" && (test "x$enable_java" = "xyes" || test "x$enable_python" = "xyes" || test "x$enable_go" = "xyes" || test "x$enable_php" = "xyes"); then AC_MSG_ERROR([--enable-sync not allowed when building bindings (php/python/java)!]) fi ], [enable_sync=yes]) -if test "x$enable_java" = "xyes" || test "x$enable_python" = "xyes" || test "x$enable_php" = "xyes" ; then +if test "x$enable_java" = "xyes" || test "x$enable_python" = "xyes" || test "x$enable_go" = "xyes" || test "x$enable_php" = "xyes" ; then enable_sync=no fi @@ -433,7 +433,7 @@ AM_CONDITIONAL([BUILD_JAVA], [test "$enable_java" = "yes"]) AC_ARG_ENABLE(chat, AS_HELP_STRING([--enable-chat], [enable chat support]), [], [enable_chat=no]) -if test "x$enable_chat" = "xyes" || test "x$enable_java" = "xyes" || test "x$enable_python" = "xyes" || test "x$enable_php" = "xyes" ; then +if test "x$enable_chat" = "xyes" || test "x$enable_java" = "xyes" || test "x$enable_python" = "xyes" || test "x$enable_go" = "xyes" || test "x$enable_php" = "xyes" ; then enable_chat=yes AC_DEFINE(ENABLE_CHAT, 1, [Define to enable chat]) fi @@ -2172,6 +2172,30 @@ if test "x$enable_python" = "xyes" ; then fi AM_CONDITIONAL([BUILD_PYTHON], [test "$enable_python" = "yes"]) +# GO Lang +AC_MSG_CHECKING([if building Go bindings]) +AC_ARG_ENABLE(go, + AS_HELP_STRING([--enable-go], [build Go language bindings]), + [AC_MSG_RESULT([$enableval])], + [ + AC_MSG_RESULT([$enableval]) + enable_go=no] +) + +if test "x$enable_go" = "xyes" ; then + AX_PKG_SWIG(2.0.0, [], [ + AC_MSG_ERROR([SWIG executable not found!]) + ]) + AX_SWIG_ENABLE_CXX + AX_SWIG_MULTI_MODULE_SUPPORT + + if test -z "$SWIG"; then + AC_MSG_ERROR([SWIG executable not found!]) + fi + +fi +AM_CONDITIONAL([BUILD_GO], [test "$enable_go" = "yes"]) + # PHP AC_MSG_CHECKING([if building PHP bindings]) AC_ARG_ENABLE(php, @@ -2288,6 +2312,7 @@ AC_MSG_NOTICE([Configured to build Mega SDK: Python bindings: $enable_python Python3 bindings: $USE_PYTHON3 + Go bindings:: $enable_go PHP bindings: $enable_php SWIG_FLAGS_PHP: $SWIG_FLAGS_PHP From 457a394388b8107955e12d019d4a40d6c6c3ab0b Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:35:00 +0200 Subject: [PATCH 02/19] Add additional cleanup files for GO --- bindings/go/include.am | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bindings/go/include.am b/bindings/go/include.am index 49e9666f77..5f19638a47 100644 --- a/bindings/go/include.am +++ b/bindings/go/include.am @@ -15,6 +15,8 @@ GO_SOURCE = $(GOBINDIR)/mega_wrap.cpp # Clean Go bindings files CLEANFILES += $(GO_SOURCE) +CLEANFILES += $(GOBINDIR)/mega_wrap.h +CLEANFILES += $(GOBINDIR)/mega.go # Sources for SWIG Go wrapper nodist_bindings_go__mega_la_SOURCES = $(GO_SOURCE) From 82bf9747234b108257e05bd143f04bdcf933cf5a Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Thu, 28 Sep 2023 16:12:59 +0200 Subject: [PATCH 03/19] Patch 2 --- bindings/go/fix.sh | 8 ++++++++ bindings/go/go.mod | 2 +- bindings/go/include.am | 19 +++++++++++++------ bindings/go/test_libmega.go | 3 --- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/bindings/go/fix.sh b/bindings/go/fix.sh index 3b2a8bef01..5b5acb22ff 100755 --- a/bindings/go/fix.sh +++ b/bindings/go/fix.sh @@ -1 +1,9 @@ sed -i 's,#include "megaapi.h",#include "../../../include/megaapi.h",' megasdk/mega_wrap.cpp + + +#$(LIBRARY_PATH): $(GO_SOURCE) +# go build -o $@ -buildmode=c-shared $(GOBINDIR) + +ln -s '../../src/.libs/libmega.so' 'libmega.so' +#ln -s '.libs/_mega.so' '_mega.so' +go build -o megasdk/libmega_go.so -buildmode=c-shared megasdk diff --git a/bindings/go/go.mod b/bindings/go/go.mod index a5a340d505..5159e3b425 100644 --- a/bindings/go/go.mod +++ b/bindings/go/go.mod @@ -1,3 +1,3 @@ module megasdk -go 1.20 +go 1.17 diff --git a/bindings/go/include.am b/bindings/go/include.am index 5f19638a47..9b0ce222ef 100644 --- a/bindings/go/include.am +++ b/bindings/go/include.am @@ -11,26 +11,33 @@ GOPACKAGE = mega SWIG_GO_FLAGS = -go -cgo -intgosize 64 -c++ # SWIG generated Go source file -GO_SOURCE = $(GOBINDIR)/mega_wrap.cpp +GO_SOURCE = $(GOBINDIR)/megaapi_wrap.cpp + +# Shared library name +LIBRARY_NAME = libmega_go.so + +# Full path to the shared library +LIBRARY_PATH = $(GOBINDIR)/$(LIBRARY_NAME) # Clean Go bindings files CLEANFILES += $(GO_SOURCE) -CLEANFILES += $(GOBINDIR)/mega_wrap.h +CLEANFILES += $(GOBINDIR)/megaapi_wrap.h CLEANFILES += $(GOBINDIR)/mega.go +CLEANFILES += $(LIBRARY_PATH) # Sources for SWIG Go wrapper nodist_bindings_go__mega_la_SOURCES = $(GO_SOURCE) bindings_go__mega_la_SOURCES = $(top_srcdir)/bindings/megaapi.i bindings_go__mega_la_CPPFLAGS = -I$(top_srcdir)/include -bindings_go__mega_la_LDFLAGS = -bindings_go__mega_la_LIBADD = $(top_builddir)/src/libmega.la +bindings_go__mega_la_LDFLAGS = -shared -shrext .so -L$(GOBINDIR) -lmega_go +bindings_go__mega_la_LIBADD = $(top_builddir)/src/$(LIBRARY_NAME) $(GO_SOURCE): $(top_srcdir)/bindings/megaapi.i $(SWIG_GO) $(SWIG_GO_FLAGS) -o $@ -outdir $(GOBINDIR) -package $(GOPACKAGE) -I$(top_srcdir)/include $< .PHONY: go-bindings -go-bindings: $(GO_SOURCE) +go-bindings: $(GO_SOURCE) $(LIBRARY_PATH) -# Ensure that the Go bindings are built when requested +# Ensure that the Go bindings and the shared library are built when requested all-local: go-bindings diff --git a/bindings/go/test_libmega.go b/bindings/go/test_libmega.go index 4abf40d57b..59031d96b3 100644 --- a/bindings/go/test_libmega.go +++ b/bindings/go/test_libmega.go @@ -20,8 +20,5 @@ func getAuth() (username string, password string) { fmt.Print("Enter your password: ") fmt.Scan(&password) - - fmt.Printf("Username: %s\n", username) - fmt.Printf("Password: %s\n", password) return } From f51e725edc2340c295aa4ad95fbac9e52018fb03 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Thu, 28 Sep 2023 16:13:39 +0200 Subject: [PATCH 04/19] Proper git ignore --- bindings/go/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 bindings/go/.gitignore diff --git a/bindings/go/.gitignore b/bindings/go/.gitignore new file mode 100644 index 0000000000..bbd64eba14 --- /dev/null +++ b/bindings/go/.gitignore @@ -0,0 +1,2 @@ +# binding Python: only SWIG auto-generated files +megasdk/* From b20293e10db10a3ff4bb5c65a15865bc86e643ec Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Thu, 28 Sep 2023 16:19:03 +0200 Subject: [PATCH 05/19] fix file rename --- bindings/go/fix.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/go/fix.sh b/bindings/go/fix.sh index 5b5acb22ff..ed5fb06ce8 100755 --- a/bindings/go/fix.sh +++ b/bindings/go/fix.sh @@ -1,4 +1,4 @@ -sed -i 's,#include "megaapi.h",#include "../../../include/megaapi.h",' megasdk/mega_wrap.cpp +sed -i 's,#include "megaapi.h",#include "../../../include/megaapi.h",' megasdk/megaapi_wrap.cpp #$(LIBRARY_PATH): $(GO_SOURCE) From 27a480a76f51be17464b16ae609e0d76b8eda702 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Thu, 28 Sep 2023 16:50:26 +0200 Subject: [PATCH 06/19] Still errors out, but no more linker issues (hopefully) --- bindings/go/.gitignore | 6 +++++- bindings/go/fix.sh | 2 +- bindings/go/include.am | 13 +++++++------ bindings/go/{test_libmega.go => main.go} | 3 +++ bindings/go/megasdk/megaapi_wrap.swigcxx | 6 ++++++ bindings/go/megasdk/package.go | 3 +++ 6 files changed, 25 insertions(+), 8 deletions(-) rename bindings/go/{test_libmega.go => main.go} (90%) create mode 100644 bindings/go/megasdk/megaapi_wrap.swigcxx create mode 100644 bindings/go/megasdk/package.go diff --git a/bindings/go/.gitignore b/bindings/go/.gitignore index bbd64eba14..eaf487cfd3 100644 --- a/bindings/go/.gitignore +++ b/bindings/go/.gitignore @@ -1,2 +1,6 @@ # binding Python: only SWIG auto-generated files -megasdk/* +megasdk/.deps +megasdk/.libs +megasdk/mega.go +megasdk/megaapi_wrap.cpp +megasdk/megaapi_wrap.h diff --git a/bindings/go/fix.sh b/bindings/go/fix.sh index ed5fb06ce8..45607d4499 100755 --- a/bindings/go/fix.sh +++ b/bindings/go/fix.sh @@ -6,4 +6,4 @@ sed -i 's,#include "megaapi.h",#include "../../../include/megaapi.h",' megasdk/m ln -s '../../src/.libs/libmega.so' 'libmega.so' #ln -s '.libs/_mega.so' '_mega.so' -go build -o megasdk/libmega_go.so -buildmode=c-shared megasdk +#go build -o megasdk/libmega_go.so -buildmode=c-shared megasdk diff --git a/bindings/go/include.am b/bindings/go/include.am index 9b0ce222ef..def843a628 100644 --- a/bindings/go/include.am +++ b/bindings/go/include.am @@ -1,11 +1,13 @@ # Output directory for Go bindings GOBINDIR = bindings/go/megasdk +pkggoexec_LTLIBRARIES = bindings/go/lib_mega.la +pkggoexecdir = $(GOBINDIR) # SWIG command for generating Go bindings SWIG_GO = swig # Go package name -GOPACKAGE = mega +GOPACKAGE = megasdk # SWIG flags for Go bindings SWIG_GO_FLAGS = -go -cgo -intgosize 64 -c++ @@ -26,12 +28,11 @@ CLEANFILES += $(GOBINDIR)/mega.go CLEANFILES += $(LIBRARY_PATH) # Sources for SWIG Go wrapper -nodist_bindings_go__mega_la_SOURCES = $(GO_SOURCE) -bindings_go__mega_la_SOURCES = $(top_srcdir)/bindings/megaapi.i +nodist_bindings_go_lib_mega_la_SOURCES = $(GO_SOURCE) +bindings_go_lib_mega_la_SOURCES = $(top_srcdir)/bindings/megaapi.i -bindings_go__mega_la_CPPFLAGS = -I$(top_srcdir)/include -bindings_go__mega_la_LDFLAGS = -shared -shrext .so -L$(GOBINDIR) -lmega_go -bindings_go__mega_la_LIBADD = $(top_builddir)/src/$(LIBRARY_NAME) +bindings_go_lib_mega_la_CPPFLAGS = -I$(top_srcdir)/include +bindings_go_lib_mega_la_LIBADD = $(top_builddir)/src/$(LIBRARY_NAME) $(GO_SOURCE): $(top_srcdir)/bindings/megaapi.i $(SWIG_GO) $(SWIG_GO_FLAGS) -o $@ -outdir $(GOBINDIR) -package $(GOPACKAGE) -I$(top_srcdir)/include $< diff --git a/bindings/go/test_libmega.go b/bindings/go/main.go similarity index 90% rename from bindings/go/test_libmega.go rename to bindings/go/main.go index 59031d96b3..2a6598ad68 100644 --- a/bindings/go/test_libmega.go +++ b/bindings/go/main.go @@ -1,5 +1,8 @@ package main +/* +#cgo LDFLAGS: -L ./megasdk -linvoke -ldl +*/ import ( "fmt" mega "megasdk/megasdk" diff --git a/bindings/go/megasdk/megaapi_wrap.swigcxx b/bindings/go/megasdk/megaapi_wrap.swigcxx new file mode 100644 index 0000000000..c4bb8db15f --- /dev/null +++ b/bindings/go/megasdk/megaapi_wrap.swigcxx @@ -0,0 +1,6 @@ +%module megasdk +%{ +#include "megaapi_wrap.h" +%} + +%include "megaapi_wrap.h" \ No newline at end of file diff --git a/bindings/go/megasdk/package.go b/bindings/go/megasdk/package.go new file mode 100644 index 0000000000..10dd1c479f --- /dev/null +++ b/bindings/go/megasdk/package.go @@ -0,0 +1,3 @@ +package megasdk + +import "C" From 85c64e61c2ac786b619fbfd2392a1ae855cabc45 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Thu, 28 Sep 2023 21:57:00 +0200 Subject: [PATCH 07/19] cgo: inconsistent definitions --- bindings/go/go.mod | 2 +- bindings/go/include.am | 2 +- bindings/go/main.go | 5 +---- bindings/go/megasdk/mega.swigcxx | 10 ++++++++++ bindings/go/megasdk/megaapi_wrap.swigcxx | 6 ------ bindings/go/megasdk/package.go | 3 ++- 6 files changed, 15 insertions(+), 13 deletions(-) create mode 100644 bindings/go/megasdk/mega.swigcxx delete mode 100644 bindings/go/megasdk/megaapi_wrap.swigcxx diff --git a/bindings/go/go.mod b/bindings/go/go.mod index 5159e3b425..b0e586d16f 100644 --- a/bindings/go/go.mod +++ b/bindings/go/go.mod @@ -1,3 +1,3 @@ -module megasdk +module mega go 1.17 diff --git a/bindings/go/include.am b/bindings/go/include.am index def843a628..8689947765 100644 --- a/bindings/go/include.am +++ b/bindings/go/include.am @@ -7,7 +7,7 @@ pkggoexecdir = $(GOBINDIR) SWIG_GO = swig # Go package name -GOPACKAGE = megasdk +GOPACKAGE = mega # SWIG flags for Go bindings SWIG_GO_FLAGS = -go -cgo -intgosize 64 -c++ diff --git a/bindings/go/main.go b/bindings/go/main.go index 2a6598ad68..d12784bebd 100644 --- a/bindings/go/main.go +++ b/bindings/go/main.go @@ -1,11 +1,8 @@ package main -/* -#cgo LDFLAGS: -L ./megasdk -linvoke -ldl -*/ import ( "fmt" - mega "megasdk/megasdk" + mega "mega/megasdk" ) func main() { diff --git a/bindings/go/megasdk/mega.swigcxx b/bindings/go/megasdk/mega.swigcxx new file mode 100644 index 0000000000..a884722f12 --- /dev/null +++ b/bindings/go/megasdk/mega.swigcxx @@ -0,0 +1,10 @@ +%module mega +%{ +#include "megaapi_wrap.h" +%} + +%include "megaapi_wrap.h" + +%feature("notfeature") MegaGfxProcessor; +%feature("notfeature") MegaTransferListener; +%feature("notfeature") MegaGlobalListener; diff --git a/bindings/go/megasdk/megaapi_wrap.swigcxx b/bindings/go/megasdk/megaapi_wrap.swigcxx deleted file mode 100644 index c4bb8db15f..0000000000 --- a/bindings/go/megasdk/megaapi_wrap.swigcxx +++ /dev/null @@ -1,6 +0,0 @@ -%module megasdk -%{ -#include "megaapi_wrap.h" -%} - -%include "megaapi_wrap.h" \ No newline at end of file diff --git a/bindings/go/megasdk/package.go b/bindings/go/megasdk/package.go index 10dd1c479f..fb4c8f154e 100644 --- a/bindings/go/megasdk/package.go +++ b/bindings/go/megasdk/package.go @@ -1,3 +1,4 @@ -package megasdk +package mega +// #cgo CXXFLAGS: -std=c++11 import "C" From 9d89c95197ec08ae99fc903248ce8086905e93bf Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Thu, 28 Sep 2023 22:29:03 +0200 Subject: [PATCH 08/19] TODO: errors.c --- bindings/go/errors.c | 29 +++++++++++++++++++++++++++++ bindings/go/megasdk/mega.swigcxx | 4 ---- bindings/go/megasdk/package.go | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 bindings/go/errors.c diff --git a/bindings/go/errors.c b/bindings/go/errors.c new file mode 100644 index 0000000000..f995addda3 --- /dev/null +++ b/bindings/go/errors.c @@ -0,0 +1,29 @@ +$ cat b043/_mega_swig.go | grep swig_type_24 +typedef long long swig_type_24; +extern _Bool _wrap_SwigDirector_MegaTransferListener_onTransferData_mega_140d19983b5f0e49(uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, swig_type_23 arg4, swig_type_24 arg5); + swig_r = (bool)(C._wrap_SwigDirector_MegaTransferListener_onTransferData_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), C.uintptr_t(_swig_i_1), C.uintptr_t(_swig_i_2), *(*C.swig_type_23)(unsafe.Pointer(&_swig_i_3)), C.swig_type_24(_swig_i_4))) + +$ cat b043/_mega_swig.go | grep swig_type_3 +typedef _gostring_ swig_type_3; +extern _Bool _wrap_SwigDirector_MegaGfxProcessor__swig_upcall_getBitmapData_mega_140d19983b5f0e49(uintptr_t arg1, swig_type_3 arg2, swig_type_4 arg3); + swig_r = (bool)(C._wrap_SwigDirector_MegaGfxProcessor__swig_upcall_getBitmapData_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), *(*C.swig_type_3)(unsafe.Pointer(&_swig_i_1)), C.swig_type_4(_swig_i_2))) + +$ cat b043/_mega_swig.go | grep swig_type_26 +typedef _gostring_ swig_type_26; +extern void _wrap_SwigDirector_MegaGlobalListener_onDrivePresenceChanged_mega_140d19983b5f0e49(uintptr_t arg1, uintptr_t arg2, _Bool arg3, swig_type_26 arg4); + C._wrap_SwigDirector_MegaGlobalListener_onDrivePresenceChanged_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), C.uintptr_t(_swig_i_1), C._Bool(_swig_i_2), *(*C.swig_type_26)(unsafe.Pointer(&_swig_i_3))) + +$ cat b043/_mega_swig.go | grep swig_type_22 +typedef long long swig_type_22; +extern _Bool _wrap_SwigDirector_MegaTransferListener__swig_upcall_onTransferData_mega_140d19983b5f0e49(uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, swig_type_21 arg4, swig_type_22 arg5); + swig_r = (bool)(C._wrap_SwigDirector_MegaTransferListener__swig_upcall_onTransferData_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), C.uintptr_t(_swig_i_1), C.uintptr_t(_swig_i_2), *(*C.swig_type_21)(unsafe.Pointer(&_swig_i_3)), C.swig_type_22(_swig_i_4))) + +$ cat b043/_mega_swig.go | grep swig_type_4 +typedef long long swig_type_4; +extern _Bool _wrap_SwigDirector_MegaGfxProcessor__swig_upcall_getBitmapData_mega_140d19983b5f0e49(uintptr_t arg1, swig_type_3 arg2, swig_type_4 arg3); + swig_r = (bool)(C._wrap_SwigDirector_MegaGfxProcessor__swig_upcall_getBitmapData_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), *(*C.swig_type_3)(unsafe.Pointer(&_swig_i_1)), C.swig_type_4(_swig_i_2))) + +$ cat b043/_mega_swig.go | grep swig_type_2 +typedef _gostring_ swig_type_2; +extern _Bool _wrap_SwigDirector_MegaGfxProcessor_readBitmap_mega_140d19983b5f0e49(uintptr_t arg1, swig_type_2 arg2); + swig_r = (bool)(C._wrap_SwigDirector_MegaGfxProcessor_readBitmap_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), *(*C.swig_type_2)(unsafe.Pointer(&_swig_i_1)))) diff --git a/bindings/go/megasdk/mega.swigcxx b/bindings/go/megasdk/mega.swigcxx index a884722f12..5b6fd7dbb9 100644 --- a/bindings/go/megasdk/mega.swigcxx +++ b/bindings/go/megasdk/mega.swigcxx @@ -4,7 +4,3 @@ %} %include "megaapi_wrap.h" - -%feature("notfeature") MegaGfxProcessor; -%feature("notfeature") MegaTransferListener; -%feature("notfeature") MegaGlobalListener; diff --git a/bindings/go/megasdk/package.go b/bindings/go/megasdk/package.go index fb4c8f154e..44b42aec82 100644 --- a/bindings/go/megasdk/package.go +++ b/bindings/go/megasdk/package.go @@ -1,4 +1,4 @@ package mega -// #cgo CXXFLAGS: -std=c++11 +// #cgo LDFLAGS: -L${SRCDIR} -lmega import "C" From aca4faf93d7a3bba1f97d477b69c3b2e9b0395a4 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Thu, 28 Sep 2023 22:33:21 +0200 Subject: [PATCH 09/19] Remove not used libraries --- bindings/go/errors.c | 17 +++++++++++++++++ bindings/go/include.am | 9 --------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/bindings/go/errors.c b/bindings/go/errors.c index f995addda3..bcaa17c2ec 100644 --- a/bindings/go/errors.c +++ b/bindings/go/errors.c @@ -1,3 +1,20 @@ +# mega/megasdk +megaapi_wrap.h:18: Warning 401: Nothing known about base class 'mega::MegaGfxProcessor'. Ignored. +megaapi_wrap.h:60: Warning 401: Nothing known about base class 'mega::MegaLogger'. Ignored. +megaapi_wrap.h:74: Warning 401: Nothing known about base class 'mega::MegaTreeProcessor'. Ignored. +megaapi_wrap.h:88: Warning 401: Nothing known about base class 'mega::MegaRequestListener'. Ignored. +megaapi_wrap.h:114: Warning 401: Nothing known about base class 'mega::MegaTransferListener'. Ignored. +megaapi_wrap.h:148: Warning 401: Nothing known about base class 'mega::MegaGlobalListener'. Ignored. +megaapi_wrap.h:202: Warning 401: Nothing known about base class 'mega::MegaListener'. Ignored. +# mega/megasdk +cgo: inconsistent definitions for C.swig_type_4 +cgo: inconsistent definitions for C.swig_type_2 +cgo: inconsistent definitions for C.swig_type_26 +cgo: inconsistent definitions for C.swig_type_3 +cgo: inconsistent definitions for C.swig_type_22 +cgo: inconsistent definitions for C.swig_type_24 + + $ cat b043/_mega_swig.go | grep swig_type_24 typedef long long swig_type_24; extern _Bool _wrap_SwigDirector_MegaTransferListener_onTransferData_mega_140d19983b5f0e49(uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, swig_type_23 arg4, swig_type_24 arg5); diff --git a/bindings/go/include.am b/bindings/go/include.am index 8689947765..47a3343525 100644 --- a/bindings/go/include.am +++ b/bindings/go/include.am @@ -1,7 +1,5 @@ # Output directory for Go bindings GOBINDIR = bindings/go/megasdk -pkggoexec_LTLIBRARIES = bindings/go/lib_mega.la -pkggoexecdir = $(GOBINDIR) # SWIG command for generating Go bindings SWIG_GO = swig @@ -15,12 +13,6 @@ SWIG_GO_FLAGS = -go -cgo -intgosize 64 -c++ # SWIG generated Go source file GO_SOURCE = $(GOBINDIR)/megaapi_wrap.cpp -# Shared library name -LIBRARY_NAME = libmega_go.so - -# Full path to the shared library -LIBRARY_PATH = $(GOBINDIR)/$(LIBRARY_NAME) - # Clean Go bindings files CLEANFILES += $(GO_SOURCE) CLEANFILES += $(GOBINDIR)/megaapi_wrap.h @@ -32,7 +24,6 @@ nodist_bindings_go_lib_mega_la_SOURCES = $(GO_SOURCE) bindings_go_lib_mega_la_SOURCES = $(top_srcdir)/bindings/megaapi.i bindings_go_lib_mega_la_CPPFLAGS = -I$(top_srcdir)/include -bindings_go_lib_mega_la_LIBADD = $(top_builddir)/src/$(LIBRARY_NAME) $(GO_SOURCE): $(top_srcdir)/bindings/megaapi.i $(SWIG_GO) $(SWIG_GO_FLAGS) -o $@ -outdir $(GOBINDIR) -package $(GOPACKAGE) -I$(top_srcdir)/include $< From c50044c829f5ca73887c54788f107a88fcc62938 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Sat, 30 Sep 2023 14:10:31 +0200 Subject: [PATCH 10/19] Fix some linker issues, still have error while loading errors --- bindings/go/.gitignore | 1 + bindings/go/errors.c | 46 -------------------------------- bindings/go/fix.sh | 9 ------- bindings/go/include.am | 6 +++++ bindings/go/megasdk/mega.swigcxx | 6 ----- bindings/go/megasdk/package.go | 4 --- 6 files changed, 7 insertions(+), 65 deletions(-) delete mode 100644 bindings/go/errors.c delete mode 100755 bindings/go/fix.sh delete mode 100644 bindings/go/megasdk/mega.swigcxx delete mode 100644 bindings/go/megasdk/package.go diff --git a/bindings/go/.gitignore b/bindings/go/.gitignore index eaf487cfd3..831585208e 100644 --- a/bindings/go/.gitignore +++ b/bindings/go/.gitignore @@ -4,3 +4,4 @@ megasdk/.libs megasdk/mega.go megasdk/megaapi_wrap.cpp megasdk/megaapi_wrap.h +megasdk/package.go diff --git a/bindings/go/errors.c b/bindings/go/errors.c deleted file mode 100644 index bcaa17c2ec..0000000000 --- a/bindings/go/errors.c +++ /dev/null @@ -1,46 +0,0 @@ -# mega/megasdk -megaapi_wrap.h:18: Warning 401: Nothing known about base class 'mega::MegaGfxProcessor'. Ignored. -megaapi_wrap.h:60: Warning 401: Nothing known about base class 'mega::MegaLogger'. Ignored. -megaapi_wrap.h:74: Warning 401: Nothing known about base class 'mega::MegaTreeProcessor'. Ignored. -megaapi_wrap.h:88: Warning 401: Nothing known about base class 'mega::MegaRequestListener'. Ignored. -megaapi_wrap.h:114: Warning 401: Nothing known about base class 'mega::MegaTransferListener'. Ignored. -megaapi_wrap.h:148: Warning 401: Nothing known about base class 'mega::MegaGlobalListener'. Ignored. -megaapi_wrap.h:202: Warning 401: Nothing known about base class 'mega::MegaListener'. Ignored. -# mega/megasdk -cgo: inconsistent definitions for C.swig_type_4 -cgo: inconsistent definitions for C.swig_type_2 -cgo: inconsistent definitions for C.swig_type_26 -cgo: inconsistent definitions for C.swig_type_3 -cgo: inconsistent definitions for C.swig_type_22 -cgo: inconsistent definitions for C.swig_type_24 - - -$ cat b043/_mega_swig.go | grep swig_type_24 -typedef long long swig_type_24; -extern _Bool _wrap_SwigDirector_MegaTransferListener_onTransferData_mega_140d19983b5f0e49(uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, swig_type_23 arg4, swig_type_24 arg5); - swig_r = (bool)(C._wrap_SwigDirector_MegaTransferListener_onTransferData_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), C.uintptr_t(_swig_i_1), C.uintptr_t(_swig_i_2), *(*C.swig_type_23)(unsafe.Pointer(&_swig_i_3)), C.swig_type_24(_swig_i_4))) - -$ cat b043/_mega_swig.go | grep swig_type_3 -typedef _gostring_ swig_type_3; -extern _Bool _wrap_SwigDirector_MegaGfxProcessor__swig_upcall_getBitmapData_mega_140d19983b5f0e49(uintptr_t arg1, swig_type_3 arg2, swig_type_4 arg3); - swig_r = (bool)(C._wrap_SwigDirector_MegaGfxProcessor__swig_upcall_getBitmapData_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), *(*C.swig_type_3)(unsafe.Pointer(&_swig_i_1)), C.swig_type_4(_swig_i_2))) - -$ cat b043/_mega_swig.go | grep swig_type_26 -typedef _gostring_ swig_type_26; -extern void _wrap_SwigDirector_MegaGlobalListener_onDrivePresenceChanged_mega_140d19983b5f0e49(uintptr_t arg1, uintptr_t arg2, _Bool arg3, swig_type_26 arg4); - C._wrap_SwigDirector_MegaGlobalListener_onDrivePresenceChanged_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), C.uintptr_t(_swig_i_1), C._Bool(_swig_i_2), *(*C.swig_type_26)(unsafe.Pointer(&_swig_i_3))) - -$ cat b043/_mega_swig.go | grep swig_type_22 -typedef long long swig_type_22; -extern _Bool _wrap_SwigDirector_MegaTransferListener__swig_upcall_onTransferData_mega_140d19983b5f0e49(uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, swig_type_21 arg4, swig_type_22 arg5); - swig_r = (bool)(C._wrap_SwigDirector_MegaTransferListener__swig_upcall_onTransferData_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), C.uintptr_t(_swig_i_1), C.uintptr_t(_swig_i_2), *(*C.swig_type_21)(unsafe.Pointer(&_swig_i_3)), C.swig_type_22(_swig_i_4))) - -$ cat b043/_mega_swig.go | grep swig_type_4 -typedef long long swig_type_4; -extern _Bool _wrap_SwigDirector_MegaGfxProcessor__swig_upcall_getBitmapData_mega_140d19983b5f0e49(uintptr_t arg1, swig_type_3 arg2, swig_type_4 arg3); - swig_r = (bool)(C._wrap_SwigDirector_MegaGfxProcessor__swig_upcall_getBitmapData_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), *(*C.swig_type_3)(unsafe.Pointer(&_swig_i_1)), C.swig_type_4(_swig_i_2))) - -$ cat b043/_mega_swig.go | grep swig_type_2 -typedef _gostring_ swig_type_2; -extern _Bool _wrap_SwigDirector_MegaGfxProcessor_readBitmap_mega_140d19983b5f0e49(uintptr_t arg1, swig_type_2 arg2); - swig_r = (bool)(C._wrap_SwigDirector_MegaGfxProcessor_readBitmap_mega_140d19983b5f0e49(C.uintptr_t(_swig_i_0), *(*C.swig_type_2)(unsafe.Pointer(&_swig_i_1)))) diff --git a/bindings/go/fix.sh b/bindings/go/fix.sh deleted file mode 100755 index 45607d4499..0000000000 --- a/bindings/go/fix.sh +++ /dev/null @@ -1,9 +0,0 @@ -sed -i 's,#include "megaapi.h",#include "../../../include/megaapi.h",' megasdk/megaapi_wrap.cpp - - -#$(LIBRARY_PATH): $(GO_SOURCE) -# go build -o $@ -buildmode=c-shared $(GOBINDIR) - -ln -s '../../src/.libs/libmega.so' 'libmega.so' -#ln -s '.libs/_mega.so' '_mega.so' -#go build -o megasdk/libmega_go.so -buildmode=c-shared megasdk diff --git a/bindings/go/include.am b/bindings/go/include.am index 47a3343525..8b9d19b894 100644 --- a/bindings/go/include.am +++ b/bindings/go/include.am @@ -25,8 +25,14 @@ bindings_go_lib_mega_la_SOURCES = $(top_srcdir)/bindings/megaapi.i bindings_go_lib_mega_la_CPPFLAGS = -I$(top_srcdir)/include +ABS_TOP_SRCDIR := $(realpath $(top_srcdir)) $(GO_SOURCE): $(top_srcdir)/bindings/megaapi.i $(SWIG_GO) $(SWIG_GO_FLAGS) -o $@ -outdir $(GOBINDIR) -package $(GOPACKAGE) -I$(top_srcdir)/include $< + @sed -i 's,#include "megaapi.h",#include "../../../include/megaapi.h",' $(top_srcdir)/bindings/go/megasdk/megaapi_wrap.cpp + @echo 'package mega' > $(top_srcdir)/bindings/go/megasdk/package.go + @echo '' >> $(top_srcdir)/bindings/go/megasdk/package.go + @echo '// #cgo LDFLAGS: -L$(ABS_TOP_SRCDIR)/src/.libs -lmega' >> $(top_srcdir)/bindings/go/megasdk/package.go + @echo 'import "C"' >> $(top_srcdir)/bindings/go/megasdk/package.go .PHONY: go-bindings go-bindings: $(GO_SOURCE) $(LIBRARY_PATH) diff --git a/bindings/go/megasdk/mega.swigcxx b/bindings/go/megasdk/mega.swigcxx deleted file mode 100644 index 5b6fd7dbb9..0000000000 --- a/bindings/go/megasdk/mega.swigcxx +++ /dev/null @@ -1,6 +0,0 @@ -%module mega -%{ -#include "megaapi_wrap.h" -%} - -%include "megaapi_wrap.h" diff --git a/bindings/go/megasdk/package.go b/bindings/go/megasdk/package.go deleted file mode 100644 index 44b42aec82..0000000000 --- a/bindings/go/megasdk/package.go +++ /dev/null @@ -1,4 +0,0 @@ -package mega - -// #cgo LDFLAGS: -L${SRCDIR} -lmega -import "C" From 24dfe776cbdec5d2c8197c3cf19a4833f08519ef Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Sat, 30 Sep 2023 14:52:35 +0200 Subject: [PATCH 11/19] It now works --- bindings/go/.gitignore | 1 - bindings/go/include.am | 4 ---- bindings/go/main.go | 42 ++++++++++++++++++++++++++++++---- bindings/go/megasdk/package.go | 4 ++++ 4 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 bindings/go/megasdk/package.go diff --git a/bindings/go/.gitignore b/bindings/go/.gitignore index 831585208e..eaf487cfd3 100644 --- a/bindings/go/.gitignore +++ b/bindings/go/.gitignore @@ -4,4 +4,3 @@ megasdk/.libs megasdk/mega.go megasdk/megaapi_wrap.cpp megasdk/megaapi_wrap.h -megasdk/package.go diff --git a/bindings/go/include.am b/bindings/go/include.am index 8b9d19b894..7d5b124941 100644 --- a/bindings/go/include.am +++ b/bindings/go/include.am @@ -29,10 +29,6 @@ ABS_TOP_SRCDIR := $(realpath $(top_srcdir)) $(GO_SOURCE): $(top_srcdir)/bindings/megaapi.i $(SWIG_GO) $(SWIG_GO_FLAGS) -o $@ -outdir $(GOBINDIR) -package $(GOPACKAGE) -I$(top_srcdir)/include $< @sed -i 's,#include "megaapi.h",#include "../../../include/megaapi.h",' $(top_srcdir)/bindings/go/megasdk/megaapi_wrap.cpp - @echo 'package mega' > $(top_srcdir)/bindings/go/megasdk/package.go - @echo '' >> $(top_srcdir)/bindings/go/megasdk/package.go - @echo '// #cgo LDFLAGS: -L$(ABS_TOP_SRCDIR)/src/.libs -lmega' >> $(top_srcdir)/bindings/go/megasdk/package.go - @echo 'import "C"' >> $(top_srcdir)/bindings/go/megasdk/package.go .PHONY: go-bindings go-bindings: $(GO_SOURCE) $(LIBRARY_PATH) diff --git a/bindings/go/main.go b/bindings/go/main.go index d12784bebd..681bc8a9d9 100644 --- a/bindings/go/main.go +++ b/bindings/go/main.go @@ -1,17 +1,51 @@ +// LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../../src/.libs go run ./main.go package main import ( "fmt" mega "mega/megasdk" + "time" ) +type MyMegaListener struct { + mega.SwigDirector_MegaListener +} + +func (l *MyMegaListener) OnRequestFinish(api mega.MegaApi, request mega.MegaRequest, e mega.MegaError) { + fmt.Printf("Request finished (%v); Result: %v\n", request.ToString(), e) + + switch request.GetType() { + case mega.MegaRequestTYPE_LOGIN: + api.FetchNodes() + case mega.MegaRequestTYPE_FETCH_NODES: + api.GetRootNode() + case mega.MegaRequestTYPE_ACCOUNT_DETAILS: + account_details := request.GetMegaAccountDetails() + fmt.Printf("Account details received\n") + fmt.Printf("Storage: %v of %v (%v %%)\n", + account_details.GetStorageUsed(), + account_details.GetStorageMax(), + 100*account_details.GetStorageUsed()/account_details.GetStorageMax()) + fmt.Printf("Pro level: %v\n", account_details.GetProLevel()) + } +} + +func (l *MyMegaListener) OnRequestStart(api mega.MegaApi, request mega.MegaRequest) { + fmt.Printf("Request start: (%v)\n", request) +} + func main() { - // Your code goes here + listener := mega.NewDirectorMegaListener(&MyMegaListener{}) + fmt.Println("Hello, World!") - a := mega.NewMegaApi("ox8xnQZL", nil, nil, "Go CRUD example") + api := mega.NewMegaApi("ox8xnQZL") + api.AddListener(listener) + user, pass := getAuth() - a.Login(user, pass) - fmt.Println("Email: %s", a.GetMyEmail()) + api.Login(user, pass) + + time.Sleep(10 * time.Second) + fmt.Println("Email: " + api.GetMyEmail()) } func getAuth() (username string, password string) { diff --git a/bindings/go/megasdk/package.go b/bindings/go/megasdk/package.go new file mode 100644 index 0000000000..c2acf308e0 --- /dev/null +++ b/bindings/go/megasdk/package.go @@ -0,0 +1,4 @@ +package mega + +// #cgo LDFLAGS: -L../../../src/.libs -lmega +import "C" From 7d1f5a2079745be455179e0b0bb61625c8267ea9 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Sat, 30 Sep 2023 15:05:41 +0200 Subject: [PATCH 12/19] Example fixes --- bindings/go/main.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/bindings/go/main.go b/bindings/go/main.go index 681bc8a9d9..f81082aaa3 100644 --- a/bindings/go/main.go +++ b/bindings/go/main.go @@ -1,4 +1,5 @@ // LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../../src/.libs go run ./main.go +// You need libmega.so.VERSION package main import ( @@ -12,8 +13,9 @@ type MyMegaListener struct { } func (l *MyMegaListener) OnRequestFinish(api mega.MegaApi, request mega.MegaRequest, e mega.MegaError) { - fmt.Printf("Request finished (%v); Result: %v\n", request.ToString(), e) + fmt.Printf("Request finished (%v); Result: %v\n", request.ToString(), e.ToString()) + // TODO: Mutex lock this for return values switch request.GetType() { case mega.MegaRequestTYPE_LOGIN: api.FetchNodes() @@ -31,7 +33,7 @@ func (l *MyMegaListener) OnRequestFinish(api mega.MegaApi, request mega.MegaRequ } func (l *MyMegaListener) OnRequestStart(api mega.MegaApi, request mega.MegaRequest) { - fmt.Printf("Request start: (%v)\n", request) + fmt.Printf("Request start: (%v)\n", request.ToString()) } func main() { @@ -43,9 +45,12 @@ func main() { user, pass := getAuth() api.Login(user, pass) + defer api.Logout() - time.Sleep(10 * time.Second) + time.Sleep(5 * time.Second) fmt.Println("Email: " + api.GetMyEmail()) + api.GetAccountDetails() + time.Sleep(5 * time.Second) } func getAuth() (username string, password string) { From 6e59b71fd0d6ce0f2283a311401d167bad200e60 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Sat, 30 Sep 2023 23:26:28 +0200 Subject: [PATCH 13/19] Small update to bring it in line with the other bindings --- bindings/go/.gitignore | 8 +++----- bindings/go/include.am | 6 ++---- bindings/go/megasdk/package.go | 4 ---- bindings/go/package.go | 4 ++++ {bindings => examples}/go/main.go | 3 ++- 5 files changed, 11 insertions(+), 14 deletions(-) delete mode 100644 bindings/go/megasdk/package.go create mode 100644 bindings/go/package.go rename {bindings => examples}/go/main.go (99%) diff --git a/bindings/go/.gitignore b/bindings/go/.gitignore index eaf487cfd3..62b16fad2a 100644 --- a/bindings/go/.gitignore +++ b/bindings/go/.gitignore @@ -1,6 +1,4 @@ # binding Python: only SWIG auto-generated files -megasdk/.deps -megasdk/.libs -megasdk/mega.go -megasdk/megaapi_wrap.cpp -megasdk/megaapi_wrap.h +mega.go +megaapi_wrap.cpp +megaapi_wrap.h diff --git a/bindings/go/include.am b/bindings/go/include.am index 7d5b124941..eb06965e30 100644 --- a/bindings/go/include.am +++ b/bindings/go/include.am @@ -1,5 +1,5 @@ # Output directory for Go bindings -GOBINDIR = bindings/go/megasdk +GOBINDIR = bindings/go # SWIG command for generating Go bindings SWIG_GO = swig @@ -22,13 +22,11 @@ CLEANFILES += $(LIBRARY_PATH) # Sources for SWIG Go wrapper nodist_bindings_go_lib_mega_la_SOURCES = $(GO_SOURCE) bindings_go_lib_mega_la_SOURCES = $(top_srcdir)/bindings/megaapi.i - bindings_go_lib_mega_la_CPPFLAGS = -I$(top_srcdir)/include -ABS_TOP_SRCDIR := $(realpath $(top_srcdir)) $(GO_SOURCE): $(top_srcdir)/bindings/megaapi.i $(SWIG_GO) $(SWIG_GO_FLAGS) -o $@ -outdir $(GOBINDIR) -package $(GOPACKAGE) -I$(top_srcdir)/include $< - @sed -i 's,#include "megaapi.h",#include "../../../include/megaapi.h",' $(top_srcdir)/bindings/go/megasdk/megaapi_wrap.cpp + @sed -i 's,#include "megaapi.h",#include "../../include/megaapi.h",' $(top_srcdir)/$(GOBINDIR)/megaapi_wrap.cpp .PHONY: go-bindings go-bindings: $(GO_SOURCE) $(LIBRARY_PATH) diff --git a/bindings/go/megasdk/package.go b/bindings/go/megasdk/package.go deleted file mode 100644 index c2acf308e0..0000000000 --- a/bindings/go/megasdk/package.go +++ /dev/null @@ -1,4 +0,0 @@ -package mega - -// #cgo LDFLAGS: -L../../../src/.libs -lmega -import "C" diff --git a/bindings/go/package.go b/bindings/go/package.go new file mode 100644 index 0000000000..7a23345632 --- /dev/null +++ b/bindings/go/package.go @@ -0,0 +1,4 @@ +package mega + +// #cgo LDFLAGS: -L../../src/.libs -lmega +import "C" diff --git a/bindings/go/main.go b/examples/go/main.go similarity index 99% rename from bindings/go/main.go rename to examples/go/main.go index f81082aaa3..c4d9136fc2 100644 --- a/bindings/go/main.go +++ b/examples/go/main.go @@ -4,8 +4,9 @@ package main import ( "fmt" - mega "mega/megasdk" "time" + + mega "mega/megasdk" ) type MyMegaListener struct { From 544241076f8d7030c55721ebe8b01d4402f8c93c Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Sun, 1 Oct 2023 02:04:03 +0200 Subject: [PATCH 14/19] Make it more portable, still kinda chunky --- bindings/go/include.am | 2 +- bindings/go/package.go | 2 +- examples/go/go.mod | 7 +++++++ examples/go/main.go | 12 +++++++++--- examples/go/prep.sh | 4 ++++ 5 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 examples/go/go.mod create mode 100644 examples/go/prep.sh diff --git a/bindings/go/include.am b/bindings/go/include.am index eb06965e30..280d414c7a 100644 --- a/bindings/go/include.am +++ b/bindings/go/include.am @@ -26,7 +26,7 @@ bindings_go_lib_mega_la_CPPFLAGS = -I$(top_srcdir)/include $(GO_SOURCE): $(top_srcdir)/bindings/megaapi.i $(SWIG_GO) $(SWIG_GO_FLAGS) -o $@ -outdir $(GOBINDIR) -package $(GOPACKAGE) -I$(top_srcdir)/include $< - @sed -i 's,#include "megaapi.h",#include "../../include/megaapi.h",' $(top_srcdir)/$(GOBINDIR)/megaapi_wrap.cpp + @sed -i 's,#include "megaapi.h",#include "include/megaapi.h",' $(top_srcdir)/$(GOBINDIR)/megaapi_wrap.cpp .PHONY: go-bindings go-bindings: $(GO_SOURCE) $(LIBRARY_PATH) diff --git a/bindings/go/package.go b/bindings/go/package.go index 7a23345632..f05960acdf 100644 --- a/bindings/go/package.go +++ b/bindings/go/package.go @@ -1,4 +1,4 @@ package mega -// #cgo LDFLAGS: -L../../src/.libs -lmega +// #cgo LDFLAGS: -L. -L../ -L./libs/ -L../libs/ -lmega import "C" diff --git a/examples/go/go.mod b/examples/go/go.mod new file mode 100644 index 0000000000..83de742e49 --- /dev/null +++ b/examples/go/go.mod @@ -0,0 +1,7 @@ +module example_project + +go 1.18 + +replace example_project/mega => ./megasdk + +require example_project/mega v0.0.0-00010101000000-000000000000 diff --git a/examples/go/main.go b/examples/go/main.go index c4d9136fc2..879107218c 100644 --- a/examples/go/main.go +++ b/examples/go/main.go @@ -1,12 +1,18 @@ -// LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../../src/.libs go run ./main.go -// You need libmega.so.VERSION package main +// To build: +// ./autogen.sh && ./configure --disable-silent-rules --enable-go --disable-examples && make -j16 +// cd examples/go +// ./prep.sh +// LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./libs go run -x ./main.go + +// TODO: ./configure --enable-static results in sqlite3 issues with libmega.a? + import ( "fmt" "time" - mega "mega/megasdk" + mega "example_project/mega" ) type MyMegaListener struct { diff --git a/examples/go/prep.sh b/examples/go/prep.sh new file mode 100644 index 0000000000..8eb57ee760 --- /dev/null +++ b/examples/go/prep.sh @@ -0,0 +1,4 @@ +#!/bin/bash +ln -s '../../include' '../../bindings/go/include' +ln -s '../../src/.libs' 'libs' +ln -s '../../bindings/go' 'megasdk' From bd308906e8397e50abebfa0299a4ca268d5d3afd Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Sun, 1 Oct 2023 02:23:03 +0200 Subject: [PATCH 15/19] Proper mutex locking in example --- examples/go/main.go | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/examples/go/main.go b/examples/go/main.go index 879107218c..b16a7674c5 100644 --- a/examples/go/main.go +++ b/examples/go/main.go @@ -10,18 +10,24 @@ package main import ( "fmt" - "time" + "sync" mega "example_project/mega" ) type MyMegaListener struct { mega.SwigDirector_MegaListener + notified bool + m sync.Mutex + cv *sync.Cond } func (l *MyMegaListener) OnRequestFinish(api mega.MegaApi, request mega.MegaRequest, e mega.MegaError) { fmt.Printf("Request finished (%v); Result: %v\n", request.ToString(), e.ToString()) + l.m.Lock() + defer l.m.Unlock() + // TODO: Mutex lock this for return values switch request.GetType() { case mega.MegaRequestTYPE_LOGIN: @@ -37,14 +43,36 @@ func (l *MyMegaListener) OnRequestFinish(api mega.MegaApi, request mega.MegaRequ 100*account_details.GetStorageUsed()/account_details.GetStorageMax()) fmt.Printf("Pro level: %v\n", account_details.GetProLevel()) } + + l.notified = true + l.cv.Broadcast() } func (l *MyMegaListener) OnRequestStart(api mega.MegaApi, request mega.MegaRequest) { fmt.Printf("Request start: (%v)\n", request.ToString()) } +func (l *MyMegaListener) Wait() { + // Wait until notified becomes true + l.m.Lock() + defer l.m.Unlock() + + for !l.notified { + l.cv.Wait() + } +} + +func (l *MyMegaListener) Reset() { + l.m.Lock() + defer l.m.Unlock() + + l.notified = false +} + func main() { - listener := mega.NewDirectorMegaListener(&MyMegaListener{}) + myListener := MyMegaListener{} + myListener.cv = sync.NewCond(&myListener.m) + listener := mega.NewDirectorMegaListener(&myListener) fmt.Println("Hello, World!") api := mega.NewMegaApi("ox8xnQZL") @@ -53,11 +81,12 @@ func main() { user, pass := getAuth() api.Login(user, pass) defer api.Logout() - - time.Sleep(5 * time.Second) + myListener.Wait() + myListener.Reset() fmt.Println("Email: " + api.GetMyEmail()) api.GetAccountDetails() - time.Sleep(5 * time.Second) + myListener.Wait() + fmt.Println("Done!") } func getAuth() (username string, password string) { From b287dc37f28786f6a06c3b867ae44645a8e6bb60 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Sun, 1 Oct 2023 02:45:57 +0200 Subject: [PATCH 16/19] Ported over some more c++ locking stuff in golang --- examples/go/main.go | 56 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/examples/go/main.go b/examples/go/main.go index b16a7674c5..ff6798be94 100644 --- a/examples/go/main.go +++ b/examples/go/main.go @@ -11,27 +11,33 @@ package main import ( "fmt" "sync" + "time" mega "example_project/mega" ) -type MyMegaListener struct { - mega.SwigDirector_MegaListener +type MyMegaRequestListener struct { + mega.SwigDirector_MegaRequestListener notified bool + err *mega.MegaError + request *mega.MegaRequest m sync.Mutex cv *sync.Cond } -func (l *MyMegaListener) OnRequestFinish(api mega.MegaApi, request mega.MegaRequest, e mega.MegaError) { +func (l *MyMegaRequestListener) OnRequestFinish(api mega.MegaApi, request mega.MegaRequest, e mega.MegaError) { fmt.Printf("Request finished (%v); Result: %v\n", request.ToString(), e.ToString()) + req := request.Copy() + err := e.Copy() + l.request = &req + l.err = &err + l.m.Lock() defer l.m.Unlock() // TODO: Mutex lock this for return values switch request.GetType() { - case mega.MegaRequestTYPE_LOGIN: - api.FetchNodes() case mega.MegaRequestTYPE_FETCH_NODES: api.GetRootNode() case mega.MegaRequestTYPE_ACCOUNT_DETAILS: @@ -48,11 +54,19 @@ func (l *MyMegaListener) OnRequestFinish(api mega.MegaApi, request mega.MegaRequ l.cv.Broadcast() } -func (l *MyMegaListener) OnRequestStart(api mega.MegaApi, request mega.MegaRequest) { +func (l *MyMegaRequestListener) OnRequestStart(api mega.MegaApi, request mega.MegaRequest) { fmt.Printf("Request start: (%v)\n", request.ToString()) } -func (l *MyMegaListener) Wait() { +func (l *MyMegaRequestListener) GetError() *mega.MegaError { + return l.err +} + +func (l *MyMegaRequestListener) GetRequest() *mega.MegaRequest { + return l.request +} + +func (l *MyMegaRequestListener) Wait() { // Wait until notified becomes true l.m.Lock() defer l.m.Unlock() @@ -62,7 +76,7 @@ func (l *MyMegaListener) Wait() { } } -func (l *MyMegaListener) Reset() { +func (l *MyMegaRequestListener) Reset() { l.m.Lock() defer l.m.Unlock() @@ -70,22 +84,42 @@ func (l *MyMegaListener) Reset() { } func main() { - myListener := MyMegaListener{} + myListener := MyMegaRequestListener{} myListener.cv = sync.NewCond(&myListener.m) - listener := mega.NewDirectorMegaListener(&myListener) + listener := mega.NewDirectorMegaRequestListener(&myListener) fmt.Println("Hello, World!") api := mega.NewMegaApi("ox8xnQZL") - api.AddListener(listener) + api.AddRequestListener(listener) user, pass := getAuth() api.Login(user, pass) defer api.Logout() myListener.Wait() + + if (*myListener.GetError()).GetErrorCode() != mega.MegaErrorAPI_OK { + fmt.Println("Login error") + return + } + myListener.Reset() + api.FetchNodes(listener) + myListener.Wait() + + if (*myListener.GetError()).GetErrorCode() != mega.MegaErrorAPI_OK { + fmt.Println("Error fetchning nodes") + return + } + fmt.Println("Email: " + api.GetMyEmail()) + + myListener.Reset() api.GetAccountDetails() myListener.Wait() + + // Give the terminal a chance to print the stuff it wants + time.Sleep(1 * time.Second) + fmt.Println("Done!") } From f392fa5a42cc08ee43467254f857a0742ec10438 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Sun, 1 Oct 2023 14:31:30 +0200 Subject: [PATCH 17/19] Small ls test program --- examples/go/fs-test/browse.go | 143 ++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 examples/go/fs-test/browse.go diff --git a/examples/go/fs-test/browse.go b/examples/go/fs-test/browse.go new file mode 100644 index 0000000000..6d388cb462 --- /dev/null +++ b/examples/go/fs-test/browse.go @@ -0,0 +1,143 @@ +package main + +// To build: +// ./autogen.sh && ./configure --disable-silent-rules --enable-go --disable-examples && make -j16 +// cd examples/go +// ./prep.sh +// LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./libs go run -x ./main.go + +// TODO: ./configure --enable-static results in sqlite3 issues with libmega.a? + +import ( + "fmt" + "sync" + "time" + + mega "example_project/mega" +) + +type MyMegaListener struct { + mega.SwigDirector_MegaListener + notified bool + err *mega.MegaError + request *mega.MegaRequest + m sync.Mutex + cv *sync.Cond + cwd *mega.MegaNode +} + +func (l *MyMegaListener) OnRequestFinish(api mega.MegaApi, request mega.MegaRequest, e mega.MegaError) { + req := request.Copy() + err := e.Copy() + l.request = &req + l.err = &err + + if err.GetErrorCode() != mega.MegaErrorAPI_OK { + fmt.Printf("INFO: Request finished with error\n") + return + } + + l.m.Lock() + defer l.m.Unlock() + + switch request.GetType() { + case mega.MegaRequestTYPE_LOGIN: + fmt.Printf("Fetching nodes. Please wait...\n") + api.FetchNodes() + case mega.MegaRequestTYPE_FETCH_NODES: + fmt.Printf("Account correctly loaded\n") + node := api.GetRootNode() + l.cwd = &node + } + + l.notified = true + l.cv.Broadcast() +} + +func (l *MyMegaListener) OnNodesUpdate(api mega.MegaApi, nodes mega.MegaNodeList) { + if nodes.Swigcptr() == 0 { + node := api.GetRootNode() + l.cwd = &node + } else { + fmt.Printf("INFO: Nodes updated\n") + } +} + +func (l *MyMegaListener) GetError() *mega.MegaError { + return l.err +} + +func (l *MyMegaListener) GetRequest() *mega.MegaRequest { + return l.request +} + +func (l *MyMegaListener) Wait() { + // Wait until notified becomes true + l.m.Lock() + defer l.m.Unlock() + + for !l.notified { + l.cv.Wait() + } +} + +func (l *MyMegaListener) Reset() { + l.err = nil + l.request = nil + l.notified = false +} + +func main() { + myListener := MyMegaListener{} + myListener.cv = sync.NewCond(&myListener.m) + listener := mega.NewDirectorMegaListener(&myListener) + + fmt.Println("Hello, World!") + api := mega.NewMegaApi("ht1gUZLZ", "", "MEGA/SDK fs test") + api.AddListener(listener) + + user, pass := getAuth() + api.Login(user, pass) + defer api.Logout() + myListener.Wait() + + if (*myListener.GetError()).GetErrorCode() != mega.MegaErrorAPI_OK { + fmt.Println("Login error") + return + } + myListener.Reset() + + for myListener.cwd == nil { + // Give the terminal a chance to print the stuff it wants + time.Sleep(1 * time.Second) + } + + node := api.GetNodeByPath("/") + if node.Swigcptr() == 0 { + fmt.Println("Failed to read node!") + return + } + + children := api.GetChildren(node) + if children.Swigcptr() == 0 { + fmt.Println("Failed to list files!") + return + } + + fmt.Printf("Number of children: %d\n", children.Size()) + for i := 0; i < children.Size(); i++ { + node := children.Get(i) + fmt.Printf("%s -> %t\n", node.GetName(), node.IsFile()) + } + + fmt.Println("Done!") +} + +func getAuth() (username string, password string) { + fmt.Print("Enter your username: ") + fmt.Scan(&username) + + fmt.Print("Enter your password: ") + fmt.Scan(&password) + return +} From 3f5a3c4e0fc89f78795c5dbb431f3b8e61d92458 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Sun, 1 Oct 2023 15:33:12 +0200 Subject: [PATCH 18/19] Better way to run --- examples/go/fs-test/browse.go | 2 +- examples/go/main.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/go/fs-test/browse.go b/examples/go/fs-test/browse.go index 6d388cb462..741be5fb1f 100644 --- a/examples/go/fs-test/browse.go +++ b/examples/go/fs-test/browse.go @@ -127,7 +127,7 @@ func main() { fmt.Printf("Number of children: %d\n", children.Size()) for i := 0; i < children.Size(); i++ { node := children.Get(i) - fmt.Printf("%s -> %t\n", node.GetName(), node.IsFile()) + fmt.Printf("%s -> IsFile: %t\n", node.GetName(), node.IsFile()) } fmt.Println("Done!") diff --git a/examples/go/main.go b/examples/go/main.go index ff6798be94..25781285a8 100644 --- a/examples/go/main.go +++ b/examples/go/main.go @@ -4,7 +4,7 @@ package main // ./autogen.sh && ./configure --disable-silent-rules --enable-go --disable-examples && make -j16 // cd examples/go // ./prep.sh -// LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./libs go run -x ./main.go +// go run -ldflags="-linkmode external -extldflags '-Wl,-rpath=./libs'" ./main.go // TODO: ./configure --enable-static results in sqlite3 issues with libmega.a? @@ -36,7 +36,6 @@ func (l *MyMegaRequestListener) OnRequestFinish(api mega.MegaApi, request mega.M l.m.Lock() defer l.m.Unlock() - // TODO: Mutex lock this for return values switch request.GetType() { case mega.MegaRequestTYPE_FETCH_NODES: api.GetRootNode() From cea6dbd290c1690acadfbb744a7016d0b43497f7 Mon Sep 17 00:00:00 2001 From: NPR <25662877+nielsproest@users.noreply.github.com> Date: Wed, 4 Oct 2023 17:47:41 +0200 Subject: [PATCH 19/19] Fixed onTransferData --- bindings/megaapi.i | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bindings/megaapi.i b/bindings/megaapi.i index 8266dae6b6..69e8278d8f 100644 --- a/bindings/megaapi.i +++ b/bindings/megaapi.i @@ -332,6 +332,12 @@ extern "C" jint JNIEXPORT JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) #endif +#ifdef SWIGGO +%include +%apply (char *STRING, size_t LENGTH) {(char *buffer, size_t size)}; +%apply (char *STRING, size_t LENGTH) {(char *bitmapData, size_t size)}; +#endif + %ignore mega::MegaApi::MEGA_DEBRIS_FOLDER; %ignore mega::MegaNode::getNodeKey; %ignore mega::MegaNode::getAttrString;