| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package main |
| 23 |
|
| 24 |
import ( |
| 25 |
"log" |
| 26 |
"os" |
| 27 |
"os/exec" |
| 28 |
"path/filepath" |
| 29 |
"runtime" |
| 30 |
) |
| 31 |
|
| 32 |
func main() { |
| 33 |
wd, err := os.Getwd() |
| 34 |
if err != nil { |
| 35 |
log.Fatal(err) |
| 36 |
} |
| 37 |
|
| 38 |
switch runtime.GOOS { |
| 39 |
case "linux": |
| 40 |
if err := removeGlob("exe_linux_64*"); err != nil { |
| 41 |
log.Fatal(err) |
| 42 |
} |
| 43 |
|
| 44 |
out, err := exec.Command("cc", "-g", "-ffile-prefix-map="+wd+"="+"/tmp", "-o", "exe_linux_64", "hello.c").CombinedOutput() |
| 45 |
log.Println(string(out)) |
| 46 |
if err != nil { |
| 47 |
log.Fatal(err) |
| 48 |
} |
| 49 |
|
| 50 |
case "darwin": |
| 51 |
if err := removeGlob("exe_mac_64*", "lib_mac_64"); err != nil { |
| 52 |
log.Fatal(err) |
| 53 |
} |
| 54 |
|
| 55 |
out, err := exec.Command("clang", "-g", "-ffile-prefix-map="+wd+"="+"/tmp", "-o", "exe_mac_64", "hello.c").CombinedOutput() |
| 56 |
log.Println(string(out)) |
| 57 |
if err != nil { |
| 58 |
log.Fatal(err) |
| 59 |
} |
| 60 |
|
| 61 |
out, err = exec.Command("clang", "-g", "-ffile-prefix-map="+wd+"="+"/tmp", "-o", "lib_mac_64", "-dynamiclib", "lib.c").CombinedOutput() |
| 62 |
log.Println(string(out)) |
| 63 |
if err != nil { |
| 64 |
log.Fatal(err) |
| 65 |
} |
| 66 |
|
| 67 |
case "windows": |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
out, err := exec.Command("gcc", "-g", "-ffile-prefix-map="+wd+"=", "-o", "exe_windows_64.exe", "hello.c").CombinedOutput() |
| 73 |
log.Println(string(out)) |
| 74 |
if err != nil { |
| 75 |
log.Fatal(err) |
| 76 |
} |
| 77 |
log.Println("Please verify that exe_windows_64.exe does not trigger any antivirus on `virustotal.com`.") |
| 78 |
default: |
| 79 |
log.Fatalf("Unsupported OS %q", runtime.GOOS) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func removeGlob(globs ...string) error { |
| 84 |
for _, glob := range globs { |
| 85 |
matches, err := filepath.Glob(glob) |
| 86 |
if err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
for _, p := range matches { |
| 90 |
os.Remove(p) |
| 91 |
} |
| 92 |
} |
| 93 |
return nil |
| 94 |
} |