Executable ROOT6/Cling scripts?

This is just an idea, inspired by the new scripting power available with ROOT6/Cling:

One often needs some glue scripts to integrate ROOT (and custom) classes with shell code, batch systems, and so on. If ROOT scripts could be made executable, they would be well suited to take this role.

While it is, of course, always possible to call scripts explicitly via “root myscript.C”, one has to provide the path to the script (absolute or relative, depending on where you are). Executable scripts have the advantage that they just need to be the PATH, and that the user doesn’t have to care in which language they are implemented.

In ROOT-5, this was possible, after a fashion, as CINT was lax enough to tolerate a line starting with “#” at the beginning of a script. Given a executable shell script “root-batch” (on PATH) like this

#!/bin/bash

exec root -l -b -q "$@"

one could make an executable CINT script (mode 755) hello.C like this

#!/usr/bin/env root-batch

{
cout << "Hello, World!" << endl;
}

and directly run it on the shell:

$ hello.C

The script “root-batch” is necessary because env only allows one parameter, and we’d like to call ROOT with the options “-l -b -q”.

Fortunately in general, but unfortunately in this case, Cling is a lot more precise when it comes to input validity, and doesn’t accept a “#!..” line at the top of a script. However, maybe this could be changed, as a special case during parsing?

Currently, many experiments use python scripts for such glue code. However, with ROOT6/Cling we now have powerful and fast true C++(98/11) scripting available that in some cases may be preferable:

  • Only one language to deal with
  • Template support
  • Superior speed, allowing for time-critical operations in scripts

One other thing that is missing to make this work for non-trivial use cases is the ability to pass command line arguments to the script. A possible solution would be for ROOT6/Cling to support a “–” command line option (like some other programs) and pass all arguments after the “–” through to the script. The arguments could be made available in global argc/argv variables. Even more C+±like, Cling could supports scripts with a “main(argc, argv)” function.

1 Like

Hi,

This should be simple to implement. Please have a look at how unnamed macros are handled (cling::MetaProcessor::readInputFromFile cling.web.cern.ch/cling/doxygen/ … ecf51d6e28 )
You could filter those out there.

[quote=“oschulz”]
Currently, many experiments use python scripts for such glue code. However, with ROOT6/Cling we now have powerful and fast true C++(98/11) scripting available that in some cases may be preferable:

  • Only one language to deal with
  • Template support
  • Superior speed, allowing for time-critical operations in scripts

One other thing that is missing to make this work for non-trivial use cases is the ability to pass command line arguments to the script. A possible solution would be for ROOT6/Cling to support a “–” command line option (like some other programs) and pass all arguments after the “–” through to the script. The arguments could be made available in global argc/argv variables. Even more C+±like, Cling could supports scripts with a “main(argc, argv)” function.[/quote]

This is a nice idea. It needs a little bit more effort to implement. You should have a look at how command line options are parsed in cling standalone. Patches are welcome.
Cheers,
Vassil

Hi,

Actually this is what the --metastr flag was invented for :slight_smile: See sft.its.cern.ch/jira/browse/ROOT-4498

Cheers, Axel.

Even without:

#!/usr/bin/env cling
extern "C" int printf(const char* fmt, ...);
printf("FOO\n");
.q
//usr/bin/env root -l $0; exit $?

at the top of my ROOT macros works for me. Could you check?

(Also posted here)

Actually, you’re better off using the Linux kernel hook for that (your simple solution could break in many interesting and subtle ways)
See:

(That’s what we use in Go to make Go programs buildable by the regular toolchain and ‘interpretable’ as a shell script.)

hth,
-s

My Mac doesn’t run Linux :wink:

Well, nobody’s perfect.

1 Like

2 posts were split to a new topic: Compiledata.h versus build systems