UseEcos.cmake 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # - This module defines variables and macros required to build eCos application.
  2. # This file contains the following macros:
  3. # ECOS_ADD_INCLUDE_DIRECTORIES() - add the eCos include dirs
  4. # ECOS_USE_ARM_ELF_TOOLS() - enable the ARM ELF toolchain for the directory where it is called
  5. # ECOS_USE_PPC_EABI_TOOLS() - enable the PowerPC toolchain for the directory where it is called
  6. # ECOS_ADD_EXECUTABLE(name source1 ... sourceN ) - create an executable for eCos
  7. # ECOS_ADJUST_DIRECTORY(source1 ... sourceN )
  8. #
  9. # It contains the following variables:
  10. # ECOS_DEFINITIONS
  11. # ECOSCONFIG_EXECUTABLE
  12. # for internal use only:
  13. # ECOS_ADD_TARGET_LIB
  14. INCLUDE(AddFileDependencies)
  15. #first check that ecosconfig is available
  16. FIND_PROGRAM(ECOSCONFIG_EXECUTABLE NAMES ecosconfig)
  17. IF(NOT ECOSCONFIG_EXECUTABLE)
  18. MESSAGE(SEND_ERROR "ecosconfig was not found. Either include it in the system path or set it manually using ccmake.")
  19. ELSE(NOT ECOSCONFIG_EXECUTABLE)
  20. MESSAGE(STATUS "Found ecosconfig: ${ECOSCONFIG_EXECUTABLE}")
  21. ENDIF(NOT ECOSCONFIG_EXECUTABLE)
  22. #check that ECOS_REPOSITORY is set correctly
  23. IF (NOT EXISTS $ENV{ECOS_REPOSITORY}/ecos.db)
  24. MESSAGE(SEND_ERROR "The environment variable ECOS_REPOSITORY is not set correctly. Set it to the directory which contains the file ecos.db")
  25. ELSE (NOT EXISTS $ENV{ECOS_REPOSITORY}/ecos.db)
  26. MESSAGE(STATUS "ECOS_REPOSITORY is set to $ENV{ECOS_REPOSITORY}")
  27. ENDIF (NOT EXISTS $ENV{ECOS_REPOSITORY}/ecos.db)
  28. #add the globale include-diretories
  29. #usage: ECOS_ADD_INCLUDE_DIRECTORIES()
  30. MACRO(ECOS_ADD_INCLUDE_DIRECTORIES)
  31. #check for ProjectSources.txt one level higher
  32. IF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../ProjectSources.txt)
  33. INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../)
  34. ELSE (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../ProjectSources.txt)
  35. INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/)
  36. ENDIF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../ProjectSources.txt)
  37. #the ecos include directory
  38. INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/ecos/install/include/)
  39. ENDMACRO(ECOS_ADD_INCLUDE_DIRECTORIES)
  40. #we want to compile for the xscale processor, in this case the following macro has to be called
  41. #usage: ECOS_USE_ARM_ELF_TOOLS()
  42. MACRO (ECOS_USE_ARM_ELF_TOOLS)
  43. SET(CMAKE_CXX_COMPILER "arm-elf-c++")
  44. SET(CMAKE_COMPILER_IS_GNUCXX 1)
  45. SET(CMAKE_C_COMPILER "arm-elf-gcc")
  46. SET(CMAKE_AR "arm-elf-ar")
  47. SET(CMAKE_RANLIB "arm-elf-ranlib")
  48. #for linking
  49. SET(ECOS_LD_MCPU "-mcpu=xscale")
  50. #for compiling
  51. ADD_DEFINITIONS(-mcpu=xscale -mapcs-frame)
  52. #for the obj-tools
  53. SET(ECOS_ARCH_PREFIX "arm-elf-")
  54. ENDMACRO (ECOS_USE_ARM_ELF_TOOLS)
  55. #usage: ECOS_USE_PPC_EABI_TOOLS()
  56. MACRO (ECOS_USE_PPC_EABI_TOOLS)
  57. SET(CMAKE_CXX_COMPILER "powerpc-eabi-c++")
  58. SET(CMAKE_COMPILER_IS_GNUCXX 1)
  59. SET(CMAKE_C_COMPILER "powerpc-eabi-gcc")
  60. SET(CMAKE_AR "powerpc-eabi-ar")
  61. SET(CMAKE_RANLIB "powerpc-eabi-ranlib")
  62. #for linking
  63. SET(ECOS_LD_MCPU "")
  64. #for compiling
  65. ADD_DEFINITIONS()
  66. #for the obj-tools
  67. SET(ECOS_ARCH_PREFIX "powerpc-eabi-")
  68. ENDMACRO (ECOS_USE_PPC_EABI_TOOLS)
  69. #since the actual sources are located one level upwards
  70. #a "../" has to be prepended in front of every source file
  71. #call the following macro to achieve this, the first parameter
  72. #is the name of the new list of source files with adjusted paths,
  73. #followed by all source files
  74. #usage: ECOS_ADJUST_DIRECTORY(adjusted_SRCS ${my_srcs})
  75. MACRO(ECOS_ADJUST_DIRECTORY _target_FILES )
  76. FOREACH (_current_FILE ${ARGN})
  77. IF (${_current_FILE} MATCHES "^/.+") # don't adjust for absolute paths
  78. SET(tmp ${_current_FILE})
  79. ELSE (${_current_FILE} MATCHES "^/.+")
  80. SET(tmp ${CMAKE_CURRENT_SOURCE_DIR}/../${_current_FILE})
  81. GET_FILENAME_COMPONENT(tmp ${tmp} ABSOLUTE)
  82. ENDIF (${_current_FILE} MATCHES "^/.+")
  83. SET(${_target_FILES} ${${_target_FILES}} ${tmp})
  84. ENDFOREACH (_current_FILE)
  85. ENDMACRO(ECOS_ADJUST_DIRECTORY)
  86. #creates the dependancy from all source files on the ecos target.ld,
  87. #adds the command for compiling ecos and adds target.ld to the make_clean_files
  88. MACRO(ECOS_ADD_TARGET_LIB)
  89. #sources depend on target.ld
  90. FOREACH (_current_FILE ${ARGN})
  91. ADD_FILE_DEPENDENCIES(${_current_FILE} ${CMAKE_CURRENT_SOURCE_DIR}/ecos/install/lib/target.ld)
  92. ENDFOREACH (_current_FILE)
  93. #use a variable for the make_clean_files since later on even more files are added
  94. SET(ECOS_ADD_MAKE_CLEAN_FILES ${ECOS_ADD_MAKE_CLEAN_FILES};ecos/install/lib/target.ld)
  95. SET_DIRECTORY_PROPERTIES(
  96. PROPERTIES
  97. ADDITIONAL_MAKE_CLEAN_FILES "${ECOS_ADD_MAKE_CLEAN_FILES}" )
  98. ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/ecos/install/lib/target.ld
  99. COMMAND sh
  100. ARGS -c \"make -C ecos || exit -1\; if [ -e ecos/install/lib/target.ld ] \; then touch ecos/install/lib/target.ld\; fi\"
  101. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ecos/makefile
  102. )
  103. ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/ecos/makefile
  104. COMMAND sh
  105. ARGS -c \" cd ecos\; ${ECOSCONFIG_EXECUTABLE} tree || exit -1\;\"
  106. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ecos/ecos.ecc
  107. )
  108. ADD_CUSTOM_TARGET( ecos make -C ${CMAKE_CURRENT_SOURCE_DIR}/ecos/ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ecos/makefile )
  109. ENDMACRO(ECOS_ADD_TARGET_LIB)
  110. #macro for creating an executable ecos application
  111. #the first parameter is the name of the executable,
  112. #the second is the list of all source files (where the path
  113. #has been adjusted beforehand by calling ECOS_ADJUST_DIRECTORY()
  114. #usage: ECOS_ADD_EXECUTABLE(my_app ${adjusted_SRCS})
  115. MACRO(ECOS_ADD_EXECUTABLE _exe_NAME )
  116. #definitions, valid for all ecos projects
  117. #the optimization and "-g" for debugging has to be enabled
  118. #in the project-specific CMakeLists.txt
  119. ADD_DEFINITIONS(-D__ECOS__=1 -D__ECOS=1)
  120. SET(ECOS_DEFINITIONS -Wall -Wno-long-long -pipe -fno-builtin)
  121. SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
  122. SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual -fno-rtti -Wctor-dtor-privacy -fno-strict-aliasing -fno-exceptions")
  123. #the executable depends on ecos target.ld
  124. ECOS_ADD_TARGET_LIB(${ARGN})
  125. #special link commands for ecos-executables
  126. 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}")
  127. 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}")
  128. ADD_EXECUTABLE(${_exe_NAME} ${ARGN})
  129. #under win32 the ".exe" suffix is appended to the binary name, copy it so that it exists also without the prefix
  130. #otherwise the following custom commands don't work
  131. IF(WIN32)
  132. ADD_CUSTOM_COMMAND(TARGET ${_exe_NAME} POST_BUILD COMMAND cp ARGS ${_exe_NAME}.exe ${_exe_NAME}_ )
  133. ADD_CUSTOM_COMMAND(TARGET ${_exe_NAME} POST_BUILD COMMAND mv ARGS ${_exe_NAME}_ ${_exe_NAME} )
  134. ENDIF(WIN32)
  135. #create a binary file
  136. ADD_CUSTOM_COMMAND(
  137. TARGET ${_exe_NAME}
  138. POST_BUILD
  139. COMMAND ${ECOS_ARCH_PREFIX}objcopy
  140. ARGS -O binary ${_exe_NAME} ${_exe_NAME}.bin
  141. )
  142. #and an srec file
  143. ADD_CUSTOM_COMMAND(
  144. TARGET ${_exe_NAME}
  145. POST_BUILD
  146. COMMAND ${ECOS_ARCH_PREFIX}objcopy
  147. ARGS -O srec ${_exe_NAME} ${_exe_NAME}.srec
  148. )
  149. #add the created files to the make_clean_files
  150. SET(ECOS_ADD_MAKE_CLEAN_FILES ${ECOS_ADD_MAKE_CLEAN_FILES};${_exe_NAME};${_exe_NAME}.bin;${_exe_NAME}.srec;${_exe_NAME}.lst;)
  151. SET_DIRECTORY_PROPERTIES(
  152. PROPERTIES
  153. ADDITIONAL_MAKE_CLEAN_FILES "${ECOS_ADD_MAKE_CLEAN_FILES}"
  154. )
  155. #cd $1; ls -a | grep --invert-match -e "\(.*CVS\)\|\(.*ecos\.ecc\)\|\(.*\.cvsignore\)\|\(\.\.\?\)" | xargs rm -rf; touch ecos.ecc
  156. 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 \")
  157. ADD_CUSTOM_TARGET(normalclean ${CMAKE_MAKE_PROGRAM} clean -C ${CMAKE_CURRENT_SOURCE_DIR})
  158. ADD_DEPENDENCIES (ecosclean normalclean)
  159. ADD_DEPENDENCIES(ecosclean clean)
  160. ADD_CUSTOM_TARGET( listing
  161. COMMAND echo -e \"\\n--- Symbols sorted by address ---\\n\" > ${_exe_NAME}.lst
  162. COMMAND ${ECOS_ARCH_PREFIX}nm -S -C -n ${_exe_NAME} >> ${_exe_NAME}.lst
  163. COMMAND echo -e \"\\n--- Symbols sorted by size ---\\n\" >> ${_exe_NAME}.lst
  164. COMMAND ${ECOS_ARCH_PREFIX}nm -S -C -r --size-sort ${_exe_NAME} >> ${_exe_NAME}.lst
  165. COMMAND echo -e \"\\n--- Full assembly listing ---\\n\" >> ${_exe_NAME}.lst
  166. COMMAND ${ECOS_ARCH_PREFIX}objdump -S -x -d -C ${_exe_NAME} >> ${_exe_NAME}.lst )
  167. ENDMACRO(ECOS_ADD_EXECUTABLE)