Skip to content

Commit

Permalink
Merge pull request #55 from NHDaly/get_resources_dir
Browse files Browse the repository at this point in the history
Make cd_to_bundle_resources() callable from Julia (again): switch to external `ApplicationBuilderAppUtils` package
  • Loading branch information
NHDaly authored Apr 29, 2019
2 parents 608de15 + 4a099c3 commit aa834c7
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 42 deletions.
15 changes: 12 additions & 3 deletions Manifest.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# This file is machine-generated - editing it directly is not advised

[[ApplicationBuilderAppUtils]]
git-tree-sha1 = "655b936802c498ddaee84c0824610250433f4ace"
repo-rev = "master"
repo-url = "https://github.com/NHDaly/ApplicationBuilderAppUtils.jl"
uuid = "96374992-6a10-11e9-2fa0-73472aac04df"
version = "0.1.0"

[[ArgParse]]
deps = ["Compat", "Test", "TextWrap"]
git-tree-sha1 = "14d5789a99e6bea3e258d668ec09359a9feaf2d1"
Expand Down Expand Up @@ -40,7 +49,7 @@ deps = ["Mmap"]
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"

[[Distributed]]
deps = ["LinearAlgebra", "Random", "Serialization", "Sockets"]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[Glob]]
Expand All @@ -56,7 +65,7 @@ uuid = "0862f596-cf2d-50af-8ef4-f2be67dfa83f"
version = "0.2.1"

[[InteractiveUtils]]
deps = ["LinearAlgebra", "Markdown"]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[LibCURL]]
Expand Down Expand Up @@ -157,7 +166,7 @@ uuid = "30578b45-9adc-5946-b283-645ec420af67"
version = "0.4.0"

[[UUIDs]]
deps = ["Random"]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[Unicode]]
Expand Down
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Nathan Daly <[email protected]>", "Ranjan Anantharaman <benditlikeran
version = "0.4.1"

[deps]
ApplicationBuilderAppUtils = "96374992-6a10-11e9-2fa0-73472aac04df"
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
PackageCompiler = "9b87118b-4619-50d2-8e1e-99f35a4d4d9d"
Expand All @@ -14,7 +15,8 @@ REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
julia = "1"

[extras]
ApplicationBuilderAppUtils = "96374992-6a10-11e9-2fa0-73472aac04df"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
test = ["ApplicationBuilderAppUtils", "Test"]
4 changes: 4 additions & 0 deletions examples/blink.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

using Blink

using ApplicationBuilderAppUtils

# In order to distribute this Application, we've copied all its dependencies
# into the .app bundle via the -R and -L build flags.
# Now, here, we have to update the Packages that use those dependencies to find
Expand Down Expand Up @@ -82,6 +84,8 @@ function helloFromBlink()
end

Base.@ccallable function julia_main(args::Vector{String})::Cint
ApplicationBuilderAppUtils.cd_to_bundle_resources()

# Apparently starting Electron too quickly means the OS doesn't get a
# chance to find the name of the application...
sleep(2)
Expand Down
4 changes: 4 additions & 0 deletions examples/hello.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using ApplicationBuilderAppUtils

# Create a temporary .html file, and open it to share the greetings.
Base.@ccallable function julia_main(ARGS::Vector{String})::Cint
tmpdir = mktempdir()
Expand All @@ -6,6 +8,8 @@ Base.@ccallable function julia_main(ARGS::Vector{String})::Cint
println(io, "Hello, World!")
println(io, "<br> -- Love, $PROGRAM_FILE")
println(io, """<br><br>Current working directory: <a href="file://$(pwd())">$(pwd())</a>""")
ApplicationBuilderAppUtils.cd_to_bundle_resources()
println(io, """<br><br>After cd to resources directory: <a href="file://$(pwd())">$(pwd())</a>""")
end
run(`open file://$filename`)
return 0
Expand Down
5 changes: 2 additions & 3 deletions examples/libui.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Libui


using Libui
using ApplicationBuilderAppUtils

#const progressbar::Ptr{uiProgressBar} = C_NULL
#const spinbox::Ptr{uiProgressBar} = C_NULL
Expand Down Expand Up @@ -168,6 +166,7 @@ end


Base.@ccallable function julia_main(ARGS::Vector{String})::Cint
ApplicationBuilderAppUtils.cd_to_bundle_resources()
global progressbar = uiNewProgressBar()
global spinbox = uiNewSpinbox(0, 100)
global slider = uiNewSlider(0, 100)
Expand Down
3 changes: 3 additions & 0 deletions examples/sdl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
using SimpleDirectMediaLayer
SDL2 = SimpleDirectMediaLayer

using ApplicationBuilderAppUtils
using Pkg


fontFile = joinpath(Pkg.dir("SimpleDirectMediaLayer"),
"assets","fonts","FiraCode","ttf","FiraCode-Regular.ttf")

Expand Down Expand Up @@ -80,6 +82,7 @@ function helloFromSDL()
end

Base.@ccallable function julia_main(args::Vector{String})::Cint
ApplicationBuilderAppUtils.cd_to_bundle_resources()
helloFromSDL()
return 0
end
Expand Down
30 changes: 1 addition & 29 deletions src/ApplicationBuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,39 +136,14 @@ function build_app_bundle(juliaprog_main;
write(snoopfile, """Base.include(@__MODULE__, raw"$(abspath("$juliaprog_main"))"); julia_main([""]); """)
end

# When Apple launches an app, it sets the current-working-directory to homedir.
# Therefore, we inject this function definition into the app, and call it from
# the C driver program.
utils_injection_file = joinpath(launcher_dir, "applicationbuilderutils.jl")
write(utils_injection_file,
"""
Base.include(@__MODULE__, raw"$(abspath(juliaprog_main))")
"""*raw"""
Base.@ccallable function cd_to_bundle_resources()::Nothing
full_binary_name = PROGRAM_FILE # PROGRAM_FILE is set manually in program.c
@static if Sys.isapple()
m = match(r".app/Contents/MacOS/[^/]+$", full_binary_name)
if m != nothing
resources_dir = joinpath(dirname(dirname(full_binary_name)), "Resources")
cd(resources_dir)
end
else
# TODO: Should we do similar verification on linux/windows? Maybe use splitpath()?
resources_dir = joinpath(dirname(dirname(full_binary_name)), "res")
cd(resources_dir)
end
println("cd_to_bundle_resources(): Changed to new pwd: $(pwd())")
end
precompile(cd_to_bundle_resources, ()) # Compile it for the binary.
""")
custom_program_c = "$(@__DIR__)/program.c"
cc_flags = Sys.isapple() ? `-mmacosx-version-min=10.10 -headerpad_max_install_names` : nothing
# Provide an environment variable telling the code it's being compiled into a mac bundle.
withenv("LD_LIBRARY_PATH"=>"$libs_dir:$libs_dir/julia",
"COMPILING_APPLE_BUNDLE"=>"true") do
verbose && println(" PackageCompiler.static_julia(...)")
# Compile executable and copy julia libs to $launcher_dir.
PackageCompiler.build_executable(utils_injection_file, binary_name, custom_program_c;
PackageCompiler.build_executable(juliaprog_main, binary_name, custom_program_c;
builddir=launcher_dir, verbose=verbose, optimize="3",
snoopfile=snoopfile, debug="0", cpu_target=cpu_target,
compiled_modules="yes",
Expand Down Expand Up @@ -269,9 +244,6 @@ function build_app_bundle(juliaprog_main;
# --------------- CLEAN UP before distributing ---------------
println("~~~~~~ Cleaning up temporary files... ~~~~~~~")

# Delete the file we added to inject code.
rm(utils_injection_file)

# Delete the tmp build files
function delete_if_present(file, path)
files = glob(file, path)
Expand Down
6 changes: 0 additions & 6 deletions src/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
JULIA_DEFINE_FAST_TLS()
#endif

// `cd_to_bundle_resources` is injected by ApplicaionBuilder.jl
extern void cd_to_bundle_resources();

// Declare C prototype of a function defined in Julia
extern int julia_main(jl_array_t*);

Expand Down Expand Up @@ -48,9 +45,6 @@ int main(int argc, char *argv[])
jl_arrayset(ARGS, s, i - 1);
}

// Navigate to inside the Appication Bundle before running julia_main
cd_to_bundle_resources();

// call the work function, and get back a value
retcode = julia_main(ARGS);

Expand Down

0 comments on commit aa834c7

Please sign in to comment.