UseEcos.cmake 9.7 KB

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