Building "hello world" with ocaml dune - ocaml

Following the tutorial at https://dune.readthedocs.io/en/stable/quick-start.html, I created a file hello_world.ml containing
print_endline "Hello, world!"
and a file dune containing
(executable
(name hello_world))
and then typed
dune build hello_world.exe
but it complains about errors in other (completely unrelated) files.
Is it possible that dune looks at other files even though they are not mentioned in the dune file (even recursively) ? And how to prevent it ?

Yes, dune will search for all files that have *.ml or *.re files in the current folder. To disable this behavior use the modules stanza and explicitly specify which modules comprise your executable. For example, if it is made of hello_world.ml and utilities.ml compilation units, then the following specification will work for you,
(executable
(name hello_world)
(modules hello_world utilities))

Related

How to specify requirements for versions of dependencies for ocaml dune projects?

Say I run dune init proj hello_world, then modify the bin/dune and bin/main.ml files so that a new dependency foobar is in use.
The bin/dune file now:
(executable
(public_name hello_world)
(name main)
(libraries hello_world foobar))
1. How can I specify that the foobar library should have a certain exact version?
2. If we know that the foobar library uses semantic versioning, how could I specify that any version with the major version as 3 is required?
ocaml version 4.14.0
dune version 3.6.1
Package dependencies for a project are specified in the dune-project file, see https://dune.readthedocs.io/en/stable/dune-files.html#package
A default dune-project file is generated by dune init proj ....
You will and with a line in the depends field like
(depends
(foobar (>= 3))
Note that this is needed in addition to specifying which libraries the executable depends on.

Using an executable as library in dune

I'm building a project using dune and I'm facing the following situation. Essentially, my project depends on another project, not developed by me, from which I want to use some parts of the source code.
Here is more or less my project tree
my_project/
|---dune-project
|---src/
|---dune
|---extrenal-project/
|---dune-project
|---dune
|---src/
|---dune
|---src-file.ml
The problem is that external-project has a dune file that builds it as an executable, instead of a library. Therefore, I cannot include it as a library in my dune file. Ideally, I don't want to modify the dune file inside of external-project.
I've tried a series of combinations. Here is the current status of my dune file:
(dirs external-project)
(executable
(name myexec)
(libraries containers)
(modules Myexec)
(promote (until-clean) (into ".."))
)
(env (dev (flags (:standard -warn-error -A))))
Which allows me to compile external-project correctly, but then does not allow me to reference any of its files. I also tried (dirs external-project external-project/src) but to no success.
So, my question is: is there a way for me to reference external-project as a library, even though it is build as an executable?
Thanks for your help!

What is the namespace in utop for definitions in .ml file listed in dune's (executable ...) rule?

What is the namespace of the test0 definition in dune utop . environment, given the following ocaml code with dune build definition:
~/_ocaml_/n01$ cat bin/dune
(executable
(public_name n01)
(name main))
~/_ocaml_/n01$ cat bin/main.ml
let test0 = "World!"
let () = print_endline ("Hello " ^ test0)
When started the code produces expected result:
~/_ocaml_/n01$ dune build
~/_ocaml_/n01$ dune exec n01
Hello World!
I believe that your issue is that you cannot access the test0 variable after launching dune utop ..
If I am not mistaken, your issue stems from the fact that the dune top <path> command loads libraries and not executable. Thus in your setup, the N01 module is not part of the utop environment. The idiomatic solution is to split your source files into a library and a small executable that only contains glue code.

generating OCaml documentation with dune

I'm in an internal package of my project and I ran dune build #doc. It does not complain and runs to completion. I then check the /_build/default directory and no _doc directory exists.
Am I missing something?
You should simply be able to generate docs by running dune build #doc if you have foo.ml, foo.opam, and a dune file containing simple contents. For example,
(library
(name foo))
To answer the question, the package name should be related to the library name or public name (if added as a stanza to the dune file).

Copying a newly builded file as a Dune's stanza

I have written a library in OCaml with all of its sources located in lib folder.
I also prepared "facade" executables in bin folder.
Now I would like to prepare some examples how to use the above mentioned executables.
To do this I need to either copy an executable beforehand or (preferably) tell Dune to use a newly created one after build.
And here is my question.
Dune's copy_files stanza does not allow1 me to copy from _build folder.
Is there any other way to use fresh executables each time after building or do I need to copy them at some point and keep up to date?
Below is the structure of the project (in case verbal description was misleading in any way).
root
lib <- source
bin <- frontend for source
examples <- how to use the above frontend
1 By not allow I mean the following usage of this stanza:
( copy_files %{project_root}/_build/default/bin/program.exe )
A solution, as suggested by #Lhooq, might be to use dune exec command with --root parameter.
In regard to the provided scenario, if we make a script:
dune exec --root=../.. ./bin/some_program.exe
(*
Where 'some_program' is the name of an .ml file located in bin folder.
I assumed here that the program is compiled to native code, not to bytecode (hence the .exe).
*)
and place it in examples directory, then by invoking it we will actually run the latest build of the program defined in some_program.ml located in bin folder.
And just to make things clear: bin folder does NOT contain any compiled files (neither .exe nor .bc) .