OCaml: Installing a library? - ocaml

I'm a complete noob in OCaml and I am attempting to use libraries for the first time. I'm attempting to use this library: https://github.com/savonet/ocaml-mm
and trying to run the examples that were listed in the repository. It's not exactly compiling correctly so I believe I may have installed it incorrectly?
These are the steps I took in installing this package:
1). git clone https://github.com/savonet/ocaml-mm.git
2). cd ocaml-mm
3). opam pin add .
This returned:
The following actions will be performed:
↗ upgrade mm 0.7.4 to 0.8.0*
↻ recompile liquidsoap 2.0.3-1 [uses mm]
===== ↻ 1 ↗ 1 =====
Do you want to continue? [Y/n] y
<><> Gathering sources ><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
[liquidsoap.2.0.3-1] found in cache
<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
⊘ removed liquidsoap.2.0.3-1
⊘ removed mm.0.7.4
∗ installed mm.0.8.0
[ERROR] The compilation of liquidsoap failed at "/usr/bin/make".
#=== ERROR while compiling liquidsoap.2.0.3-1 =================================#
# context 2.0.5 | linux/x86_64 | ocaml-base-compiler.4.12.0 | https://opam.ocaml.org#1c609c75
# path ~/.opam/cs3110-2022sp/.opam-switch/build/liquidsoap.2.0.3-1
# command /usr/bin/make
# exit-code 2
# env-file ~/.opam/log/liquidsoap-9751-e4f8df.env
# output-file ~/.opam/log/liquidsoap-9751-e4f8df.out
### output ###
# [...]
# OCAMLOPT -c stream/frame_content.mli
# OCAMLOPT -c stream/frame_content.ml
# File "stream/frame_content.ml", line 372, characters 10-43:
# 372 | (Audio.Mono.sub a' !dst_pos !len))
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Error: This expression has type Mm.Audio.Mono.t = float array
# but an expression was expected of type int
# make[2]: *** [../Makefile.rules:122: stream/frame_content.cmx] Error 2
# make[2]: Leaving directory '/home/ddavidjeong/.opam/cs3110-2022sp/.opam-switch/build/liquidsoap.2.0.3-1/src'
# make[1]: *** [../Makefile.rules:69: all-auto-ocaml-prog] Error 2
# make[1]: Leaving directory '/home/ddavidjeong/.opam/cs3110-2022sp/.opam-switch/build/liquidsoap.2.0.3-1/src'
# make: *** [Makefile.rules:29: all-subdirs] Error 2
<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
┌─ The following actions failed
│ λ build liquidsoap 2.0.3-1
└─
┌─ The following changes have been performed
│ ⊘ remove liquidsoap 2.0.3-1
│ ↗ upgrade mm 0.7.4 to 0.8.0
└─
<><> liquidsoap.2.0.3-1 troubleshooting <><><><><><><><><><><><><><><><><><><><>
=> We're sorry that your liquidsoap install failed. Check out our installation
instructions at: https://www.liquidsoap.info/doc-2.0.3-1/install.html#opam
for more information.
[NOTE] Pinning command successful, but your installed packages may be out of sync.
The former state can be restored with:
opam switch import "/home/ddavidjeong/.opam/cs3110-2022sp/.opam-switch/backup/state-20220316072305.export"
Or you can retry to install your package selection with:
opam install --restore
I thought this still mean the ocaml-mm library was installed since liquidsoap was a previous library I was fumbling with and it only recompiled that library.
Now when I attempt to compile the library code in VS, the modules in the example when being opened are unbounded and none of the examples compile.
I'm thinking that I either installed it incorrectly or I'm just using libraries completely wrong.

First, if you don't need the development version of the mm library, you can simply use
opam install mm
rather than cloning by hand the mm git repository.
Even, if you need the development version, you can ask opam to install it directly with
opam pin add --dev-repo mm
Second, when you say that none of the example compile, do you mean when you call make in the example directory of the library? Or are you trying to compile an example file directly?
If it is the second option, you are probably missing the library dependency in your dune file (do you have a dune file?). Typically, the dune file for the drums example in the mm library repo is:
(executable
(name drums)
(modules drums)
(optional)
(libraries mm.audio mm.oss))
One last potential issue is that you will need to launch one build of the example for the dependencies to be registred in your editor.

Related

Build and run a Qt application on macOS via Bazel

I tried to build and run a Qt5 (5.15.2) application on macOS (10.15.7) using Bazel 5.0.0.
Unfortunately, I run into some problems.
The building part seems to work, but not the run part.
I installed Qt5 on my machine using Homebrew:
brew install qt#5
brew link qt#5
I adapted https://github.com/justbuchanan/bazel_rules_qt/ to my needs. See this PR. When I try to run:
bazel run --cxxopt=-std=c++17 //tests/qt_resource:main
I receive the runtime error:
dyld: Symbol not found: __ZN10QByteArray6_emptyE
Steps to reproduce the issue:
# brew install bazel # Install Bazel
# brew install qt#5 # Install Qt5
git clone https://github.com/Vertexwahn/bazel_rules_qt.git
cd bazel_rules_qt
git checkout add-macos-support
bazel build --cxxopt=-std=c++17 //... # should work
bazel run --cxxopt=-std=c++17 //tests/qt_resource:main # should give you the error message
Nevertheless, building everything using bazel build --cxxopt=-std=c++17 //... seems to work.
I am not 100% sure if the link options -F/usr/local/opt/qt5/Frameworks and -framework QtCore, etc. are correct.
Maybe someone can confirm this.
Did I use the correct link options?
For me, it is a bit unclear what dependencies the main binary expects. I tried to copy QtCore.framework to the location of the main binary manually but this does not change the error message.
What files does the main binary expect?
If I try to run macdeployqt on my main binary I get also some errors. I do within my workspace root dir a cd bazel-bin/tests/qt_resource and run then /usr/local/opt/qt5/bin/macdeployqt main:
ERROR: Could not find bundle binary for "main"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/otool-classic: can't open file: (No such file or directory)\n"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/otool-classic: can't open file: (No such file or directory)\n"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/otool-classic: can't open file: (No such file or directory)\n"
WARNING:
WARNING: Could not find any external Qt frameworks to deploy in "main"
WARNING: Perhaps macdeployqt was already used on "main" ?
WARNING: If so, you will need to rebuild "main" before trying again.
ERROR: Could not find bundle binary for "main"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/strip: can't open file: (No such file or directory)\n"
ERROR: ""
My hope was that macdeployqt would collect all needed resources for me. Any idea why this is not working?
How can macdeployqt be used to collect all needed dependencies for the main binary?
If I convert my main to an app via lipo -create -output universall_app main and do then a /usr/local/opt/qt5/bin/macdeployqt universall_app I get the same error message.
The CMake approach
To make sure that there is no general problem with my system setup I tried to use CMake to build a Qt5 application:
git clone https://github.com/euler0/mini-cmake-qt.git
cmake -DCMAKE_PREFIX_PATH=/usr/local/opt/qt5 .
make -j
This produces an example.app.
With a double click on this application bundle,
the application can be started.
This worked on my system.
Future directions
It seems that rules_apple can be used to create an application bundle.
I am not sure if I need to transform my Qt application binary to an app bundle to be able to execute it.
One could use --sandbox_debugto identify what Bazel is doing and dtruss for the CMake version to compare the differences.
I am currently not sure what trying to do next and hope for an easy solution. I am also fine with a Qt6 solution.
Update: Alternative Answer
It would also be helpful if someone can point out how to build a minimal Qt application using make only on macOS and a brew installed Qt5 or tell me what the linker and compiler options must look like.
I followed your steps with Mac OSX 10.15.7, Qt (installed by homebrew) 5.15.1 and both bazel 4.2.2-homebrew and 5.0.0-homebrew and initially I could not build the project from git:
* 3fe5f6c - (4 weeks ago) Add macOS support — Vertexwahn (HEAD -> add-macos-support, origin/add-macos-support)
This is the result that I get when building:
% bazel build --cxxopt=-std=c++17 //...
DEBUG: /private/var/tmp/_bazel_home/761aafaa2237a9607dd915f1f52bca3e/external/com_justbuchanan_rules_qt/qt_configure.bzl:43:14: Installation available on the default path: /usr/local/opt/qt5
INFO: Analyzed 14 targets (0 packages loaded, 0 targets configured).
INFO: Found 14 targets...
ERROR: /Users/home/Git/my_repo/bazel_rules_qt/tests/qt_qml/BUILD:4:10: Compiling tests/qt_qml/main.cc failed: (Aborted): wrapped_clang failed: error executing command external/local_config_cc/wrapped_clang '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -O0 -DDEBUG '-std=c++11' ... (remaining 38 argument(s) skipped)
Use --sandbox_debug to see verbose messages from the sandbox
tests/qt_qml/main.cc:1:10: fatal error: 'QtQml/QQmlApplicationEngine' file not found
#include <QtQml/QQmlApplicationEngine>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Error in child process '/usr/bin/xcrun'. 1
INFO: Elapsed time: 0,594s, Critical Path: 0,32s
INFO: 3 processes: 3 internal.
FAILED: Build did NOT complete successfully
After playing around with headers and include paths in qt.BUILD:
diff --git a/qt.BUILD b/qt.BUILD
index 517c8db..8f110b5 100644
--- a/qt.BUILD
+++ b/qt.BUILD
## -28,11 +28,12 ## QT_LIBRARIES = [
cc_library(
name = "qt_%s_osx" % name,
# When being on Windows this glob will be empty
- hdrs = glob(["%s/**" % include_folder], allow_empty = True),
+ hdrs = glob(["include/%s/**" % include_folder], allow_empty = True),
includes = ["."],
linkopts = ["-F/usr/local/opt/qt5/lib"] + [
"-framework %s" % library_name.replace("5", "") # macOS qt libs do not contain a 5 - e.g. instead of Qt5Core the lib is called QtCore
],
+ strip_include_prefix= "include"
# Available from Bazel 4.0.0
# target_compatible_with = ["#platforms//os:osx"],
)
I could build and run the project:
% bazel build --cxxopt=-std=c++17 //...
DEBUG: /private/var/tmp/_bazel_home/761aafaa2237a9607dd915f1f52bca3e/external/com_justbuchanan_rules_qt/qt_configure.bzl:43:14: Installation available on the default path: /usr/local/opt/qt5
INFO: Analyzed 14 targets (1 packages loaded, 7422 targets configured).
INFO: Found 14 targets...
INFO: Elapsed time: 11,761s, Critical Path: 7,23s
INFO: 3 processes: 1 internal, 2 darwin-sandbox.
INFO: Build completed successfully, 3 total actions
% bazel run --cxxopt=-std=c++17 //tests/qt_resource:main
DEBUG: /private/var/tmp/_bazel_home/761aafaa2237a9607dd915f1f52bca3e/external/com_justbuchanan_rules_qt/qt_configure.bzl:43:14: Installation available on the default path: /usr/local/opt/qt5
INFO: Analyzed target //tests/qt_resource:main (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //tests/qt_resource:main up-to-date:
bazel-bin/tests/qt_resource/main
INFO: Elapsed time: 3,657s, Critical Path: 0,00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
opened resource file
file1
Related to your question whether the linker options are correct
-F/usr/local/opt/qt5/Frameworks -framework QtCore
Yes they are correct, you could alternatively use -F/usr/local/opt/qt5/lib (as you are already using in qt.BUILD) as all the files under the Frameworks folder are links to the lib folder.
With macdeployqt and lipo I get the same results as in the OP even after the test is running successfully.

Problem with Ocaml and make: "Error: the file ____.cmxa is not a compilation unit description"

I'm running make in the top level of the UniMath directory and make keeps returning errors. I'm working on fedora 35 and I'm using Ocaml version 4.11.2.
Here is a print out of the errors
make[2]: Entering directory '/home/rymndbkr/UniMath/sub/coq'
OCAMLBEST -o bin/coqdep_boot
File "_none_", line 1:
Error: tools/coqdep_boot.cmx is not a compilation unit description.
OCAMLBEST -o bin/ocamllibdep
File "_none_", line 1:
Error: tools/ocamllibdep.cmx is not a compilation unit description.
COQMKTOP -o bin/coqtop.opt
File "topbin/coqtop_bin.ml", line 11, characters 20-32:
11 | let drop_setup () = Mltop.remove ()
^^^^^^^^^^^^
Error: Unbound module Mltop
make[2]: *** [Makefile.build:422: bin/coqtop.opt] Error 2
make[2]: Leaving directory '/home/rymndbkr/UniMath/sub/coq'
make[1]: *** [Makefile.make:178: submake] Error 2
make[1]: Leaving directory '/home/rymndbkr/UniMath/sub/coq'
Makefile:76: .coq_makefile_output.conf: No such file or directory
make: *** [Makefile:247: sub/coq/bin/coq_makefile] Error 2
I'll describe the process I went through in hopes that it is reproducible. I was following the UniMath installation instructions on their github (https://github.com/UniMath/UniMath/blob/master/INSTALL.md). They did not have any installation instructions for fedora so I attempted to convert their Debian instructions. The main thing they wanted Debian users to do was run the command
sudo apt-get install build-essential git ocaml ocaml-nox ocaml-native-compilers camlp5 libgtk2.0 libgtksourceview2.0 liblablgtk-extras-ocaml-dev ocaml-findlib libnum-ocaml-dev emacs
To convert this, I made sure I had Ocaml version 4.11.2. I just followed the steps on Ocaml's site (https://ocaml.org/docs/install.html). There was a script which prompted me to modify ~/.bash_profile. I said yes. I also said yes when it prompted me to add a hook.
I also installed ocaml-num via the command dnf since this was required for later versions of ocaml.
I also installed #development-tools via dnf since another stack exchange post recommended it as the best fedora equivalent of build-essentials, the latter of which is part of the downloads for prepping a Debian computer for UniMath.
There are some things I did not mention that the UniMath site said to install to prep a Debian computer. I either had these things (or their equivalents installed) or could not figure out if they had equivalents.
I decided to just go ahead and try to finish the installation. I simply ran
git clone https://github.com/UniMath/UniMath
cd \UniMath
make BUILD_COQIDE=yes
It is on the last of these commands that the errors arose.
P.S. - Please let me know if there is information I should add to this question, or any way to improve it.
The error message not a compilation unit description is likely due to a mismatch between the Ocaml version that compiled the file and the one that is trying to read it.
You can use ocamlobjinfo to check that hypothesis.
A possible fix is to use make clean to reinitialize the state, and then rebuild it after ensuring that the environment is consistent with a call to eval $(opam env).

Installing OpenCV 3 in Homebrew using libstdc++

I am installing Caffe on Mac OS X 10.11 El Capitan, which somehow requires that I install OpenCV with libstdc++. To do so, I did "brew update opencv" and adding the following lines below "def install" as mentioned on the Caffe website (http://caffe.berkeleyvision.org/install_osx.html):
def install
# ADD THE FOLLOWING:
ENV.append "CXXFLAGS", "-stdlib=libstdc++"
ENV.append "CFLAGS", "-stdlib=libstdc++"
ENV.append "LDFLAGS", "-stdlib=libstdc++ -lstdc++"
# The following is necessary because libtool likes to strip LDFLAGS:
ENV["CXX"] = "/usr/bin/clang++ -stdlib=libstdc++"
...
However, when I go install it using "brew install --build-from-source -vd opencv", I get the following error:
cd /tmp/opencv-20170909-8122-1xa5qpl/opencv-3.3.0/build/modules/dnn &&
/usr/local/Cellar/cmake/3.9.2/bin/cmake -E cmake_symlink_library
../../lib/libopencv_dnn.3.3.0.dylib ../../lib/libopencv_dnn.3.3.dylib
../../lib/libopencv_dnn.dylib [ 59%] Built target opencv_dnn make: ***
[all] Error 2 /usr/local/Homebrew/Library/Homebrew/debrew.rb:11:in
`raise' BuildError: Failed executing: make
1. raise
2. ignore
3. backtrace
4. irb
5. shell Choose an action:
Anyone know how to solve this? Thank you in advance!
EDIT: Actually, I realized that my original opencv.rb file is producing the exact same problem. How do I fix/find an opencv.rb file that works?
It looks like he build you are going for in not compatible with the OS. If installing on Mac would suggest trying this method of implementation. It also uses homebrew but allows for a quick and simple implementation.

ocaml batteries unbound toploop error

I am trying to install bap required for ropc https://github.com/pakt/ropc as per the instructions given in ropc/bap/INSTALL in Ubuntu 14.04 . I have run these commands successfully .
sudo apt-get install ocaml ocaml-native-compilers ocaml-findlib camlidl \
libocamlgraph-ocaml-dev libextlib-ocaml-dev binutils-dev automake \
libcamomile-ocaml-dev otags libpcre3-dev camlp4-extra bison flex
Now , when I make it inside the bap directory , I get the following Unbound module toploop error, which seems to be a compatibility issue between batteries and ocaml. How do I get around this ? Please see the image for further details
Image uploaded
I read that I should add -I +compiler-libs somewhere. But where exactly? I have no experience working with ocaml and I am just trying to build ropc successfully.
I tried to compile the pact/ropc project, and I can only confirm problems.
Although the ocaml configuration succeeds, the compile shows an undefined error:
Error: Some fatal warnings were triggered (2 occurrences)
../Makefile.shared:84: recipe for target 'nat.cmo' failed
make[2]: *** [nat.cmo] Error 2
make[2]: Leaving directory '/home/strobel/tmp/ropc/bap-0.4/bigint-3.12/otherlibs/num'
Makefile:5: recipe for target 'all' failed
make[1]: *** [all] Error 2
make[1]: Leaving directory '/home/strobel/tmp/ropc/bap-0.4/bigint-3.12'
Makefile:356: recipe for target 'all-recursive' failed
The bap component used in ROPC is available in opam but ROPC is not, and it shows signs of bitrot. There are 3 issues opened for the github project telling about compile problems.
I have a quite well provisioned ocaml setup, with packages directly from opam (very up to date versions), and my approach was to see if it would compile here.
What to do?
Maybe uninstall your debian ocaml packages, start anew with opam. There is the possibility to install old ocaml versions, too, so maybe walk back in time to a 3.12 ocaml version. But getting the required packages in corresponding versions might not be so easy, opam is quite new. It will be an advanced ocaml debugging job IMO.

ocaml-glpk (glpk bindings) and OASIS

Preface: I am new to OCaml, OPAM, and OASIS.
tldr question: How do I properly set up a package with opam that is not already available in the repository (I can't just do opam install X)? More details follow:
I am trying to include ocaml-glpk in an OCaml project. I installed ocaml-glpk just by running make and make install as stated in the README, and the given example compiles and runs correctly. However, I am using OASIS to generate the build system of my project, and I am not sure how to set it up. I have the same example (renamed to glpkExample.ml in a src folder) and the following in my _oasis file:
Executable "glpkExample"
Path: src
MainIs: glpkExample.ml
CompiledObject: best
BuildDepends:
glpk
After running oasis setup -setup-update dynamic, I run make and get the following error:
ocaml setup.ml -build
Finished, 0 targets (0 cached) in 00:00:00.
+ /home/dimitrios/.opam/system/bin/ocamlfind ocamlopt -g -linkpkg -package glpk src/glpkExample.cmx -o src/glpkExample.native
File "_none_", line 1:
Error: Cannot find file /home/dimitrios/.opam/system/lib/glpk/glpk.cmxa
Command exited with code 2.
Compilation unsuccessful after building 4 targets (3 cached) in 00:00:00.
E: Failure("Command ''/usr/bin/ocamlbuild' src/glpkExample.native -tag debug' terminated with error code 10")
make: *** [build] Error 1
It seems the glpk library is missing a cmxa file needed to compile a native executable. I am not sure how to fix this. To compile glpkExample.ml correctly, my Makefile includes /home/dimitrios/.opam/system/lib/glpk and also uses the OCamlMakefile, which is extremely long and convoluted. Any help on setting this up with OASIS or how to get ocaml-glpk to work nicely with OASIS would be greatly appreciated.
Thanks!
This website is not appropriate for bug reports. You should really report it here.
The temporary solution is to use CompiledObject: byte to compile in bytecode.
If you're using opam then it is best to install application with it, not manually. Try to clean up your system and remove whatever you installed, and then do:
$ eval `opam config env`
$ opam install ocaml-glpk
Afterwards, if glpk is packaged in opam correctly, it should work with your setup, i.e., just with oasis's BuildDepends field and nothing more.