PackageCompiler.jl does not support ARGS (arguments)?

Hello,

It seems that PackageCompiler does not work with ARGS.
I tried several ways and none worked:

CASE 1:

In precompile_app.jl:

MyProgram.julia_main(["CHECK_DATA_1", "DEV"])

In MyProgram.jl:

module MyProgram

	argument_list = ARGS

	println("Length of arguments array: " * string(length(ARGS)))
	println("Content of arguments array: ")

	program_main()
		# My Source Code
	end

    Base.@ccallable function julia_main(ARGS::Vector{String})::Cint
		program_main()
		return 0
	end
end

When compiling with PackageCompiler, then I get this error message:
Failed to precompile MyProgram [8055130b-358c-4aa5-b077-72da6b1478e9] to C:\Users\sbbstm.julia\compiled\v1.8\MyProgram\jl_5E4F.tmp.
ERROR: LoadError: MethodError: no method matching julia_main()
Closest candidates are:
julia_main(::Vector{String}) at C:\JuliaWS\MyProgram\MyProgram\src\MyProgram.jl:890

CASE 2:

In precompile_app.jl:

MyProgram.julia_main()

In MyProgram.jl:

module MyProgram

	argument_list = ARGS

	println("Length of arguments array: " * string(length(ARGS)))
	println("Content of arguments array: ")

 	program_main()
		# My Source Code
	end

	Base.@ccallable function julia_main(ARGS::Vector{String})::Cint
		program_main()
		return 0
	end
end

When compiling with PackageCompiler, then I get this error message:
Failed to precompile MyProgram [8055130b-358c-4aa5-b077-72da6b1478e9] to C:\Users\sbbstm.julia\compiled\v1.8\MyProgram\jl_517D.tmp.
ERROR: LoadError: MethodError: no method matching julia_main()
Closest candidates are:
julia_main(::Vector{String}) at C:\JuliaWS\MyProgram\MyProgram\src\MyProgram.jl:890

CASE 3:

In precompile_app.jl:

MyProgram.julia_main()
module MyProgram

	argument_list = ARGS

	println("Length of arguments array: " * string(length(ARGS)))
	println("Content of arguments array: ")

 	program_main()
		# My Source Code
	end

	julia_main()::Cint
		program_main()
		return 0
	end
end

It compiles an creates a MyProgram.exe in the folder bin, but when starting the compiled MyProgram.exe, it does nothing related to ARGS!
It even does not write the result of these 2 lines, neither when I give arguments nor when i give no arguments when starting MyProgram.exe:

println("Length of arguments array: " * string(length(ARGS)))
println("Content of arguments array: ")

WITHOUT PackageCompiler.jl:

When starting the same source code as in CASE 3 without PackageCompiler and giving no args, then I get this here (that is what I expected and what is OK):
Length of arguments array: 0
Content of arguments array:

When starting the program (source code via Julia.exe) with arguments, such as “Julia.exe MyProgram.jl bla 123” then it works correctly. The output is:
Length of arguments array: 2
Content of arguments array:
bla
123

It seems that ARGS does not work with PackageCompiler.
Is there any workaround or bugfix?

My system:

  • Winows 10 64 Bits
  • Julia 1.8.5
  • PackageCompiler v2.1.7

Thanks for your advices in advance!

Kind regards,
Miroslav Stimac

Hello Miroslav,

PackageCompiler works with ARGS. You need to use the julia_main() function signature as you did in CASE 3. But keep in mind the entry point, for a compiled julia application, is julia_main(), so your println() functions were never called.
In the interpreted version you got another output, because the file is just executed top down, including global scope (where your println() calls live).

ARGS is a global constant in Julia, you don’t need to put anywhere as function parameter, you can just use it.

Try this, if you want to compile it:

module compiled_example

function julia_main()::Cint
    println("Length of arguments array: $(length(ARGS))")
	println("Content of arguments array: ", ARGS)

    # program main, add your code here

    exit(0)    
end

end

and if you want to run it as script:

#! /usr/bin/env julia

module compiled_example

function julia_main()::Cint
    println("Length of arguments array: $(length(ARGS))")
	println("Content of arguments array: ", ARGS)

    # program main, add your code here

    exit(0)    
end

julia_main()

end

1 Like

Easy way is using Home · Comonicon.jl

Thank you very much!
You are right!
If I put the code in the function, then it works correctly and I can use ARGS.

Kind regards,
Miroslav Stimac