File size: 1,760 Bytes
2d66860
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Syntax:
#
# 	pmgen_command(<output>
# 		[<input>...]
# 		[PREFIX <prefix>]
# 		[DEBUG]
# 	)
#
# Builds `<output>_pm.h` in the current binary directory from pmgen source files `<input>`, which must have
# the `*.pmg` extension.  If `<input>...` contains more than one file, `<prefix>` must be provided.
#
# Defines the following variables:
#   - `PMGEN_<output>_DEFINED`: Boolean indicating whether this command was successfully invoked.
#   - `PMGEN_<output>_OUTPUT`: The header file generated by `pmgen`.
#
# Usage example:
#
# 	pmgen_command(my_dsp
# 		my_dsp.pmg
# 	)
# 	yosys_pass(my_dsp
# 		my_dsp.cc
# 		${PMGEN_my_dsp_OUTPUT}
# 	)
#
# Usage example with multiple files:
#
# 	pmgen_command(my_dsp
# 		my_dsp_macc.pmg
# 		my_dsp_carry.pmg
# 		PREFIX
# 			my_dsp
# 	)
#
function(pmgen_command arg_NAME)
	cmake_parse_arguments(PARSE_ARGV 1 arg "DEBUG" "PREFIX" "")
	set(arg_INPUTS ${arg_UNPARSED_ARGUMENTS})

	set(pmgen_script ${YOSYS_CMAKE_SOURCE_DIR}/passes/pmgen/pmgen.py)
	set(pmgen_output ${CMAKE_CURRENT_BINARY_DIR}/${arg_NAME}_pm.h)
	cmake_path(RELATIVE_PATH pmgen_output BASE_DIRECTORY ${YOSYS_CMAKE_BINARY_DIR} OUTPUT_VARIABLE pmgen_output_rel)
	add_custom_command(
		DEPENDS ${pmgen_script} ${arg_INPUTS}
		OUTPUT ${pmgen_output}
		COMMAND ${Python3_EXECUTABLE}
			${pmgen_script}
			"$<$<BOOL:${arg_DEBUG}>:-g>"
			"$<$<BOOL:${arg_PREFIX}>:-p;${arg_PREFIX}>"
			-o ${pmgen_output}
			${arg_INPUTS}
		COMMAND_EXPAND_LISTS
		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
		VERBATIM
		COMMENT "Compiling pattern matcher ${pmgen_output_rel}"
	)

	# The usage of this command is somewhat inspired by `flex_target()` and `bison_target()`.
	set(PMGEN_${arg_NAME}_DEFINED TRUE)
	set(PMGEN_${arg_NAME}_OUTPUT ${pmgen_output} PARENT_SCOPE)
endfunction()