From: Christopher Nelson Date: Thu, 24 Mar 2016 21:50:21 +0000 (-0400) Subject: misc/cgo/testcarchive: split main.c into unix and non-unix X-Git-Tag: go1.7beta1~1083 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=93c5fbf3905920a8d0cb0618816165f368e9d965;p=gostls13.git misc/cgo/testcarchive: split main.c into unix and non-unix 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 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- diff --git a/misc/cgo/testcarchive/carchive_test.go b/misc/cgo/testcarchive/carchive_test.go index 6771ad3be1..2c81a6acd2 100644 --- a/misc/cgo/testcarchive/carchive_test.go +++ b/misc/cgo/testcarchive/carchive_test.go @@ -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) diff --git a/misc/cgo/testcarchive/main.c b/misc/cgo/testcarchive/main.c index a90138f898..163b5398e5 100644 --- a/misc/cgo/testcarchive/main.c +++ b/misc/cgo/testcarchive/main.c @@ -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 #include #include #include @@ -10,35 +9,16 @@ #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 index 0000000000..4d9d16f03b --- /dev/null +++ b/misc/cgo/testcarchive/main_unix.c @@ -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 +#include +#include +#include + +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 index 0000000000..eded8af1a2 --- /dev/null +++ b/misc/cgo/testcarchive/main_windows.c @@ -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; +}