UseEcos.cmake 10.1 KB
Newer Older
1 2 3 4 5 6
#.rst:
# UseEcos
# -------
#
# This module defines variables and macros required to build eCos application.
#
7 8
# This file contains the following macros:
# ECOS_ADD_INCLUDE_DIRECTORIES() - add the eCos include dirs
9 10 11 12 13 14 15 16 17 18 19 20 21 22
# ECOS_ADD_EXECUTABLE(name source1 ...  sourceN ) - create an eCos
# executable ECOS_ADJUST_DIRECTORY(VAR source1 ...  sourceN ) - adjusts
# the path of the source files and puts the result into VAR
#
# Macros for selecting the toolchain: ECOS_USE_ARM_ELF_TOOLS() - enable
# the ARM ELF toolchain for the directory where it is called
# ECOS_USE_I386_ELF_TOOLS() - enable the i386 ELF toolchain for the
# directory where it is called ECOS_USE_PPC_EABI_TOOLS() - enable the
# PowerPC toolchain for the directory where it is called
#
# It contains the following variables: ECOS_DEFINITIONS
# ECOSCONFIG_EXECUTABLE ECOS_CONFIG_FILE - defaults to ecos.ecc, if your
# eCos configuration file has a different name, adjust this variable for
# internal use only:
23
#
24
# ::
25
#
26
#   ECOS_ADD_TARGET_LIB
27

28 29 30 31 32 33 34 35 36 37
#=============================================================================
# Copyright 2006-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
38
# (To distribute this file outside of CMake, substitute the full
39 40
#  License text for the above reference.)

41
# first check that ecosconfig is available
42 43 44
find_program(ECOSCONFIG_EXECUTABLE NAMES ecosconfig)
if(NOT ECOSCONFIG_EXECUTABLE)
   message(SEND_ERROR "ecosconfig was not found. Either include it in the system path or set it manually using ccmake.")
45
else()
46
   message(STATUS "Found ecosconfig: ${ECOSCONFIG_EXECUTABLE}")
47
endif()
48

49
# check that ECOS_REPOSITORY is set correctly
50 51
if (NOT EXISTS $ENV{ECOS_REPOSITORY}/ecos.db)
   message(SEND_ERROR "The environment variable ECOS_REPOSITORY is not set correctly. Set it to the directory which contains the file ecos.db")
52
else ()
53
   message(STATUS "ECOS_REPOSITORY is set to $ENV{ECOS_REPOSITORY}")
54
endif ()
55

56
# check that tclsh (coming with TCL) is available, otherwise ecosconfig doesn't work
57 58 59
find_package(Tclsh)
if (NOT TCL_TCLSH)
   message(SEND_ERROR "The TCL tclsh was not found. Please install TCL, it is required for building eCos applications.")
60
else ()
61
   message(STATUS "tlcsh found: ${TCL_TCLSH}")
62
endif ()
63 64 65

#add the globale include-diretories
#usage: ECOS_ADD_INCLUDE_DIRECTORIES()
66
macro(ECOS_ADD_INCLUDE_DIRECTORIES)
67
#check for ProjectSources.txt one level higher
68 69
   if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../ProjectSources.txt)
      include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../)
70
   else ()
71
      include_directories(${CMAKE_CURRENT_SOURCE_DIR}/)
72
   endif ()
73 74

#the ecos include directory
75
   include_directories(${CMAKE_CURRENT_BINARY_DIR}/ecos/install/include/)
76

77
endmacro()
78 79 80 81


#we want to compile for the xscale processor, in this case the following macro has to be called
#usage: ECOS_USE_ARM_ELF_TOOLS()
82 83 84 85 86 87
macro (ECOS_USE_ARM_ELF_TOOLS)
   set(CMAKE_CXX_COMPILER "arm-elf-c++")
   set(CMAKE_COMPILER_IS_GNUCXX 1)
   set(CMAKE_C_COMPILER "arm-elf-gcc")
   set(CMAKE_AR "arm-elf-ar")
   set(CMAKE_RANLIB "arm-elf-ranlib")
88
#for linking
89
   set(ECOS_LD_MCPU "-mcpu=xscale")
90
#for compiling
91
   add_definitions(-mcpu=xscale -mapcs-frame)
92
#for the obj-tools
93
   set(ECOS_ARCH_PREFIX "arm-elf-")
94
endmacro ()
95 96

#usage: ECOS_USE_PPC_EABI_TOOLS()
97 98 99 100 101 102
macro (ECOS_USE_PPC_EABI_TOOLS)
   set(CMAKE_CXX_COMPILER "powerpc-eabi-c++")
   set(CMAKE_COMPILER_IS_GNUCXX 1)
   set(CMAKE_C_COMPILER "powerpc-eabi-gcc")
   set(CMAKE_AR "powerpc-eabi-ar")
   set(CMAKE_RANLIB "powerpc-eabi-ranlib")
103
#for linking
104
   set(ECOS_LD_MCPU "")
105
#for compiling
106
   add_definitions()
107
#for the obj-tools
108
   set(ECOS_ARCH_PREFIX "powerpc-eabi-")
109
endmacro ()
110

111
#usage: ECOS_USE_I386_ELF_TOOLS()
112 113 114 115 116 117
macro (ECOS_USE_I386_ELF_TOOLS)
   set(CMAKE_CXX_COMPILER "i386-elf-c++")
   set(CMAKE_COMPILER_IS_GNUCXX 1)
   set(CMAKE_C_COMPILER "i386-elf-gcc")
   set(CMAKE_AR "i386-elf-ar")
   set(CMAKE_RANLIB "i386-elf-ranlib")
118
#for linking
119
   set(ECOS_LD_MCPU "")
120
#for compiling
121
   add_definitions()
122
#for the obj-tools
123
   set(ECOS_ARCH_PREFIX "i386-elf-")
124
endmacro ()
125

126 127 128 129 130 131 132

#since the actual sources are located one level upwards
#a "../" has to be prepended in front of every source file
#call the following macro to achieve this, the first parameter
#is the name of the new list of source files with adjusted paths,
#followed by all source files
#usage: ECOS_ADJUST_DIRECTORY(adjusted_SRCS ${my_srcs})
133 134 135 136 137
macro(ECOS_ADJUST_DIRECTORY _target_FILES )
   foreach (_current_FILE ${ARGN})
      get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
      if (NOT ${_abs_FILE} STREQUAL ${_current_FILE})
         get_filename_component(_abs_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../${_current_FILE} ABSOLUTE)
138
      endif ()
139
      list(APPEND ${_target_FILES} ${_abs_FILE})
140 141
   endforeach ()
endmacro()
142

143
# the default ecos config file name
144
# maybe in future also out-of-source builds may be possible
145
set(ECOS_CONFIG_FILE ecos.ecc)
146

147
#creates the dependency from all source files on the ecos target.ld,
148
#adds the command for compiling ecos
149
macro(ECOS_ADD_TARGET_LIB)
150
# when building out-of-source, create the ecos/ subdir
151 152
    if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/ecos)
        file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ecos)
153
    endif()
154

155
#sources depend on target.ld
156
   set_source_files_properties(
157
      ${ARGN}
158
      PROPERTIES
159 160 161
      OBJECT_DEPENDS
      ${CMAKE_CURRENT_BINARY_DIR}/ecos/install/lib/target.ld
   )
162

163
   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ecos/install/lib/target.ld
164
      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\"
165
      DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/ecos/makefile
166 167
   )

168
   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ecos/makefile
169
      COMMAND sh -c \" cd ${CMAKE_CURRENT_BINARY_DIR}/ecos\; ${ECOSCONFIG_EXECUTABLE} --config=${CMAKE_CURRENT_SOURCE_DIR}/ecos/${ECOS_CONFIG_FILE} tree || exit -1\;\"
170
      DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ecos/${ECOS_CONFIG_FILE}
171 172
   )

173
   add_custom_target( ecos make -C ${CMAKE_CURRENT_BINARY_DIR}/ecos/ DEPENDS  ${CMAKE_CURRENT_BINARY_DIR}/ecos/makefile )
174
endmacro()
175

176
# get the directory of the current file, used later on in the file
177
get_filename_component( ECOS_CMAKE_MODULE_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
178 179 180 181 182 183

#macro for creating an executable ecos application
#the first parameter is the name of the executable,
#the second is the list of all source files (where the path
#has been adjusted beforehand by calling ECOS_ADJUST_DIRECTORY()
#usage: ECOS_ADD_EXECUTABLE(my_app ${adjusted_SRCS})
184
macro(ECOS_ADD_EXECUTABLE _exe_NAME )
185 186 187
   #definitions, valid for all ecos projects
   #the optimization and "-g" for debugging has to be enabled
   #in the project-specific CMakeLists.txt
188 189
   add_definitions(-D__ECOS__=1 -D__ECOS=1)
   set(ECOS_DEFINITIONS -Wall -Wno-long-long -pipe -fno-builtin)
190 191 192 193

#the executable depends on ecos target.ld
   ECOS_ADD_TARGET_LIB(${ARGN})

194 195
# when using nmake makefiles, the custom buildtype supresses the default cl.exe flags
# and the rules for creating objects are adjusted for gcc
196 197 198
   set(CMAKE_BUILD_TYPE CUSTOM_ECOS_BUILD)
   set(CMAKE_C_COMPILE_OBJECT     "<CMAKE_C_COMPILER>   <FLAGS> -o <OBJECT> -c <SOURCE>")
   set(CMAKE_CXX_COMPILE_OBJECT   "<CMAKE_CXX_COMPILER> <FLAGS> -o <OBJECT> -c <SOURCE>")
199
# special link commands for ecos-executables
200 201
   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}")
   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}")
202
# some strict compiler flags
203 204
   set (CMAKE_C_FLAGS "-Wstrict-prototypes")
   set (CMAKE_CXX_FLAGS "-Woverloaded-virtual -fno-rtti -Wctor-dtor-privacy -fno-strict-aliasing -fno-exceptions")
205

206 207
   add_executable(${_exe_NAME} ${ARGN})
   set_target_properties(${_exe_NAME} PROPERTIES SUFFIX ".elf")
208 209

#create a binary file
210
   add_custom_command(
211 212 213
      TARGET ${_exe_NAME}
      POST_BUILD
      COMMAND ${ECOS_ARCH_PREFIX}objcopy
214
      ARGS -O binary ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.elf ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.bin
215 216 217
   )

#and an srec file
218
   add_custom_command(
219 220 221
      TARGET ${_exe_NAME}
      POST_BUILD
      COMMAND ${ECOS_ARCH_PREFIX}objcopy
222
      ARGS -O srec ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.elf ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.srec
223 224
   )

225
#add the created files to the clean-files
226
   set_directory_properties(
227
      PROPERTIES
228
       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;"
229 230
   )

231 232 233
   add_custom_target(ecosclean ${CMAKE_COMMAND} -DECOS_DIR=${CMAKE_CURRENT_BINARY_DIR}/ecos/ -P ${ECOS_CMAKE_MODULE_DIR}/ecos_clean.cmake  )
   add_custom_target(normalclean ${CMAKE_MAKE_PROGRAM} clean WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
   add_dependencies (ecosclean normalclean)
234 235


236
   add_custom_target( listing
237 238 239 240 241 242
      COMMAND echo -e   \"\\n--- Symbols sorted by address ---\\n\" > ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst
      COMMAND ${ECOS_ARCH_PREFIX}nm -S -C -n ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.elf >> ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst
      COMMAND echo -e \"\\n--- Symbols sorted by size ---\\n\" >> ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst
      COMMAND ${ECOS_ARCH_PREFIX}nm -S -C -r --size-sort ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.elf >> ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst
      COMMAND echo -e \"\\n--- Full assembly listing ---\\n\" >> ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst
      COMMAND ${ECOS_ARCH_PREFIX}objdump -S -x -d -C ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.elf >> ${CMAKE_CURRENT_BINARY_DIR}/${_exe_NAME}.lst )
243

244
endmacro()
245