UseEcos.cmake 10 KB

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