Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PROGRAM_FILE not set when compiled through juliac.jl #54

Closed
NHDaly opened this issue Apr 23, 2018 · 7 comments
Closed

PROGRAM_FILE not set when compiled through juliac.jl #54

NHDaly opened this issue Apr 23, 2018 · 7 comments

Comments

@NHDaly
Copy link
Member

NHDaly commented Apr 23, 2018

Base.PROGRAM_FILE is empty when compiled through juliac.jl. (The global ARGS is empty, which is where PROGRAM_FILE is set from.)

PROGRAM_FILE is set here, by popping the first value off of ARGS, iff ARGS is not empty (checked here).

Currently, the default program.c skips setting the global ARGS, opting instead to pass an Array to julia_main. This means that there is no way to access PROGRAM_FILE from compiled code.

@NHDaly
Copy link
Member Author

NHDaly commented Apr 23, 2018

Here is an example:

# examples/args.jl

foo() = println("ARGS: $ARGS")  # Sometimes code accesses ARGS directly, as a global

# Create a temporary .html file, and open it to share the greetings.
Base.@ccallable function julia_main(args::Vector{String})::Cint
    println("@__DIR__: $(@__DIR__)")
    println("@__FILE__: $(@__FILE__)")
    println("PROGRAM_FILE: $(PROGRAM_FILE)")
    foo()  # access global ARGS
    return 0
end

julia_main(["a b c"])
$ julia examples/args.jl d e f
@__DIR__: /Users/daly/src/build-jl-app-bundle/examples
@__FILE__: /Users/daly/src/build-jl-app-bundle/examples/args.jl
PROGRAM_FILE: examples/args.jl
ARGS: String["d", "e", "f"]  # NOTE: these are the command line args, not the inputs to `julia_main()`.
$ julia ~/.julia/v0.6/PackageCompiler/juliac.jl -v -a -e examples/args.jl && ./builddir/args d e f
...
@__DIR__: /Users/daly/src/build-jl-app-bundle/examples    # Note these are not a viable replacement (compile-time macros)
@__FILE__: /Users/daly/src/build-jl-app-bundle/examples/args.jl
PROGRAM_FILE:
ARGS: String[]

@NHDaly
Copy link
Member Author

NHDaly commented Apr 23, 2018

I tried calling jl_set_ARGS(argc, argv) in program.c, but no matter where I put the call (before libsupport_init, before julia_init, before julia_main), I couldn't get it to work. Any ideas why? The repl uses jl_set_ARGS to set ARGS (here).

Also, I even tried setting PROGRAM_FILE manually through jl_set_global(jl_core_module, jl_symbol("PROGRAM_FILE"), (jl_value_t*)jl_cstr_to_string(argv[0]));, but it was still empty, which really surprised me.

Does anyone have suggestions?

@NHDaly
Copy link
Member Author

NHDaly commented Apr 23, 2018

Also, I even tried setting PROGRAM_FILE manually through jl_set_global(jl_core_module, jl_symbol("PROGRAM_FILE"), (jl_value_t*)jl_cstr_to_string(argv[0]));, but it was still empty, which really surprised me.

Ah, okay, I was using the wrong module. I fixed it by adding that line, but using jl_base_module, to a custom program.c:
NHDaly/ApplicationBuilder.jl@95098a4#diff-0e48c24b6c52db1e06db58eb5c62d917R48

Although this seems like kind of a hack, since ARGS still isn't available as a global. For whatever reason I couldn't get jl_set_global to work for ARGS.

If you all think it's reasonable, I'm happy to send a PR to add this to examples/program.c, but that doesn't feel like a complete solution to me...

@NHDaly
Copy link
Member Author

NHDaly commented May 5, 2018

@lucatrv: I'm interested in your thoughts here as well. Do you have any idea how we could set global ARGS and/or PROGRAM_FILE?

Does the method I used in my custom program.c, above seem like a hack, or should I send a PR to add it to this repo's examples/program.c?

I hope your travel was nice!! 😄

@lucatrv
Copy link
Collaborator

lucatrv commented May 5, 2018

I think we can add the fix for PROGRAM_FILE = argv[0], since we already pass all the following argv elements to julia_main.

Yes my travel to Japan was very nice, thanks!!

@NHDaly
Copy link
Member Author

NHDaly commented May 5, 2018

Oh wowwwww i've always wanted to go to japan!!! That's excellent! :)

(opened PR #57)

@NHDaly NHDaly closed this as completed Jul 17, 2018
@NHDaly
Copy link
Member Author

NHDaly commented Jul 17, 2018

Fixed in #57

KristofferC added a commit that referenced this issue Feb 9, 2020
* workaround julia#34076

* fix typo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants