Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Modules / UseEcos.cmake
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 #=============================================================================
20 # Copyright 2006-2009 Kitware, Inc.
21 #
22 # Distributed under the OSI-approved BSD License (the "License");
23 # see accompanying file Copyright.txt for details.
24 #
25 # This software is distributed WITHOUT ANY WARRANTY; without even the
26 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 # See the License for more information.
28 #=============================================================================
29 # (To distribute this file outside of CMake, substitute the full
30 #  License text for the above reference.)
31
32 # first check that ecosconfig is available
33 find_program(ECOSCONFIG_EXECUTABLE NAMES ecosconfig)
34 if(NOT ECOSCONFIG_EXECUTABLE)
35    message(SEND_ERROR "ecosconfig was not found. Either include it in the system path or set it manually using ccmake.")
36 else()
37    message(STATUS "Found ecosconfig: ${ECOSCONFIG_EXECUTABLE}")
38 endif()
39
40 # check that ECOS_REPOSITORY is set correctly
41 if (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")
43 else ()
44    message(STATUS "ECOS_REPOSITORY is set to $ENV{ECOS_REPOSITORY}")
45 endif ()
46
47 # check that tclsh (coming with TCL) is available, otherwise ecosconfig doesn't work
48 find_package(Tclsh)
49 if (NOT TCL_TCLSH)
50    message(SEND_ERROR "The TCL tclsh was not found. Please install TCL, it is required for building eCos applications.")
51 else ()
52    message(STATUS "tlcsh found: ${TCL_TCLSH}")
53 endif ()
54
55 #add the globale include-diretories
56 #usage: ECOS_ADD_INCLUDE_DIRECTORIES()
57 macro(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
68 endmacro()
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()
73 macro (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-")
85 endmacro ()
86
87 #usage: ECOS_USE_PPC_EABI_TOOLS()
88 macro (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-")
100 endmacro ()
101
102 #usage: ECOS_USE_I386_ELF_TOOLS()
103 macro (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-")
115 endmacro ()
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})
124 macro(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 ()
132 endmacro()
133
134 # the default ecos config file name
135 # maybe in future also out-of-source builds may be possible
136 set(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
140 macro(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 )
165 endmacro()
166
167 # get the directory of the current file, used later on in the file
168 get_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})
175 macro(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 supresses 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>   <FLAGS> -o <OBJECT> -c <SOURCE>")
189    set(CMAKE_CXX_COMPILE_OBJECT   "<CMAKE_CXX_COMPILER> <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 "-Wstrict-prototypes")
195    set (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_directory_properties(
218       PROPERTIES
219        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;"
220    )
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
235 endmacro()
236