cleanup(platform): we don't need CGO anymore (#355)

* cleanup(platform): we don't need CGO anymore

Since go1.16, we have the `ios` port. So we can easily
disambiguate between ios and darwin.

This means we don't need to rely on CGO to correctly guess
whether we are on ios or darwin anymore.

So, now miniooni does not depend on a C compiler even
when you are not cross compiling.

* Update internal/platform/platform.go
This commit is contained in:
Simone Basso 2021-06-04 11:46:06 +02:00 committed by GitHub
parent acd4ffff35
commit 4764d7f378
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 77 deletions

View File

@ -24,28 +24,20 @@ import "runtime"
// has been disabled. In such case, the code will return "ios" when // has been disabled. In such case, the code will return "ios" when
// using arm{,64} and "macos" when using x86{,_64}. // using arm{,64} and "macos" when using x86{,_64}.
func Name() string { func Name() string {
if name := cgoname(); name != "unknown" { return name(runtime.GOOS)
return name
}
return puregoname(runtime.GOOS, runtime.GOARCH)
} }
func puregoname(goos, goarch string) string { // name is a utility function for implementing Name.
func name(goos string) string {
// Note: since go1.16 we have the ios port, so the ambiguity
// between ios and darwin is now gone.
//
// See https://golang.org/doc/go1.16#darwin
switch goos { switch goos {
case "android", "linux", "windows": case "android", "linux", "windows", "ios":
return goos return goos
case "darwin": case "darwin":
return detectDarwin(goarch)
}
return "unknown"
}
func detectDarwin(goarch string) string {
switch goarch {
case "386", "amd64":
return "macos" return "macos"
case "arm", "arm64":
return "ios"
} }
return "unknown" return "unknown"
} }

View File

@ -1,31 +0,0 @@
// +build cgo
package platform
//
// /* Guess the platform in which we are.
//
// See: <https://sourceforge.net/p/predef/wiki/OperatingSystems/>
// <http://stackoverflow.com/a/18729350> */
//
//#if defined __ANDROID__
//# define OONI_PLATFORM "android"
//#elif defined __linux__
//# define OONI_PLATFORM "linux"
//#elif defined _WIN32
//# define OONI_PLATFORM "windows"
//#elif defined __APPLE__
//# include <TargetConditionals.h>
//# if TARGET_OS_IPHONE
//# define OONI_PLATFORM "ios"
//# else
//# define OONI_PLATFORM "macos"
//# endif
//#else
//# define OONI_PLATFORM "unknown"
//#endif
import "C"
func cgoname() string {
return C.OONI_PLATFORM
}

View File

@ -1,7 +0,0 @@
// +build !cgo
package platform
func cgoname() string {
return "unknown"
}

View File

@ -16,51 +16,32 @@ func TestGood(t *testing.T) {
} }
} }
func TestPuregoname(t *testing.T) { func TestName(t *testing.T) {
var runtimevariables = []struct { var runtimevariables = []struct {
expected string expected string
goarch string
goos string goos string
}{{ }{{
expected: "android", expected: "android",
goarch: "*",
goos: "android", goos: "android",
}, { }, {
expected: "ios", expected: "ios",
goarch: "arm64", goos: "ios",
goos: "darwin",
}, {
expected: "ios",
goarch: "arm",
goos: "darwin",
}, { }, {
expected: "linux", expected: "linux",
goarch: "*",
goos: "linux", goos: "linux",
}, { }, {
expected: "macos", expected: "macos",
goarch: "amd64",
goos: "darwin",
}, {
expected: "macos",
goarch: "386",
goos: "darwin", goos: "darwin",
}, { }, {
expected: "unknown", expected: "unknown",
goarch: "*",
goos: "solaris", goos: "solaris",
}, {
expected: "unknown",
goarch: "mips",
goos: "darwin",
}, { }, {
expected: "windows", expected: "windows",
goarch: "*",
goos: "windows", goos: "windows",
}} }}
for _, v := range runtimevariables { for _, v := range runtimevariables {
t.Run(fmt.Sprintf("with %s/%s", v.goos, v.goarch), func(t *testing.T) { t.Run(fmt.Sprintf("with %s", v.goos), func(t *testing.T) {
if puregoname(v.goos, v.goarch) != v.expected { if name(v.goos) != v.expected {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })