Skip to content

Instantly share code, notes, and snippets.

@terasakisatoshi
Created March 31, 2019 04:45
Show Gist options
  • Save terasakisatoshi/14f8fa8bab35683061306f96b1fcf96f to your computer and use it in GitHub Desktop.
Save terasakisatoshi/14f8fa8bab35683061306f96b1fcf96f to your computer and use it in GitHub Desktop.
Build Executable Julia Module including Plots.jl on Windows 10 via PackageCompiler
$dllpath=(julia -e 'using GR; s=joinpath(dirname(dirname(pathof(GR))),\"deps\",\"gr\",\"bin\"); s = s* \";\"; print(s)')
$Env:Path+=$dllpath
julia -e 'using PackageCompiler; build_executable(\"hello.jl\",\"hello\")'
module Hello
using Plots
function main()
xs = -π:0.01:π
p=plot(xs, sin.(xs));
savefig(p, "sin_curve.png")
end
Base.@ccallable function julia_main(ARGS::Vector{String})::Cint
main()
return 0
end
end
@terasakisatoshi
Copy link
Author

terasakisatoshi commented Mar 31, 2019

Usage

To resolve an error that tells libgr.dll does not found, we need add path of which libgr.dll lives.
I found it lives in C:\Users\USER1st\.julia\packages\GR\IVBgs\deps\gr\bin on my Windows 10 PC.

  • Open PowerShell and execute our PowerShell script.
PS > .\gen_exec.ps1
PS > .\builddir\hello.exe

you will find builddir/sin_curve.png as output result.

References (about PowerShell)

@terasakisatoshi
Copy link
Author

Another Solution

I found we do not need prepare PowerShell script ...

# gen_executable.jl
using PackageCompiler
using GR

s=joinpath(dirname(dirname(pathof(GR))),"deps","gr","bin")
s*=';'

ENV["PATH"] *= s

build_executable("hello.jl","hello")
$ julia gen_executable.jl

@terasakisatoshi
Copy link
Author

image

@terasakisatoshi
Copy link
Author

terasakisatoshi commented Mar 31, 2019

Appendix

  • multi platform support
# gen_executable.jl
using PackageCompiler
using Pkg

if in("GR",keys(Pkg.installed()))
    @eval import GR
else
    Pkg.add("GR")
    @eval import GR
end

if Sys.iswindows()
    s=joinpath(dirname(dirname(pathof(GR))),"deps","gr","bin")
    s*=';'
    if !endswith(ENV["PATH"],';')
        ENV["PATH"] *= ';'
    end
    ENV["PATH"] *= s
else
    s=joinpath(dirname(dirname(pathof(GR))),"deps","gr","lib")
    s*=':'
    if haskey(ENV, "LD_LIBRARY_PATH")
        ENV["LD_LIBRARY_PATH"] *= s
        if !endswith(ENV["LD_LIBRARY_PATH"],':')
            ENV["LD_LIBRARY_PATH"] *= ':'
        end
    else
        ENV["LD_LIBRARY_PATH"] = s
    end
end

build_executable("hello.jl","hello")

For those who came from terasakisatoshi/myjulia, all you have to do is copy hello.jl and gen_executable.jl to your work space and execute command below:

$ docker run --rm -it -v $PWD:/work -w /work terasakisatoshi/myjulia:<tag> julia gen_executable.jl
$ docker run --rm -it -v $PWD:/work -w /work terasakisatoshi/myjulia:<tag> ./builddir/hello

where <tag> depends on your OS, for example if you're using MacOSX(e.g. Mojave), for example, replace <tag> with mac.

@terasakisatoshi
Copy link
Author

terasakisatoshi commented Apr 4, 2019

License

MIT License

Copyright (c) 2019 Satoshi Terasaki

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment