-
Notifications
You must be signed in to change notification settings - Fork 10
/
ament2riot.cmake
288 lines (247 loc) · 10.7 KB
/
ament2riot.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Copyright 2018 INRIA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################
# cmake2riot
################################################################
#
# This file should be used as a CMAKE_TOOLCHAIN_FILE
# Warning : It replace some common cmake functions,
# your project may not work properly.
#
################################################################
# Toolchain Files are called twice in the process...
if(CALL_ONCE)
return()
endif()
set(CALL_ONCE 1)
# Basic configuration of the toolchain
message("[cmake2riot] toolchain configuration")
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1)
# Define BOARD
if(NOT DEFINED BOARD)
if(DEFINED ENV{BOARD})
set(BOARD $ENV{BOARD})
else()
set(BOARD native)
endif()
endif()
# Define RIOT
set(RIOT 1)
# Create the RIOT module's Makefile
set(MAKEFILE_PATH "${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/Makefile")
file(WRITE "${MAKEFILE_PATH}" "")
file(APPEND "${MAKEFILE_PATH}" "MODULE = ${PROJECT_NAME}\n")
file(APPEND "${MAKEFILE_PATH}" "include $(RIOTBASE)/Makefile.base\n")
# Create the RIOT module's Makefile.include
set(MAKEFILE_INCLUDE_PATH "${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/Makefile.include")
file(WRITE "${MAKEFILE_INCLUDE_PATH}" "")
################################################################
# Replace try_compile
################################################################
macro(try_compile)
# Do nothing
endmacro()
################################################################
# Replace add_executable
################################################################
#
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
# [EXCLUDE_FROM_ALL]
# source1 [source2 ...])
#
################################################################
macro(add_executable target)
message("[cmake2riot] executing custom add_executable command")
# Parse arguments
set(options WIN32 MACOSX_BUNDLE EXCLUDE_FROM_ALL)
set(oneValueArgs "")
set(multiValueArgs "")
cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
# Display Warnings for unsupported parameters
foreach(opt in ${options})
if(args_${opt})
message(WARNING "[cmake2riot] ${opt} option is not supported")
endif()
endforeach()
# Make the directory where the RIOT project will be put
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/cmake2riot/${target})
# Foreach source arguments
set(${target}_sources "")
foreach(arg ${args_UNPARSED_ARGUMENTS})
# Search source file
if(IS_ABSOLUTE ${arg})
set(src ${arg})
elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${arg})
set(src ${arg})
elseif(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${arg})
set(src ${CMAKE_CURRENT_BINARY_DIR}/${arg})
else()
message(ERROR "Could not find source : ${arg}")
endif()
# Add install rule for source file
if("${target}" STREQUAL "${PROJECT_NAME}")
install(FILES ${src} DESTINATION ${target})
else()
install(FILES ${src} DESTINATION ${PROJECT_NAME}/${target})
endif()
# Add the source to ${target}_sources
set(${target}_sources ${${target}_sources} ${arg})
# Add the source directory to ${target}_src_dirs
get_filename_component(dir ${src} DIRECTORY)
list(APPEND ${target}_src_dirs ${dir})
endforeach(arg)
# Hack to get ament-generated *_BUILD_DEPENDS variables
unset(_AMENT_PACKAGE_NAME)
ament_package_xml()
# Fill the RIOT module's Makefile
file(WRITE "${MAKEFILE_PATH}" "APPLICATION = ${target}\n")
file(APPEND "${MAKEFILE_PATH}" "RIOTBASE ?= ${CMAKE_INSTALL_PREFIX}/RIOT\n")
file(APPEND "${MAKEFILE_PATH}" "BOARD ?= ${BOARD}\n")
file(APPEND "${MAKEFILE_PATH}" "QUIET ?= 1\n")
file(APPEND "${MAKEFILE_PATH}" "WERROR ?= 0\n")
if("${target}" STREQUAL "${PROJECT_NAME}")
foreach(src ${${target}_sources})
if("${src}" MATCHES ".c$|.cpp$")
file(APPEND "${MAKEFILE_PATH}" "SRC += ${src}\n")
else()
message(WARNING "[cmake2riot] adding a header to library : ${src}")
endif()
endforeach()
endif()
file(APPEND "${MAKEFILE_PATH}" "CFLAGS += -DROS_PACKAGE_NAME=\\\"${PROJECT_NAME}\\\"\n")
file(APPEND "${MAKEFILE_PATH}" "include ${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/Makefile.include\n")
file(APPEND "${MAKEFILE_PATH}" "include $(RIOTBASE)/Makefile.include\n")
# Fill the RIOT module's Makefile.include
foreach(dep ${${PROJECT_NAME}_BUILD_DEPENDS})
file(APPEND "${MAKEFILE_INCLUDE_PATH}" "DIRS += ${CMAKE_INSTALL_PREFIX}/${dep}\n")
file(APPEND "${MAKEFILE_INCLUDE_PATH}" "USEMODULE += ${dep}\n")
file(APPEND "${MAKEFILE_INCLUDE_PATH}" "include ${CMAKE_INSTALL_PREFIX}/${dep}/Makefile.include\n")
endforeach()
foreach(idir ${${PROJECT_NAME}_INCLUDE_DIRS})
file(APPEND "${MAKEFILE_INCLUDE_PATH}" "INCLUDES += -I${idir}\n")
endforeach()
# Add dummy executable target to enable related commands
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/dummy.c" "int main() {}")
_add_executable(${target} "${CMAKE_CURRENT_BINARY_DIR}/dummy.c")
# Add custom target to trigger sources generation if any
add_custom_target(${target}_dummy ALL DEPENDS ${${target}_sources})
endmacro()
################################################################
# Replace add_library
################################################################
#
# add_library(<name> [STATIC | SHARED | MODULE]
# [EXCLUDE_FROM_ALL]
# source1 [source2 ...])
#
################################################################
function(add_library target)
message("[cmake2riot] executing custom add_library command")
# Parse arguments
set(options STATIC SHARED MODULE EXCLUDE_FROM_ALL)
set(oneValueArgs "")
set(multiValueArgs "")
cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
# Display Warnings for unsupported parameters
foreach(opt in ${options})
if(args_${opt})
message(WARNING "[cmake2riot] ${opt} option is not supported")
endif()
endforeach()
# Make the directory where the RIOT project will be put
file(MAKE_DIRECTORY ${CMAKE_INSTALL_PREFIX}/${target})
# Foreach source arguments
set(${target}_sources "")
foreach(arg ${args_UNPARSED_ARGUMENTS})
# Search source file
if(IS_ABSOLUTE ${arg})
set(src ${arg})
elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${arg})
set(src ${arg})
elseif(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${arg})
set(src ${CMAKE_CURRENT_BINARY_DIR}/${arg})
else()
message(ERROR "Could not find source : ${arg}")
endif()
# Add install rule for source file
if("${target}" STREQUAL "${PROJECT_NAME}")
install(FILES ${src} DESTINATION ${target})
else()
install(FILES ${src} DESTINATION ${PROJECT_NAME}/${target})
endif()
# Add the source to ${target}_sources
set(${target}_sources ${${target}_sources} ${arg})
# Add the source directory to ${target}_src_dirs
get_filename_component(dir ${src} DIRECTORY)
list(APPEND ${target}_src_dirs ${dir})
endforeach(arg)
# Search additional sources files that are not listed in args (headers)
list(REMOVE_DUPLICATES ${target}_src_dirs)
foreach(dir ${${target}_src_dirs})
# Get unlisted headers
file(GLOB srcs "${dir}/*.h")
# Add install rule for unlisted files
if("${target}" STREQUAL "${PROJECT_NAME}")
install(FILES ${srcs} DESTINATION ${target})
else()
install(FILES ${srcs} DESTINATION ${PROJECT_NAME}/${target})
endif()
endforeach()
# Hack to get ament-generated *_BUILD_DEPENDS variables
unset(_AMENT_PACKAGE_NAME)
ament_package_xml()
# Fill the RIOT module's Makefile
if("${target}" STREQUAL "${PROJECT_NAME}")
foreach(src ${${target}_sources})
if("${src}" MATCHES ".c$|.cpp$")
file(APPEND "${MAKEFILE_PATH}" "SRC += ${src}\n")
else()
message(WARNING "[cmake2riot] adding a header to library : ${src}")
endif()
endforeach()
endif()
file(APPEND "${MAKEFILE_PATH}" "CFLAGS += -DROS_PACKAGE_NAME=\\\"${PROJECT_NAME}\\\"\n")
# Fill the RIOT module's Makefile.include
foreach(dep ${${PROJECT_NAME}_BUILD_DEPENDS})
file(APPEND "${MAKEFILE_INCLUDE_PATH}" "DIRS += ${CMAKE_INSTALL_PREFIX}/${dep}\n")
file(APPEND "${MAKEFILE_INCLUDE_PATH}" "USEMODULE += ${dep}\n")
file(APPEND "${MAKEFILE_INCLUDE_PATH}" "include ${CMAKE_INSTALL_PREFIX}/${dep}/Makefile.include\n")
endforeach()
foreach(idir ${${PROJECT_NAME}_INCLUDE_DIRS})
file(APPEND "${MAKEFILE_INCLUDE_PATH}" "INCLUDES += -I${idir}\n")
endforeach()
if(NOT "${target}" STREQUAL "${PROJECT_NAME}")
# Create the RIOT sub-module's Makefile
set(SUB_MAKEFILE_PATH "${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/${target}/Makefile")
file(WRITE "${SUB_MAKEFILE_PATH}" "MODULE = ${target}\n")
foreach(src ${${target}_sources})
if("${src}" MATCHES ".c$|.cpp$")
file(APPEND "${MAKEFILE_PATH}" "SRC += ${src}\n")
else()
message(WARNING "[cmake2riot] adding a header to library : ${src}")
endif()
endforeach()
file(APPEND "${SUB_MAKEFILE_PATH}" "include $(RIOTBASE)/Makefile.base\n")
# Add submodule to module's Makefile.include
file(APPEND "${MAKEFILE_INCLUDE_PATH}" "DIRS += ${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/${target}\n")
file(APPEND "${MAKEFILE_INCLUDE_PATH}" "USEMODULE += ${target}\n")
endif()
# Add dummy library target to enable related commands
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/dummy.c" "")
_add_library(${target} STATIC "${CMAKE_CURRENT_BINARY_DIR}/dummy.c")
# Add custom target to trigger sources generation if any
add_custom_target(${target}_dummy ALL DEPENDS ${${target}_sources})
endfunction()