Skip to content

Commit

Permalink
Cocotb TESTCASE (#78)
Browse files Browse the repository at this point in the history
Co-authored-by: Benoit Denkinger <[email protected]>
  • Loading branch information
benoitdenkinger and Benoit Denkinger authored Oct 1, 2024
1 parent feffecc commit d4950f8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
3 changes: 2 additions & 1 deletion cmake/sim/cocotb/add_cocotb_iverilog_tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ function(add_cocotb_iverilog_tests IP_LIB DIRECTORY)
include("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../../utils/subdirectory_search.cmake")
include("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../../utils/colours.cmake")

SUBDIRLIST(TEST_SUBDIRS ${DIRECTORY})
set(EXCLUDE_PATTERNS "_")
SUBDIRLIST_EXCLUDE(TEST_SUBDIRS ${DIRECTORY} "${EXCLUDE_PATTERNS}")

# Assume the IP library is the latest one provided if full name is not given
ip_assume_last(IP_LIB ${IP_LIB})
Expand Down
26 changes: 18 additions & 8 deletions cmake/sim/cocotb/cocotb_iverilog.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include_guard(GLOBAL)
# ]]]
function(cocotb_iverilog IP_LIB)
# Parse the function arguments
cmake_parse_arguments(ARG "" "TOP_MODULE;OUTDIR;EXECUTABLE;IVERILOG_CLI_FLAGS;TIMEUNIT;TIMEPRECISION;TOPLEVEL_LANG;TESTCASE;PATH_MODULE;COCOTB_TEST" "SIM_ARGS;PLUSARGS" ${ARGN})
cmake_parse_arguments(ARG "" "TOP_MODULE;OUTDIR;EXECUTABLE;IVERILOG_CLI_FLAGS;TIMEUNIT;TIMEPRECISION;TOPLEVEL_LANG;TESTCASE;COCOTB_TEST" "PATH_MODULE;SIM_ARGS;PLUSARGS" ${ARGN})
# Check for any unrecognized arguments
if(ARG_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION} passed unrecognized argument " "${ARG_UNPARSED_ARGUMENTS}")
Expand Down Expand Up @@ -70,7 +70,8 @@ function(cocotb_iverilog IP_LIB)
message(FATAL_ERROR "No cocotb module provided. Use the function argument COCOTB_TEST.")
endif()
if(ARG_PATH_MODULE)
set(PATH_MODULE ${ARG_PATH_MODULE})
# Column separated paths to python files/modules are needed
string(REPLACE ";" ":" PATH_MODULE "${ARG_PATH_MODULE}")
else()
message(FATAL_ERROR "No cocotb module path provided. Use the function argument PATH_MODULE.")
endif()
Expand Down Expand Up @@ -134,7 +135,7 @@ function(cocotb_iverilog IP_LIB)
endif()

# Set the stamp file path (is the stamp file really needed?)
set(STAMP_FILE "${BINARY_DIR}/${IP_LIB}_${CMAKE_CURRENT_FUNCTION}.stamp")
set(STAMP_FILE "${OUTDIR}/${IP_LIB}_${CMAKE_CURRENT_FUNCTION}.stamp")

# Add a custom command to run iverilog
add_custom_command(
Expand All @@ -149,15 +150,21 @@ function(cocotb_iverilog IP_LIB)
-f ${CMDS_FILE}
-o ${ARG_EXECUTABLE}
${SOURCES}
COMMAND touch ${STAMP_FILE}
COMMAND /bin/sh -c date > ${STAMP_FILE}
DEPENDS ${SOURCES}
COMMENT "Running iverilog on ${IP_LIB}"
)

# Use TESTCASE for target name if it exists to have a unique name
if(TESTCASE)
set(CUSTOM_TARGET_NAME ${TESTCASE})
else()
set(CUSTOM_TARGET_NAME ${COCOTB_TEST})
endif()
# Add a custom target that depends on the executable and stamp file
add_custom_target(
# Add cocotb module name to be able to create multiple targets
${COCOTB_TEST}
${CUSTOM_TARGET_NAME}
DEPENDS ${ARG_EXECUTABLE} ${STAMP_FILE} ${IP_LIB}
)

Expand Down Expand Up @@ -190,11 +197,13 @@ function(cocotb_iverilog IP_LIB)
string(STRIP ${COCOTB_LIB_VPI_ICARUS} COCOTB_LIB_VPI_ICARUS)

set(COCOTB_ENV_VARS
VIRTUAL_ENV=${Python3_VIRTUAL_ENV}
PYTHONPATH=${PATH_MODULE}
MODULE=${COCOTB_TEST}
TESTCASE=${TESTCASE}
TOPLEVEL=${TOP_MODULE}
TOPLEVEL_LANG=${TOPLEVEL_LANG}
COCOTB_RESULTS_FILE=${OUTDIR}/results.xml
)
set(COCOTB_IVERILOG_CMD
# iverilog run-time engine must be in your path
Expand All @@ -213,18 +222,19 @@ function(cocotb_iverilog IP_LIB)
# sim command prefix, e.g., for debugging: 'gdb --args'
${ARG_SIM_CMD_PREFIX}
${COCOTB_IVERILOG_CMD}
DEPENDS ${COCOTB_TEST}
DEPENDS ${CUSTOM_TARGET_NAME}
COMMENT "Running cocotb simulation on ${IP_LIB}"
WORKING_DIRECTORY ${OUTDIR}
)

# Set the command as a property to be easily found by add_test()
string(TOUPPER ${COCOTB_TEST} COCOTB_TEST_PROP)
string(TOUPPER ${CUSTOM_TARGET_NAME} COCOTB_TEST_PROP)
set_target_properties(${IP_LIB} PROPERTIES COCOTB_IVERILOG_${COCOTB_TEST_PROP}_CMD "${COCOTB_IVERILOG_CMD}")
set_target_properties(${IP_LIB} PROPERTIES COCOTB_IVERILOG_${COCOTB_TEST_PROP}_ENV "${COCOTB_ENV_VARS}")

# Add a custom target that depends on the executable and stamp file
add_custom_target(
run_${COCOTB_TEST}
run_${CUSTOM_TARGET_NAME}
DEPENDS ${COCOTB_RESULTS_FILE}
)

Expand Down
45 changes: 35 additions & 10 deletions cmake/utils/subdirectory_search.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,40 @@ include_guard(GLOBAL)

# https://stackoverflow.com/questions/7787823/cmake-how-to-get-the-name-of-all-subdirectories-of-a-directory

MACRO(SUBDIRLIST result curdir)
FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
SET(dirlist "")
FOREACH(child ${children})
IF(IS_DIRECTORY ${curdir}/${child})
LIST(APPEND dirlist ${child})
ENDIF()
ENDFOREACH()
SET(${result} ${dirlist})
ENDMACRO()
macro(SUBDIRLIST output_var dir)
file(GLOB children RELATIVE ${dir} ${dir}/*)
set(dirlist "")
foreach(child ${children})
if(IS_DIRECTORY ${dir}/${child})
list(APPEND dirlist ${child})
endif()
endforeach()
set(${output_var} ${dirlist})
endmacro()

macro(SUBDIRLIST_EXCLUDE output_var dir excluded_patterns)
# Get all subdirectories
SUBDIRLIST(SUBDIRS ${dir})

set(filtered_subdirs "")

foreach(subdir ${SUBDIRS})
get_filename_component(subdir_name ${subdir} NAME)
set(exclude_dir FALSE)

# Check if the subdirectory starts with any of the excluded patterns
foreach(pattern ${excluded_patterns})
if(subdir_name MATCHES "^${pattern}")
set(exclude_dir TRUE)
endif()
endforeach()

# If it's not excluded, add to the filtered list
if(NOT exclude_dir)
list(APPEND filtered_subdirs ${subdir})
endif()
endforeach()

# Return the filtered list
set(${output_var} ${filtered_subdirs})
endmacro()

0 comments on commit d4950f8

Please sign in to comment.