What's the purpose of a package.opam file in dune? - ocaml

The Dune documentation writes:
A typical dune project will have a dune-project and one or more .opam file at the root as well as dune files wherever interesting things are: libraries, executables, tests, documents to install, etc…
What's the purpose of this package-name.opam file at the root? As far as I can tell it has something to do with packaging things up nicely for export to opam, but if we don't want to publish our code as a package to opam, is there no other purpose for the package.opam file?

The opam file is considered by the opam CLI to be the source of truth for installing the dependencies of the project. Nowadays, we are encouraged to put our actual project dependencies in the dune-project file and let dune generate the opam file for us.
But opam itself doesn't know about dune or dune-project. If you go to a directory which contains an opam file and run opam install --deps-only to install its dependencies, it looks for the listing in the opam file.

Related

I can't manage to install any ocaml modules

I'm trying to install a linear programming solver in ocaml, but i can't get any modules to work
I found this : https://github.com/Gbury/Ocaml-simplex
and this : https://github.com/smimram/ocaml-glpk
I'm just following what's written to do (make / make install) but evertytime after the installation, when i try to open Glpk or open Simplex it's not working. In fact even the examples files included are not working
I don't know if I'm doing something wrong or if those modules are just not working
Please help me I'm becoming mad.
Additional information on my problem:
I'm on Debian, the last version I guess. To install the modules, I downloaded the zip files, i installed the required modules, then unzipped, ./configure, make, make install, just what's written on GitHub.
But then when i open an example test files, which begins by open Glpk (the module I'm trying to install), when i try to compile i've an error :
open Glpk
^^^^
Error: Unbound module Glpk
I mean I've found 3 modules to make linear programming solver:
https://github.com/OCamlPro-Iguernlala/ocplib-simplex
https://github.com/Gbury/Ocaml-simplex
https://github.com/smimram/ocaml-glpk
And I can't get any of them to work.
From your description, your issue is not installing libraries but using them. Once you have installed a library, you still need to inform the compiler on where are the compiled interfaces files (.cmi) for this library, and which object files should be linked (aka .cm(x)o and .cm(x)a) .
It will be much easier to let a package manager (like opam) and a build tool (like dune) take care of those minutiæ.
For instance, with opam and dune, you can first install ocplib-simplex:
opam install simplex
Then building a executable from a main.ml file using this library can be done with a dune file
(executable (name main) (libraries ocplib-simplex) )
and a call to
dune build main.exe
I managed to get Glpk working in no time. I think you should learn a bit about OPAM, the OCaml Package Manager. Its purpose is exactly to make library installation as easy as possible. Here's a step-by-step:
Prerequisites
I'm going to assume you have OPAM installed. If this is not the case, you'll find the information you need here. The easiest way to get OPAM working is to execute the following in a shell:
sh <(curl -sL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh) && \
opam init && \
eval $(opam env)
Installing Glpk
First, I tried to install Glpk as is.
opam install glpk
This failed with following error:
<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
┌─ The following actions failed
│ λ build glpk 0.1.8
└─
╶─ No changes have been performed
The packages you requested declare the following system dependencies. Please make sure they are installed before retrying:
libglpk-dev
This is because the OCaml library Glpk is actually a binding to a system library which needs to be installed separately. On Debian, you can use apt to do this.
sudo apt install libglpk-dev
Then I retried installing Glpk.
opam install glpk
It succeeded. Great.
Requiring Glpk
Now that the Glpk library is installed, you need to require it in order to make it available.
In a REPL
When using the OCaml toplevel, you need to use the following commands in order to require Glpk.
#use "topfind";;
#require "glpk";;
In a project
If you want to use libraries in a fully-fledged project (with multiple files, executables and whatnot), you should use a build system. The most popular OCaml build system today is Dune, which Octachron covered in his answer.
Further considerations
You should consider reading Real World OCaml, a great book that teaches from the ground up everything you need to know to build software with OCaml.

In Opam how do I tell which package owns a given file?

In Opam how do I tell which package owns a given file under ${OPAMROOT}/system? If there is no direct mean to achieve this, is it possible to list files owned by a given package?
The following command lists all packages and the files included in them - one can dump the output to a file to search it.
opam list -s | xargs -t -n 1 opam show --list-files > files.txt 2>&1
(tested with opam 2.1)
There is no such facility, as opam allows packages to install their files virtually everywhere. But in general structure is quite simple and most packages respect it:
Each installed package has its own subfolder in lib, etc, doc and share.
For each package opam creates an entry install/<package-name>.install that may contain files that this package installed, if special facilities, provided by opam, were used in the installation process.
binaries are put into bin or sbin without further subdivision.

How can I add a local library into my Leinigen project?

I'd like to add a local library of utilities that I wrote to my project in Leinigen without having to make jars of the library, or without copying the code.
Is that possible?
You can use the checkouts feature of leiningen to add a symbolic link to the project directory containing the library.
cd project-dir # where the project.clj file is
mkdir checkouts
ln -s ~/library/project/dir/ checkouts/library-name
Then add a dependency to the project.clj file
EDIT: If your included code is not it's own project then perhaps including the source directly with git submodules is an option, though some would recommend making it a project that can have a version. It's also worth considering running lein install to build jars and put them in your local maven repo since it only takes two words.
ps: i'm assuming your library is a clojure project.

How to use ocamlbuild with OPAM in ocaml?

I wrote two libraries (Bson.ml and Mongo.ml) in ocaml.
I wish to enable it for opam.
In the instruction of opam, it says it needs make build and make install.
I am always using ocamlbuild and ocamlfind to build and install my library locally.
How can I produce a make file for opam?
Just do it the way other packages do it. For example the sequence package also uses vanilla ocamlbuild and ocamlfind. Here's the opam package description for it: https://github.com/OCamlPro/opam-repository/tree/master/packages/sequence.0.3.4 The opam file specifically.

How can I build libpoppler from source?

I just download poppler to Linux system,and I want to incorporate it in my app to parse pdf file.
(My goal is to convert pdf file to plain text.)
How can I do this?
Poppler's git tree includes a useless INSTALL doc that just tells you to run ./configure, but they don't include automake/autoconf auto-generated files (including configure) in git. (Probably they do include them in tarball source releases.)
I just built poppler from git source (on Ubuntu 15.04) like so:
git clone --depth 50 --no-single-branch git://git.freedesktop.org/git/poppler/poppler
cmake -G 'Unix Makefiles' # other -G options are to generate project files for various IDEs
# look at the output. If it didn't find some libraries,
# install them with your package manager and re-run cmake
make -j4
# optionally:
sudo make install
It appears that they maintain an autoconf/automake build setup, so you can use that OR cmake to create a Makefile.
If you just want to see if the latest git poppler works better than the distro package, you don't need to sudo make install, you can just run utils/pdftotext or whatever right from the source directory. It apparently tells the linker to embed the build path into the binary, as a library search path, so running /usr/local/src/poppler/utils/pdftotext works, and finds /usr/local/src/poppler/libpoppler.so.52.
If the latest poppler does work better than the distro-packaged poppler, you should install it to /usr/local/bin with sudo make install. When you upgrade to the next version of your distro, check your /usr/local. Often the new distro version will be newer than when you built it from source, so you should just remove your version from /usr/local/{bin,share,lib,man,include}. (Or make uninstall in the source dir, if supported).
Their website explains it very clearly :
Poppler is available from git. To clone the repository use the following command:
git clone git://git.freedesktop.org/git/poppler/poppler
Once you download the source code, read the INSTALL file where it says :
cd to the directory containing the package's source code and type
./configure to configure the package for your system.
Type `make' to compile the package.
Type `make install' to install the programs and any data files and
documentation.
Since some time has passed and it seems there was some uncertainty, I also took a look.
At the end of 2021, their homepage says
We run continuous integration via the gitlab CI
I checked out their .gitlab-ci.yml which has many build tasks. It would seem these days we build libpoppler like this:
git clone git://git.freedesktop.org/git/poppler/test test.repo
mkdir -p build && cd build
cmake -DTESTDATADIR=`pwd`/../test.repo -G Ninja ..
ninja