makefilecocotb

Pass argument from makefile to cocotb testbench


Given the D-FF example from cocotb/examples/dff/. What is the proper way to pass an argument from the Makefile to the cocotb testbench dff_cocotb.py without modifying the native cocotb makefiles?

I tried to modify line 30 of cocotb/examples/dff/tests/Makefile:

sim:
    $(MODULE).py testarg

respectively

sim: $(MODULE).py
$(MODULE).py:
    $(MODULE).py testarg

which doesn't work and shows the error message:

usage: cocotb [-h] test
cocotb: error: too few arguments

Solution

  • Hmm. It looks like the Makefile launches the simulator, which in turn invokes cocotb via VPI hooks into the simulator. If I understand correctly, it specifies the target testbench to the cocotb framework through environment variables.

    This implies you may be able to pass your arguments to your $(MODULE).py using the environment as well. I.e., launch make as:

    MY_TB_ARGS=<whatver> make
    

    and in $(MODULE).py, access them via

    import os
    myTbArgs = os.environ['MY_TB_ARGS']