Imported Upstream version 2.8.9
[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(NOT ECOSCONFIG_EXECUTABLE)
37    MESSAGE(STATUS "Found ecosconfig: ${ECOSCONFIG_EXECUTABLE}")
38 ENDIF(NOT ECOSCONFIG_EXECUTABLE)
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 (NOT EXISTS $ENV{ECOS_REPOSITORY}/ecos.db)
44    MESSAGE(STATUS "ECOS_REPOSITORY is set to $ENV{ECOS_REPOSITORY}")
45 ENDIF (NOT EXISTS $ENV{ECOS_REPOSITORY}/ecos.db)
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 (NOT TCL_TCLSH)
52    MESSAGE(STATUS "tlcsh found: ${TCL_TCLSH}")
53 ENDIF (NOT TCL_TCLSH)
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 (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../ProjectSources.txt)
62       INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/)
63    ENDIF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../ProjectSources.txt)
64
65 #the ecos include directory
66    INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/ecos/install/include/)
67
68 ENDMACRO(ECOS_ADD_INCLUDE_DIRECTORIES)
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 (ECOS_USE_ARM_ELF_TOOLS)
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 (ECOS_USE_PPC_EABI_TOOLS)
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 (ECOS_USE_I386_ELF_TOOLS)
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 (NOT ${_abs_FILE} STREQUAL ${_current_FILE})
130       LIST(APPEND ${_target_FILES} ${_abs_FILE})
131    ENDFOREACH (_current_FILE)
132 ENDMACRO(ECOS_ADJUST_DIRECTORY)
133
134 # the default ecos config file name
135 # maybe in the future also out-of-source builds may be possible
136 SET(ECOS_CONFIG_FILE ecos.ecc)
137
138 #creates the dependancy 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(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/ecos)
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(ECOS_ADD_TARGET_LIB)
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(ECOS_ADD_EXECUTABLE)
236