Name
dk-Exe - Produces target-specific executables.
Synopsis
dk Exe [OPTION]… (1) TOOL_MODULE_ID [(2) SCRIPT_MODULE_ID] [(3) SCRIPT_MODULE_ARGS]…
Compiler arguments
(1) TOOL_MODULE_ID (required)
The fully-qualified id of the tool module. It should be StdStd_Std.Run if './dk StdStd_Std.Run' or './dk Run' was the launcher, and it should be StdStd_Env.Run if './dk StdStd_Env.Run' was the launcher, and it should be StdStd_V2_3.Run if './dk StdStd_V2_3.Run' was the launcher. Used primarily to display what command is required to re-run the tool.
(2) SCRIPT_MODULE_ID
The id of the script module to run or access. Example: `dk Run YourLibrary_Std.SomeModule.Id`. You can separate Run options from the script options by using `--`, as in: `dk Run [options]… -- YourLibrary_Std.SomeModule.Id [SCRIPT_MODULE_ARGS]…`.
(3) SCRIPT_MODULE_ARGS
Arguments and options to pass to the script module.
Exe generates build files, then uses those build files to produce OCaml bytecode, and then uses the bytecode to produce target-specific executables.
Lifecycle when executable runs
The sequence is:
1. The entry module you specify on the `dk` command line is analyzed and all of its transitive dependent modules are located. The entry and transitive modules are compiled into a bytecode executable (just like a standard Run), except the [at_exit] handlers are not called at the end of the module initialization.
2. After module initialization, the OCaml runtime is shut down. As the runtime is shut down, the [at_exit] handlers will be called.
Options
--analysis-trace-file=TRACE_FILE
Capture a replayable trace of the module analysis. Every input and every decision made during analysis is recorded in TRACE_FILE. The traces are relocatable (ie. they can be used as a regression test on a different machine with different directories) because the full paths to modules are not recorded. Instead each You/Us/Them root directory is encoded as an serially incrementing number, and as long as the You/Us/Them directories are registered in the same order on the replay machine, the replay will be the same as the original trace.
--cache-dir=VAL (absent=/builds/diskuv/distributions/1.0/dksdk-coder/build/xdg/cache/dkcoder)
Cache directory. Within the cache are download and build directories that can be deleted when you needed space, but otherwise should be preserved for speed. The default cache directory is %LOCALAPPDATA%DkCoder on Windows, $HOME/.local/share/dkcoder on Unix or $XDG_DATA_HOME/dkcoder if you have XDG configured. The default cache directory may be changed by in the `policy.scfg` configuration file described in the CONFIGURATION section.
--data-dir=VAL (absent=/builds/diskuv/distributions/1.0/dksdk-coder/build/xdg/data/dkcoder)
Data directory. The metadata database containing installations and packages will be stored within the data directory. The default data directory is %LOCALAPPDATA%DkCoder on Windows, $HOME/.local/share/dkcoder on Unix or $XDG_DATA_HOME/dkcoder if you have XDG configured. The default data directory may be changed by in the `policy.scfg` configuration file described in the CONFIGURATION section.
-o DESTDIR, --output-dir=DESTDIR (absent=target)
Save the target-specific executables to DESTDIR. The default is to place the executables in `target/`.
--pwd=VAL (absent=/builds/diskuv/distributions/1.0/dksdk-coder or DKCODER_PWD env)
The directory of the shell or other user process that launched DkCoder. Defaults to the environment value DKCODER_PWD or failing that the current directory. The wrapper scripts ./dk and ./dk.cmd will set the DKCODER_PWD value to the current directory that was active at the time the ./dk and ./dk.cmd wrappers were invoked.
Adhoc modules
The contents of a script module can be specified on the command line instead of pre-existing on the filesystem. These options are `--struct` (`-S`), `--struct-url` (`-s`), `--expr-unit` (`-U`) and `--expr-unit-url` (`-u`). Each of these options is transformed and then concatenated into a single "adhoc" script module.
Your adhoc script module is treated just like any other `You` module. In particular, your adhoc script module has access to all your project code and to external `Us` and `Them` libraries.
The main use-case is to quickly try out expressions and code without having to go to a text editor or IDE. In particular, a user should be able to copy-and-paste a multi-line `dk Run -S "..."` command line from an intranet/Internet site, and have it behave identically regardless of how their browser handles tabs and newlines.
If you are posting a multi-line `dk Run -S "..."` command line, follow the format below to make it work in both PowerShell and a standard Unix shell. The format relies enclosing each `-S` and `-E` option with a double quote; most shells allow double quotes across multiple lines.
dk -S "
(* the contents of a struct without any double quotes *)
let print_endline = Tr1Stdlib_V414Io.StdIo.print_endline
" -U "
(* the expression without any double quotes or exclamation points.
Use {|...|} for strings. *)
print_endline {|okay line 1|};
print_endline {|okay line 2|}
" Run
-s URL, --struct-url=URL
Enqueue the contents of URL to an adhoc script module that will be passed to the DkCoder tool. The contents of URL must be OCaml code that can be placed as the `...` in a `struct ... end` construct. Often URL is a conventional `.ml` file. See the documentation section ADHOC MODULE SEQUENCING for how this option contributes to the adhoc module.
-S STRUCTURE, --struct=STRUCTURE
Enqueue STRUCTURE for addition to an adhoc script module that will be passed to the DkCoder tool. The STRUCTURE must be OCaml code that can be placed as the `...` in a `struct ... end` construct. See the documentation section ADHOC MODULE SEQUENCING for how this option contributes to the adhoc module.
-u URL, --expr-unit-url=URL
Enqueue the contents of URL to an adhoc script module that will be passed to the DkCoder tool. The contents of URL must be OCaml code that can be placed as the `...` in a `let () = ...` unit expression statement. Conventional `.ml` files are not expressions, so they can't be used in this command line option. See the documentation section ADHOC MODULE SEQUENCING for how this option contributes to the adhoc module.
-U UNIT_EXPRESSION, --expr-unit=UNIT_EXPRESSION
Enqueue UNIT_EXPRESSION for addition to an adhoc script module that will be passed to the DkCoder tool. The UNIT_EXPRESSION must be OCaml code that can be placed as the `...` in a `let () = ...` unit expression statement. See the documentation section ADHOC MODULE SEQUENCING for how this option contributes to the adhoc module.
Adhoc module sequencing
The sequence of steps to create the adhoc module is as follows:
The adhoc script module starts out empty.
1. Gather all the `--struct` (`-S`) from left to right as they appear on the command line, and then gather the `--struct-url` (`-s`). Each `--struct-url` (`-s`) is fetched if the execution policy allows it, but otherwise the contents of the fetched `--struct-url` are treated identically to a `--struct`.
2. Each `--struct`, and the content of each fetched `--struct-url`, are formatted with the ocamlformat code formatter for copy-and-paste resiliency, and then added to the adhoc module.
3. Gather all the `--expr-unit` (`-U`) from left to right as they appear on the command line, and then gather the `--expr-unit-url` (`-u`). Each `--expr-unit-url` (`-e`) is fetched if the execution policy allows it, but otherwise the contents of the fetched `--expr-unit-url` are treated identically to a `--expr-unit`.
4. Each `--expr-unit`, and the content of each fetched `--expr-unit-url`, are converted to `let __init (context: DkCoder_Std.Context.t) = (* the expression *); __init context` statements, then formatted with the ocamlformat code formatter, and then added to the adhoc module.
5. The adhoc module is saved into a cache directory as `ZzZz_Zz/Adhoc.ml`. The cache directory is keyed by the contents of the adhoc module. Because the contents were processed with a code formatter, the cache contents should be identical across different users even if copy-and-pasted from an intranet/Internet forum.
Configuration
Configuration files for DkCoder are not automatically created; the default configuration should be sufficient for typical situations. If you would like to customize DkCoder, you can create or edit the `policy.scfg` configuration file in one of two places
System configuration
System configuration always overrides user configuration. This is different from other applications you have used, but lets the system administrator (a role common in the Windows world) to define policy. The system `policy.scfg` configuration file is at %ProgramData%DkCoderpolicy.scfg on Windows or /etc/dkcoder/policy.scfg on Unix. You may create it if it doesn't exist.
User configuration
The user `policy.scfg` configuration file is at %LOCALAPPDATA%DkCoderpolicy.scfg on Windows, $HOME/.config/dkcoder/policy.scfg on Unix or $XDG_CONFIG_HOME/dkcoder/policy.scfg if you have XDG configured. You may create it if it doesn't exist.
The schema is as follows:
dk.exe {
cache {
# Any @USER@ tokens will be replaced by the username (currently `root`).
dir 'C:cache@USER@'
}
data {
# Any @USER@ tokens will be replaced by the username (currently `root`).
dir 'C:data@USER@'
}
}
The scfg format of the configuration file is available at https://git.sr.ht/~emersion/scfg
Build mode
DkCoder's build mode (`-O` or `--optimize`) controls how fast and how big the generated executables are.
Debug
Optimizations off and debug symbols on (default). The generated Windows executables can only run on PCs with Visual Studio installed.
ReleaseSmall
Size optimizations on and debug symbols off. The size optimizations remove dead functions and OCaml back traces.
ReleaseWithDebugInfo
Size optimizations on and debug symbols on except Windows targets have minimal debug symbols. The size optimizations remove dead functions for non-Windows targets and removes OCaml back traces. For Windows targets a PDB file will be generated; however, Microsoft C runtime debug symbols are off and most C libraries will have no debug symbols. For Windows, the advantage of ReleaseWithDebugInfo compared to Debug is ReleaseWithDebugInfo's generated Windows executables do not need Visual Studio installed.
The DkCoder build mode was influenced by Zig's build mode. However, unlike Zig, DkCoder has no modes that reduce safety checks.
-O BUILDMODE, --optimize=BUILDMODE (absent=Debug)
Build mode. `Debug` is optimizations off and debug information enabled (default). `ReleaseSmall` is size optimizations on and debug information disabled. `ReleaseWithDebugInfo` is size optimizations on and debug information enabled except the Windows targets where minimal debug information enabled. BUILDMODE must be one of Debug, ReleaseSmall or ReleaseWithDebugInfo. Similar to Zig's build mode with no mode but no lowering of safety.
Compiler options
The dk executable will supply these options to the Exe tool.
--base-sitelib-dir=VAL (required)
Findlib site library directory for the base system packages. The base system packages must include the ocaml-system packages in a ocaml/ directory. The production DkCoder layout (that is, what is unpacked when you do a `./dk DkRun_Project.Run`) is to bundle lib/ocaml/ alongside many lib/<otherpackage> directories. The `lib/` directory is the base site library directory.
--entry-module-file=FILE
The location of the entry script module, if known. If it is a relative path, it is related to the --pwd option (which defaults to the current directory).
--ext-sitelib-dir=EXTENDED_SITELIB_DIR
Findlib site library directory for the extended system packages. This is an optional set of packages that are layered on top of the base system packages. Packages in the extended system can override packages in the base system. One use of the extended system is during internal DkCoder development, where the base is just the ocaml-system packages while the extended system are the other system packages. Another use in the future is to do provisional upgrades. Updates would be downloaded to EXTENDED_SITELIB_DIR, and a last-known-good rollback would be to not unset the --ext-sitelib-dir option.
--helpers-dir=VAL (required)
Helpers directory. Contains the OCaml binaries and C shared libraries.
--host-abi=ABI (required)
Application binary interface (ABI) for the host (current) machine. Whether running a script or cross-compiling a native executable, the generated code will self-identify, through any call to DkCoder_Std.Context.abi, as the ABI given in Exe.
--host-os=OS (required)
Operating system for the host (current) machine. Whether running a script or cross-compiling a native executable, the generated code will self-identify, through any call to DkCoder_Std.Context.os, as the operating system given in Exe.
--preclean
Cleans the build directory before running the command. Ordinarily the build directory is re-used for the same command as a performance optimization. However, re-using compiled artifacts in a build directory can cause `make inconsistent assumptions over interface` errors in OCaml if something outside of `./dk` compiles incompatible code inside these directories. Enable this for CI and when you get any `inconsistent assumptions` errors.
--project-dir=VAL (absent DKCODER_PROJECT_DIR env)
The project directory. Defaults to the current directory. Running `./dk` and `./dk.cmd` (but not `dk`) sets the current directory to the directory containing `./dk` and `./dk.cmd`, so that directory is the default project directory. The dune-ide generator places the Dune output into the `_build` subfolder of the project directory. There are and will be many other uses of the project directory.
--project-name=VAL (required)
The name of the project.
--recompile-project-name=VAL
(Advanced) The name of the project being recompiled.
--share-dir=VAL (required)
Share directory. Contains Us system scripts, themes and other static assets.
--tool-env-url-base=VAL
(Advanced) The environment URL base used to launch the DkCoder tool if and only if 'Env' was the tool's unit (ex. './dk StdStd_Env.Run' for the Run tool). The environment variable DKTOOL_ENV_URL_BASE is used by './dk' but the DKTOOL_ENV_URL_BASE environment variable does not propagate to the tool module (ex. StdStd_Env.Run) as a safety measure. Instead ./dk sets this option and the URL base can be accessed through the [DkCoder_Std.Version] module.
--you-dir=VAL
A directory containing libraries of You scripts for the project. More than one directory may be specified by repeating the --you-dir option. If none are specified, defaults to the `{sources: you: []}` JSON array in `dkproject.jsonc`. If the JSON array is not present or is empty, then defaults to the `src/` subdirectory of the project directory. All script directories must be descendants of the project directory.
Generators
There are a couple generators that can be selected with the --generator option.
Build script generator. Defaults to `dune-ide` unless there is a non-DkCoder `dune-project` file present in the project directory.
Generator: dune
This generator creates build files for the Dune build tool. All of those files are generated inside the generator build directory. This is the generator is to use for the highest reproducibility and for publishing (tweaks will be needed) to an opam repository. It is also the generator that is the most similar to an upcoming native-code generator.
Generator: dune-ide
This generator creates build files for the Dune build tool, configured to work with Merlin-based IDEs like Visual Studio Code with the OCaml Platform plugin. The generator:
- places the Dune project file `.z-dk-dune-project` inside the project tree (you should add it to `.gitignore`)
- places the Merlin configuration `.merlin` inside the project tree (you should add it to `.gitignore`)
- places `dune-file` files in `#s` inside the project tree (you should add `#s` to `.gitignore`)
- uses the conventional `_build/` subfolder for Dune compilation (you should add it to `.gitignore`)
- does not generate `.opam` files (to not pollute the project tree)
- does not generate a `.dkcodermeta` metadata file (to not pollute the project tree)
You directories
The --you-dir option, which can be repeated, takes precedence for specifying the You directories.
The project's `dkproject.jsonc`, if it is present, can contain a `{sources: you: [...]}` JSON array. The directories in the array, if non-empty, will be the You directories if the --you-dir option was not specified.
The default You directory is the project directory's `src/` folder when all other search paths are empty.
Regardless of the You search path, if you specified a script as an absolute path (ex. `./dk test/Xy_Std/Hi.ml`) then the parent directory of the library (ex. `Xy_Std`) is added as the first You directory. In the example, the `test/` directory would be the first You directory.
Common options
--color=WHEN (absent=auto)
Colorize the output. WHEN must be one of auto, always or never.
--help[=FMT] (default=auto)
Show this help in format FMT. The value FMT must be one of auto, pager, groff or plain. With auto, the format is pager or plain whenever the TERM env var is dumb or undefined.
--log-level=LEVEL
Set the verbosity of log messages. If the option is repeated the last LEVEL is used. LEVEL must be one of ERROR, WARNING, INFO or DEBUG.
--quiet
Be quiet. Takes precedence over the --log-level option.
--version
Show version information.
Debugging options
--debug-on-compile-failure
Drop into an OCaml toplevel after a build failure. Any modules that had been successfully compiled will be available in the OCaml toplevel. This feature is particularly useful when builds are failing with type errors like 'Modules do not match: ... is not included in ...'.
Advanced options
--debug-solver
Enable debug logging of the solver
--dk-exe=VAL
Set the location of the dk executable. Provides the value for [DkCoder_Std.Context.dk_exe] and is supplied by dk.exe when launching this DkCoder tool.
--dump-config
Dump build configuration.
--fixed-length-modules=VAL (absent=true)
Enable fixed length modules. Fixed length modules are represented on disk with a short module name derived from the module's fully qualified package identifier; module aliases are introduced so the shortened module names are accessed with the true module names. Currently the Dune and Merlin tools require a correspondence between source filenames and build filenames for navigation, so fixed length modules are not recommended within IDEs.
--keep-assembly
Keep assembly language source code in cache directories
Exit status
dk Exe exits with:
0
on success.
123
on indiscriminate errors reported on standard error.
124
on command line parsing errors.
125
on unexpected internal errors (bugs).
Environment
These environment variables affect the execution of Exe:
DKCODER_PROJECT_DIR
See option --project-dir.
DKCODER_PWD
See option --pwd.
See also
dk(1)