-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·215 lines (194 loc) · 6.87 KB
/
Copy pathbuild.py
File metadata and controls
executable file
·215 lines (194 loc) · 6.87 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
"""
Universal Go cross-compilation script.
Builds a Go project for multiple operating systems and architectures.
All settings are configurable via command-line arguments.
"""
import argparse
import subprocess
import sys
import os
import shutil
from pathlib import Path
import re
# ----------------------------------------------------------------------
# Default values
DEFAULT_OUTPUT_DIR = "./build"
DEFAULT_NAME_TEMPLATE = "{project}-{os}-{arch}-{version}"
DEFAULT_PLATFORMS = [
"windows/amd64",
"windows/386",
"linux/amd64",
"linux/386",
"linux/arm",
"linux/arm64",
"darwin/amd64",
"darwin/arm64",
"freebsd/amd64",
"openbsd/amd64",
]
DEFAULT_VERSION = "final"
# ----------------------------------------------------------------------
def parse_platforms(platforms_str):
"""Parse a comma-separated list of 'os/arch' into a list of tuples."""
if not platforms_str:
return []
items = [p.strip() for p in platforms_str.split(",") if p.strip()]
result = []
for item in items:
parts = item.split("/")
if len(parts) != 2:
raise ValueError(f"Invalid platform format: '{item}'. Expected 'os/arch'.")
result.append((parts[0], parts[1]))
return result
def get_project_name(project_path):
"""Read the module name from go.mod to use as project name."""
mod_file = Path(project_path) / "go.mod"
if mod_file.exists():
with open(mod_file, "r", encoding="utf-8") as f:
for line in f:
if line.startswith("module "):
# module name can be a path like github.com/user/project
# we take the last component as the project name
mod_name = line[len("module "):].strip()
# remove possible trailing comments
mod_name = mod_name.split()[0]
# return last part after slash, or the whole if no slash
return mod_name.split("/")[-1]
# fallback to directory name
return Path(project_path).name
def build_for_platform(project_path, output_dir, goos, goarch, name_template, version, ldflags, verbose):
"""Build the Go project for a single platform."""
# Determine output file name
# We replace placeholders in the template
# Possible placeholders: {project}, {os}, {arch}, {version}
# Also add .exe for windows if not already present in template
project_name = get_project_name(project_path)
name = name_template.format(
project=project_name,
os=goos,
arch=goarch,
version=version,
)
# Ensure .exe extension for windows if the template didn't already include it
if goos == "windows" and not name.endswith(".exe"):
name += ".exe"
output_file = Path(output_dir) / name
output_file.parent.mkdir(parents=True, exist_ok=True)
# Build command
cmd = ["go", "build"]
if ldflags:
cmd.extend(["-ldflags", ldflags])
cmd.extend(["-o", str(output_file)])
cmd.append(".")
env = os.environ.copy()
env["GOOS"] = goos
env["GOARCH"] = goarch
if verbose:
print(f"\nBuilding for {goos}/{goarch} -> {output_file}")
print(f" Command: {' '.join(cmd)}")
print(f" GOOS={goos} GOARCH={goarch}")
try:
proc = subprocess.run(
cmd,
cwd=project_path,
env=env,
capture_output=True,
text=True,
check=False,
)
if proc.returncode != 0:
print(f"ERROR: Build failed for {goos}/{goarch}")
print(proc.stderr)
return False
if verbose:
print(f" Build successful.")
return True
except Exception as e:
print(f"Exception while building for {goos}/{goarch}: {e}")
return False
# ----------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
description="Universal Go cross-compilation script",
epilog="Example: %(prog)s -p ./myapp -o ./dist -n 'myapp-{os}-{arch}' -v 1.0.0 -pl windows/amd64,linux/amd64"
)
parser.add_argument(
"-p", "--project-path",
default=".",
help="Path to the Go project (directory containing go.mod). Default: current directory."
)
parser.add_argument(
"-o", "--output-dir",
default=DEFAULT_OUTPUT_DIR,
help=f"Output directory for binaries. Default: {DEFAULT_OUTPUT_DIR}"
)
parser.add_argument(
"-n", "--name-template",
default=DEFAULT_NAME_TEMPLATE,
help=f"Name template for output files. Placeholders: {{project}}, {{os}}, {{arch}}, {{version}}. Default: '{DEFAULT_NAME_TEMPLATE}'"
)
parser.add_argument(
"-pl", "--platforms",
default=",".join(DEFAULT_PLATFORMS),
help=f"Comma-separated list of platforms (os/arch). Default: {','.join(DEFAULT_PLATFORMS)}"
)
parser.add_argument(
"-v", "--version",
default=DEFAULT_VERSION,
help=f"Version string to substitute in name template. Default: {DEFAULT_VERSION}"
)
parser.add_argument(
"-ld", "--ldflags",
default="",
help="Additional ldflags to pass to go build (e.g. '-X main.version=1.0')"
)
parser.add_argument(
"--verbose",
action="store_true",
help="Print detailed build information."
)
args = parser.parse_args()
# Resolve paths
project_path = Path(args.project_path).resolve()
if not project_path.exists():
print(f"Error: Project path '{project_path}' does not exist.")
sys.exit(1)
output_dir = Path(args.output_dir).resolve()
# Ensure output dir exists (will be created per platform)
# Parse platforms
try:
platforms = parse_platforms(args.platforms)
except ValueError as e:
print(f"Error parsing platforms: {e}")
sys.exit(1)
if not platforms:
print("No platforms specified. Nothing to build.")
sys.exit(0)
if args.verbose:
print(f"Project path: {project_path}")
print(f"Output directory: {output_dir}")
print(f"Name template: {args.name_template}")
print(f"Version: {args.version}")
print(f"Platforms: {platforms}")
print(f"Ldflags: {args.ldflags if args.ldflags else '(none)'}")
# Build for each platform
success_count = 0
for goos, goarch in platforms:
ok = build_for_platform(
project_path=project_path,
output_dir=output_dir,
goos=goos,
goarch=goarch,
name_template=args.name_template,
version=args.version,
ldflags=args.ldflags,
verbose=args.verbose,
)
if ok:
success_count += 1
print(f"\nBuild complete: {success_count}/{len(platforms)} succeeded.")
if success_count < len(platforms):
sys.exit(1)
if __name__ == "__main__":
main()