diff --git a/internal/binding/templater/template_cgo_qmake.go b/internal/binding/templater/template_cgo_qmake.go index d76b63cc..14e09fb3 100644 --- a/internal/binding/templater/template_cgo_qmake.go +++ b/internal/binding/templater/template_cgo_qmake.go @@ -662,7 +662,24 @@ func createCgo(module, path, target string, mode int, ipkg, tags string) string tmp = strings.Replace(tmp, "$(EXPORT_QMAKE_XARCH_CFLAGS)", "", -1) tmp = strings.Replace(tmp, "$(EXPORT_QMAKE_XARCH_LFLAGS)", "", -1) case "android", "android-emulator": //TODO: - tmp = strings.Replace(tmp, fmt.Sprintf("-Wl,-soname,lib%v.so", filepath.Base(path)), "-Wl,-soname,libgo_base.so", -1) + libName := filepath.Base(path) + if utils.QT_VERSION_NUM() >= 5140 { + libName += "_" + // Calculate arch suffix + if target == "android-emulator" { + // Android emulator is x86 + libName += "x86" + } else if utils.GOARCH() == "arm64" { + // AARCH64 on android + libName += "arm64-v8a" + } else { + // ARMv7 on android + libName += "armeabi-v7a" + } + } + utils.Log.WithField("module", module).WithField("path", path).WithField("target", target).WithField("mode", mode).WithField("pkg", ipkg).Debugf("cgoTemplate libName: %s", libName) + + tmp = strings.Replace(tmp, fmt.Sprintf("-Wl,-soname,lib%v.so", libName), "-Wl,-soname,libgo_base.so", -1) tmp = strings.Replace(tmp, "-shared", "", -1) case "js", "wasm": tmp = strings.Replace(tmp, "\"", "", -1) diff --git a/internal/cmd/deploy/build.go b/internal/cmd/deploy/build.go index 12e52090..67f1b1d3 100644 --- a/internal/cmd/deploy/build.go +++ b/internal/cmd/deploy/build.go @@ -33,6 +33,11 @@ func build(mode, target, path, ldFlagsCustom, tagsCustom, name, depPath string, switch target { case "android", "android-emulator", "ios", "ios-simulator": utils.Save(filepath.Join(path, "cgo_main_wrapper.go"), "package main\nimport (\n\"C\"\n\"os\"\n\"unsafe\"\n)\n//export go_main_wrapper\nfunc go_main_wrapper(argc C.int, argv unsafe.Pointer) {\nos.Args=make([]string,int(argc))\nfor i,b := range (*[1<<3]*C.char)(argv)[:int(argc):int(argc)] {\nos.Args[i] = C.GoString(b)\n}\nmain()\n}") + + // Quick fix of problem in library soname experienced with Qt 5.14+ and android + if utils.QT_VERSION_NUM() >= 5140 && strings.HasPrefix(target, "android") { + ldFlags = utils.AppendToFlag(ldFlags, "-extldflags", "-Wl,-soname,libgo_base.so") + } case "windows": ending = ".exe" case "sailfish", "sailfish-emulator": @@ -59,7 +64,8 @@ func build(mode, target, path, ldFlagsCustom, tagsCustom, name, depPath string, } if utils.Log.Level == logrus.DebugLevel && target != "wasm" { - ldFlags = append(ldFlags, "-extldflags=-v") + // ldFlags = append(ldFlags, "-extldflags=-v -Wl,-soname,libgo_base.so") + ldFlags = utils.AppendToFlag(ldFlags, "-extldflags", "-v") } cmd := exec.Command("go", "build", "-p", strconv.Itoa(runtime.GOMAXPROCS(0)), "-v") diff --git a/internal/utils/flags.go b/internal/utils/flags.go new file mode 100644 index 00000000..d559929f --- /dev/null +++ b/internal/utils/flags.go @@ -0,0 +1,52 @@ +package utils + +import ( + "fmt" + "strings" +) + +// AppendToFlag is a quick function that appends content to a flag if is already defined +func AppendToFlag(flags []string, flag, content string) []string { + // no content to append to flag + if content == "" { + return flags + } + + var ( + extractedFlag string + match, simpleFlag bool + ) + for index, flagEntry := range flags { + // checks if is simple flag or has an assignation + if asigIndex := strings.IndexRune(flagEntry, '='); asigIndex != -1 { + // extracts flag + extractedFlag = flagEntry[:asigIndex] + simpleFlag = false + } else { + extractedFlag = flagEntry + simpleFlag = true + } + + // if flags are equal appends content to existing assignation + if extractedFlag == flag { + if !simpleFlag { + flagEntry += " " + content + } else { + flagEntry = fmt.Sprintf("%s=%s", flag, content) + } + flags[index] = flagEntry + match = true + break + } + } + // Flag not found, so appends new flag to list + if !match { + flags = append(flags, fmt.Sprintf("%s=%s", flag, content)) + } + + return flags +} + +// TODO func SetFlag + +// TODO MergeFlags diff --git a/internal/utils/flags_test.go b/internal/utils/flags_test.go new file mode 100644 index 00000000..7c5f52d8 --- /dev/null +++ b/internal/utils/flags_test.go @@ -0,0 +1,21 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestAppendToFlag(t *testing.T) { + + flags := []string{"-p", "-pkgdir=/usr/lib/testdir", "-mod", "-modfile go.mod"} + + flags = AppendToFlag(flags, "-extldflags", "-v") + require.Equal(t, "-extldflags=-v", flags[4], "Flag was not inserted") + + flags = AppendToFlag(flags, "-extldflags", "-Wl,soname,libname.so") + require.Equal(t, "-extldflags=-v -Wl,soname,libname.so", flags[4], "Flag was not appended with new content") + + flags = AppendToFlag(flags, "-p", "test") + require.Equal(t, "-p=test", flags[0], "Content was not appended to simple flag") +}