UseEcos.cmake 10 KB

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