☰

Bundles and workspaces

Author(s): Jose F. Morales, The Ciao Development Team.

In the same way that modules can be seen as collections of predicates, Ciao bundles define collections of modules. The visibility of bundles is determined by workspaces. A workspace is a directory that may contain bundles, each of them in a separate directory with the same name declared on its manifest. See Bundle management for details about creating and managing bundles.

Bundles

Bundles are usually organized as follows:

Manifest/        Metadata
cmds/            Modules implementing main/{0,1} (for CLI tools)
{src,lib,...}/   Source code (modules, packages, included files, etc.)

Bundles include some metadata describing versions, dependencies, (relative) path aliases, entry points for manuals and commands, additional pre-build, build, and installation rules, etc. This metadata is separated from the actual code into a single Manifest.pl file at the root of the bundle, or inside the Manifest/ directory. A typical Manifest.pl file has the following form:

:- bundle(<Name>).
version(<Version>).
depends([Dep1, ..., DepN]).
alias_paths([
    <Alias1> = <RelPath1>,
    ...
    <AliasN> = <RelPathN>
]).
lib(<RelPath>).
cmd(<CmdName>, [main=<RelPathMainMod>, ...]).
eng(<RelPathEngMain>, [<Opt>, ...]).
manual(<ManName>, [main=<RelPathDocCfg>, ...]).
readme(<OutName>, [main=<RelPathSrc>, ...]).
test_dir(<RelPath>).

where:

  • version/1 specifies the bundle version.
  • depends/1 declares dependencies with other bundles.
  • alias_paths/1 declares path aliases.
  • (multiple) lib/1 declares directories containing compilable modules.
  • (multiple) cmd/2 declares entry points for commands (executables).
  • (multiple) eng/2 declares engines (C code, see emugen), from their main module. The options are target(Target) (e.g., wasm32-emscripten), debug_level(Level) and cdefs(Defs), see eng_defs; the main module gives the others (e.g., usepath(Spec), another directory with engine sources) with :- engine_opts(Opts).
  • (multiple) manual/2 declares entry points for manuals.
  • (multiple) readme/2 declares generated readme files.
  • (multiple) test_dir/1 declares directories with unit tests (recursively), run by ciao test.

Items: the contents of a bundle form a tree of items, which can be built, installed, or cleaned separately, as the target B.I of the builder commands (e.g., ciao build core.engine). A sentence I.S, where I is the path of an item (e.g., tools, or tools.extra for the item extra nested in tools), declares S for that item, and the item itself if it is new. Several sentences may have the same path. S is one of:

  • cmd/1, cmd/2, or eng/2, as above;
  • item_dep(I2): the item I2 (a path from the bundle) is built first; item_dep(I2, Props) with properties, as for bundles ([optional]: only when I2 is on, see Optional items);
  • item_alias(I2): the item is another name for I2;
  • explicit_only: the item is not built with its parent, only when it is named as a target or it is a dependency.

E.g., the engine of core is the item core.engine:

engine.eng('engine/ciaoengine', []).

Versioning: Versions are specified as atoms (see version_strings) of the form 'X.Y.Z', where X, Y, and Z are non-negative integers, and an optional -Prerelease suffix. Omitted version numbers are assumed to be 0. Dependencies may include version constraints, e.g., core-[version>='1.15'] (see version_compare/3 for the definition of the comparison).

Currently the system supports only one version of a bundle at the same time. If two versions need to coexist at the same time, the bundles must be renamed (e.g. bndv1, bndv2).

Additional build rules: Although the Ciao compiler is incremental and performs the compilation on demand, some components may require configuration steps and pre-building. For example, this is often the case for bindings to foreign third-party libraries. Currently these rules must be written in a lower-level module Manifest/<bundle>.hooks.pl.

Workspaces

A workspace directory looks like:

build/           (Generated automatically)
<bundle1>/
...
<bundleN>/

The bundles that are visible are those of two kinds of workspaces:

  • the Ciao root, which contains the system bundles (core, builder, ...); Ciao executables locate it from their own location, so no variable needs to be set (CIAOROOT can override it);
  • the user workspaces given in CIAOPATH (a colon-separated list, ~/.ciao by default, none if the user has no home directory: no HOME and no passwd entry); the first one is where ciao get downloads new bundles.

Within a workspace W, bundle B is:

  • W/bndls/B, when W/bndls is a bundle catalog (see below);
  • W/B, otherwise.

A bundle can also be a workspace by itself: a directory B/.wksp makes B a workspace with the single bundle B and its outputs under B/.wksp/.

Each workspace directory contains a build/ directory with intermediate and final results of compilation (build/bin/) and documentation generation (build/doc/). Compilation is out-of-tree: all the outputs of the bundles of a workspace, including the intermediate compiler output, are kept there, never next to the sources (see Layout of the build directory).

A built workspace (or a whole Ciao root) does not depend on its location: it can be moved or copied, and it keeps working without rebuilding.

Bundle catalogs: workspaces may contain directories holding bundles. Such directories must be marked with an empty BUNDLE_CATALOG file (only bndls/ at the top of a workspace is used as a catalog; other catalogs are ignored with a warning). In that case, bundles are visible only if they are marked with an empty ACTIVATE file. This mecanism is useful to implement catalogs of bundles.

Bundle hooks

This is advanced usage: most bundles are described by their manifest alone, and they do not need hooks.

The manifest declares what a bundle contains (modules, commands, manuals). A bundle whose build needs more than that defines it in Prolog, in the hooks module Manifest/<bundle>.hooks.pl. Typical cases are configuration flags, engines and other C code, bindings to third-party libraries, and custom steps. The module uses the ciaobld(bundlehooks) package:

:- module(_, [], [ciaobld(bundlehooks)]).

Hooks are the clauses whose head is one of the hooks listed in Reference of hooks (the members of the traits grouped by the alias bundledef, defined in the module bundledef_traits of the builder). A hook H applies to the bundle, and I.H to its item at the path I (as in the manifest, e.g., engine or tools.extra); a hook can have a body (H :- Body), which is run when the builder uses it. Any other clause defines an ordinary predicate, so the names of hooks cannot be used for them. By convention, a comment % (hook) precedes each run of clauses of the same hook with a plain head (a dotted head is always a hook). The hooks module can also declare configuration flags and third-party code (see below), and import any module (e.g., from ciaobld(_), the builder libraries).

A grade defined outside the builder has a trait of its own for its steps (e.g., the wasm grade of the ciaowasm bundle, the trait bundledef_wasm: build_wasm/0 and install_wasm/0), defined in a module (here grade_wasm). A hooks module that defines those steps imports that module (:- use_module(ciaobld(grade_wasm))) and declares :- impl(bundledef_wasm): the bundle and all its items (also those declared before) implement it, and its steps are written as other hooks (H or I.H).

An item is declared by a block: I { H1. ... Hn. } declares the item I and defines its hooks H1, ..., Hn (the same as I {} and I.H1, ..., I.Hn); a block in a block is nested (a { b { H. } } is the item a.b with the hook H). Configuration flags have a block of their own, flag(Name) { ... } (see below), and so do third-party libraries, foreign_dep(Lib) { ... } (both belong to the bundle, not to an item).

If the hooks module does not compile (e.g., a hook I.H for an item that is not declared, or a clause I.H where H is not a hook), the builder stops with an error.

Items

A block I { ... } declares the item at the path I, whose parent (the path without its last name) must be the bundle or an item declared before; a hook I.T needs that declaration before it, or the item in the manifest (the manifest is part of the hooks module: its sentences come before the hooks, and an item path in it declares its items). Declaring an item more than once (in the hooks module or in the manifest) has the effect of declaring it once. The items come in the order of their declarations, those of the manifest first. T is one of the targets of the manifest or one of these:

  • cmd_raw(Kind, File, Props): a command that is not compiled from Prolog (e.g., a shell script);
  • assets(Dir): data files of the bundle.

E.g., ciaowasm defines its ciaosh-wasm command as an item, whose build copies a script instead of compiling it:

ciaosh_wasm {
    cmd('ciaosh-wasm', [main='NONE_AUTOGEN', shscript, ...]).
    build_bin :-
        cmd_build_copy(core, shscript, ~ciaosh_wasm_sh, 'ciaosh-wasm').
}.

or, one by one:

ciaosh_wasm {}.
ciaosh_wasm.cmd('ciaosh-wasm', [main='NONE_AUTOGEN', shscript, ...]).
ciaosh_wasm.build_bin :-
    cmd_build_copy(core, shscript, ~ciaosh_wasm_sh, 'ciaosh-wasm').

Optional items

An item with optional(P) is built only when its flag is yes: the flag B:I of the bundle B for the item at the path I (e.g., --ciaopp:tr_parallel=no, or --b:a.c=no for a nested item), with its comment and details. It defaults to yes when the item is present, that is, the path P (relative to the bundle) exists (it may be left out of a distribution), and the optional items that it needs are on; optional (with no path) is an item that is always present. When it is yes the bundle has the compilation fact has_B__I (with __ for each dot in I, e.g., has_b__a__c), else it is declared with no clauses, so that :- if(has_B__I) is false and a misspelled name is an error (see builder_aux:generate_config_auto/2, which also declares has_B for each optional dependency B, present or not):

ctchecks {
    optional('src/ctchecks').
    comment("Cost support for compile-time checking (ctchecks)").
    details("Interval checks of cost functions and cost function plots in
"||
            "the static checker.").
    lib('src/ctchecks').
}.
ctchecks.item_dep(infercost). % (needs the optional item infercost)
ctchecks.item_dep(res_plai).

An optional item needs the optional items of its item_dep/1; an item that is not optional can only use them as item_dep(I2, [optional]). An item that is off is not built with its parent, and naming it as a target is an error. The lib/1 directories of an item belong to it: the other lib/1 directories of the bundle leave them out, whether the item is on or not (e.g., lib('src') of the bundle does not compile src/ctchecks). ciao info shows the value of the flag of each optional item.

An optional item with needs_dep(D) also needs the optional dependency D of its bundle (declared in depends/1): its flag defaults to no, and cannot be yes, when D is not active (absent, disabled, or not accepted by optional_dep_active/1). E.g., the part of ciaopp that needs poly_clpq:

polyhedra_clpq {
    optional('domains/polyhedra_clpq').
    needs_dep(poly_clpq).
    comment("Polyhedra domain based on CLP(Q) (polyhedra_clpq)").
    lib('domains/polyhedra_clpq').
}.

Build steps

Each builder command runs some steps on the bundle and its items: prepare_build_bin and build_bin (for build, grade bin), prepare_build_docs and build_docs (grade docs), clean_bin, register and unregister, test, bench, and analyze_ (for analyze). By default a step does what the targets of the item say (e.g., build_bin compiles its lib and cmd targets). A hook for a step replaces that default for the item; prepare_build_bin and prepare_build_docs only add work before the build (e.g., checking or generating files). E.g., the core bundle generates its ciao-env command with:

ciao_env.build_bin :-
    copy_as_cmd(core, shscript, ~bundle_path(core, 'cmds'), 'ciao-env').

A bundle can also define its own commands, run with ciao custom_run B Name Args, with hooks custom_run(Name, Args).

Configuration flags

A configuration flag of the bundle (see ciao configure) is declared with a block flag(Name) { ... }, whose clauses are its properties; Name must match [a-z][0-9a-z_]*. The properties are:

  • comment(Text), details(Text): a short and a long description;
  • valid_values(Vs): the allowed values;
  • rule_default(V), or a rule rule_default(V) :- Goal: the default value, which the user can change (Goal computes it);
  • a rule rule_set_value(V) :- Goal: a value computed by Goal, which the user does not choose;
  • a rule needed_if :- Cond: the flag only applies when Cond holds (e.g., flag(register_shell(yes)));
  • interactive, or interactive([advanced]): asked by configure --interactive (in the advanced mode only);
  • default_comment(Text): shown with the default value;
  • default_value_comment(V, Text): why the default is V;
  • needs(V, Needs): the value V requires each element of Needs, B:F=V2 (a value of another flag) or path(Dir) (a directory of the bundle);
  • config_prolog_flag(Flag, Vs, V): the builder sets the Prolog flag Flag to the value of this flag (when it is not V) in the compilations it runs.

The rules are clauses of the hooks module: their bodies may call its predicates (an undefined one stops the builder, as any error in the module) and use any control construct, run left to right; flag(F(V2)) (of this bundle) or flag(B:F(V2)) reads another flag, configured first if needed.

The value of a flag is read with get_bundle_flag(B:Name) (from bundle/bundle_flags). E.g., from the core bundle:

flag(register_shell) {
    comment("Register in the shell startup files").
    valid_values([yes, no]).
    rule_default(yes).
    interactive.
}.

Foreign dependencies and third-party code

A foreign dependency (code that is not from us) is declared as a block foreign_dep(Name) { ... }: how the system's copy is detected, and how to download and build it (a library), or a command of the system, or a set of packages of a package manager. E.g.:

foreign_dep(node) {
    cmd(node).
    comment("Node.js (https://nodejs.org)").
}.
foreign_dep(npm_packages) {
    build_system(npm).
    package('esbuild').
    package('split.js').
}.

A clause of a block can also be written after it, outside, as D(K).H :- B (e.g., foreign_dep(npm_packages).package('mathjax@4')., under a conditional compilation directive); this holds for the elements of any map (blocks flag(K) { ... } too).

cmd(Name) detects a command in the PATH (with no source, it can only come from the system); with version(V), the oldest version accepted, its version is the first number in the first line of Name --version. comment(Str) says what it is (in the reason when it is missing). build_system(M) for a package manager M (npm) makes the block its packages (package(Name) clauses, which may be rules), installed by M (which needs its command, e.g., npm) into a directory of third-party/ shared by all bundles (for npm, third-party/3rd-npm/); they can only be our own. Its properties are config_tool(Tool) (a configuration tool, e.g., gsl-config: found in the PATH, it gives the location and version of the system's copy, with --prefix and --version, and the compiler and linker options of the library) or pkg_config(Name) (the same through pkg-config), depends(Ls) (the blocks Ls of the bundle that our build of this library needs, e.g., depends([gmp]) for PPL), indirect (the bundle uses this library only through the blocks that depend on it), version(V) (the version of our own build, and the oldest system copy accepted: an older one is not used, nor one whose version cannot be read, i.e., Name --version fails or prints no number, or Tool --version or pkg-config --modversion give nothing or something that is not numbers separated by dots, optionally followed by -Pre), source_url(URL) and source_md5(MD5) (or source_url(Platform, URL) and source_md5(Platform, MD5), for a platform), patch(File), build_system(S) (gnu_build_system, prebuilt or custom), option1(Opt) and option2(Opt, V) (the options --Opt and --Opt=V of its configure), update_config_scripts, libdir(Dir) (for prebuilt), and, for custom, the rules custom_configure(Env), custom_build(Env) and custom_install(Env).

A block with both a detection and a source (or packages) gives the bundle the configuration flag Name.origin (e.g., --ciao_ppl:gmp.origin=only_system), how the library is obtained: system_then_own (the default: the system's copy if found, else our own build, when the block has a source for the platform), only_system or only_own; its default is the flag builder:foreign_origin (e.g., --builder:foreign_origin=only_own for every foreign dependency with that flag). The other blocks have a fixed choice: only the system's copy (no source, e.g., a command), or only our own (no detection, e.g., packages). The bundle can be enabled only if the choice of each of its blocks can be satisfied here (else configure disables it, with the reason).

A block with config_tool(Tool) or cmd(Tool) also gives the bundle the configuration flag Name.path, the system's tool: by default Tool found in the PATH (empty if it is not found), else the path or name given at configure (e.g., --ciao_emacs:emacs.path=/usr/local/bin/emacs-29). A block has at most one of config_tool/1, pkg_config/1 and cmd/1.

A bundle has one copy of each library. A system copy of a library was built with the system's copies of its dependencies, so it is used only if each dependency allows them (its flag is not only_own, and it is detected in the system when something else uses it); our own build of a library is built with the chosen copy of each dependency (the detected location of a system copy is passed to its build, in CPPFLAGS, LDFLAGS and PKG_CONFIG_PATH), after our own builds of its dependencies. An indirect library comes with the system copies of the libraries that use it (no detection), is chosen by its flag when our own build of one of them needs it, and is not needed otherwise.

Our own copies are inside the tree and relocatable, while a system copy is referred to by its location in this machine: a tree that is copied to other machines (a binary distribution, or an executable, e.g., made by ciaoc -S, with its shared libraries in lib/ next to it) needs every foreign dependency built here, with --builder:foreign_origin=only_own.

The hooks ask for the choice with foreign_dep_use_own/1 (from ciaobld(foreign_config)): foreign_dep_use_own(B:L), the library L of B is our own build (with L unbound, some library of B is). A block with no detection has no usable system copy. The tool of config_tool(Tool) may be a path (e.g., a script of the bundle).

Before prepare_build_bin of a bundle, the builder installs (if needed) and activates its libraries whose choice is our own build, each after its dependencies: the source is downloaded into third-party/cache/ (kept, so that a reinstall does not need the network), built, and installed into third-party/store/, whose entries are shared by the bundles with a block of the same name and version. An entry records the bundle that installed it and the properties of its block that affect the build: a block with other properties is an error, which names the bundle to uninstall it from (or the blocks must agree).

The command ciao foreign:install B does the same on request (or B.L, only for the library L, whatever its choice); foreign:uninstall B.L removes its installed copy and its downloaded source; foreign:activate and foreign:deactivate link the installed copy into third-party/, or remove those links; and foreign:info B (or B.L) shows how they are obtained: per library, whether it is indirect and its dependencies, its flag L.origin and the choice, the system's copy found (location and version), and our own build (version, a source for the platform, installed, for which bundle, active) (see ciao help-all, "Foreign third-party code").

A body calls a hook H of a bundle B explicitly, with (B as bundledef).H, as in traits (the hook as defined in the hooks module or in the manifest of B; a call to an unknown hook is reported).

E.g., from the ciao_gsl bundle:

foreign_dep(gsl) {
    config_tool('gsl-config').
    version('2.6').
    source_url(tar('http://ftp.gnu.org/gnu/gsl/gsl-2.6.tar.gz')).
    source_md5("bda73a3dd5ff2f30b5956764399db6e7").
    build_system(gnu_build_system).
}.

Reference of hooks

The hooks are the members of the traits grouped by the alias bundledef, one per part of the builder that reads them. The manifest has only facts of the data members (version, dependencies, targets, and the structure of items); the other hooks, and rules, are written in the hooks module. The description of the bundle (the trait bundledef_desc: version/1, alias_paths/1, dep/2 from depends/1, service/2) is usually in the manifest.

The targets of the bundle and its items (T and I.T in the manifest and in the hooks; the traits bundledef_bin, bundledef_docs and bundledef_tasks):

lib(Dir)
(grade bin) modules compiled by build
lib_force_build(Dir)
(bin) force the build of Dir (with lib/1)
skip_lib(Dir)
(bin) a subdirectory that lib/1 does not compile (e.g., a part switched off by its flag; a rule, as lib/1)
src(Dir)
(bin) sources that are not compiled
assets(Dir), assets(Dir, Files)
(bin) other files
cmd(Main), cmd(Name, Props)
(bin) a command
cmd_raw(Kind, File, Props)
(bin) a command not compiled from Prolog
eng(Main, Opts)
(bin) an engine (see eng/2 in the manifest)
manual(Name, Props)
(docs) a manual
readme(Out, Props)
(docs) a generated readme file
test_dir(Dir)
(test) the unit tests of the modules under Dir, run by test (a test hook replaces them)

The properties of cmd(Name, Props) are main=F (its main module), kind=K (plexe by default, or shscript for a script), libexec (an auxiliary command, not in build/bin), static (a static executable), standalone, and bootstrap_ciaoc (compiled with the bootstrap compiler); those of cmd_raw/3 include link_as(Name) (another name for the command).

The structure of a bundle (the trait bundledef_item):

I { ... }
(in the hooks) I is an item; implicit for the items of the manifest
item_alias(I)
the item is another name for I
item_dep(I)
I is built first
explicit_only
the item is not built with its parent

The steps (a hook with the name of a step replaces its default for the item; in the traits bundledef_bin, bundledef_docs, bundledef_analyze and bundledef_tasks):

build
prepare_build_bin, build_bin, prepare_build_docs, build_docs
clean
clean_bin, clean_docs
install, uninstall
install_bin, install_docs, uninstall_bin, uninstall_docs
register, unregister, test, bench
the step of the same name
analyze
analyze_
custom_run
custom_run(Name, Args)

Configuration (the trait bundledef_config): cond_config_fact(Flag, Value, Fact) (Fact is one of the configuration facts generated for compiling the code that uses the bundle, when its flag Flag has value Value), optional_dep_active(Dep) (an optional dependency, declared with optional in depends/1, that is present and enabled, is used only if this hook accepts it, when the bundle defines it), and can_enable(R) (R is yes or no(Why): whether the bundle can be enabled here, asked after its flags are configured, when configure tries to enable it (the bundle is named, given a flag on the command line or --enable, or not configured yet); with no(Why) it is disabled, with the bundles that require it, and it is an error if it was given --enable; see the options --disable and --enable of ciao configure). Declarations: flag(Name) { ... }, foreign_dep(Name) { ... }.

Layout of the build directory

This section describes the current layout of the build/ directory of a workspace, for reference. It is internal to the build system and it may change between versions: only the commands and options of the builder are a stable interface (and the build/bin and build/doc directories, where commands and manuals are). It can be skipped.

build/bin
the commands of the bundles
build/libexec
auxiliary commands
build/cache
compiled modules (.po, .itf, ...), named after their bundle and their path in it (<bundle>.<path>), so that the name does not depend on where the bundle is
build/eng/<eng>/objs/<OS><ARCH>
the engines, one directory per platform
build/bundlereg
the registry of each bundle (B.bundlereg: its alias paths, relative to the bundle), its configuration (B.bundlecfg) and the configuration flags given by the user (B.bundlecfg_input)
build/doc
the manuals
build/tmpcomp
the bootstrap build: the stages of the boot (stage1 for the bootstrap compiler) and stageF (only at the root)