UseEcos.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #.rst:
  2. # UseEcos
  3. # -------
  4. #
  5. # This module defines variables and macros required to build eCos application.
  6. #
  7. # This file contains the following macros:
  8. # ECOS_ADD_INCLUDE_DIRECTORIES() - add the eCos include dirs
  9. # ECOS_ADD_EXECUTABLE(name source1 ... sourceN ) - create an eCos
  10. # executable ECOS_ADJUST_DIRECTORY(VAR source1 ... sourceN ) - adjusts
  11. # the path of the source files and puts the result into VAR
  12. #
  13. # Macros for selecting the toolchain: ECOS_USE_ARM_ELF_TOOLS() - enable
  14. # the ARM ELF toolchain for the directory where it is called
  15. # ECOS_USE_I386_ELF_TOOLS() - enable the i386 ELF toolchain for the
  16. # directory where it is called ECOS_USE_PPC_EABI_TOOLS() - enable the
  17. # PowerPC toolchain for the directory where it is called
  18. #
  19. # It contains the following variables: ECOS_DEFINITIONS
  20. # ECOSCONFIG_EXECUTABLE ECOS_CONFIG_FILE - defaults to ecos.ecc, if your
  21. # eCos configuration file has a different name, adjust this variable for
  22. # internal use only:
  23. #
  24. # ::
  25. #
  26. # ECOS_ADD_TARGET_LIB
  27. #=============================================================================
  28. # Copyright 2006-2009 Kitware, Inc.
  29. #
  30. # Distributed under the OSI-approved BSD License (the "License");
  31. # see accompanying file Copyright.txt for details.
  32. #
  33. # This software is distributed WITHOUT ANY WARRANTY; without even the
  34. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  35. # See the License for more information.
  36. #=============================================================================
  37. # (To distribute this file outside of CMake, substitute the full
  38. # License text for the above reference.)
  39. # first check that ecosconfig is available
  40. find_program(ECOSCONFIG_EXECUTABLE NAMES ecosconfig)
  41. if(NOT ECOSCONFIG_EXECUTABLE)
  42. message(SEND_ERROR "ecosconfig was not found. Either include it in the system path or set it manually using ccmake.")
  43. else()
  44. message(STATUS "Found ecosconfig: ${ECOSCONFIG_EXECUTABLE}")
  45. endif()
  46. # check that ECOS_REPOSITORY is set correctly
  47. if (NOT EXISTS $ENV{ECOS_REPOSITORY}/ecos.db)
  48. message(SEND_ERROR "The environment variable ECOS_REPOSITORY is not set correctly. Set it to the directory which contains the file ecos.db")
  49. else ()
  50. message(STATUS "ECOS_REPOSITORY is set to $ENV{ECOS_REPOSITORY}")
  51. endif ()
  52. # check that tclsh (coming with TCL) is available, otherwise ecosconfig doesn't work
  53. find_package(Tclsh)
  54. if (NOT TCL_TCLSH)
  55. message(SEND_ERROR "The TCL tclsh was not found. Please install TCL, it is required for building eCos applications.")
  56. else ()
  57. message(STATUS "tlcsh found: ${TCL_TCLSH}")
  58. endif ()
  59. #add the globale include-diretories
  60. #usage: ECOS_ADD_INCLUDE_DIRECTORIES()
  61. macro(ECOS_ADD_INCLUDE_DIRECTORIES)
  62. #check for ProjectSources.txt one level higher
  63. if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../ProjectSources.txt)
  64. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../)
  65. else ()
  66. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/)
  67. endif ()
  68. #the ecos include directory
  69. include_directories(${CMAKE_CURRENT_BINARY_DIR}/ecos/install/include/)
  70. endmacro()
  71. #we want to compile for the xscale processor, in this case the following macro has to be called
  72. #usage: ECOS_USE_ARM_ELF_TOOLS()
  73. macro (ECOS_USE_ARM_ELF_TOOLS)
  74. set(CMAKE_CXX_COMPILER "arm-elf-c++")
  75. set(CMAKE_COMPILER_IS_GNUCXX 1)
  76. set(CMAKE_C_COMPILER "arm-elf-gcc")
  77. set(CMAKE_AR "arm-elf-ar")
  78. set(CMAKE_RANLIB "arm-elf-ranlib")
  79. #for linking
  80. set(ECOS_LD_MCPU "-mcpu=xscale")
  81. #for compiling
  82. add_definitions(-mcpu=xscale -mapcs-frame)
  83. #for the obj-tools
  84. set(ECOS_ARCH_PREFIX "arm-elf-")
  85. endmacro ()
  86. #usage: ECOS_USE_PPC_EABI_TOOLS()
  87. macro (ECOS_USE_PPC_EABI_TOOLS)
  88. set(CMAKE_CXX_COMPILER "powerpc-eabi-c++")
  89. set(CMAKE_COMPILER_IS_GNUCXX 1)
  90. set(CMAKE_C_COMPILER "powerpc-eabi-gcc")
  91. set(CMAKE_AR "powerpc-eabi-ar")
  92. set(CMAKE_RANLIB "powerpc-eabi-ranlib")
  93. #for linking
  94. set(ECOS_LD_MCPU "")
  95. #for compiling
  96. add_definitions()
  97. #for the obj-tools
  98. set(ECOS_ARCH_PREFIX "powerpc-eabi-")
  99. endmacro ()
  100. #usage: ECOS_USE_I386_ELF_TOOLS()
  101. macro (ECOS_USE_I386_ELF_TOOLS)
  102. set(CMAKE_CXX_COMPILER "i386-elf-c++")
  103. set(CMAKE_COMPILER_IS_GNUCXX 1)
  104. set(CMAKE_C_COMPILER "i386-elf-gcc")
  105. set(CMAKE_AR "i386-elf-ar")
  106. set(CMAKE_RANLIB "i386-elf-ranlib")
  107. #for linking
  108. set(ECOS_LD_MCPU "")
  109. #for compiling
  110. add_definitions()
  111. #for the obj-tools
  112. set(ECOS_ARCH_PREFIX "i386-elf-")
  113. endmacro ()
  114. #since the actual sources are located one level upwards
  115. #a "../" has to be prepended in front of every source file
  116. #call the following macro to achieve this, the first parameter
  117. #is the name of the new list of source files with adjusted paths,
  118. #followed by all source files
  119. #usage: ECOS_ADJUST_DIRECTORY(adjusted_SRCS ${my_srcs})
  120. macro(ECOS_ADJUST_DIRECTORY _target_FILES )
  121. foreach (_current_FILE ${ARGN})
  122. get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
  123. if (NOT ${_abs_FILE} STREQUAL ${_current_FILE})
  124. get_filename_component(_abs_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../${_current_FILE} ABSOLUTE)
  125. endif ()
  126. list(APPEND ${_target_FILES} ${_abs_FILE})
  127. endforeach ()
  128. endmacro()
  129. # the default ecos config file name
  130. # maybe in future also out-of-source builds may be possible
  131. set(ECOS_CONFIG_FILE ecos.ecc)
  132. #creates the dependency from all source files on the ecos target.ld,
  133. #adds the command for compiling ecos
  134. macro(ECOS_ADD_TARGET_LIB)
  135. # when building out-of-source, create the ecos/ subdir
  136. if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/ecos)
  137. file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ecos)
  138. endif()
  139. #sources depend on target.ld
  140. set_source_files_properties(
  141. ${ARGN}
  142. PROPERTIES
  143. OBJECT_DEPENDS
  144. ${CMAKE_CURRENT_BINARY_DIR}/ecos/install/lib/target.ld
  145. )
  146. add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ecos/install/lib/target.ld
  147. COMMAND sh -c \"make -C ${CMAKE_CURRENT_BINARY_DIR}/ecos || exit -1\; if [ -e ${CMAKE_CURRENT_BINARY_DIR}/ecos/install/lib/target.ld ] \; then touch ${CMAKE_CURRENT_BINARY_DIR}/ecos/install/lib/target.ld\; fi\"
  148. DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/ecos/makefile
  149. )
  150. add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ecos/makefile
  151. COMMAND sh -c \" cd ${CMAKE_CURRENT_BINARY_DIR}/ecos\; ${ECOSCONFIG_EXECUTABLE} --config=${CMAKE_CURRENT_SOURCE_DIR}/ecos/${ECOS_CONFIG_FILE} tree || exit -1\;\"
  152. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ecos/${ECOS_CONFIG_FILE}
  153. )
  154. add_custom_target( ecos make -C ${CMAKE_CURRENT_BINARY_DIR}/ecos/ DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/ecos/makefile )
  155. endmacro()
  156. # get the directory of the current file, used later on in the file
  157. get_filename_component( ECOS_CMAKE_MODULE_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
  158. #macro for creating an executable ecos application
  159. #the first parameter is the name of the executable,
  160. #the second is the list of all source files (where the path
  161. #has been adjusted beforehand by calling ECOS_ADJUST_DIRECTORY()
  162. #usage: ECOS_ADD_EXECUTABLE(my_app ${adjusted_SRCS})
  163. macro(ECOS_ADD_EXECUTABLE _exe_NAME )
  164. #definitions, valid for all ecos projects
  165. #the optimization and "-g" for debugging has to be enabled
  166. #in the project-specific CMakeLists.txt
  167. add_definitions(-D__ECOS__=1 -D__ECOS=1)
  168. set(ECOS_DEFINITIONS -Wall -Wno-long-long -pipe -fno-builtin)
  169. #the executable depends on ecos target.ld
  170. ECOS_ADD_TARGET_LIB(${ARGN})
  171. # when using nmake makefiles, the custom buildtype supresses the default cl.exe flags
  172. # and the rules for creating objects are adjusted for gcc
  173. set(CMAKE_BUILD_TYPE CUSTOM_ECOS_BUILD)
  174. set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <FLAGS> -o <OBJECT> -c <SOURCE>")
  175. set(CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <FLAGS> -o <OBJECT> -c <SOURCE>")
  176. # special link commands for ecos-executables
  177. set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <OBJECTS> -o <TARGET> ${_ecos_EXTRA_LIBS} -nostdlib -nostartfiles -L${CMAKE_CURRENT_BINARY_DIR}/ecos/install/lib -Ttarget.ld ${ECOS_LD_MCPU}")
  178. set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <OBJECTS> -o <TARGET> ${_ecos_EXTRA_LIBS} -nostdlib -nostartfiles -L${CMAKE_CURRENT_BINARY_DIR}/ecos/install/lib -Ttarget.ld ${ECOS_LD_MCPU}")
  179. # some strict compiler flags
  180. set (CMAKE_C_FLAGS "-Wstrict-prototypes")
  181. set (CMAKE_CXX_FLAGS "-Woverloaded-virtual -fno-rtti -Wctor-dtor-privacy -fno-strict-aliasing -fno-exceptions")
  182. add_executable(${_exe_NAME} ${ARGN})
  183. set_target_properties(${_exe_NAME} PROPERTIES SUFFIX ".elf")
  184. #create a binary file
  185. add_custom_command(
  186. TARGET ${_exe_NAME}
  187. POST_BUILD
  188. COMMAND ${ECOS_ARCH_PREFIX}objcopy
  189. ARGS -O binary ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.elf ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.bin
  190. )
  191. #and an srec file
  192. add_custom_command(
  193. TARGET ${_exe_NAME}
  194. POST_BUILD
  195. COMMAND ${ECOS_ARCH_PREFIX}objcopy
  196. ARGS -O srec ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.elf ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.srec
  197. )
  198. #add the created files to the clean-files
  199. set_directory_properties(
  200. PROPERTIES
  201. ADDITIONAL_MAKE_CLEAN_FILES "${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.bin;${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.srec;${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst;"
  202. )
  203. add_custom_target(ecosclean ${CMAKE_COMMAND} -DECOS_DIR=${CMAKE_CURRENT_BINARY_DIR}/ecos/ -P ${ECOS_CMAKE_MODULE_DIR}/ecos_clean.cmake )
  204. add_custom_target(normalclean ${CMAKE_MAKE_PROGRAM} clean WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  205. add_dependencies (ecosclean normalclean)
  206. add_custom_target( listing
  207. COMMAND echo -e \"\\n--- Symbols sorted by address ---\\n\" > ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst
  208. COMMAND ${ECOS_ARCH_PREFIX}nm -S -C -n ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.elf >> ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst
  209. COMMAND echo -e \"\\n--- Symbols sorted by size ---\\n\" >> ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst
  210. COMMAND ${ECOS_ARCH_PREFIX}nm -S -C -r --size-sort ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.elf >> ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst
  211. COMMAND echo -e \"\\n--- Full assembly listing ---\\n\" >> ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst
  212. COMMAND ${ECOS_ARCH_PREFIX}objdump -S -x -d -C ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.elf >> ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst )
  213. endmacro()