Skip to content

Instantly share code, notes, and snippets.

@mkitti
Last active December 5, 2025 08:29
Show Gist options
  • Select an option

  • Save mkitti/8a73f7993377e92b702e66d292d21461 to your computer and use it in GitHub Desktop.

Select an option

Save mkitti/8a73f7993377e92b702e66d292d21461 to your computer and use it in GitHub Desktop.
Build a multiply_numbers.exe executable from Julia. Parses numbers from the command line and multiplies them.
#!/usr/bin/env -S julia
using Pkg
if !contains(ENV["PATH"], DEPOT_PATH[1]*"/bin")
@warn "The environment variable PATH does not contain $(DEPOT_PATH[1]*"/bin"). Consider modifying your shell startup."
@info "Adding $(DEPOT_PATH[1])/bin to \$PATH" ENV["PATH"]
ENV["PATH"] = "$(DEPOT_PATH[1])/bin:$(ENV["PATH"])"
end
if isnothing(Sys.which("juliac"))
@info "Installing JuliaC"
Pkg.Apps.add("JuliaC")
end
if !isdir("multiply_numbers")
@info "Generating multiply_numbers package"
Pkg.generate("multiply_numbers")
end
code_file::String = "multiply_numbers/src/multiply_numbers.jl"
if !isfile(code_file) || !contains("@main", read(code_file, String))
@info "Writing $code_file"
program = """
module multiply_numbers
function (@main)(args::Vector{String})
numbers = parse.(Int, args)
println(Core.stdout, prod(numbers))
return 0
end
end # module multiply_numbers
"""
println(program)
write("multiply_numbers/src/multiply_numbers.jl", program)
end
@info "Compiling multiply_numbers.exe with juliac multiply_numbers --output-exe multiply_numbers.exe --bundle build --trim"
run(`juliac multiply_numbers --output-exe multiply_numbers.exe --bundle build --trim`)
@info "The size of build/bin/multiply_numbers.exe is $(filesize("build/bin/multiply_numbers.exe")/1024^2) MiB"
@info "Running build/bin/multiply_numbers.exe 5 9 10"
run(`build/bin/multiply_numbers.exe 5 9 10`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment