summaryrefslogtreecommitdiff
path: root/mk/cocotb.mk
blob: 240d808c51026d1c7c0fb9da59dabf3d050b9114 (plain)
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
targets += pydoc test

target/test/prepare = $(prepare_verilator_target)

cocotb_modules = $(call per_target,cocotb_modules)
pydoc_modules  = $(call per_target,pydoc_modules)

cocotb_pythonpath_decl = PYTHONPATH="$(subst $(space),:,$(strip $(cocotb_pythonpath)) $$PYTHONPATH)"

cocotb_pythonpath = \
  $(addprefix $(src)/, \
    $(foreach dep,$(dep_tree/$(rule_top)), \
      $(call core_paths,$(dep),cocotb_paths)))

define cocotb_setup_common
  $$(call target_var,cocotb_modules) := $$(strip $$(core_info/$$(rule_top)/cocotb_modules))

  ifeq (,$$(cocotb_modules))
    $$(error core '$$(rule_top)' has no cocotb test modules)
  endif
endef

define target/pydoc/setup
  $(cocotb_setup_common)

  $$(call target_var,pydoc_modules) := $$(strip $$(core_info/$$(rule_top)/pydoc_modules))

  ifeq (,$$(pydoc_modules))
    $$(error core '$$(rule_top)' has no modules for pydoc to cover)
  endif
endef

define target/test/setup
  $(setup_verilator_target)
  $(cocotb_setup_common)

  $$(call target_var,vl_main) = $$(cocotb_share)/lib/verilator/verilator.cpp
  $$(call target_var,vl_flags) += --vpi --public-flat-rw
  $$(call target_var,vl_ldflags) += \
    -Wl,-rpath,$$(cocotb_libdir),-rpath,$$(dir $$(cocotb_libpython)) -L$$(cocotb_libdir) \
    -lcocotbvpi_verilator -lgpi -lcocotb -lgpilog -lcocotbutils $$(cocotb_libpython)
endef

define target/pydoc/rules
  .PHONY: $$(rule_top_path)/pydoc

  $$(rule_top_path)/pydoc: | $$(obj)
	$$(call run,PYDOC) cd $$(obj) && $$(cocotb_pythonpath_decl) $$(PYDOC3) -w $$(pydoc_modules)

  $(call target_entrypoint,$$(rule_top_path)/pydoc)
endef

define target/test/rules
  $(verilator_target_rules)

  .PHONY: $$(rule_top_path)/test

  $$(rule_top_path)/test &: $$(vtop_exe) $$(call core_objs,$$(rule_top),obj_deps) | $$(obj)
	$$(call run_no_err,COCOTB) cd $$(obj) && rm -f log.txt results.xml && \
		LIBPYTHON_LOC=$$(cocotb_libpython) COCOTB_RESULTS_FILE=results.xml \
		$$(cocotb_pythonpath_decl) MODULE=$$(subst $$(space),$$(comma),$$(cocotb_modules)) \
		$$(src)/$$< | tee log.txt

  $(call target_entrypoint,$$(rule_top_path)/test)
endef