[CMake] cmake module for creating eCos application

Alexander Neundorf a.neundorf-work at gmx.net
Tue Dec 6 15:59:51 EST 2005


Hi, 
 
attached you can find a cmake file containing macros for creating eCos 
applications. eCos is an open source embedded RTOS 
( http://ecos.sourceware.org ). 
 
I wrote an introduction for cmake-newbies how to use it: 
http://www.neundorf.net/cmake/using_ecos_with_cmake.php 
Please have a look at it. I'm not used to writing longer texts than 
emails in english, so also comments regarding the language are 
appreciated. 
 
It would be nice if the attached cmake file could be included in the 
cmake distribution.  
 
What do you think ? 
 
Bye 
Alex 
 

-- 
Lust, ein paar Euro nebenbei zu verdienen? Ohne Kosten, ohne Risiko!
Satte Provisionen für GMX Partner: http://www.gmx.net/de/go/partner
-------------- next part --------------
# this file contains the following macros:
# ECOS_ADD_INCLUDE_DIRECTORIES()
# ECOS_USE_ARM_ELF_TOOLS()
# ECOS_USE_PPC_EABI_TOOLS()
# ECOS_ADJUST_DIRECTORY()
# ECOS_ADD_EXECUTABLE()

#first check that ecosconfig is available
FIND_PROGRAM(ECOSCONFIG NAMES ecosconfig)
IF(NOT ECOSCONFIG)
   MESSAGE(SEND_ERROR "ecosconfig was not found. Either include it in the system path or set it manually using ccmake.")
ELSE(NOT ECOSCONFIG)
   MESSAGE(STATUS "Found ecosconfig: ${ECOSCONFIG}")
ENDIF(NOT ECOSCONFIG)

#check that ECOS_REPOSITORY is set correctly
IF (NOT EXISTS $ENV{ECOS_REPOSITORY}/ecos.db)
   MESSAGE(SEND_ERROR "The environment variable ECOS_REPOSITORY is not set correctly. Set it to the directory which contains the file ecos.db")
ELSE (NOT EXISTS $ENV{ECOS_REPOSITORY}/ecos.db)
   MESSAGE(STATUS "ECOS_REPOSITORY is set to $ENV{ECOS_REPOSITORY}")
ENDIF (NOT EXISTS $ENV{ECOS_REPOSITORY}/ecos.db)


#add the globale include-diretories
#usage: ECOS_ADD_INCLUDE_DIRECTORIES()
MACRO(ECOS_ADD_INCLUDE_DIRECTORIES)

#check for ProjectSources.txt one level higher
   IF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../ProjectSources.txt)
      INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../)
   ELSE (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../ProjectSources.txt)
      INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/)
   ENDIF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../ProjectSources.txt)

   IF (EXISTS ${CMAKE_SOURCE_DIR}/firmware/common/)
      INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/firmware/common/)
   ENDIF (EXISTS ${CMAKE_SOURCE_DIR}/firmware/common/)

#the ecos include directory
   INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/ecos/install/include/)

ENDMACRO(ECOS_ADD_INCLUDE_DIRECTORIES)


#we want to compile for the xscale processor, in this case the following macro has to be called
#usage: ECOS_USE_ARM_ELF_TOOLS()
MACRO (ECOS_USE_ARM_ELF_TOOLS)
   SET(CMAKE_CXX_COMPILER "arm-elf-c++")
   SET(CMAKE_COMPILER_IS_GNUCXX 1)
   SET(CMAKE_C_COMPILER "arm-elf-gcc")
   SET(CMAKE_AR "arm-elf-ar")
   SET(CMAKE_RANLIB "arm-elf-ranlib")
#for linking
   SET(ECOS_LD_MCPU "-mcpu=xscale")
#for compiling
   ADD_DEFINITIONS(-mcpu=xscale -mapcs-frame)
#for the obj-tools
   SET(ECOS_ARCH_PREFIX "arm-elf-")
ENDMACRO (ECOS_USE_ARM_ELF_TOOLS)

#usage: ECOS_USE_PPC_EABI_TOOLS()
MACRO (ECOS_USE_PPC_EABI_TOOLS)
   SET(CMAKE_CXX_COMPILER "powerpc-eabi-c++")
   SET(CMAKE_COMPILER_IS_GNUCXX 1)
   SET(CMAKE_C_COMPILER "powerpc-eabi-gcc")
   SET(CMAKE_AR "powerpc-eabi-ar")
   SET(CMAKE_RANLIB "powerpc-eabi-ranlib")
#for linking
   SET(ECOS_LD_MCPU "")
#for compiling
   ADD_DEFINITIONS()
#for the obj-tools
   SET(ECOS_ARCH_PREFIX "powerpc-eabi-")
ENDMACRO (ECOS_USE_PPC_EABI_TOOLS)


#since the actual sources are located one level upwards
#a "../" has to be prepended in front of every source file
#call the following macro to achieve this, the first parameter
#is the name of the new list of source files with adjusted paths,
#followed by all source files
#usage: ECOS_ADJUST_DIRECTORY(adjusted_SRCS ${my_srcs})
MACRO(ECOS_ADJUST_DIRECTORY _target_FILES )
   FOREACH (_current_FILE ${ARGN})
      IF (${_current_FILE} MATCHES "^/.+")  # don't adjust for absolute paths
         SET(tmp ${_current_FILE})
      ELSE (${_current_FILE} MATCHES "^/.+")
         SET(tmp ${CMAKE_CURRENT_SOURCE_DIR}/../${_current_FILE})
         GET_FILENAME_COMPONENT(tmp ${tmp} ABSOLUTE)
      ENDIF (${_current_FILE} MATCHES "^/.+")

      SET(${_target_FILES} ${${_target_FILES}} ${tmp})
   ENDFOREACH (_current_FILE)
ENDMACRO(ECOS_ADJUST_DIRECTORY)

MACRO(ADD_FILE_DEPENDANCY file)
   SET(${file}_deps ${${file}_deps} ${ARGN})
   SET_SOURCE_FILES_PROPERTIES(
      ${file}
      PROPERTIES
      OBJECT_DEPENDS
      "${${file}_deps}"
   )
ENDMACRO(ADD_FILE_DEPENDANCY)

#creates the dependancy from all source files on the ecos target.ld,
#adds the command for compiling ecos and adds target.ld to the make_clean_files
MACRO(ECOS_ADD_TARGET_LIB)
#sources depend on target.ld
    FOREACH (_current_FILE ${ARGN})
      ADD_FILE_DEPENDANCY(${_current_FILE} ${CMAKE_CURRENT_SOURCE_DIR}/ecos/install/lib/target.ld)
    ENDFOREACH (_current_FILE)

#use a variable for the make_clean_files since later on even more files are added
   SET(ECOS_ADD_MAKE_CLEAN_FILES ${ECOS_ADD_MAKE_CLEAN_FILES};ecos/install/lib/target.ld)
   SET_DIRECTORY_PROPERTIES(
      PROPERTIES
       ADDITIONAL_MAKE_CLEAN_FILES "${ECOS_ADD_MAKE_CLEAN_FILES}" )

   ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/ecos/install/lib/target.ld
      COMMAND sh
      ARGS -c \"make -C ecos || exit -1\; if [ -e ecos/install/lib/target.ld ] \; then touch ecos/install/lib/target.ld\; fi\"
      DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ecos/makefile
   )

   ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/ecos/makefile
      COMMAND sh
      ARGS -c \" cd ecos\; ${ECOSCONFIG} tree || exit -1\;\"
      DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ecos/ecos.ecc
   )

   ADD_CUSTOM_TARGET( ecos make -C ${CMAKE_CURRENT_SOURCE_DIR}/ecos/ DEPENDS  ${CMAKE_CURRENT_SOURCE_DIR}/ecos/makefile )
ENDMACRO(ECOS_ADD_TARGET_LIB)


#macro for creating an executable ecos application
#the first parameter is the name of the executable,
#the second is the list of all source files (where the path
#has been adjusted beforehand by calling ECOS_ADJUST_DIRECTORY()
#usage: ECOS_ADD_EXECUTABLE(my_app ${adjusted_SRCS})
MACRO(ECOS_ADD_EXECUTABLE _exe_NAME )
   #definitions, valid for all ecos projects
   #the optimization and "-g" for debugging has to be enabled
   #in the project-specific CMakeLists.txt
   ADD_DEFINITIONS(-D__ECOS__ -Wall -Wno-long-long -pipe -fno-builtin)

   SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
   SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual -fno-rtti -Wctor-dtor-privacy -fno-strict-aliasing -fno-exceptions")

#the executable depends on ecos target.ld
   ECOS_ADD_TARGET_LIB(${ARGN})

#special link commands for ecos-executables
   SET(CMAKE_CXX_LINK_EXECUTABLE  "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <OBJECTS>  -o <TARGET> ${_ecos_EXTRA_LIBS} -nostdlib  -nostartfiles -L${CMAKE_CURRENT_SOURCE_DIR}/ecos/install/lib -Ttarget.ld ${ECOS_LD_MCPU}")
   SET(CMAKE_C_LINK_EXECUTABLE  "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <OBJECTS>  -o <TARGET> ${_ecos_EXTRA_LIBS} -nostdlib  -nostartfiles -L${CMAKE_CURRENT_SOURCE_DIR}/ecos/install/lib -Ttarget.ld ${ECOS_LD_MCPU}")

   ADD_EXECUTABLE(${_exe_NAME} ${ARGN})

#under win32 the ".exe" suffix is appended to the binary name, copy it so that it exists also without the prefix
#otherwise the following custom commands don't work
   IF(WIN32)
      ADD_CUSTOM_COMMAND(TARGET ${_exe_NAME} POST_BUILD COMMAND cp ARGS ${_exe_NAME}.exe ${_exe_NAME}_ )
      ADD_CUSTOM_COMMAND(TARGET ${_exe_NAME} POST_BUILD COMMAND mv ARGS ${_exe_NAME}_ ${_exe_NAME} )
   ENDIF(WIN32)

#create a binary file
   ADD_CUSTOM_COMMAND(
      TARGET ${_exe_NAME}
      POST_BUILD
      COMMAND ${ECOS_ARCH_PREFIX}objcopy
      ARGS -O binary ${_exe_NAME} ${_exe_NAME}.bin
   )

#and an srec file
   ADD_CUSTOM_COMMAND(
      TARGET ${_exe_NAME}
      POST_BUILD
      COMMAND ${ECOS_ARCH_PREFIX}objcopy
      ARGS -O srec ${_exe_NAME} ${_exe_NAME}.srec
   )

#add the created files to the make_clean_files
   SET(ECOS_ADD_MAKE_CLEAN_FILES ${ECOS_ADD_MAKE_CLEAN_FILES};${_exe_NAME};${_exe_NAME}.bin;${_exe_NAME}.srec;${_exe_NAME}.lst;)

   SET_DIRECTORY_PROPERTIES(
      PROPERTIES
       ADDITIONAL_MAKE_CLEAN_FILES "${ECOS_ADD_MAKE_CLEAN_FILES}"
   )

#   ADD_CUSTOM_TARGET(ecosclean sh ${CMAKE_SOURCE_DIR}/cmake/make_ecos_clean.sh ${CMAKE_CURRENT_SOURCE_DIR}/ecos)
   ADD_CUSTOM_TARGET(ecosclean sh -c \"cd ${CMAKE_CURRENT_SOURCE_DIR}/ecos\; ls -a | grep --invert-match -e \\\"\\\(.*CVS\\\)\\|\\\(.*ecos\\.ecc\\\)\\|\\\(.*\\.cvsignore\\\)\\|\\\(^\\.\\.\\?\\\)\\\" |xargs rm -rf\; touch ecos.ecc \")

#cd $1; ls -a  | grep --invert-match -e "\(.*CVS\)\|\(.*ecos\.ecc\)\|\(.*\.cvsignore\)\|\(\.\.\?\)" | xargs rm -rf;  touch ecos.ecc

   ADD_DEPENDENCIES(ecosclean clean)


   ADD_CUSTOM_TARGET( listing
      COMMAND echo -e   \"\\n--- Symbols sorted by address ---\\n\" > ${_exe_NAME}.lst
      COMMAND ${ECOS_ARCH_PREFIX}nm -S -C -n ${_exe_NAME} >> ${_exe_NAME}.lst
      COMMAND echo -e \"\\n--- Symbols sorted by size ---\\n\" >> ${_exe_NAME}.lst
      COMMAND ${ECOS_ARCH_PREFIX}nm -S -C -r --size-sort ${_exe_NAME} >> ${_exe_NAME}.lst
      COMMAND echo -e \"\\n--- Full assembly listing ---\\n\" >> ${_exe_NAME}.lst
      COMMAND ${ECOS_ARCH_PREFIX}objdump -S -x -d -C ${_exe_NAME} >> ${_exe_NAME}.lst )

ENDMACRO(ECOS_ADD_EXECUTABLE)



More information about the CMake mailing list