Archive

Archive for the ‘openmodelica’ Category

OpenModelica and Homebrew on OS X

July 9, 2012 2 comments

Last week I went for the task of compiling OpenModelica on OS X (Snow Leopard in my case).  OpenModelica is a free implementation of the Modelica language, which is used to model complex physical systems.  One of the main strengths is the direct use of physical equations, which leads to more readable and reusable models.

I will be using Homebrew to ‘brew’ all the dependencies.  Homebrew provides a flexible way to upgrade or install UNIX tools which are or aren’t included on OS X.  It rely on ‘Formulas’ written in Ruby to manage the installation of tools.  In fact you can browse online the Braumeister portal to check what packages are already being managed.

Installing Homebrew is pretty simple as you can find on the instructions.  You should be aware that Homebrew not always play well with tools installed by other package managers such MacPorts.  Most likely you will be able (and want) to remove other package managers. You can find help on removing MacPorts over here.

Basically what you need is Intel CPU, OS X 10.5 or higher with Xcode and Homebrew installed, and you are good to go!

If you want to go without Homebrew, there is a very good post by Bill Janssen on the OpenModelica mailing list.  I took the Qt version hint from him. Apart from Qt, most of the other dependencies handled by Homebrew are pointing to the latest versions of each tool.

Getting  the dependencies for OpenModelica

Some tools are already provided either by OS X or Xcode, such as: gcc, make, bison, flex, java, python.

You will need to upgrade pkg-config to version 0.25 which is the default on Homebrew.  Later on  you’ll also need a newer autoconf version 2.63, just to generate the OpenModelica configuration scripts.  I typically search for a formula, look for info and eventually I read the formula on the editor before installing them, like so:

$ brew search pkg-config
$ brew info pkg-config
$ brew edit pkg-config
$ brew install pkg-config
$ brew install autoconf

The installation of autoconf will tell you that it was not symlinked, and we will deal with this later.

You can install further packages as below. Note the verbose flag is used just because I like to see what is going on.  If you prefer you can install one package at a time.  After installing smlnj it will instruct you to amend your PATH in order for your system to find the tool.

$ brew install -v readline sqlite sundials smlnj

OpenModelica uses Qt for the graphical user interface.  However we need a slightly older version of Qt for it to work.  Here is another beauty of Homebrew, it is based on Git which allows you to checkout formulas for previous versions.  You can install and switch between various installed versions at will.  I already had Qt 4.8.2 installed, but it didn’t work out with OpenModelica.  Let us try Qt 4.7.4 instead.  First of all you will need Git.  Then I unlinked Qt and searched for other available versions of Qt.  You can also look for versions on Braumeister.  To pull other versions you should be on the Homebrew prefix location.  With Git you can go back to another version of Qt and install it. (It took some 30 min. to compile). This formula also applies patches to fix issues with Qt on the Lion version of OS X.

$ brew install git
$ brew unlink qt
$ brew versions qt
$ cd `brew --prefix`
$ git checkout 83f742e /usr/local/Library/Formula/qt.rb   # check the actual hash of Qt 4.7.4
$ brew install qt

Once you are done installing you can checkout the latest Qt version back again.  You can activate a given version with brew switch [formula] [version].  Using  brew info [formula]  it will tell you what is your currently active version.

Some plotting and graphics capability is done with Qwt.  Just remember that it depends on Qt.  If you switch between Qt versions you need to uninstall and install Qwt once again so that it gets compiled against the current Qt.

$ brew uninstall qwt
$ brew install qwt

So far most dependencies were already available.   I created three new formulas which were missing.  You can see the pull requests for omniORB, RML-MMC and lp_solve over my Homebrew fork (here).  If the formulas are not yet pulled into Homebrew, you can search and copy them into the formula directory.

While searching for formulas it will also look into the pull requests for new formulas.

$ brew search [formula]

You can look around, copy and create new formulas into the appropriate directory, typically:

$ ls /usr/local/Library/Formula/

With the formulas in place you can move on and install lp_solve.  If you take a look into this formula you will see that a small patch is applied to remove a long deprecated compiler flag.

$ brew install lp_solve

For omniORB you have the option to install also the omniORBpy Python mappings, which allows one to reach CORBA from Python.  It is needed if you plan to use the OpenModelica Python interface.  Please follow the instructions after the install on how to amend the PYTHONPATH variable to be able to use these tools.

$ brew install omniorb --python

The last dependency to compile OpenModelica is the RML-MMC package.  It is used to implement the MetaModelica compiler.  It depends on smlnj (Standard ML of New Jersey) which was already supplied a little earlier by Homebrew.  Currently the latest working version is only available via SVN (update 10/07/12: you can find the tarballs on the debian repository, it is now included into the Hombrew formula).

Typically Homebrew prefers to have a compressed tarball of the source, but in this case you will be pulling from a SVN repository.  This also illustrates another feature on which you can choose for a development branch (–HEAD option) rather than a stable tarball.  OS X is shipped with SVN and for this formula you just need the username and password of the repository.

$ brew install --HEAD rml-mmc
$ <enter>
$ <username> anonymous
$ <password> none

Compiling OpenModelica

So far so good.  By now you probably have all dependencies in place.

You will have to download the OpenModelica code from SVN (update 10/07/12: you can also find the tarball on the debian repository).  Create a directory somewhere you like and checkout the source from the repository.  I will checkout at ~/build/openmodelica.

$ mkdir -p ~/build/openmodelica
$ cd ~/build/openmodelica
$ svn checkout https://openmodelica.org/svn/OpenModelica/trunk/
$ <enter>
$ <username> anonymous
$ <password> none
$ cd trunk

To proceed with the compilation a few tweaks are need.  I will first explain the reasons and then give you a patch to apply into the code.

As it is today, two OpenModelica packages (OMOptim and OMOptimBasis) won’t build.  That is because the build scripts are only aware of Windows/MinGW build setup.  You will skip them by commenting them out of the makefile.

Maybe you noticed that Qt and Qwt were installed as OS X frameworks.  A framework is like a shared library, but it also encapsulates other resources, header files and documentation.   You will need to adjust some configuration files to set the framework libraries.

In the following you have a patch that shows the small tweaks I did to compile OpenModelica.  You can apply this patch after you checkout the source code.  It is easy to generate and apply SVN patches, as you can find here.  You can copy and paste this patch into a text file, say at  ~/build/openmodelica/patch_openmodelica.diff .

Index: OMEdit/OMEditGUI/OMEdit.config.in
===================================================================
--- OMEdit/OMEditGUI/OMEdit.config.in    (revision 12266)
+++ OMEdit/OMEditGUI/OMEdit.config.in    (working copy)
@@ -2,7 +2,11 @@
QMAKE_CXX = @CXX@
QMAKE_LINK = @CXX@

-LIBS += @CORBALIBS@ -lqwt@with_qwt_suffix@ -L../../OMPlot/bin/ -lOMPlot
-INCLUDEPATH += @with_qwt@ @CORBA_QMAKE_INCLUDES@ ../../OMPlot/OMPlotGUI
+QMAKEFEATURES += @with_qwt@/features
+CONFIG += qwt
+LIBS += @CORBALIBS@ -F@with_qwt@/lib/ -framework qwt -L../../OMPlot/bin/ -lOMPlot
+INCLUDEPATH +=  @with_qwt@/lib/qwt.framework/Headers @CORBA_QMAKE_INCLUDES@ ../../OMPlot/OMPlotGUI

QMAKE_LFLAGS += @LDFLAGS@
+
+
Index: Makefile.in
===================================================================
--- Makefile.in    (revision 12266)
+++ Makefile.in    (working copy)
@@ -61,7 +61,7 @@
omoptimbasis: mkbuilddirs
$(MAKE) -C OMOptim/OMOptimBasis/build -f Makefile.unix

-qtclients: omnotebook omshell omedit omplot omoptim omoptimbasis
+qtclients: omnotebook omshell omedit omplot   # omoptim omoptimbasis

qtclean: qtclean-common
$(MAKE) -C OMShell/OMShellGUI -f Makefile.unix clean
Index: OMNotebook/OMNotebookGUI/OMNotebook.config.in
===================================================================
--- OMNotebook/OMNotebookGUI/OMNotebook.config.in    (revision 12266)
+++ OMNotebook/OMNotebookGUI/OMNotebook.config.in    (working copy)
@@ -6,7 +6,11 @@
CORBAINC = @CORBA_QMAKE_INCLUDES@
USE_CORBA = @QT_USE_CORBA@

-PLOTLIBS = -lqwt@with_qwt_suffix@ -L../../OMPlot/bin/ -lOMPlot
-PLOTINC = @with_qwt@ ../../OMPlot/OMPlotGUI
+QMAKEFEATURES += @with_qwt@/features
+CONFIG += qwt

+PLOTLIBS = -F@with_qwt@/lib/ -framework qwt -L../../OMPlot/bin/ -lOMPlot
+PLOTINC = @with_qwt@/lib/qwt.framework/Headers ../../OMPlot/OMPlotGUI
+
QMAKE_LFLAGS += @LDFLAGS@
+
Index: OMPlot/OMPlotGUI/OMPlotGUI.config.in
===================================================================
--- OMPlot/OMPlotGUI/OMPlotGUI.config.in    (revision 12266)
+++ OMPlot/OMPlotGUI/OMPlotGUI.config.in    (working copy)
@@ -2,5 +2,9 @@
QMAKE_CXX = @CXX@
QMAKE_LINK = @CXX@

-LIBS += -lqwt@with_qwt_suffix@
-INCLUDEPATH += @with_qwt@
+QMAKEFEATURES += @with_qwt@/features
+CONFIG += qwt
+LIBS += -F@with_qwt@/lib/ -framework qwt
+INCLUDEPATH += @with_qwt@/lib/qwt.framework/Headers
+
+

In the following you will first apply the patch.  Then activate the newer autoconf, run it to generate the configure scripts and deactivate it again (just to to leave as Homebrew installed it).  Finally we run the configure and make to build OpenModelica.  It took me about 30 minutes to compile.

$ cd ~/build/openmodelica/trunk
$ patch -p0 -i ../patch_openmodelica.diff
$ brew link autoconf
$ autoconf
$ brew unlink autoconf
$ ./configure --without-paradiseo --with-omniORB=/usr/local --with-qwt=/usr/local/Cellar/qwt/6.0.1  CFLAGS=-I/usr/local/Cellar/lp_solve/5.5.2.0  CPPFLAGS=-I/usr/local/Cellar/lp_solve/5.5.2.0
$ make

If it indeed succeeds then you should have the following main OpenModelica binaries ready to be used:

./build/Applications/OMEdit.app/Contents/MacOS/OMEdit
./build/Applications/OMNotebook.app/Contents/MacOS/OMNotebook
./build/Applications/OMShell.app/Contents/MacOS/OMShell
./build/bin/OMPlot
./build/bin/OMShell-terminal
./build/bin/omc

Further Remarks

OMNotebook will compile just fine, but it has issues with Qt on OS X.  It will segfault just after starting.  If you know how to fix it, please step up.  As I mentioned OMOptim and OMOptimBasis are not yet ready to build on OS X and were skipped.

If you are compiling for newer or older OS X systems, you might need to adjust the CC  and CXX compiler flags as Bill Janssen did on his notes.

You may have noticed that I didn’t actually ‘install’ OpenModelica.  I am still looking into how to do it in such a way that it is nicely integrated into the Applications directory.

Looking forward, if not for the two SVN checkouts we could also have a Homebrew formula and a single ‘brew’ command that builds and eventually install OpenModelica on OS X.

And that was it! This is how I compiled OpenModelica. Your comments are appreciated!