forked from f4pga/f4pga-arch-defs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_targets.cmake
296 lines (271 loc) · 9.15 KB
/
file_targets.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
289
290
291
292
293
294
295
296
# This CMake include file defines utility functions for handling generated files
# that are required in another CMake directory other than the one where it was
# generated.
#
# Key functions:
#
# * ADD_FILE_TARGET - Creates a file target. All source files, whether
# generated or not should call this function.
# * GET_FILE_TARGET - Given a absolute or local source path, what is the target
# that will build this file.
# * GET_REL_TARGET - Given a absolute or local source path and prefix
# generates target name in a form $prefix_$file.
# * GET_FILE_LOCATION - Given a absolute or local source path, what is the
# actual location of the file.
#
# When writing targets that rely on files using these functions, never use a raw
# source path, e.g. ${CMAKE_CURRENT_SOURCE_DIR}/example_dir/example.file or
# example.file. Instead always call GET_FILE_LOCATION on the path. This is
# because the actual source location may not be in the source tree, but instead
# be in the build tree.
#
# When writing targets that rely on files using these functions, always DEPEND
# on both the file location (from GET_FILE_LOCATION) and the file target (from
# GET_FILE_TARGET). If you only DEPEND on the file location, the file will not
# be generated the first time through the build.
#
# Utility functions:
#
# * APPEND_FILE_LOCATION - Appends to a list the file location of specified
# file.
# * APPEND_FILE_DEPENDENCY - Appends to a list both the file location and file
# target of specified file.
function(GET_REL_TARGET var prefix src_file)
if(${src_file} MATCHES "^/")
set(SOURCE_LOCATION ${src_file})
else()
set(SOURCE_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/${src_file})
endif()
get_filename_component(CANON_LOCATION ${SOURCE_LOCATION}
ABSOLUTE
BASE_DIR ${symbiflow-arch-defs_SOURCE_DIR}
)
string(
REPLACE
"${symbiflow-arch-defs_SOURCE_DIR}"
""
REL_CANON_LOCATION
${CANON_LOCATION}
)
string(
REPLACE
"/"
"_"
TARGET_PATH
${REL_CANON_LOCATION}
)
set(${var} ${prefix}${TARGET_PATH} PARENT_SCOPE)
endfunction()
function(GET_FILE_TARGET var src_file)
get_rel_target(TARGET_PATH file ${src_file})
set(${var} ${TARGET_PATH} PARENT_SCOPE)
endfunction()
function(GET_FILE_LOCATION var src_file)
# Sets var in PARENT_SCOPE to file location for given src_file.
get_file_target(SRC_TARGET ${src_file})
get_target_property(SRC_LOCATION ${SRC_TARGET} LOCATION)
if("${SRC_LOCATION}" STREQUAL "NOT_FOUND")
message(
FATAL_ERROR
"File ${src_file} is not a valid verilog target, missing LOCATION."
)
endif()
set(${var} ${SRC_LOCATION} PARENT_SCOPE)
endfunction()
function(APPEND_FILE_LOCATION var src_file)
# Appends to list var in PARENT_SCOPE both file location for
# given src_file.
get_file_target(SRC_TARGET ${src_file})
get_target_property(SRC_LOCATION ${SRC_TARGET} LOCATION)
if("${SRC_LOCATION}" STREQUAL "NOT_FOUND")
message(
FATAL_ERROR
"File ${src_file} is not a valid verilog target, missing LOCATION."
)
endif()
list(APPEND ${var} ${SRC_LOCATION})
set(${var} "${${var}}" PARENT_SCOPE)
endfunction()
function(APPEND_FILE_DEPENDENCY var src_file)
# Appends to list var in PARENT_SCOPE both file location and file target for
# given src_file.
get_file_target(SRC_TARGET ${src_file})
get_target_property(SRC_LOCATION ${SRC_TARGET} LOCATION)
if("${SRC_LOCATION}" STREQUAL "NOT_FOUND")
message(
FATAL_ERROR
"File ${src_file} is not a valid verilog target, missing LOCATION."
)
endif()
list(APPEND ${var} ${SRC_TARGET})
list(APPEND ${var} ${SRC_LOCATION})
get_target_property(INCLUDE_FILES ${SRC_TARGET} INCLUDE_FILES)
foreach(SRC ${INCLUDE_FILES})
append_file_dependency(${var} ${SRC})
endforeach()
set(${var} "${${var}}" PARENT_SCOPE)
endfunction()
function(APPEND_FILE_INCLUDES var src_file)
# Appends to list var in PARENT_SCOPE both includes listed for file target for
# given src_file.
get_file_target(SRC_TARGET ${src_file})
get_target_property(SRC_INCLUDES ${SRC_TARGET} INCLUDES)
if("${SRC_INCLUDES}" STREQUAL "NOT_FOUND")
message(
FATAL_ERROR
"File ${SRC} is not a valid verilog target, missing INCLUDES list."
)
endif()
list(APPEND ${var} ${SRC_INCLUDES})
set(${var} "${${var}}" PARENT_SCOPE)
endfunction()
function(GET_VERILOG_INCLUDES var file)
# Appends to list var in PARENT_SCOPE all verilog source dependency based on
# scanning input at configure time.
#
# Note this function cannot be used at generation time because execute_process
# is called during configure step and generated files don't exist yet.
execute_process(
COMMAND
${PYTHON_EXECUTABLE} ${symbiflow-arch-defs_SOURCE_DIR}/utils/deps_verilog.py
--file_per_line ${CMAKE_CURRENT_SOURCE_DIR}/${file}
WORKING_DIRECTORY ${symbiflow-arch-defs_SOURCE_DIR}
OUTPUT_VARIABLE INCLUDES
)
string(
REPLACE
"\n"
";"
INCLUDES_LIST
"${INCLUDES}"
)
foreach(INCLUDE ${INCLUDES_LIST})
string(STRIP ${INCLUDE} INCLUDE)
if(NOT "${INCLUDE}" STREQUAL "")
get_filename_component(
ABS_PATH_TO_INCLUDE
${INCLUDE}
ABSOLUTE
BASE_DIR
${symbiflow-arch-defs_SOURCE_DIR}
)
list(APPEND ${var} ${ABS_PATH_TO_INCLUDE})
endif()
endforeach()
set(${var} "${${var}}" PARENT_SCOPE)
endfunction()
function(GET_XML_INCLUDES var file)
# Appends to list var in PARENT_SCOPE all xml source dependency based on
# scanning input at configure time.
#
# Note this function cannot be used at generation time because execute_process
# is called during configure step and generated files don't exist yet.
execute_process(
COMMAND
${PYTHON_EXECUTABLE} ${symbiflow-arch-defs_SOURCE_DIR}/utils/deps_xml.py
--file_per_line ${CMAKE_CURRENT_SOURCE_DIR}/${file}
WORKING_DIRECTORY ${symbiflow-arch-defs_SOURCE_DIR}
OUTPUT_VARIABLE INCLUDES
)
string(
REPLACE
"\n"
";"
INCLUDES_LIST
"${INCLUDES}"
)
foreach(INCLUDE ${INCLUDES_LIST})
string(STRIP ${INCLUDE} INCLUDE)
if(NOT "${INCLUDE}" STREQUAL "")
get_filename_component(
ABS_PATH_TO_INCLUDE
${INCLUDE}
ABSOLUTE
BASE_DIR
${symbiflow-arch-defs_SOURCE_DIR}
)
list(APPEND ${var} ${ABS_PATH_TO_INCLUDE})
endif()
endforeach()
set(${var} "${${var}}" PARENT_SCOPE)
endfunction()
function(ADD_FILE_TARGET)
# ~~~
# ADD_FILE_TARGET(
# FILE <source file location>
# [GENERATED | SCANNER_TYPE <verilog|xml>]
# [ABSOLUTE]
# )
# ~~~
#
# Creates new file target for given source location. Even if ADD_FILE_TARGET
# is being called on a file that is located in the binary directory, always
# pass the source location it would have if the source and binary directories
# were the same. This is important for dependency definitions that assume all
# inputs are in one folder.
#
# ADD_FILE_TARGET ensures that all sources are in one folder, because of how
# the verilog include directive is defined.
#
# SCANNER_TYPE argument can be used if GENERATED or ABSOLUTE are not set. Valid
# SCANNER_TYPE are "verilog" and "xml".
# ABSOLUTE parameter must be used to mark passed path is absolute
#
# GENERATED must be passed if the source file is generated and is placed
# within the binary directory.
set(options GENERATED ABSOLUTE)
set(oneValueArgs FILE SCANNER_TYPE)
set(multiValueArgs)
cmake_parse_arguments(
ADD_FILE_TARGET
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
get_file_target(TARGET_NAME ${ADD_FILE_TARGET_FILE})
set(INCLUDE_FILES "")
if(NOT ${ADD_FILE_TARGET_GENERATED})
if("${ADD_FILE_TARGET_SCANNER_TYPE}" STREQUAL "")
elseif("${ADD_FILE_TARGET_SCANNER_TYPE}" STREQUAL "verilog")
get_verilog_includes(INCLUDE_FILES ${ADD_FILE_TARGET_FILE})
elseif("${ADD_FILE_TARGET_SCANNER_TYPE}" STREQUAL "xml")
get_xml_includes(INCLUDE_FILES ${ADD_FILE_TARGET_FILE})
else()
message(
FATAL_ERROR "Unknown SCANNER_TYPE=${ADD_FILE_TARGET_SCANNER_TYPE}."
)
endif()
endif()
set(INCLUDE_FILES_TARGETS "")
foreach(INCLUDE ${INCLUDE_FILES})
append_file_dependency(INCLUDE_FILES_TARGETS ${INCLUDE})
endforeach()
if(NOT ${ADD_FILE_TARGET_GENERATED} AND NOT ${ADD_FILE_TARGET_ABSOLUTE})
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ADD_FILE_TARGET_FILE}
COMMAND
${CMAKE_COMMAND} -E create_symlink
${CMAKE_CURRENT_SOURCE_DIR}/${ADD_FILE_TARGET_FILE}
${CMAKE_CURRENT_BINARY_DIR}/${ADD_FILE_TARGET_FILE}
)
endif()
if(${ADD_FILE_TARGET_ABSOLUTE})
set(FILE_PATH ${ADD_FILE_TARGET_FILE})
else()
set(FILE_PATH ${CMAKE_CURRENT_BINARY_DIR}/${ADD_FILE_TARGET_FILE})
endif()
add_custom_target(
${TARGET_NAME}
DEPENDS
${FILE_PATH}
${INCLUDE_FILES_TARGETS}
)
set_target_properties(
${TARGET_NAME}
PROPERTIES LOCATION ${FILE_PATH}
)
set_target_properties(${TARGET_NAME} PROPERTIES INCLUDE_FILES "${INCLUDE_FILES}")
set_target_properties(${TARGET_NAME} PROPERTIES INCLUDES "")
set_target_properties(${TARGET_NAME} PROPERTIES INCLUDE_FILES "${INCLUDE_FILES}")
endfunction(ADD_FILE_TARGET)