Skip to content

Commit e49e93f

Browse files
aykevldeadprogram
authored andcommitted
main: calculate default output path if -o is not specified
This matches the Go command and is generally convenient to have.
1 parent 7d4bf09 commit e49e93f

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

compileopts/config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,29 @@ func (c *Config) LibcPath(name string) (path string, precompiled bool) {
245245
return filepath.Join(goenv.Get("GOCACHE"), name+"-"+archname), false
246246
}
247247

248+
// DefaultBinaryExtension returns the default extension for binaries, such as
249+
// .exe, .wasm, or no extension (depending on the target).
250+
func (c *Config) DefaultBinaryExtension() string {
251+
parts := strings.Split(c.Triple(), "-")
252+
if parts[0] == "wasm32" {
253+
// WebAssembly files always have the .wasm file extension.
254+
return ".wasm"
255+
}
256+
if len(parts) >= 3 && parts[2] == "windows" {
257+
// Windows uses .exe.
258+
return ".exe"
259+
}
260+
if len(parts) >= 3 && parts[2] == "unknown" {
261+
// There appears to be a convention to use the .elf file extension for
262+
// ELF files intended for microcontrollers. I'm not aware of the origin
263+
// of this, it's just something that is used by many projects.
264+
// I think it's a good tradition, so let's keep it.
265+
return ".elf"
266+
}
267+
// Linux, MacOS, etc, don't use a file extension. Use it as a fallback.
268+
return ""
269+
}
270+
248271
// CFlags returns the flags to pass to the C compiler. This is necessary for CGo
249272
// preprocessing.
250273
func (c *Config) CFlags() []string {

main.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ func Build(pkgName, outpath string, options *compileopts.Options) error {
149149
}
150150

151151
return builder.Build(pkgName, outpath, config, func(result builder.BuildResult) error {
152+
if outpath == "" {
153+
if strings.HasSuffix(pkgName, ".go") {
154+
// A Go file was specified directly on the command line.
155+
// Base the binary name off of it.
156+
outpath = filepath.Base(pkgName[:len(pkgName)-3]) + config.DefaultBinaryExtension()
157+
} else {
158+
// Pick a default output path based on the main directory.
159+
outpath = filepath.Base(result.MainDir) + config.DefaultBinaryExtension()
160+
}
161+
}
162+
152163
if err := os.Rename(result.Binary, outpath); err != nil {
153164
// Moving failed. Do a file copy.
154165
inf, err := os.Open(result.Binary)
@@ -1319,11 +1330,6 @@ func main() {
13191330

13201331
switch command {
13211332
case "build":
1322-
if outpath == "" {
1323-
fmt.Fprintln(os.Stderr, "No output filename supplied (-o).")
1324-
usage(command)
1325-
os.Exit(1)
1326-
}
13271333
pkgName := "."
13281334
if flag.NArg() == 1 {
13291335
pkgName = filepath.ToSlash(flag.Arg(0))

0 commit comments

Comments
 (0)