In this tutorial, we are going to construct a package called mypkg which defines a simple function and to glue it in the interpreter.
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):
PKG=$HOME/mypkg;mkdir $PKG
Shell]
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,...).
mkdir $PKG/src
Shell]
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.
mkdir $PKG/include; mkdir $PKG/include/mypkg;
Shell]
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);
}
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
source $HOME/Devel/buildmmx/local_env
Shell]
where ~/.../buildmmx is the folder where
To generate default configurations files, we proceed as follows:
mmxmake $PKG -init
Shell]
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
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:
BUILD=$HOME/buildmypkg; mkdir $BUILD; cd $BUILD; cmake
$PKG
Shell]
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):
make
Shell]
[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.
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:
mkdir $PKG/glue
Shell]
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:
cd $PKG; mmxglue $PKG
Shell]
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
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:
cp ~/Devel/buildmmx/CMakeModules/FindBasix.cmake
CMakeModules
Shell]
cp ~/Devel/buildmmx/CMakeModules/FindLibtool.cmake
CMakeModules
Shell]
The folder ~/Devel/buildmmx is the folder where
cd $BUILD; make
Shell]
== 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
source $BUILD/local_env; mmx-light
Shell]
Mmx]
use "mypkg"
Mmx]
help f
Mmx]
f 3.2
Mmx]