### Executable Installation Example Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/executable.md An example showing how an `executable` stanza relates to an `install` stanza for public installation. ```dune (install (section bin) (files (.exe as ))) ``` -------------------------------- ### Running the Example Source: https://github.com/ocaml/dune/blob/main/doc/sites.md Command to build and execute the example application, demonstrating plugin loading and execution. ```console $ dune build @install && dune exec ./app.exe Registration of Plugin1 Main app starts... Plugin1 is doing something... ``` -------------------------------- ### Basic Install Stanza Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/install.md Installs the file 'hello.txt' to the 'share' section of the 'mypackage'. ```dune (install (files hello.txt) (section share) (package mypackage)) ``` -------------------------------- ### Setting up a relocatable OCaml switch Source: https://github.com/ocaml/dune/wiki/dev-meeting-2025-03-19 Commands to add a repository for relocatable OCaml, create a new switch with it, and install build tools. This setup allows the OCaml environment to be moved or copied to different locations. ```shell $ opam repo add --dont-select relocatable git+https://github.com/dra27/opam-repository.git#relocatable $ mkdir test && cd test $ opam switch create . --repos=relocatable,default ocaml.5.3.1 $ opam install ocamlbuild ocamlfind ``` -------------------------------- ### Build All Installable Components with Dune Source: https://github.com/ocaml/dune/blob/main/example/hello_world.t/README.md Use this command to build all components of the project that are intended for installation. ```bash dune build @install ``` -------------------------------- ### Dynamically Load Installed Plugins Source: https://github.com/ocaml/dune/blob/main/doc/advanced/findlib-dynamic.md Use OCaml code to find and load all installed plugins for your tool. This example filters packages based on a naming convention and loads them using `Fl_dynload.load_packages`. ```ocaml let () = Findlib.init () let () = let pkgs = Fl_package_base.list_packages () in let pkgs = List.filter (fun pkg -> 14 <= String.length pkg && String.sub pkg 0 14 = "mytool-plugin-") pkgs in Fl_dynload.load_packages pkgs ``` -------------------------------- ### Install Source Tree Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/install.md Use `source_trees` to install entire directories. The content is installed under `/doc//` followed by the directory name. ```dune (install (section doc) (source_trees manual)) ``` -------------------------------- ### Get Version from Git Repository Source: https://github.com/ocaml/dune/blob/main/doc/dune-libs.md Example Git command to describe the current state of a repository, producing a human-readable version string. ```console $ git describe --always --dirty --abbrev=7 ``` -------------------------------- ### Rule to Generate Installable File List Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/install.md Generates 'foo.sexp' containing a list of files from the 'resources' subdirectory, intended for inclusion in an install stanza. ```dune (rule (deps (source_tree resources)) (action (with-stdout-to foo.sexp (system "echo '(' resources/* ')')"))) ``` -------------------------------- ### Install Files with `install` Stanza Source: https://context7.com/ocaml/dune/llms.txt Use the `install` stanza to copy build artifacts and files into the installation prefix under specified sections. Supports renaming and globbing. ```dune ; Install a data file to /share/mypackage/ (install (files data/config.json) (section share) (package mypackage)) ; Install with a renamed destination (install (section share_root) (files (mylib.el as emacs/site-lisp/mylib.el))) ; Install using globs and preserve directory structure (install (files (glob_files style/*.css) (glob_files_rec content/*.html)) (section share)) ; Install a whole source directory tree (install (section doc) (source_trees manual examples)) ; Install with path rewriting via with_prefix (install (files (glob_files (style/*.css with_prefix web/stylesheets))) (section share)) ``` -------------------------------- ### Install ppx_deriving Source: https://github.com/ocaml/dune/blob/main/doc/tutorials/developing-with-dune/using-ppx.md Install the ppx_deriving library using opam. This is a prerequisite for using its features in your project. ```shell opam install ppx_deriving.5.2.1 ``` -------------------------------- ### Install with Renamed File Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/install.md Installs 'mylib.el' to a custom destination 'emacs/site-lisp/mylib.el' within the 'share_root' section. ```dune (install (section share_root) (files (mylib.el as emacs/site-lisp/mylib.el))) ``` -------------------------------- ### Install Dune using opam Source: https://github.com/ocaml/dune/blob/main/README.md Recommended installation method for Dune using the opam package manager. Ensure opam is configured correctly. ```console $ opam install dune ``` -------------------------------- ### Install Files from Parent Directories Safely Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/install.md When installing files from parent directories using globs, use `with_prefix .` to ensure they are installed relative to the target directory, avoiding deprecated `..` paths. ```dune (install (files (glob_files (../*.txt with_prefix .))) (section share)) ``` -------------------------------- ### Install Test Dependencies Source: https://github.com/ocaml/dune/blob/main/doc/hacking.md Installs only the dependencies required for running tests, marked with {with-dev-setup} in Dune's opam file. ```console $ make dev-deps ``` -------------------------------- ### Include External File List in Install Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/install.md Includes files listed in 'foo.sexp' for installation into the 'share' section. ```dune (install (files (include foo.sexp)) (section share)) ``` -------------------------------- ### Install Files with Globs Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/install.md Use `glob_files` for single-directory globs and `glob_files_rec` for recursive globs within the `files` field of an install stanza. ```dune (install (files (glob_files style/*.css) (glob_files_rec content/*.html)) (section share)) ``` -------------------------------- ### Install Melange with opam Source: https://github.com/ocaml/dune/blob/main/doc/melange.md Use opam to install the Melange compiler. ```console $ opam install melange ``` -------------------------------- ### Install Files to a Site Source: https://github.com/ocaml/dune/blob/main/doc/sites.md Use the install stanza to add files to a defined site. Files can be aliased to specific paths within the site's directory structure. ```dune (install (section (site (mygui themes))) (files (layout.css as default/layout.css) (ok.png as default/ok.png) (ko.png as default/ko.png))) ``` ```dune (install (section (site mygui themes)) (files (layout.css as material/layout.css) (ok.png as material/ok.png) (ko.png as material/ko.png))) ``` -------------------------------- ### Named Dependencies Example Source: https://github.com/ocaml/dune/blob/main/doc/concepts/dependency-spec.md This example demonstrates how to use named dependencies to organize CSS, JavaScript, and image files for a bundle command. Named dependencies can be referenced within actions. ```dune (rule (target archive.tar) (deps index.html (:css (glob_files *.css)) (:js foo.js bar.js) (:img (glob_files *.png) (glob_files *.jpg))) (action (run %{bin:bundle} index.html -css %{css} -js %{js} -img %{img} -o %{target}))) ``` -------------------------------- ### Install Js_of_ocaml Compiler Source: https://github.com/ocaml/dune/blob/main/doc/jsoo.md Install the Js_of_ocaml compiler using opam. ```console opam install js_of_ocaml-compiler ``` -------------------------------- ### Install Source Tree with Alias Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/install.md Use the `as` keyword with `source_trees` to change the destination path. `(manual as .)` installs the contents of `manual` directly into the section directory. ```dune (install (section doc) (source_trees (manual as .))) ``` -------------------------------- ### Start Dune in Watch Mode Source: https://github.com/ocaml/dune/blob/main/otherlibs/dune-rpc-lwt/examples/rpc_client/README.md To use the RPC client, first start Dune in watch mode. This command initiates the RPC server. ```bash $ dune build --watch ``` -------------------------------- ### Getting Installed Package File Path with `pkg` Source: https://github.com/ocaml/dune/blob/main/doc/concepts/variables.md Use `pkg::
:` to get the path of a file installed by a package. It supports workspace, lock-file, and installed packages across various sections. ```dune %{pkg:my-package:lib:my_lib.ml} ``` -------------------------------- ### Getting Library File Path with `lib` Source: https://github.com/ocaml/dune/blob/main/doc/concepts/variables.md Use `lib::` to get the installation path of a file within a public library. It prioritizes local workspace files over installed ones. ```dune %{lib:my-lib:path/to/file.ml} ``` -------------------------------- ### Navigate to Project Directory Source: https://github.com/ocaml/dune/blob/main/doc/quick-start.md After initializing your project, change into the newly created project directory to begin working on it. ```console $ cd project_name ``` -------------------------------- ### Composing with Installed Theories Source: https://github.com/ocaml/dune/blob/main/doc/rocq.md Example of defining a Rocq theory that depends on an installed theory like `mathcomp.ssreflect`. ```dune (rocq.theory (name my_mathcomp_theory) (theories mathcomp.ssreflect)) ``` -------------------------------- ### Set up Dune development environment with Nix Source: https://github.com/ocaml/dune/blob/main/doc/hacking.md Use `nix develop` to set up the development environment. For a persistent setup, use the `--profile` flag to save the state. ```sh nix develop --profile nix/profiles/dune ``` ```sh nix develop nix/profiles/dune ``` -------------------------------- ### Initialize New Dune Project Source: https://github.com/ocaml/dune/blob/main/doc/usage.md Use this command to set up a new Dune project with specified libraries and inline test support. It creates a directory structure and necessary Dune files. ```console $ dune init proj myproj --libs base,cmdliner --inline-tests --ppx ppx_inline_test ``` -------------------------------- ### Dune Comment Example Source: https://github.com/ocaml/dune/blob/main/doc/reference/lexical-conventions.md Demonstrates how to use end-of-line comments in Dune configuration files. Comments start with a semicolon and extend to the end of the line. ```dune ; This is a comment ``` -------------------------------- ### Initialize New Library Component Source: https://github.com/ocaml/dune/blob/main/doc/usage.md Create a new library component in a specified directory. This command sets up the library with its name, dependencies, and public visibility. ```console $ dune init lib mylib src --libs core --inline-tests --public ``` -------------------------------- ### Get Developer Tool Executable Path with Dune Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune-tools.md Use 'dune tools which' to print the path to a developer tool's executable. By default, it errors if the tool is not installed. The '--allow-not-installed' flag prints the path where the tool would be installed, even if it doesn't exist yet. ```console $ dune tools which [--allow-not-installed] ``` ```console $ dune tools which ocamlformat ``` ```console _build/.dev-tools.locks/ocamlformat/ocamlformat/target/bin/ocamlformat ``` ```console $ dune tools which merlin --allow-not-installed ``` ```console _build/.dev-tools.locks/merlin/merlin/target/bin/ocamlmerlin ``` -------------------------------- ### Dune Promote Command Example Source: https://github.com/ocaml/dune/blob/main/doc/tests.md Demonstrates the 'dune promote' command after a test run shows a diff between expected and actual output. This command updates the expected test files with the new output. ```console $ dune runtest [...] -tests.expected +tests.output File "tests.expected", line 1, characters 0-1: -Hello, world! +Good bye! $ dune promote Promoting _build/default/tests.output to tests.expected. ``` -------------------------------- ### Bootstrap and Build Dune Source: https://github.com/ocaml/dune/blob/main/doc/hacking.md This command bootstraps Dune if necessary and then builds the project using `./dune.exe build @install`. It's the primary command for development. ```console $ make dev ``` -------------------------------- ### MDX Stanza: Preludes Field Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/mdx.md Provide prelude files to be passed to MDX. These files can contain setup code or definitions used across multiple documentation examples. ```dune (preludes ) ``` -------------------------------- ### Exclude multiple directories with Dune dirs stanza Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/dirs.md Combine glob patterns with set difference to exclude multiple directories. This example excludes directories starting with `test` or `foo`. ```dune (dirs :standard \ test* foo*) ;; exclude all dirs that start with test or foo ``` -------------------------------- ### Using PPX Rewriters with Flags Source: https://github.com/ocaml/dune/blob/main/doc/reference/preprocessing-spec.md Example of specifying PPX rewriters and command-line flags. Libraries are listed first, followed by flags, with `--` used to separate flags that do not start with a hyphen. ```dune (preprocess (pps ppx1 -foo ppx2 -- -bar 42)) ``` -------------------------------- ### Getting Package Version with `version` Source: https://github.com/ocaml/dune/blob/main/doc/concepts/variables.md Use `version:` to expand to the version of a given package. Local packages take precedence over public ones. Packages without installed libraries might not be detected. ```dune %{version:my-package} ``` -------------------------------- ### Coq Plugin Loading in .v file Source: https://github.com/ocaml/dune/blob/main/doc/coq.md Demonstrates how to load a Coq plugin within a .v file, showing syntax variations for different Coq versions. The 'Hello.' command invokes the plugin's functionality. ```coq (* For Coq < 8.16 *) Declare ML Module "my_plugin". (* For Coq = 8.16 *) Declare ML Module "my_plugin:my-coq-plugin.plugin". (* At some point Coq 8.17 or 8.18 will transition to the syntax below, check Coq's manual *) Declare ML Module "my-coq-plugin.plugin". Hello. ``` -------------------------------- ### Initialize a Library Project with dune init proj Source: https://context7.com/ocaml/dune/llms.txt Scaffold a new OCaml library project named 'mylib' using the '--kind=lib' flag. ```console dune init proj --kind=lib mylib ``` -------------------------------- ### Example of dynamic_include in Dune Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/dynamic_include.md Demonstrates how to use the dynamic_include stanza to include a file that is a target of a rule. This setup prevents rule loading cycles by splitting dynamic rule loading and generation into different directories. ```dune (subdir b (dynamic_include ../a/foo.inc)) (subdir a (rule (write-file foo.inc "(rule (write-file file bar))" ))) ``` -------------------------------- ### Run Dune Formatter Source: https://github.com/ocaml/dune/blob/main/doc/howto/formatting.md Execute `dune build @fmt` to format source files and view differences. Use `dune promote` to accept changes or `dune build @fmt --auto-promote` (or `dune fmt`) to combine formatting and promotion. ```console $ dune build @fmt --- hello.ml +++ hello.ml.formatted @@ -1,3 +1,3 @@ -let () - = print_endline - "hello, world" +let () = print_endline "hello, world" ``` ```console $ dune promote Promoting _build/default/hello.ml.formatted to hello.ml. ``` ```console $ dune build @fmt --auto-promote ``` ```console $ dune fmt ``` -------------------------------- ### Install Packages Manually with Dune Source: https://github.com/ocaml/dune/blob/main/doc/usage.md Manually install packages using the 'dune install' command. If no packages are specified, all available packages in the workspace are installed. ```console $ dune install [PACKAGE]... ``` -------------------------------- ### Build Commands with Profiles Source: https://context7.com/ocaml/dune/llms.txt Demonstrates how to invoke Dune builds using different profiles. ```console $ dune build # uses "dev" profile (default) $ dune build --profile release # uses "release" profile $ dune build --profile ci # uses a custom "ci" profile ``` -------------------------------- ### Initialize an Executable Project with Dune Source: https://github.com/ocaml/dune/blob/main/doc/quick-start.md Use this command to create a new project that will build an executable program. Replace `project_name` with your desired project name. ```console $ dune init proj project_name ``` -------------------------------- ### Build and Install Dependencies Only Source: https://github.com/ocaml/dune/blob/main/doc/tutorials/dune-package-management/setup.md Build and install project dependencies without building the project itself, useful for caching dependency installations. ```sh $ dune build @pkg-install ``` -------------------------------- ### Install Dune to a Specific Directory Source: https://github.com/ocaml/dune/blob/main/doc/make-help.txt Installs Dune to a specific directory by setting the PREFIX variable during the 'make install' command. ```makefile PREFIX=/path/to/install make install ``` -------------------------------- ### Install Dune Source: https://github.com/ocaml/dune/blob/main/doc/make-help.txt Installs Dune on your system after building it in release mode. You can specify a custom installation directory using PREFIX. ```makefile make install ``` -------------------------------- ### Build and Serve Dune Manual Source: https://github.com/ocaml/dune/blob/main/doc/make-help.txt Builds the Dune manual and serves it locally. This is useful for previewing documentation changes. ```makefile make livedoc ``` -------------------------------- ### Verify Dune Installation Source: https://github.com/ocaml/dune/blob/main/doc/howto/install-dune.md Confirm that Dune has been successfully installed by checking its version. This command should output the installed Dune version. ```console $ dune --version 3.12.1 ``` -------------------------------- ### Cram Test File Example Source: https://context7.com/ocaml/dune/llms.txt A sample Cram test file demonstrating shell commands and their expected outputs. ```text ; test/my_test.t (Cram test file) $ echo hello hello $ myapp --version myapp 1.0.0 $ myapp compute 1 2 3 ``` -------------------------------- ### Build Dune.install with Bootstrap Profile Source: https://github.com/ocaml/dune/blob/main/doc/explanation/bootstrap.md This command builds the `dune.install` file using the bootstrap Dune executable. It utilizes the `dune-bootstrap` profile to avoid a full build, primarily copying the bootstrap executable and generating the install file. ```bash $ ./_boot/dune.exe build dune.install --release --profile dune-bootstrap ``` -------------------------------- ### Specify Cram Setup Scripts Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune/cram.md The `setup_scripts` field lists shell files to be sourced before each Cram test execution. Each script is sourced only once, and the order of evaluation is deterministic but undefined. ```dune (cram (setup_scripts ./setup.sh ./another.sh)) ``` -------------------------------- ### Example Directory Structure and Output Source: https://github.com/ocaml/dune/blob/main/doc/melange.md Illustrates how the source directory structure maps to the build output when using the melange.emit stanza with a specific target and library. ```default ├── dune # (melange.emit (target output) (libraries lib)) ├── app.ml └── lib ├── dune # (library (name lib) (modes melange)) └── helper.ml ``` ```default output ├── app.js └── lib ├── lib.js └── helper.js ``` -------------------------------- ### Assembling Complex Actions with Action_builder Source: https://github.com/ocaml/dune/blob/main/doc/explanation/tour/rule-generation.md This example demonstrates assembling a complex build action using combinators from `Action_builder.With_targets`. It combines multiple actions, including running an executable, and handles dependencies and sandbox configurations. ```ocaml let mdx_action ~loc:_ = let open Action_builder.With_targets.O in let mdx_input_dependencies = (* ... *) in let executable, command_line = (* ... *) in let deps, sandbox = (* ... *) in let+ action = Action_builder.with_no_targets deps >>> Action_builder.with_no_targets (Action_builder.env_var "MDX_RUN_NON_DETERMINISTIC") >>> Action_builder.with_no_targets (Action_builder.map mdx_input_dependencies ~f:(fun d -> (), d) |> Action_builder.dyn_deps) >>> Command.run_dyn_prog ~dir:(Path.build dir) ~stdout_to:files.corrected executable command_line and+ locks = Expander.expand_locks expander stanza.locks |> Action_builder.with_no_targets in Action.Full.add_locks locks action |> Action.Full.add_sandbox sandbox in Super_context.add_rule sctx ~loc ~dir (mdx_action ~loc) ``` -------------------------------- ### Workspace Build Command and Output Source: https://context7.com/ocaml/dune/llms.txt Demonstrates building and testing against multiple OCaml versions simultaneously using a workspace file. Shows where build artifacts are placed. ```console $ dune build --workspace dune-workspace.dev @all @runtest # Builds and tests against all three OCaml versions at once # Build artifacts land in: # _build/4.14.2/ # _build/5.1.1/ # _build/5.2.0/ ``` -------------------------------- ### Install wasm_of_ocaml-compiler Source: https://github.com/ocaml/dune/blob/main/doc/wasmoo.md Install the wasm_of_ocaml compiler using opam. This is the first step to enable Wasm compilation. ```console opam install wasm_of_ocaml-compiler ``` -------------------------------- ### Install cmdliner package Source: https://github.com/ocaml/dune/blob/main/doc/tutorials/developing-with-dune/structure.md Installs the cmdliner library using opam. This is a prerequisite for command-line argument parsing. ```sh opam install cmdliner.1.3.0 ``` -------------------------------- ### Initialize an Executable Project with dune init proj Source: https://context7.com/ocaml/dune/llms.txt Scaffold a new OCaml executable project named 'myapp'. This command generates the basic directory structure and configuration files. ```console dune init proj myapp cd myapp dune build dune test dune exec myapp ``` -------------------------------- ### Install odoc Tool Source: https://github.com/ocaml/dune/blob/main/doc/documentation.md Install the odoc tool using opam, which is required for generating documentation in Dune. ```bash $ opam install odoc ``` -------------------------------- ### Build Project with Dune Source: https://github.com/ocaml/dune/blob/main/doc/tutorials/dune-package-management/setup.md Build the entire project, including downloading, building, and installing dependencies. The first build may take longer. ```sh dune build ``` -------------------------------- ### Rust Inline Test Example Source: https://github.com/ocaml/dune/blob/main/doc/faq.md Example of defining and testing a private function in Rust using inline tests. ```rust // define a private function fn foo() { ... } // test the function right next to its definition #[test] fn test_of_foo() { ... } ``` -------------------------------- ### Install a Developer Tool with Dune Source: https://github.com/ocaml/dune/blob/main/doc/reference/dune-tools.md Use 'dune tools install' to install a developer tool without running it. Dune resolves dependencies, creates a lock directory, and builds the tool. This pre-installs tools for faster subsequent 'exec' or 'which' calls. ```console $ dune tools install ``` ```console $ dune tools install ocamlformat ``` ```console $ dune tools install ocamllsp ```