Building your own mmx package with mmxtools

In this tutorial, we are going to construct a package called mypkg which defines a simple function and to glue it in the interpreter.

1.Creating a package

The first step to create a package (hereafter mypkg) is to make a folder named after your package (preferably in lower case and it should not start with a number):

Shell]

PKG=$HOME/mypkg;mkdir $PKG

Next, we create a folder src which will contains the source files that produce the library libmypkg.* attached to the package mypkg. All the files *.cpp, *.c of this folder will be compiled and put in the library libmypkg(.so,.dyl,...).

Shell]

mkdir $PKG/src

We edit the file my_fct.cpp in the folder src and insert the following instructions:

namespace mmx {
    double my_fct(double x) { return 2*x; }
}

This file defines a function called my_fct (which multiplies a double by ).

Next, we create a folder that will contain all the header files of the package.

Shell]

mkdir $PKG/include; mkdir $PKG/include/mypkg;

All the headers will be put in the folder include/mypkg so that the include instructions will have the form: #include "mypkg/...". This avoids name conflicts in case several packages provide header files with the same name. In this folder, we edit the file my_fct.hpp and insert the declaration of my_fct:

namespace mmx {
    double my_fct(double x);
}

2.Generating the configuration files

We are now ready to generate a preliminary version of the package mypkg with its configuration files. For this purpose, we use the script mmxmake, which is provided by the package mmxtools of mmx. In order to be able to use this script, we update our environment as follows:

Shell]

source $HOME/Devel/buildmmx/local_env

where ~/.../buildmmx is the folder where mmx has been built.

To generate default configurations files, we proceed as follows:

Shell]

mmxmake $PKG -init

mmxmake: created /Users/mourrain/mypkg/specif

mmxmake: updated /Users/mourrain/mypkg/specif/mypkg-cmake.mmx mmxmake: created /Users/mourrain/mypkg/CMakeModules mmxmake: updated /Users/mourrain/mypkg/CMakeModules/FindMypkg.cmake.in mmxmake: updated /Users/mourrain/mypkg/CMakeModules/MypkgConfig.cmake.in mmxmake: updated /Users/mourrain/mypkg/include/mypkg/mypkg-config.hpp.cmake mmxmake: created /Users/mourrain/mypkg/doc mmxmake: created /Users/mourrain/mypkg/doc/tools mmxmake: updated /Users/mourrain/mypkg/doc/tools/tm2html mmxmake: created /Users/mourrain/mypkg/doc/doxygen mmxmake: updated /Users/mourrain/mypkg/doc/doxygen/doxyfile.in mmxmake: created /Users/mourrain/mypkg/css mmxmake: created /Users/mourrain/mypkg/doc/css mmxmake: updated /Users/mourrain/mypkg/doc/css/mmxdoc.css mmxmake: updated /Users/mourrain/mypkg/CMakeLists.txt mmxmake: created /Users/mourrain/mypkg/doc/texmacs mmxmake: updated /Users/mourrain/mypkg/doc/html/installation_cmake.en.tm mmxmake: updated /Users/mourrain/mypkg/src/CMakeLists.txt

mmxmake: updated /Users/mourrain/mypkg/doc/CMakeLists.txt

3.Configuration and construction

The files needed for the configuration of the package have been generated. We can now configure and build the package. For that purpose, we create a folder buildmypkg which will contain the files generated during the construction (binary files, libraries, ...). The configuration is run out of source in this folder. We use the command cmake applied on the path to the source folder mypkg:

Shell]

BUILD=$HOME/buildmypkg; mkdir $BUILD; cd $BUILD; cmake $PKG

mkdir: /Users/mourrain/buildmypkg: File exists

== Configuring mypkg 0.1 [*] enable library build (SRC=ON) [ ] disable test build (TEST=OFF) [*] enable application build (APP=ON) [*] enable glue with mmx interpreter (GLUE=ON) [ ] disable axel plugins (AXL=OFF) [*] enable dtk plugins (DTK=ON) [ ] disable automatic documentation (DOC=OFF) [ ] disable benchmarks (BENCH=OFF) [ ] disable examples (EXPL=OFF) [ ] disable static inclusion (EMBEDDED=OFF) [*] enable shared library build (SHARED=ON) [ ] disable static library build (STATIC=OFF) [ ] disable debugging (DEBUG=OFF) [*] enable optimization (OPTIMIZE=ON) [*] enable cpp exceptions (EXCEPTIONS=ON) [ ] disable threads (THREADS=OFF) [ ] disable external packages (EXTERNAL=OFF) [ ] disable development mode (DEV=OFF) [ ] simd level (SIMD=OFF) -- Configuring done -- Generating done

– Build files have been written to: /Users/mourrain/buildmypkg

To compile the files of the packages, the command make can be used (in the folder buildmypkg):

Shell]

make

[35m[1mScanning dependencies of target mypkg

[0m[100%] [32mBuilding CXX object src/CMakeFiles/mypkg.dir/my_fct.cpp.o [0m[31m[1mLinking CXX shared library ../lib/libmypkg.dylib

[0m[100%] Built target mypkg

This building step creates in the folder ~/buildmypkg/lib a dynamic library libmypkg.so (or libmypg.dyl) which contains the definition of my_fct.

4.Connecting an external function in the interpreter

We are now going to connect the C++ function my_fct with a function in the interpreter. For that,

we create also a folder glue which will contain the files needed for the plugin that will be loaded the interpreter:

Shell]

mkdir $PKG/glue

In this folder, we edit the file f.mmx and insert the following instructions:

foreign cpp import {
  cpp_include "mypkg/my_fct.hpp";
  
  f: Double -> Double == my_fct;
}

The instruction cpp_include "mypkg/my_fct.hpp" includes the corresponding C++ header file and declares the C++ function my_fct. The next instruction declares a new function f in the interpreter which is calling the C++ function my_fct. To generate the C++ files that will be compiled to get the plugin, we use the command mmxglue:

Shell]

cd $PKG; mmxglue $PKG

mmxglue: processing autonomous package mypkg-0.1

mmxglue: processing f.mmx mmxglue: gluing f mmxglue: updated /Users/mourrain/mypkg/glue/f.cpp mmxglue: updated /Users/mourrain/mypkg/glue/glue_mypkg.cpp mmxmake: updated /Users/mourrain/mypkg/CMakeModules/FindMypkg.cmake.in mmxmake: updated /Users/mourrain/mypkg/doc/doxygen/doxyfile.in mmxmake: updated /Users/mourrain/mypkg/CMakeLists.txt

mmxmake: updated /Users/mourrain/mypkg/glue/CMakeLists.txt

This creates in the glue folder a file f.cpp binding the function my_fct with the mmx function f: Double -> Double, a file glue_mypkg used when loading the plugin in the interpreter. It also updates the cmake configuration files of the package.

In order to be able to compile the plugin, we will need some files from the package basix. We setup the local configuration, as follows:

Shell]

cp ~/Devel/buildmmx/CMakeModules/FindBasix.cmake CMakeModules

Shell]

cp ~/Devel/buildmmx/CMakeModules/FindLibtool.cmake CMakeModules

The folder ~/Devel/buildmmx is the folder where mmx has been built. To compile the plugin, we can now type:

Shell]

cd $BUILD; make

== Configuring mypkg 0.1

[*] enable library build (SRC=ON) [ ] disable test build (TEST=OFF) [*] enable application build (APP=ON) [*] enable glue with mmx interpreter (GLUE=ON) [ ] disable axel plugins (AXL=OFF) [*] enable dtk plugins (DTK=ON) [ ] disable automatic documentation (DOC=OFF) [ ] disable benchmarks (BENCH=OFF) [ ] disable examples (EXPL=OFF) [ ] disable static inclusion (EMBEDDED=OFF) [*] enable shared library build (SHARED=ON) [ ] disable static library build (STATIC=OFF) [ ] disable debugging (DEBUG=OFF) [*] enable optimization (OPTIMIZE=ON) [*] enable cpp exceptions (EXCEPTIONS=ON) [ ] disable threads (THREADS=OFF) [ ] disable external packages (EXTERNAL=OFF) [ ] disable development mode (DEV=OFF) [ ] simd level (SIMD=OFF) -- Configuring done -- Generating done -- Build files have been written to: /Users/mourrain/buildmypkg [ 33%] Built target mypkg [35m[1mScanning dependencies of target mmxmypkg [0m[ 66%] [32mBuilding CXX object glue/CMakeFiles/mmxmypkg.dir/f.cpp.o [0m[100%] [32mBuilding CXX object glue/CMakeFiles/mmxmypkg.dir/glue_mypkg.cpp.o [0m[31m[1mLinking CXX shared library ../lib/libmmxmypkg.dylib

[0m[100%] Built target mmxmypkg

Shell]

source $BUILD/local_env; mmx-light

Mmx] 

use "mypkg"

Mmx] 

help f

f : Double -> Double
(Native)

Mmx] 

f 3.2

Mmx] 

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License. If you don't have this file, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.