How to get system and architecture?

I’m wondering if there’s a straightforward way to get runtime system and architecture in Crystal?
Like runtime.GOOS and runtime.GOARCH in Go.
Maybe like but at runtime:

os = arch = ""
{% if flag?(:linux) %}
  os = "linux"
{% elsif flag?(:windows) %}
  os = "windows"
{% elsif flag?(:darwin) %}
  os = "darwin"
{% elsif flag?(:freebsd) %}
  os = "freebsd"
  ...
{% else %}
  os = "unknown"
{% end %}

{% if flag?(:x86_64) %}
  arch = "amd64"
{% elsif flag?(:aarch64) %}
  arch = "arm64"
  ...
{% else %}
  arch = "unknown"
{% end %}
puts "os: #{os}, arch: #{arch}"
1 Like

The constant Crystal::TARGET_TRIPLE provides the target triple which the program is built for.

(Unfortuntately, the constant does not show up in the API docs, see Fix generate docs for builtins `HOST_TRIPLE` and `TARGET_TRIPLE` by straight-shoota · Pull Request #14570 · crystal-lang/crystal · GitHub)

Note that the compile-time target triple is not necessarily an accurate representation of the runtime system due to cross-execution capabilities. I.e. an exectuable could be built for one target but can still be able to run on different ones.

2 Likes