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