package cgosotest
/*
-#cgo LDFLAGS: -L. -lcgosotest
+// intentionally write the same LDFLAGS differently
+// to test correct handling of LDFLAGS.
+#cgo linux LDFLAGS: -L. -lcgosotest
+#cgo freebsd LDFLAGS: -L. -l cgosotest
+#cgo openbsd LDFLAGS: -L. -l cgosotest
+#cgo netbsd LDFLAGS: -L. libcgosotest.so
+#cgo darwin LDFLAGS: -L. libcgosotest.dylib
+#cgo windows LDFLAGS: -L. libcgosotest.dll
+
void init(void);
void sofunc(void);
*/
# license that can be found in the LICENSE file.
set -e
-$(go env CC) $(go env GOGCCFLAGS) -shared -o libcgosotest.so cgoso_c.c
+
+args=
+dyld_envvar=LD_LIBRARY_PATH
+ext=so
+if [ "$(uname)" == "Darwin" ]; then
+ args="-undefined suppress -flat_namespace"
+ dyld_envvar=DYLD_LIBRARY_PATH
+ ext=dylib
+fi
+
+dylib=libcgosotest.$ext
+$(go env CC) $(go env GOGCCFLAGS) -shared $args -o $dylib cgoso_c.c
go build main.go
-LD_LIBRARY_PATH=. ./main
-rm -f libcgosotest.so main
+
+eval "$dyld_envvar"=. ./main
+rm -rf $dylib main *.dSYM
var linkobj []string
var bareLDFLAGS []string
- // filter out -lsomelib, and -framework X if on Darwin
+ // filter out -lsomelib, -l somelib, *.{so,dll,dylib}, and (on Darwin) -framework X
for i := 0; i < len(cgoLDFLAGS); i++ {
f := cgoLDFLAGS[i]
- if !strings.HasPrefix(f, "-l") {
- if goos == "darwin" && f == "-framework" { // skip the -framework X
- i += 1
- continue
+ switch {
+ // skip "-lc" or "-l somelib"
+ case strings.HasPrefix(f, "-l"):
+ if f == "-l" {
+ i++
}
+ // skip "-framework X" on Darwin
+ case goos == "darwin" && f == "-framework":
+ i++
+ // skip "*.{dylib,so,dll}"
+ case strings.HasSuffix(f, ".dylib"),
+ strings.HasSuffix(f, ".so"),
+ strings.HasSuffix(f, ".dll"):
+ continue
+ default:
bareLDFLAGS = append(bareLDFLAGS, f)
}
}
[ "$CGO_ENABLED" != 1 ] ||
[ "$GOHOSTOS" == windows ] ||
-[ "$GOHOSTOS" == darwin ] ||
(xcd ../misc/cgo/testso
./test.bash
) || exit $?