How to use ocamlbuild with OPAM in ocaml? - 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.

Related

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

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.

How to build an OASIS package

I tried to build this project: https://github.com/frenetic-lang/fattire, but after installing the dependencies and running $ make, I get the error make: .\configure: Command not found
I am using omap package manager and tried to build the project with OCaml 4.03.0.
What is the problem here?
To build an OASIS package from scratch issue the following commands
oasis setup
ocaml setup.ml -configure
ocaml setup.ml -build
ocaml setup.ml -install
Usually, package maintainers make the first step themselves and commit to the repository the generated files, and often they provide a Makefile together with the ./configure scripts. In OP case the package is broken and some pregenerated files are missing, so it is easier to build it from scratch.

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.

Loris package installing issue

I'm trying to install Loris package link which is a library also for Python (my language). I have installed the package through bash commands in my OSX 10.12 system following these steps:
cd to the directory containing the package's source code and type
./configure to configure the package for your system. If you're
using csh on an old version of System V, you might need to type
sh ./configure instead to prevent csh from trying to execute
configure itself.
Running `configure' takes awhile. While running, it prints some
messages telling which features it is checking for.
Type `make' to compile the package.
Optionally, type `make check' to run any self-tests that come with
the package.
Type `make install' to install the programs and any data files and
documentation.
You can remove the program binaries and object files from the
source code directory by typing make clean. To also remove the
files that configure created (so you can compile the package for
a different kind of computer), type make distclean. There is
also a make maintainer-clean target, but that is intended mainly
for the package's developers. If you use it, you may have to get
all sorts of other programs in order to regenerate files that came
with the distribution.
The problem is that when I run both python 2.7 IDLE and the Atom editor it says that the module named Loris was not found. By default, make install will install the package's files in
/usr/local/bin,/usr/local/man.
Is this the problem? How can I install properly this library?

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.