]> Cypherpunks repositories - gostls13.git/commitdiff
misc/cgo/testcarchive: split main.c into unix and non-unix
authorChristopher Nelson <nadiasvertex@gmail.com>
Thu, 24 Mar 2016 21:50:21 +0000 (17:50 -0400)
committerIan Lance Taylor <iant@golang.org>
Sat, 26 Mar 2016 00:57:23 +0000 (00:57 +0000)
This is in support of https://golang.org/cl/18057 which adds
support for c-archive to the Windows platform.

The signal handling tests do not compile on Windows. This splits
them out into a separate main_unix.c file, and conditionally
includes them for non-Windows platforms.

Change-Id: Ic79ce83da7656d6703505e514554748a482b81a1
Reviewed-on: https://go-review.googlesource.com/21086
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

misc/cgo/testcarchive/carchive_test.go
misc/cgo/testcarchive/main.c
misc/cgo/testcarchive/main_unix.c [new file with mode: 0644]
misc/cgo/testcarchive/main_windows.c [new file with mode: 0644]

index 6771ad3be14b2baff72ae01158f8aab987b6e899..2c81a6acd2030c08573f117b17d1d34c7655cedd 100644 (file)
@@ -116,6 +116,22 @@ func goEnv(key string) string {
        return strings.TrimSpace(string(out))
 }
 
+func compilemain(t *testing.T, libgo string) {
+       ccArgs := append(cc, "-o", "testp"+exeSuffix)
+       if GOOS == "windows" {
+               ccArgs = append(ccArgs, "main_windows.c")
+       } else {
+               ccArgs = append(ccArgs, "main_unix.c")
+       }
+       ccArgs = append(ccArgs, "main.c", libgo)
+       t.Log(ccArgs)
+
+       if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
+               t.Logf("%s", out)
+               t.Fatal(err)
+       }
+}
+
 func TestInstall(t *testing.T) {
        defer func() {
                os.Remove("libgo.a")
@@ -131,11 +147,7 @@ func TestInstall(t *testing.T) {
                t.Fatal(err)
        }
 
-       ccArgs := append(cc, "-o", "testp"+exeSuffix, "main.c", filepath.Join("pkg", GOOS+"_"+GOARCH, "libgo.a"))
-       if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
-               t.Logf("%s", out)
-               t.Fatal(err)
-       }
+       compilemain(t, filepath.Join("pkg", GOOS+"_"+GOARCH, "libgo.a"))
 
        binArgs := append(bin, "arg1", "arg2")
        if out, err := exec.Command(binArgs[0], binArgs[1:]...).CombinedOutput(); err != nil {
@@ -156,11 +168,7 @@ func TestInstall(t *testing.T) {
                t.Fatal(err)
        }
 
-       ccArgs = append(cc, "-o", "testp"+exeSuffix, "main.c", "libgo.a")
-       if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
-               t.Logf("%s", out)
-               t.Fatal(err)
-       }
+       compilemain(t, "libgo.a")
 
        if out, err := exec.Command(binArgs[0], binArgs[1:]...).CombinedOutput(); err != nil {
                t.Logf("%s", out)
@@ -178,10 +186,7 @@ func TestInstall(t *testing.T) {
                t.Fatal(err)
        }
 
-       if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
-               t.Logf("%s", out)
-               t.Fatal(err)
-       }
+       compilemain(t, "libgo.a")
 
        if out, err := exec.Command(binArgs[0], binArgs[1:]...).CombinedOutput(); err != nil {
                t.Logf("%s", out)
index a90138f898a0f7eefe76727804a31a98a512dd2c..163b5398e5ecb4bb5d024ee55bfa68df7360b3ac 100644 (file)
@@ -2,7 +2,6 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-#include <signal.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
 #include "p.h"
 #include "libgo.h"
 
-static void (*oldHandler)(int, siginfo_t*, void*);
-
-static void handler(int signo, siginfo_t* info, void* ctxt) {
-       if (oldHandler) {
-               oldHandler(signo, info, ctxt);
-       }
-}
+extern int install_handler();
+extern int check_handler();
 
 int main(void) {
-       struct sigaction sa;
-       struct sigaction osa;
        int32_t res;
 
-       // Install our own signal handler.
-       memset(&sa, 0, sizeof sa);
-       sa.sa_sigaction = handler;
-       sigemptyset(&sa.sa_mask);
-       sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
-       memset(&osa, 0, sizeof osa);
-       sigemptyset(&osa.sa_mask);
-       if (sigaction(SIGSEGV, &sa, &osa) < 0) {
-               perror("sigaction");
-               return 2;
+       int r1 = install_handler();
+       if (r1!=0) {
+               return r1;
        }
-       if (osa.sa_handler == SIG_DFL || (osa.sa_flags&SA_ONSTACK) == 0) {
-               fprintf(stderr, "Go runtime did not install signal handler\n");
-               return 2;
-       }
-       oldHandler = osa.sa_sigaction;
 
        if (!DidInitRun()) {
                fprintf(stderr, "ERROR: buildmode=c-archive init should run\n");
@@ -50,14 +30,9 @@ int main(void) {
                return 2;
        }
 
-       // Make sure our signal handler is still the one in use.
-       if (sigaction(SIGSEGV, NULL, &sa) < 0) {
-               perror("sigaction check");
-               return 2;
-       }
-       if (sa.sa_sigaction != handler) {
-               fprintf(stderr, "ERROR: wrong signal handler: %p != %p\n", sa.sa_sigaction, handler);
-               return 2;
+       int r2 = check_handler();
+       if (r2!=0) {
+               return r2;
        }
 
        res = FromPkg();
diff --git a/misc/cgo/testcarchive/main_unix.c b/misc/cgo/testcarchive/main_unix.c
new file mode 100644 (file)
index 0000000..4d9d16f
--- /dev/null
@@ -0,0 +1,53 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+struct sigaction sa;
+struct sigaction osa;
+
+static void (*oldHandler)(int, siginfo_t*, void*);
+
+static void handler(int signo, siginfo_t* info, void* ctxt) {
+       if (oldHandler) {
+               oldHandler(signo, info, ctxt);
+       }
+}
+
+int install_handler() {
+       // Install our own signal handler.
+       memset(&sa, 0, sizeof sa);
+       sa.sa_sigaction = handler;
+       sigemptyset(&sa.sa_mask);
+       sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
+       memset(&osa, 0, sizeof osa);
+       sigemptyset(&osa.sa_mask);
+       if (sigaction(SIGSEGV, &sa, &osa) < 0) {
+               perror("sigaction");
+               return 2;
+       }
+       if (osa.sa_handler == SIG_DFL || (osa.sa_flags&SA_ONSTACK) == 0) {
+               fprintf(stderr, "Go runtime did not install signal handler\n");
+               return 2;
+       }
+       oldHandler = osa.sa_sigaction;
+
+       return 0;
+}
+
+int check_handler() {
+       if (sigaction(SIGSEGV, NULL, &sa) < 0) {
+               perror("sigaction check");
+               return 2;
+       }
+       if (sa.sa_sigaction != handler) {
+               fprintf(stderr, "ERROR: wrong signal handler: %p != %p\n", sa.sa_sigaction, handler);
+               return 2;
+       }
+       return 0;
+}
+
diff --git a/misc/cgo/testcarchive/main_windows.c b/misc/cgo/testcarchive/main_windows.c
new file mode 100644 (file)
index 0000000..eded8af
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+/*
+ * Dummy implementations for Windows, because Windows doesn't
+ * support Unix-style signal handling.
+ */
+
+int install_handler() {
+       return 0;
+}
+
+
+int check_handler() {
+       return 0;
+}