Description
The output path option of crystal build
, --output
/-o
informs the compiler where to put the final executable.
By default, the compiler derives the name from the first source file and puts it in the current directory. It's very common that the file name of the executable is intended to be identical to the source file.
If you want to put the executable in a different folder, the output path option requires to specify the path name, which is often duplicate of the source file.
It would be nice if the output path option would also accept a directory path. The compiler would still derive the name from the first source file and put it in the output directory. The derivation includes potential path suffixes like .exe
on Windows, which makes the compiler command portable. Currently it's necessary to specify different output file paths depending on operating system.
A directory path would be indicated by a trailing path separator.
As a result the following commands would be semantically equivalent:
# unix
crystal build -o .build/foobar foobar.cr
# windows
crystal build -o .build/foobar.exe foobar.cr
# portable
crystal build -o .build/ foobar.cr
Both produce an output file in .build/foobar
(unix) or .build/foobar.exe
(windows).
There is an edge case when the output path does not end in a path separator but is an existing directory:
mkdir .build
crystal build -o .build foobar.cr
This could be an error. But I think it's okay to just treat the path as a directory.
Prior art: go build
: https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies
The
-o
flag forcesbuild
to write the resulting executable or object to the named output file or directory [..]. If the named output is an existing directory or ends with a slash or backslash, then any resulting executables will be written to that directory.
Activity