Friday, January 23, 2015

Installing OCaml for MacOS Noobie!

After successfully installing OCaml and Ocaide plugin for Eclipse on a  Mac, I now think it's straightforward, however, before I completely forget the pain involved in figuring out what goes where. I wanted to put down some pointers for noobies like me, in hope that it will save people some valuable time.

In general, ocaml.org is a great spot for finding any information related to OCaml. Installing OCaml is no different, you can read all about it here. There is tons of very useful and detailed information that I highly recommend anyone who is interested in OCaml to check out. In this post I just summarize the steps it took me to get from zero to OCaml installed (hero!).

Feel free to correct me or share your own personal successes and experiences.

The following steps worked for me:

xcode-select --install
  • brew install ocaml 
  • brew install opam - I encountered a problem and apparently I am not the only one - see details and solutions here.
    • Basically if after installing opam and trying to run it, you get "Illegal instruction: 4"  Try running:
 brew reinstall --build-from-source opam
      you can thank @avsm for the workaround.
Now that you have OCaml installed, let's discuss IDEs.

IDEs

Eclipse 


My IDE of choice is Eclipse1 ocaide plugin very useful. I used it on Ubuntu and thought that installing it on Mac might be a good way to go.

There are multiple guides (from Upenn, JSU) on the web on how to do just that, just make sure that you get all the prerequesite components such as Java 7 installed and ready to go.

Upenn CS120 provides a nice list for you to follow, make sure that you do! - thanks guys!

  1. Mac Users Only Install X11 libraries (needed for the OCaml graphics libraries to work):
    • Install Apple's XQuartz version, which is available here: http://xquartz.macosforge.org/landing/
    • Before moving on to the next step, make sure you LOG OUT of your user account so that the X11 install can complete.
    • Check to see that the directory /usr/X11/lib exists and that it contains the file libX11.6.dylib (this should have been created when you installed XQuartz). If this isn't the case, try reinstalling XQuartz and then contact the course staff for help.
    • [MM] Removed these instructions, because ~/lib isn't on the default PATH for Macs, and because with most recent SW updates this step isn't necessary.
    • From the terminal, in your home directory (e.g. /Users/stevez) create a directory called lib (if one does not already exist):
       > cd > mkdir lib 
    • Create a symbolic link in that lib directory to the libX11.6.dylib file:
       > cd ~/lib > ln -s /opt/X11/lib/libX11.6.dylib libX11.6.dylib 
    • Next, install the Java Development Kit (JDK) 7. You can download it from this this site (after accepting the terms & conditions). Make sure you do this step before installing Eclipse or OCaIDE.


[Update] As was pointed out by numerous stackoverflow posts like this one , Eclipse IDE tend not to recognise environment variables. The solution is to wrap the execution into a bash script. It works, however, I couldn't manage to make it work with the MacOS launch database. Even after the update of the Info.plist, Eclipse still won't recognise environment variables. At this stage, I run the script manually and it works.

Vim 

[Update] VIM grows on me. If you are interest to configure VIM to use OCaml, please follow these instructions by @avsm.

Here is how my ~/.vimrc looks like:
execute pathogen#infect()
let s:ocamlmerlin=substitute(system('opam config var share'),'\n$','','''') .  "/ocamlmerlin"
let g:syntastic_ocaml_checkers = ['merlin']
au BufRead,BufNewFile *.ml,*.mli compiler ocaml
execute "set rtp+=".s:ocamlmerlin."/vim"
execute "set rtp+=".s:ocamlmerlin."/vimbufsync"
execute ":source " . "/Users/Yan/.opam/system/share/vim/syntax/ocp-indent.vim"

You can then do some cool stuff as kindly shared here by Thomas Leonard:

Then you get some nice shortcuts:
  • \s switches between the .ml and .mli file
  • \c comments the current line / selection (\C to uncomment)
  • % jumps to matching let/in, if/then, etc (see :h matchit-install)
  • \t tells you the type of the thing under the cursor (if you compiled with -annot)
Thomas also adds
"Also, Vim can then parse the output of the compiler and jump to the correct location."
However, he doesn't say how to do that. I searched the web and eventually figured it out. So here is what you need:

  1. Makefile to compile you code
  2. Vim 7.4+ (earlier versions don't seem to support it.
  3. and the knowledge how to use vim's quickfix
Let's tackle these one by one:

1. Here is a sample of a Makefile:


NAME =
OCAMLBUILD = ocamlbuild -use-ocamlfind  -classic-display
PACKAGES = -package -tag thread

all:
                $(OCAMLBUILD) $(PACKAGES) filename.byte 

clean:
                rm *.byte

test:   
                $(OCAMLBUILD) $(PACKAGES) filename.byte 




2. Make sure you have an updated version of Vim.

3.  You can read the full documentation on it here, or check this quick and what I thought I useful, summary provided by this stack overflow answer - run: make and then:

:copen " Open the quickfix window
:ccl   " Close it
:cw    " Open it if there are "errors", close it otherwise (some people prefer this)
:cn    " Go to the next error in the window
:cnf   " Go to the first error in the next file

That's all folks! Enjoy.

Please let me know if above helped you or you whether you encountered other issues so I can update the post accordingly.

After some issues with PATH variables in Eclipse, I am seriously considering converting to Vim.

No comments:

Post a Comment