CmakeEcos: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
mNo edit summary
No edit summary
Line 71: Line 71:


# here is the target environment located
# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH  /home/alex/src/ecos-arm-integrator/install )
SET(CMAKE_FIND_ROOT_PATH  /home/alex/src/ecos-arm-integrator/install /home/alex/ecos-arm-install )


# adjust the default behaviour of the FIND_XXX() commands:
# adjust the default behaviour of the FIND_XXX() commands:
Line 102: Line 102:
<pre>
<pre>
ADD_EXECUTABLE(hello main.c)
ADD_EXECUTABLE(hello main.c)
INSTALL(TARGETS hello DESTINATION bin)
</pre>
</pre>


Line 108: Line 109:
~/src/helloworld/ $ mkdir build
~/src/helloworld/ $ mkdir build
~/src/helloworld/ $ cd build
~/src/helloworld/ $ cd build
~/src/helloworld/build/ $ cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-eCos-arm-elf.cmake ..
~/src/helloworld/build/ $ cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-eCos-arm-elf.cmake -DCMAKE_INSTALL_PREFIX=/home/alex/ecos-arm-install ..
-- Configuring done
-- Configuring done
-- Generating done
-- Generating done
Line 117: Line 118:
Linking C executable hello.elf
Linking C executable hello.elf
[100%] Built target hello
[100%] Built target hello
~/src/helloworld/build/ $ make install
...
-- Installing /home/alex/ecos-arm-install/bin/hello
</pre>
</pre>



Revision as of 20:55, 17 July 2007

How to use CMake to cross compile software for eCos

Install the eCos host tools

To build eCos software, you need the host tools for eCos, ecosconfig and optionally configtool. Make sure both ecosconfig and configtool can be found via the PATH. There are different way how to get them.

  • For some Linux distributions there exist ready-to-use packages, which you can simply install. E.g. for Ubuntu this

is done using "apt-get install ecosconfig".

and at http://www.ecoscentric.com/devzone/configtool.shtml .

Install a toolchain for building eCos applications

You need to install a suitable cross compiling toolchain to be able to build software for eCos. You can download them either from ftp://ecos.sourceware.org/pub/ecos/gnutools or build it yourself following the instructions you can find at http://ecos.sourceware.org/build-toolchain.html . Install the toolchain to any location you like, e.g. $HOME/ecostoolchains/ . Make sure the compiler is available via PATH, e.g. EXPORT PATH=$HOME/ecostoolchains/arm-elf/bin:$PATH.

Get the eCos sources and set ECOS_REPOSITORY

eCos release 2.0 is very old, it is recommended to use current anonymous cvs. You can find instructions here: http://ecos.sourceware.org/anoncvs.html

If you can't access cvs you can also download a weekly snapshot of cvs provided by eCosCentric: http://www.ecoscentric.com/devzone/snapshots.shtml

Once you have the eCos sources installed somewhere on your development machine, e.g. in $HOME/ecos, set the ECOS_REPOSITORY environment variable. This is required for running ecosconfig and configtool. It has to point to the directory where the file ecos.db is located, e.g. $HOME/ecos/packages/ .

Build an eCos

eCos is a configurable operating system, so you have to configure the system you want to use. Here's a simple example for the ARM integrator board:

~/ $ cd src/ecos-arm-integrator
~/src/ecos-arm-integrator/ $ ecosconfig new integrator_arm9 default
~/src/ecos-arm-integrator/ $ ecosconfig tree
~/src/ecos-arm-integrator/ $ make
~/src/ecos-arm-integrator/ $ ls install/
include  lib
~/src/ecos-arm-integrator/ $ ls install/lib
extras.o  libextras.a  libtarget.a  target.ld  vectors.o

The first step will create an ecos.ecc file, which contains the configuration of the operating system. The second step uses this information to generate makefiles, which you use to build in the final make step. Once make has finished, you will find an include/ and a lib/ directory created in the install/ subdirectory. This is the target environment which will be used for compiling your software for this eCos.

Write a CMake toolchain file

For CMake to be able to crosscompile software, it requires you to write a toolchain file, which tells CMake some information about the toolchain. With the examples used above it will look like:

INCLUDE(CMakeForceCompiler)

# the name of the target operating system
SET(CMAKE_SYSTEM_NAME eCos)

# which compiler to use
# since the compiler can't build a simple program by default without knowing
# where the eCos target library is, the FORCE macros have to be used
# use arm-elf-gcc for C, the compiler id is "GNU", the sizeof(void*) is 4
CMAKE_FORCE_C_COMPILER(/home/alex/src/ecostoolchains/arm-elf/bin/arm-elf-gcc GNU 4)
# use arm-elf-g++ for C++, the compiler id is "GNU", the sizeof(void*) is 4
CMAKE_FORCE_CXX_COMPILER(/home/alex/src/ecostoolchains/arm-elf/bin/arm-elf-g++ GNU)

# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH  /home/alex/src/ecos-arm-integrator/install /home/alex/ecos-arm-install )

# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search 
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Save this file as Toolchain-eCos-arm-elf.cmake to some location where you will put all your toolchain files, e.g. $HOME.

Build the software for eCos

Let's say you have the classical hello world software with a CMake based buildsystem and want to build this for the eCos you built above. main.c:

include <stdio.h>

int main()
{
   printf("Hello world\n");
   return 0;
}

CMakeLists.txt:

ADD_EXECUTABLE(hello main.c)
INSTALL(TARGETS hello DESTINATION bin)

Then run CMake on it to generate the buildfiles, the important point is that you tell it to use the toochain file you just wrote:

~/src/helloworld/ $ mkdir build
~/src/helloworld/ $ cd build
~/src/helloworld/build/ $ cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-eCos-arm-elf.cmake -DCMAKE_INSTALL_PREFIX=/home/alex/ecos-arm-install ..
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/src/helloworld/build
~/src/helloworld/build/ $ make
Scanning dependencies of target hello
[100%] Building C object CMakeFiles/hello.dir/main.o
Linking C executable hello.elf
[100%] Built target hello
~/src/helloworld/build/ $ make install
...
-- Installing /home/alex/ecos-arm-install/bin/hello

So that's all. It actually doesn't matter whether it's just a "hello world" or some complex piece of software, the only difference is the usage of the toolchain file. If the software has all required configure checks, it should just build also for eCos.