-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.php
executable file
·66 lines (53 loc) · 1.77 KB
/
build.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env php
<?php
chdir("go");
$goFiles = glob('*.go');
if (empty($goFiles)) {
echo "No Go files found in the 'go' directory.\n";
exit(1);
}
// TODO: Maybe build this using GitHub actions?
// Right now Apple Silicon must build Apple Silicon
// and Intel must build Intel.
$targets = [
// Apple Silicon macOS
'darwin-arm64' => [
'arch' => "CGO_ENABLED=1 GOOS=darwin GOARCH=arm64",
'static' => "-ldflags '-w -s'",
],
// Intel macOS
'darwin-amd64' => [
//'arch' => "GOOS=darwin GOARCH=amd64",
//'static' => "-tags netgo -ldflags '-w -extldflags \"-static\"'",
],
// Linux
'linux-amd64' => [
'arch' => "CC=x86_64-linux-musl-gcc CGO_ENABLED=1 GOOS=linux GOARCH=amd64",
'static' => "-tags netgo -ldflags '-w -extldflags \"-static\"'",
],
];
// Get target provided in command line:
$options = getopt("", ["target:"]);
foreach ($targets as $target => $flags) {
if (isset($options['target']) && $options['target'] != $target) {
continue;
}
foreach ($goFiles as $file) {
$output = [];
$returnCode = 0;
echo "Building $file for $target...\n";
// Build the Go file with cross-compilation
$archFlags = $flags['arch'] ?? "";
$staticFlags = $flags['static'] ?? "";
$buildCommand = "$archFlags go build $staticFlags -o ../bin/$target/" . basename($file, '.go') . " $file";
echo " % $buildCommand\n";
exec($buildCommand, $output, $returnCode);
if ($returnCode !== 0) {
echo "Error building $file:\n";
echo implode("\n", $output) . "\n";
exit(1);
}
echo "Built $file for $target successfully.\n";
}
}
echo "Go command-line tool build completed.\n";