MCA2 Calculator Tutorial Chapter 2

From Mca2
Revision as of 14:53, 19 March 2014 by Pbecker (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Checking Out the MCA2 Sources

Start by cloning the main MCA2 Git repository and some basic dependencies:

$ cd YOUR_SOURCE_FOLDER
$ git clone git://idsgit.fzi.de/mca2/mca2.git
$ cd mca2
$ script/git_clone_base.py
$ script/ic/udpate_cmake.py


Compiling everything

We use CMake to build MCA2. In cmake, you have the choise to use either the classic "make" program for building your source. Or you may choose the new kid in town "ninja". Use ninja, if you are on raring or newer (04.2013). Use make, if you have problems with ninja.

Make

First, go to the mca2 home directory and create a build folder:

[home] $ mkdir build

Note that this folder can reside anywhere you want, so you can keep it outside your mca2 home directory as well. For this tutorial we will assume it is located inside your mca2 home directory.

Next, configure the build:

[home] $ cd build
[build-dir] $ cmake ..


You should now be ready to compile everything:

[build-dir] $ make -j9

The number after the -j parameter indicates the number of build threads to use. A good rule of thumb is number of processor cores + 1.

If it doesn't work on the first try you have refresh the CMakeLists.txt by executing

[mca2-home-dir] $ script/ic/update_cmake.py

from your mca2 base directory.


Ninja

Ninja is a new build system. Follow these steps, if you don't use Make.

[home] $ mkdir build

Note that this folder can reside anywhere you want, so you can keep it outside your mca2 home directory as well. For this tutorial we will assume it is located inside your mca2 home directory.

Next, configure the build:

[home] $ cd build
[build-dir] $ cmake .. -G"Ninja"

or

[build-dir] $ cmake .. -G"Eclipse CDT4 - Ninja"

Go to your build directory of MCA2 and execute

[build-dir] $ ninja

to build everything.

If it doesn't work on the first try you have refresh the CMakeLists.txt by executing

[mca2-home-dir] $ script/ic/update_cmake.py

from your mca2 base directory.

Creating Your New Project

To create a new project, open a terminal window, go to the mca2 home directory and set the environment variables:

[build-dir] $ source script/mcasetenv

With this command the MCAHOME path and MCAPROJECTHOME path is set to what it was the last time is has been set. Setting the environment variables is important for most python scripts to work. You can view them by executing the env command in the terminal window. Note that these environment variables have to be set every time you open a new terminal window for mca2 usage. To create a project named "calculator" you type

[mca-home-dir] $ new_project calculator

This scipt creates predefined subdirectories to the projects/ directory. The next step is to create a part named "pPocketCalc":

[mca-home-dir] $ new_part.py -C projects/calculator/src/calculator "PocketCalc"

Note that the script will automatically add a leading "p" to denote that the .cpp file it creates corresponds to a part. Similarly the scripts to create a group or a module will add a leading "g" or "m" to make distinguishing between the different types of source files easier. Now create a group named "gPocketCalc" and a module named "mAdd":

[mca-home-dir] $ new_group.py -C projects/calculator/src/calculator "PocketCalc"
[mca-home-dir] $ new_module.py -C projects/calculator/src/calculator "Add"

The message

Error! File /home/stuermer/src/mca2/projects/calculator/src/calculator/Logging.{h|cpp} already exists, aborting.

indicates, that the file Logging.{h|cpp} have been generated previously by new_part.py and new_group.py script.

These scripts will create a .cpp and a .h file each in the directory projects/calculator/src/calculator/.

Now it’s time to have a look into the created code. When you open the pPocketCalc.cpp you will notice that there is already an include-command for the header file of your group and also the con- structor call gPocketCalc() to create an object of your group is already added. The new part.py script automatically assumes that the highest group in your mca2-program’s hierarchy is named in the same way the part is. Of course you can alter this by demand by editing the cpp-file. Open the file gPocketCalc.cpp and go to the section where the includes are made. Here you add an inclusion for the module you created:

#include "mAdd.h"

//gPocketCalc.cpp

Go to the body of the gPocketCalc constuctor and create a pointer and a new object for your module:

gPocketCalc::gPocketCalc(tParent *parent, const std::string& name, bool fixit)
  : tGroup(parent, name,
           eSI DIMENSION, eSO DIMENSION, eCI DIMENSION, eCO DIMENSION,
           m_si_description, m_so_description, m_ci_description, m_co_description)
{
  mAdd *addition = new mAdd(this);

//gPocketCalc.cpp

By default the this pointer is the only parameter the constructor of a module needs as every module inherits from tModule – you can interpret it as if it shows the module from which group it is created and where it belongs to.

Building your new files

The last thing you have to do to make your project compileable is to modify the CMakeLists.txt files. Open the file projects/calculator/CMakeLists.txt in your project directory. Adjust the name of your project and add the source subfolder. The file should look somehow like this:

# this is for emacs file handling -*- mode: cmake; indent-tabs-mode: nil -*-

# To be used by other modules

SET(CALCULATOR_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/src" "${CMAKE_CURRENT_SOURCE_DIR}/src/calculator" CACHE INTERNAL "")
SET(CALCULATOR_IDE_FOLDER "calculator")

ADD_SUBDIRECTORY (src/calculator)

#projects/calculator/CMakeLists.txt

Next, create the CMakeLists.txt file for the actual source directory, in projects/calculator/src/calculator/:

INCLUDE (MCADescriptionBuilder)

ICMAKER_SET("calculator" IDE_FOLDER ${CALCULATOR_IDE_FOLDER})

ICMAKER_ADD_SOURCES(
  pPocketCalc.cpp
  gPocketCalc.cpp
  mAdd.cpp
  Logging.cpp
  )

ICMAKER_ADD_HEADERS(
  gPocketCalc.h
  mAdd.h
  Logging.h
  )

MCA_GENERATE_DESCRIPTIONS()

ICMAKER_LOCAL_CPPDEFINES(-DCALCULATOR_EXPORT_SYMBOLS)
ICMAKER_GLOBAL_CPPDEFINES(-D_IC_BUILDER_MCA2_CALCULATOR_)
ICMAKER_INCLUDE_DIRECTORIES(
  ${MCAL_KERNEL_INCLUDE_DIRS}
  ${MCAL_GENERAL_INCLUDE_DIRS}
  ${CALCULATOR_INCLUDE_DIRS}
  )

ICMAKER_INTERNAL_DEPENDENCIES(
  icl_core
  icl_core_config
  icl_core_logging
  mca2_kernel
  )

ICMAKER_BUILD_PROGRAM()

#projects/calculator/src/calculator/CMakeLists.txt

Make

You can compile everything from your build directory by running

[build-dir] $ make

as before. If there have been changes to any CMakeLists.txt files, cmake will be re-run automatically. To build only the project you're currently working on (and its dependencies), use

[build-dir] $ make calculator

Ninja

In case you use ninja execute

[build-dir] $ ninja calculator

< Back === Forward >

Personal tools