Make compatible ocaml, camlp4, ppx, node, js_of_ocaml, ocamlbuild - ocaml

After installing npm and node, compiling OCaml files with js_of_ocaml gave errors, thus I did opam switch reinstall system:
:testweb $ opam switch reinstall system
Your system compiler has been changed. Do you want to upgrade your OPAM installation ? [Y/n] y
=-=- Upgrading system -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 🐫
[WARNING] base-ocamlbuild.base is not available because it requires OCaml >= 3.10 & < 4.03. Skipping.
[WARNING] camlp4.4.02+system is not available because your system doesn't comply with preinstalled &
ocaml-version >= "4.02" & ocaml-version < "4.03". Skipping.
[WARNING] ppx_tools.4.02.3 is not available because your system doesn't comply with ocaml-version >=
"4.02.0" & ocaml-version < "4.03.0". Skipping.
The following dependencies couldn't be met:
- deriving -> camlp4
Your request can't be satisfied:
- camlp4 is not available because your system doesn't comply with ocaml-version >= "4.04".
No solution found, exiting
The former package state can be restored with opam switch import "/Users/softtimur/.opam/backup/state-20160418124642.export" --switch system
Then, i realized ocamlfind did not work anymore:
:testweb $ ocamlfind ocamlc -package js_of_ocaml.ppx -linkpkg cubes.ml -o T
-bash: /Users/softtimur/.opam/system/bin/ocamlfind: No such file or directory
PS: node --version gives v6.1.0; npm --version gives 3.8.6; ocaml -version gives The OCaml toplevel, version 4.03.0.
js_of_ocaml --version was 2.7, but after the opam swtich reinstall system, it gives -bash: /Users/softtimur/.opam/system/bin/js_of_ocaml: No such file or directory.
Does anyone know what to do to make all these packages compatible?
Earlier: Here were the compilation errors after installing npm and node, and before doing opam switch reinstall system:
:testweb $ ocamlfind ocamlc -package js_of_ocaml.ppx -linkpkg cubes.ml -o T
Failure("Ast_mapper: OCaml version mismatch or malformed input")
File "cubes.ml", line 1:
Error: Error while running external preprocessor
Command line: /Users/softtimur/.opam/system/lib/js_of_ocaml/./ppx_js '/var/folders/fn/0rmvqqbs4k76lg5g6b7kll100000gn/T/camlppxffe989' '/var/folders/fn/0rmvqqbs4k76lg5g6b7kll100000gn/T/camlppx02e06a'
Another way also gave an error:
:testweb $ ocamlfind ocamlc -package js_of_ocaml -syntax camlp4o -package js_of_ocaml.syntax -linkpkg -o cubes.byte cubes.ml
Fatal error: unknown C primitive `caml_is_printable'
File "cubes.ml", line 1:
Error: Error while running external preprocessor
Command line: camlp4 '-I' '/usr/local/lib/ocaml/camlp4' '-I' '/Users/softtimur/.opam/system/lib/js_of_ocaml' '-parser' 'o' '-parser' 'op' '-printer' 'p' 'pa_js.cmo' 'cubes.ml' > /var/folders/fn/0rmvqqbs4k76lg5g6b7kll100000gn/T/ocamlpp8b28e8
Initially:
My initial goal is to write and compile cubes.ml such that 1) it wraps an OCaml function to make a JS function that can be called in web; 2) the OCaml function and the bytecode can be tested in a command line under Linux.
cubes.ml is as follows:
let () =
let oneArgument (a: int) = a + 100 in
Js.Unsafe.global##.jsOneArgument := Js.wrap_callback oneArgument;
print_string "hello\n";
exit 0
Then, ocamlfind ocamlc -package js_of_ocaml.ppx -linkpkg cubes.ml -o T should generate T such that ./T should return hello. And js_of_ocaml T -o cubes.js should generate cubes.js such that the function jsOneArgument of cubes.js can well be called by other JS or HTML files.
Before I messed up the packages, ./T returned Unimplemented Javascript primitive caml_pure_js_expr!, that is why i installed npm, node, etc...

I switched back to OCaml 4.02.3:
opam switch 4.02.3
Then,
eval `opam config env`
Then,
opam install ppx_tools js_of_ocaml
As a result, ocamlfind ocamlc -package js_of_ocaml.ppx -linkpkg cubes.ml -o T generates T, and js_of_ocaml T -o cubes.js generates cubes.js. node cubes.js does return hello.
However, ./T still returns Unimplemented Javascript primitive caml_pure_js_expr!, I am going to open another post for that...

Related

How to compile a file that uses the JsooTop module?

I have this in a file named main.ml:
let () = JsooTop.initialize ()
I tried compiling the file using:
ocamlfind ocamlc -package js_of_ocaml -linkpkg -o main.byte main.ml
But this error appears:
File "main.ml", line 1, characters 9-27:
Error: Unbound module JsooTop
It appears that JsooTop is not present on my machine, so I ran opam install js_of_ocaml-toplevel, and tried compiling the file again using:
ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-toplevel -linkpkg -o main.byte main.ml
js_of_ocaml main.byte
But I get warnings:
Warnings from ocamlfind:
findlib: [WARNING] Interface topdirs.cmi occurs in several directories: /usr/lib/ocaml, /usr/lib/ocaml/compiler-libs
Warnings from the js_of_ocaml executable:
There are some missing primitives
Dummy implementations (raising 'Failure' exception) will be used if they are not available at runtime.
You can prevent the generation of dummy implementations with the commandline option '--disable genprim'
Missing primitives provided by +dynlink.js:
caml_add_debug_info
caml_dynlink_add_primitive
caml_dynlink_get_current_libs
caml_dynlink_lookup_symbol
caml_dynlink_open_lib
caml_remove_debug_info
Missing primitives provided by +toplevel.js:
caml_get_current_environment
caml_get_section_table
caml_invoke_traced_function
caml_realloc_global
caml_reify_bytecode
caml_static_alloc
caml_static_free
caml_static_release_bytecode
caml_terminfo_setup
My question is: what is the proper way to compile a file that uses the JsooTop module?
First, make sure that the required OPAM packages are present:
opam install js_of_ocaml js_of_ocaml-toplevel
To build a program that uses the JsooTop module, compile the file in this way:
ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-toplevel -linkpkg -o main.byte main.ml
js_of_ocaml --toplevel --dynlink +dynlink.js +toplevel.js main.byte
Note the inclusion of --toplevel, --dynlink, +dynlink.js, and +toplevel.js. The commands above will produce a JavaScript file named main.js.

How to link OCaml object file with Core module?

I have simple program, which uses function from Core.Std module, e.g.:
open Core.Std
let _ = List.last [1;2;3]
I can compile and link it by ocamlfind or ocamlbuild:
ocamlfind ocamlc -package core -linkpkg -thread app.ml
ocamlbuild -use-ocamlfind -pkgs core -tag thread app.native
But I don't know how to compile and link such file using "normal" OCaml compiler:
ocamlc -c -I ~/.opam/system/lib/core_kernel/ -I ~/.opam/system/lib/core/ app.ml
ocamlc ~/.opam/system/lib/core_kernel/core_kernel.cma ~/.opam/system/lib/core/core.cma app.cmo
Last of above commands gives the following error:
File "_none_", line 1:
Error: Error on dynamically loaded library: /home/maciej/.opam/system/lib/stublibs/dllcore_kernel_stubs.so: /home/maciej/.opam/system/lib/stublibs/dllcore_kernel_stubs.so: undefined symbol: caml_ba_alloc
What am I doing wrong?
You can use ocamlfind ocamlc -only-show … to see the full command constructed by ocamlfind . Your error points towards some missing transitive dependencies of Core and an old version of OCaml and Core.

ocamlopt and ocamlbuild give an Unbound module error despite ocamlfind seeing the required module

I'm attempting to build the xencat tool from this project. When I try to use ocamlopt to build it, I get
$ ocamlopt -o xencat xencat.ml
File "xencat.ml", line 1, characters 5-13:
Error: Unbound module Cmdliner
Following guidance here, I verified that cmdliner was installed and that it was visible to ocamlfind, and then tried again:
Here's what ocamlfind list shows:
$ ocamlfind list
bigarray (version: [distributed with Ocaml])
bytes (version: [distributed with OCaml 4.02 or above])
...
cmdliner (version: 0.9.8)
...
No luck:
$ ocamlfind ocamlopt -o xencat xencat.ml
File "xencat.ml", line 1, characters 5-13:
Error: Unbound module Cmdliner
Following this page, I tried ocamlbuild:
$ocamlbuild -use-ocamlfind xencat.byte
+ ocamlfind ocamlc -c -o xencat.cmo xencat.ml
File "xencat.ml", line 1, characters 5-13:
Error: Unbound module Cmdliner
Command exited with code 2.
Clearly I'm missing something, but being new to Ocaml, I don't know what else to try.
ocamlbuild -use-ocamlfind -package cmdliner xencat.byte
?
To build this project use the following command at the root of the project:
./configure
make build
You will find a xencat.native file in the root folder.
You can also use opam to build it, as opam will handle all dependencies to you. Just go to the root of the project and say:
opam pin add vchan .
and answer yes to whatever opam asks to do.

Basic Ocaml: How do I compile this?

Just beginning with ocaml and am struggling with the various compilers and tooling. E.g. ocamlopt, ocamlc, ocamlbuild, corebuild and so on. So, how do I compile the following?
open Core.Std
module Regex = Re2.Regex
let ls pattern =
let pat = Regex.create_exn pattern in
let matcher = Regex.matches pat in
Sys.ls_dir "."
|> List.filter ~f:matcher
|> List.iter ~f:(fun s -> print_string s; print_newline ())
let () =
match In_channel.input_line stdin with
| None -> print_string "No Input"
| Some pat -> ls pat
In utop I can just #require "re2" and go from there.
Without the inclusion of the regular expressions module I would just use corebuild ls.native, assuming that the above code is placed into ls.ml.
[edit]
Have so far tried
ocamlbuild -use-ocamlfind -package core -package re2
which spit out
ocamlfind ocamldep -package core -package re2 -modules ls.ml > ls.ml.depends
ocamlfind ocamlc -c -package core -package re2 -o ls.cmo ls.ml
+ ocamlfind ocamlc -c -package core -package re2 -o ls.cmo ls.ml
ocamlfind: Error from package `threads': Missing -thread or -vmthread switch
Command exited with code 2.
So after some googling I was led to this blog I tried
ocamlbuild -tag thread -use-ocamlfind -package core -package re2
which spits out over 6000 lines of what looks like make output before failing with:
collect2: error: ld returned 1 exit status
File "caml_startup", line 1:
Error: Error during linking
Command exited with code 2.
so I'm not sure what to try next.
I am using Ubuntu 14.04 on a 64bit machine. I ditched the apt-get versions of ocaml:
sudo apt-get remove --purge ocaml ocaml-base-nox ocaml-compiler-libs \
ocaml-interp ocaml-native-compilers \
ocaml-nox campl4 ocaml-base ocaml-docs opam
Then I installed opam from source according to the instructions here.
Next I installed core, utop, and re2 with opam install core utop re2
And finally I ran ocamlbuild -use-ocamlfind -package re2 -package core -tag thread ls.native
which built the desired executable.
I would suggest you to use oasis tool. It is not as hard, as one may think. But it removes all library burden from you.
Lets create a simple _oasis file for your project:
OASISFormat: 0.4
Name: ls
Version: 0.1
Synopsis: Testing oasis
Authors: Fizz_ed
License: MIT
Plugins: META (0.4), DevFiles (0.4)
BuildTools: ocamlbuild, camlp4o
BuildDepends: core, camlp4, threads,
sexplib.syntax,
bin_prot.syntax,
comparelib.syntax,
herelib,
herelib.syntax
Executable "ls"
Path: .
MainIs: ls.ml
CompiledObject: best
BuildDepends: re2
After you've created this file, run oasis setup command (if oasis is not installed, then install it using opam, or your package manager).
After that you will have a common configure script and makefiles. So all you need is to type
./configure
make
and your file will be compiled.
Or you can just use ocamlbuild directly, it will work too, since oasis created all the necessary files
ocamlbuild ls.native
A few explanations about the file. The preamle contains everything you need to build your core-style applications.
Executable section describes your executable file, and also adds a dependency to re2 library.
Update: fixed some issues.

llvm OCaml bindings

I'm working on llvm OCaml bindings.
I installed llvm package through opam (opam install llvm), when I use llvm in utop, I get the following error:
#require "llvm";;
Error: The external function 'llvm_global_succ' is not available.
The opam llvm version is 3.2.
I also tried building llvm3.3 from the official site (./configure --with-ocaml-libdir='ocamlc -where'), the build was successful (all the llvm command-line tools are working), but I got the same error in utop.
I'm on Mac OS 10.7.5.
Edit: I solved it..
ocamlmktop -o llvmtop llvm.cma -cc g++
Then launch llvmtop, you can use llvm bindings after open Llvm.
I haven't found an equivalent for utop yet..
Edit2: Now utop is working!
Thanks to the utop documentation here
create a myutop_main.ml file:
let () = UTop_main.main ()
create a custom utop with llvm bindings:
ocamlfind ocamlmktop -o llvmutop -thread -linkpkg -package utop -package llvm myutop_main.ml -cc g++
(as I'm asked to answer my own question...)
To use system toplevel
ocamlmktop -o llvmtop llvm.cma -cc g++
Then launch llvmtop, you can use llvm bindings after open Llvm.
I haven't found an equivalent for utop yet..
To use utop
Thanks to the utop documentation here
create a myutop_main.ml file:
let () = UTop_main.main ()
create a custom utop with llvm bindings:
ocamlfind ocamlmktop -o llvmutop -thread -linkpkg -package utop -package llvm myutop_main.ml -cc g++