UseEcos.cmake 9.7 KB

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