The standalone command-line compiler

Author(s): The Ciao Development Team.

ciaoc [CH00b] is the Ciao standalone command-line compiler. ciaoc can be used to create executables or to compile individual files to object code (to be later linked with other files). ciaoc is specially useful when working from the command line. Also, it can be called to compile Ciao programs from other tools such as, e.g., shell scripts, Makefiles, or project files. All the capabilities of ciaoc are also available from the interactive top-level shell, which uses the ciaoc modules as its components.

Introduction to building executables

An executable can be built from a single file or from a collection of inter-related files. In the case of only one file, this file must define the predicate main/0 or main/1. This predicate is the one which will be called when the executable is started. As an example, consider the following file, called hello.pl:

main :-
     write('Hello world'), 
     nl.

To compile it from the command line using the ciaoc standalone compiler it suffices to type ``ciaoc hello'' (in Win32 you may have to put the complete path to the ciaoc folder of the Ciao distribution, where the installation process leaves a ciaoc.bat file):

$ ciaoc hello

This produces an executable called hello in Unix systems and hello.cpx under Win32 systems. This executable can then be run in Win32 by double-clicking on it and on Unix systems by simply typing its name (see Running executables from the command line for how to run executables from the command line in Win32):

$ ./hello
Hello world

If the application is composed of several files the process is identical. Assume hello.pl is now:

:- use_module(aux, [p/1]).

main :-
     p(X),
     write(X), 
     nl.

where the file aux.pl contains:

:- module(aux,[p/1]).

p('Hello world').

This can again be compiled using the ciaoc standalone compiler as before:

$ ciaoc hello
$ ./hello
Hello world

The invocation of ciaoc hello compiles the file hello.pl and all connected files that may need recompilation -- in this case the file aux.pl. Also, if any library files used had not been compiled previously they would be compiled at this point (See Intermediate files in the compilation process). Also, if, say, hello.pl is changed and recompiled, the object code resulting from the previous compilation of aux.pl will be reused. This is all done without any need for Makefiles, and considerably accelerates the development process for large applications. This process can be observed by selecting the -v option when invoking ciaoc (which is equivalent to setting the verbose_compilation Prolog flag to on in the top-level interpreter).

If main/1 is defined instead of main/0 then when the executable is started the argument of main/1 will be instantiated to a list of atoms, each one of them corresponding to a command line option. Consider the file say.pl:

main(Argv) :-
     write_list(Argv), nl.

write_list([]).
write_list([Arg|Args]) :- 
     write(Arg),
     write(' '),
     write_list(Args).

Compiling this program and running it results in the following output:

$ ciaoc say
$ ./say hello dolly
hello dolly

The name of the generated executable can be controlled with the -o option (See Usage (ciaoc)).

Running executables from the command line

As mentioned before, what the ciaoc compiler generates and how it is started varies somewhat from OS to OS. In general, the product of compiling an application with ciaoc is a file that contains the bytecode (the product of the compilation) and invokes the Ciao engine on it.

  • In Unix this is a script (see the first lines of the file) which invokes the ciao engine on this file. To run the generated executable from a Unix shell it suffices to type its name at the shell command line, as in the examples above.

  • In a Win32 system, the compiler produces a similar file with a .cpx ending and an additional .bat file.

    The Ciao installation process typically makes sure that the Windows registry contains the right entries so that .cpx executables will run upon double-clicking and from a command shell (in NT systems).

    The .bat files allow running the Ciao executable from any other processes (which typically does not use the Windows registry).

    Finally, in a system in which Cygwin is installed, executables can also be used directly from the bash shell command line, without any associated .bat files, by simply typing their name at the bash shell command line, in the same way as in Unix.

Except for a couple of header lines, the contents of executables are almost identical under different OSs (except for self-contained ones). The bytecode they contain is architecture-independent. In fact, it is possible to create an executable under Unix and run it on Windows or viceversa, by making only minor modifications (e.g., creating the .bat file and/or setting environment variables or editing the start of the file to point to the correct engine location).

Types of executables generated

While the default options used by ciaoc are sufficient for normal use, by selecting other options ciaoc can generate several different types of executables, which offer interesting tradeoffs among size of the generated executable, portability, and startup time [CH00b]:

Dynamic executables:

ciaoc produces by default dynamic executables. In this case the executable produced is a platform-independent file which includes in compiled form all the user defined files. On the other hand, any system libraries used by the application are loaded dynamically at startup. More precisely, any files that appear as library(...) in use_module/1 and ensure_loaded/1 declarations will not be included explicitly in the executable and will instead be loaded dynamically. Is is also possible to mark other path aliases (see the documentation for file_search_path/2) for dynamic loading by using the -d option. Files accessed through such aliases will also be loaded dynamically.

Dynamic loading allows making smaller executables. Such executables may be used directly in the same machine in which they were compiled, since suitable paths to the location of the libraries will be included as default in the executable by ciaoc during compilation.

The executable can also be used in another machine, even if the architecture and OS are different. The requirement is that the Ciao libraries (which will also include the appropriate Ciao engine for that architecture and OS) be installed in the target machine, and that environment variables are set appropriately for the executable to be able to find them (see Environment variables used by Ciao executables). How to do this differs slightly from OS to OS.

Static executables:

Selecting the -s option ciaoc produces a static executable. In this case the executable produced (again a platform-independent file) will include in it all the auxiliary files and any system libraries needed by the application. Thus, such an executable is almost complete, needing in order to run only the Ciao engine, which is platform-specific.

Note: Currently there is an exception to this related to libraries which are written in languages other than Prolog, as, e.g., C. C files are currently always compiled to dynamically loadable object files (.so files), and they thus need to be included manually in a distribution of an application. This will be automated in upcoming versions of the Ciao system.

Again, if the executable is run in the same machine in which it was compiled then the engine is found automatically. If the executable is moved to another machine, the executable only needs access to a suitable engine (which can be done by setting the appropriate environment variables, see Environment variables used by Ciao executables).

This type of compilation produces larger executables, but has the advantage that these executables can be installed and run in a different machine, with different architecture and OS, even if Ciao is not installed on that machine. To install (or distribute) such an executable, one only needs to copy the executable file itself and the appropriate engine for the target platform (See Installing Ciao)), and to set things so that the executable can find the engine.

Note: It is also possible to produce real standalone executables, i.e., executables that do not need to have an engine around. However, this is not automated yet, although it is planned for an upcoming version of the compiler. In particular, the compiler can generate a .c file for each .pl file. Then all the .c files can be compiled together into a real executable (the engine is added one more element during link time) producing a complete executable for a given architecture. The downside of course is that such an executable will not be portable to other architectures without recompilation.

Dynamic executables, with lazy loading:

Selecting the -l option is very similar to the case of dynamic executables above, except that the code in the library modules is not loaded when the program is started but rather it is done during execution, the first time a predicate defined in that file is called. This is advantageous if a large application is composed of many parts but is such that typically only some of the parts are used in each invocation. An executable with lazy load has the advantage that it starts fast, loading a minimal functionality on startup, and then loads the different modules automatically as needed.

Self-contained executables:

Self-contained executables are static executables (i.e., this option also implies static compilation) which include a Ciao engine along with the bytecode, so they do not depend on an external one for their execution. This is useful to create executables which run even if the machine where the program is to be executed does not have a Ciao engine installed and/or libraries. The disadvantage is that such execuatbles are platform-dependent (as well as larger than those that simply use an external library). This type of compilation is selected with the -S option. Cross-compilation is also possible with the -SS option, so you can specify the target OS and architecture. To be able to use the latter option, it is necessary to have installed a ciaoengine for the target machine in the Ciao library (this requires compiling the engine in that OS/architecture and installing it, so that it is available in the library).

Intermediate files in the compilation process

Compiling an individual source (i.e., .pl) file produces a .itf file and a .po file. The .itf file contains information of the modular interface of the file, such as information on exported and imported predicates and on the other modules used by this module. This information is used to know if a given file should be recompiled at a given point in time and also to be able to detect more errors statically including undefined predicates, mismatches on predicate charaterictics across modules, etc. The .po file contains the platform-independent object code for a file, ready for linking (statically or dynamically).

It is also possible to use ciaoc to explicitly generate the .po file for one or more .pl files by using the -c option.

If you want to view the WAM instructions of one or more .pl files you can use the -w option. That will generate a .wam file with such instructions in a pretty format per each .pl file.

Usage (ciaoc)

The following provides details on the different command line options available when invoking ciaoc:

ciaoc [Opts] <Files>

Compile the listed files. If there is more than one file, the first
one is considered the main module (it must include the main predicate
when creating an executable).

The default extension for files is '.pl'.

-h, --help
	Show this help.
-u	<File> Use File for compilation, often used to include LibDir paths, etc.
--iso-strict
	Turn on stricter ISO compatibility for user files and modules declared with module/2.
-op	<Suffix> Use Suffix as the suffix for optimized (or otherwise tuned) code.
-L	<LibDir> Look for libraries also in the LibDir directory.
-c	Generate .po objects for the input modules.
-w	Generate .wam files (WAM code) for the input modules.
-S	Make standalone executable for the current OS and architecture, implies -s.
-SS	<EngCfg> Make standalone executable for the EngCfg OS and architecture (and 
	optionally debugging level) (see ciao_sysconf for valid values for Target), implies -s.
-ll	<Module> Force Module to be loaded lazily, implies -l.
-ac	<Packages> Use Packages for compiling all modules.
-acm	<Module> <Packages> Use the given Packages (term) for compiling Module.
-d	<Path> Files using this path alias are dynamic (default: library).
-o	<File> Specify output file name.
-v, --verbose-compilation
	Verbose mode.
-ri, --itf-format-r
	Generate human-readable .itf files.
-x, --check-libraries
	Check for changes also in the Ciao standard library during incremental  
	compilation (for developers).
-s, --executables-static
	Make a static executable (otherwise dynamic files are not included).
-l, --executables-lazyload
	Idem with lazy load of dynamic files (except insecure cases).
-np, --use-global-module-options-no
	Ignore global module options.
-na, --read-assertions-no
	Do not read the assertions in the code.
-rc, --runtime-checks
	Generate code with runtime checks; requires reading assertions.
--rtchecks-trust-no
	Disable rtchecks for trust assertions.
--rtchecks-entry-no
	Disable rtchecks for entry assertions.
--rtchecks-exit-no
	Disable rtchecks for exit assertions.
--rtchecks-test
	Enable rtchecks for test assertions (for debugging 
	purposes only, unittest library is recommended).
--rtchecks-level-exports
	Use rtchecks only for external calls of the exported predicates.
--rtchecks-asrloc-no
	Do not use assertion locators in the error messages.
--rtchecks-predloc-no
	Do not use predicate locators in the error messages.
--rtchecks-namefmt-short
	Show the name of predicates and properties in a reduced format.
--rtchecks-callloc-no
	Do not show the stack of predicates that caused the failure.
--rtchecks-callloc-literal
	Show the stack of predicates that caused the failure. Instrument it 
	in the literal. This mode provides more information, because reports 
	also the literal in the body of the predicate.