Skip to content

Instantly share code, notes, and snippets.

@nat-418
Last active November 25, 2021 23:51
Show Gist options
  • Save nat-418/6cf1283cc71e841f91ea0c4cedd4b397 to your computer and use it in GitHub Desktop.
Save nat-418/6cf1283cc71e841f91ea0c4cedd4b397 to your computer and use it in GitHub Desktop.
How to make standalone native binaries from Tcl scripts.

How to make Tcl Starpacks

Tcl scripts can be made into standalone executables for various platforms in what are called Starpacks. Statically linking Tcl libraries in a Starpack helps make Tcl programs more portable.

Building a Starpack requires:

  1. The sdx tool.
  2. A TclKit runtime for your operating system and architecture.
  3. A TclKit runtime for the target operating system and architecture.

A TclKit is modified Tcl binary that has whatever dependencies your program needs. They can be generated using Roy Keene's Kit creator.

The Tcler's Wiki has detailed discussion of how Starpacks work, their history, etc. Here's a quick-start given some example.tcl script:

$ tclkit sdx qwrap example.tcl -runtime tcl8.6-linux-x86_64-tcllib.tclkit

These commands build a virtual filesystem, arrange it, and then produce the final executable. See this article for a more thorough explanation of how Starkits work and different ways to build them.

See the starscript.tcl file for an example of how to programmatically do this.

#!/usr/bin/env tclsh
# Generate Kits here: http://kitcreator.rkeene.org/kitcreator
proc starscript target {
set name [file root $target]
set sdx [file join lib sdx]
set tclkit [file join lib tclkit]
set runtimes [glob [file join lib runtimes *]]
file mkdir bin
foreach runtime $runtimes {
set os_arch [file root [file tail $runtime]]
exec $tclkit $sdx qwrap $target -runtime $runtime
file rename $name [file join bin $name-$os_arch]
}
}
foreach target $argv {
if {$target eq ""} {
puts "Error: bad input '$target'."
break
} else {
starscript $target
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment