Last active
March 18, 2026 00:10
-
-
Save penelopeysm/9338c160eeb05437205535c2edcf80ee to your computer and use it in GitHub Desktop.
PackageCompiler + various Julia formatters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # JuliaFormatter and Runic sysimages | |
| # adapted from https://github.com/domluna/JuliaFormatter.jl/issues/633#issuecomment-1518805248 | |
| # Place this in `~/.bash_profile` (or the corresponding config file for your shell). | |
| # You should then be able to run the following commands in your shell: | |
| # | |
| # - `jf1`: Format with JuliaFormatter@v1 | |
| # - `jf2`: Format with JuliaFormatter@v2 | |
| # - `runic`: Format with Runic | |
| # | |
| # The time savings from a compiled version are most significant for `jf1`; for the other two, | |
| # the compiled version is still faster than running `julia -e ...`, but less strikingly so. | |
| export _JULIA_FORMATTER_V1_SO=$HOME/.julia/formatterv1.so | |
| export _JULIA_FORMATTER_V2_SO=$HOME/.julia/formatterv2.so | |
| export _RUNIC_SO=$HOME/.julia/runic.so | |
| jf1() { | |
| project=${1:-$PWD} | |
| _juliaformat "$project" "JuliaFormatter@1" | |
| } | |
| jf2() { | |
| project=${1:-$PWD} | |
| _juliaformat "$project" "JuliaFormatter@2" | |
| } | |
| runic() { | |
| project=${1:-$PWD} | |
| _juliaformat "$project" "Runic" | |
| } | |
| _juliaformat() { | |
| project=$1 | |
| FORMATTER=$2 | |
| if [[ "$FORMATTER" == "JuliaFormatter@1" ]]; then | |
| SO=$_JULIA_FORMATTER_V1_SO | |
| FORMAT_CMD='println(JuliaFormatter.format(".") ? "No changes made." : "Files were reformatted.")' | |
| elif [[ "$FORMATTER" == "JuliaFormatter@2" ]]; then | |
| SO=$_JULIA_FORMATTER_V2_SO | |
| FORMAT_CMD='println(JuliaFormatter.format(".") ? "No changes made." : "Files were reformatted.")' | |
| elif [[ "$FORMATTER" == "Runic" ]]; then | |
| SO=$_RUNIC_SO | |
| FORMAT_CMD='Runic.main(["--inplace", "."]); println("done")' | |
| else | |
| >&2 echo "error: 2nd argument to _juliaformat must be either JuliaFormatter@1, JuliaFormatter@2, or Runic" | |
| return 1 | |
| fi | |
| if [[ ! -e "$SO" ]]; then | |
| echo "Could not find sysimage for $FORMATTER at $SO, so will build it..." | |
| _build_formatter_so $FORMATTER | |
| fi | |
| echo "Running $FORMATTER on $project" | |
| OLD=$PWD | |
| cd $project | |
| julia --startup-file=no --threads=auto -J $SO -O0 --compile=min -e "$FORMAT_CMD" | |
| if [ $? -ne 0 ]; then | |
| >&2 printf "\n\nFailed to run $FORMATTER; if the above error did not help identify the issue, you may need to regenerate the sysimages. To do this, run the following command:\n\n rm -f \"$SO\"; _build_formatter_so $FORMATTER\n\n" | |
| cd $OLD | |
| return 1 | |
| fi | |
| cd $OLD | |
| } | |
| _build_formatter_so() { | |
| FORMATTER=$1 | |
| if [[ "$FORMATTER" == "JuliaFormatter@1" ]]; then | |
| PKG_NAME="JuliaFormatter" | |
| PKG_ADD_CMD='Pkg.add(name="JuliaFormatter", version="1")' | |
| PRECOMPILE_FILE_CONTENT='"using JuliaFormatter; format(\".\")"' | |
| SO=$_JULIA_FORMATTER_V1_SO | |
| elif [[ "$FORMATTER" == "JuliaFormatter@2" ]]; then | |
| PKG_NAME="JuliaFormatter" | |
| PKG_ADD_CMD='Pkg.add(name="JuliaFormatter", version="2")' | |
| PRECOMPILE_FILE_CONTENT='"using JuliaFormatter; format(\".\")"' | |
| SO=$_JULIA_FORMATTER_V2_SO | |
| elif [[ "$FORMATTER" == "Runic" ]]; then | |
| PKG_NAME="Runic" | |
| PKG_ADD_CMD='Pkg.add("Runic")' | |
| PRECOMPILE_FILE_CONTENT='"using Runic; exit(Runic.main([\"--inplace\", \".\"]))"' | |
| SO=$_RUNIC_SO | |
| else | |
| >&2 echo "error: unknown formatter '$FORMATTER', available options are JuliaFormatter@1, JuliaFormatter@2, Runic" | |
| return 1 | |
| fi | |
| OLD=$PWD | |
| WORKDIR=$(mktemp -d) | |
| cd $WORKDIR | |
| git clone --depth 1 --quiet https://github.com/TuringLang/Turing.jl # Not used; just an example project | |
| cd Turing.jl | |
| echo "Building sysimage for formatter $FORMATTER at $SO" | |
| { | |
| cmd='using Pkg; Pkg.activate(; temp=true); Pkg.add("PackageCompiler"); ' | |
| cmd+=$PKG_ADD_CMD | |
| cmd+='; open("precompile_file.jl", "w") do io; write(io, ' | |
| cmd+="$PRECOMPILE_FILE_CONTENT" | |
| cmd+='); end; using PackageCompiler; create_sysimage(["' | |
| cmd+=$PKG_NAME | |
| cmd+='"]; sysimage_path="' | |
| cmd+=$SO | |
| cmd+='", precompile_execution_file="precompile_file.jl")' | |
| echo $cmd | |
| julia --startup-file=no --compile=yes -O3 --threads=auto -e "$cmd" | |
| } || { | |
| >&2 echo "sysimage failed to build, exiting" | |
| cd $OLD | |
| return 1 | |
| } | |
| cd $OLD | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment