Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow relative paths for ip_sources() and ip_include_directories(), r… #110

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions cmake/hwip.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
include("${CMAKE_CURRENT_LIST_DIR}/utils/socmake_graph.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/utils/alias_dereference.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/utils/safe_get_target_property.cmake")
include("${CMAKE_CURRENT_LIST_DIR}//utils/file_paths.cmake")

#[[[
# This function creates an INTERFACE library for a given IP.
Expand Down Expand Up @@ -227,6 +228,8 @@ endfunction()
# existing list. Ne source files can also be prepended with the optional keyword PREPEND. The source
# files are later used to create the list of files to be compiled (e.g., by a simulator) by a tool to
# execute its tasks. The source files are passed as a list after the parameters and keywords.
#
# Relative file paths are accepted and will be converted to absolute, relative to ${CMAKE_CURRENT_SOURCE_DIR}
#
# :param IP_LIB: The target IP library.
# :type IP_LIB: string
Expand All @@ -240,24 +243,27 @@ endfunction()
#]]
function(ip_sources IP_LIB LANGUAGE)
cmake_parse_arguments(ARG "PREPEND;REPLACE" "" "" ${ARGN})
# Delete PREPEND and REPLACE from argument list, so only sources are left
list(REMOVE_ITEM ARGN "PREPEND")
list(REMOVE_ITEM ARGN "REPLACE")

check_languages(${LANGUAGE})
# If alias IP is given, dereference it (VENDOR::LIB::IP::0.0.1) -> (VENDOR__LIB__IP__0.0.1)
alias_dereference(_reallib ${IP_LIB})

# Convert all listed files to absolute paths relative to ${CMAKE_CURRENT_SOURCE_DIR}
convert_paths_to_absolute(file_list ${ARGN})

if(NOT ARG_REPLACE)
# Get the existing source files if any
get_ip_sources(_sources ${_reallib} ${LANGUAGE} NO_DEPS)
else()
list(REMOVE_ITEM ARGN "REPLACE")
endif()

# If the PREPEND option is passed first remove it from the list of file and prepend the new sources
# If the PREPEND option is passed prepend the new sources to the old ones
if(ARG_PREPEND)
list(REMOVE_ITEM ARGN "PREPEND")
set(_sources ${ARGN} ${_sources})
set(_sources ${file_list} ${_sources})
else()
set(_sources ${_sources} ${ARGN})
set(_sources ${_sources} ${file_list})
endif()
# Set the target property with the new list of source files
set_property(TARGET ${_reallib} PROPERTY ${LANGUAGE}_SOURCES ${_sources})
Expand Down Expand Up @@ -316,8 +322,10 @@ function(ip_include_directories IP_LIB LANGUAGE)
check_languages(${LANGUAGE})
# If alias IP is given, dereference it (VENDOR::LIB::IP::0.0.1) -> (VENDOR__LIB__IP__0.0.1)
alias_dereference(_reallib ${IP_LIB})
# Convert all listed files to absolute paths relative to ${CMAKE_CURRENT_SOURCE_DIR}
convert_paths_to_absolute(dir_list ${ARGN})
# Append the new include directories to the exsiting ones
set_property(TARGET ${_reallib} APPEND PROPERTY ${LANGUAGE}_INCLUDE_DIRECTORIES ${ARGN})
set_property(TARGET ${_reallib} APPEND PROPERTY ${LANGUAGE}_INCLUDE_DIRECTORIES ${dir_list})
endfunction()

#[[[
Expand Down
22 changes: 22 additions & 0 deletions cmake/utils/file_paths.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[[[
# This function converts relative paths to absolute paths relative to ${CMAKE_CURRENT_SOURCE_DIR}
# It replicates the behaviour of target_sources() CMake Function
#
# :param OUTPUT_LIST: The variable to store the output file list
# :type OUTPUT_LIST: string
# :param ARGN: list of files to convert
# :type ARGN: path
#
#]]
function(convert_paths_to_absolute OUTPUT_LIST)
unset(output_list)
foreach(path ${ARGN})
cmake_path(ABSOLUTE_PATH path
BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} NORMALIZE
OUTPUT_VARIABLE path
)
list(APPEND output_list ${path} )
endforeach()

set(${OUTPUT_LIST} ${output_list} PARENT_SCOPE)
endfunction()
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Test relative include directories paths, the behaviour is matching the CMake behaviour of target_include_directories():
# Changed in version 3.13: Relative source file paths are interpreted as being relative to the current source directory (i.e. CMAKE_CURRENT_SOURCE_DIR). See policy CMP0076.

include("${CMAKE_CURRENT_LIST_DIR}/../../../../CMakeLists.txt")
set(THIS_DIR ${CMAKE_CURRENT_LIST_DIR})

set(TEST_NAME ip_include_directories_rel_path)
ct_add_test(NAME ${TEST_NAME})
function(${${TEST_NAME}})
execute_process(
COMMAND cmake -S ${THIS_DIR}/test
-B ${CMAKE_BINARY_DIR}/${TEST_NAME}/build
COMMAND_ERROR_IS_FATAL ANY
)
endfunction()
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Test relative include directories paths, the behaviour is matching the CMake behaviour of target_include_directories():
# Changed in version 3.13: Relative source file paths are interpreted as being relative to the current source directory (i.e. CMAKE_CURRENT_SOURCE_DIR). See policy CMP0076.

cmake_minimum_required(VERSION 3.25)
project(ip_include_directories_rel_path NONE)

include("../../../../../SoCMakeConfig.cmake")

add_ip(ip)

set(SV_INCDIRS
.
justdir
)
ip_include_directories(ip SYSTEMVERILOG
${SV_INCDIRS}
)

add_subdirectory(otherdir)
include(incdir/incdir.cmake_)

get_ip_include_directories(SV_ABS_INCDIRS ip SYSTEMVERILOG)

foreach(abspath relpath IN ZIP_LISTS SV_ABS_INCDIRS SV_INCDIRS)
# If there is only . at the end, remove it, otherwise it wont match
string(REGEX REPLACE "\\.$" "" relpath ${relpath})
if(NOT "${CMAKE_CURRENT_SOURCE_DIR}/${relpath}" PATH_EQUAL ${abspath})
message(FATAL_ERROR "NOT EQUAL: ${CMAKE_CURRENT_SOURCE_DIR}/${relpath} - ${abspath}")
endif()

endforeach()
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
list(APPEND SV_INCDIRS
incdir/
incdir/incdir2
)
ip_include_directories(ip SYSTEMVERILOG
incdir/
incdir/incdir2
)

set(SV_INCDIRS ${SV_INCDIRS} PARENT_SCOPE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
list(APPEND SV_INCDIRS
otherdir/.
otherdir/incdir
)
ip_include_directories(ip SYSTEMVERILOG
.
incdir
)

set(SV_INCDIRS ${SV_INCDIRS} PARENT_SCOPE)
17 changes: 9 additions & 8 deletions tests/tests/ip_link/ip_link1.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include("${CMAKE_CURRENT_LIST_DIR}/../../../CMakeLists.txt")
set(CDIR ${CMAKE_CURRENT_LIST_DIR})

set(TEST_NAME ip_link1)

Expand Down Expand Up @@ -28,31 +29,31 @@ function(${${TEST_NAME}})
LIBRARY lib
VERSION 1.2.3
)
ip_sources(${IP} VERILOG ip1_f1.v ip1_f2.v)
ip_sources(${IP} VERILOG ${CDIR}/ip1_f1.v ${CDIR}/ip1_f2.v)
set(IP1 ${IP})

add_ip(ip2
VENDOR vendor
LIBRARY lib
VERSION 1.2.3
)
ip_sources(${IP} VERILOG ip2_f1.v ip2_f2.v)
ip_sources(${IP} VERILOG ${CDIR}/ip2_f1.v ${CDIR}/ip2_f2.v)
set(IP2 ${IP})

add_ip(ip3
VENDOR vendor
LIBRARY lib
VERSION 1.2.3
)
ip_sources(${IP} VERILOG ip3_f1.v ip3_f2.v)
ip_sources(${IP} VERILOG ${CDIR}/ip3_f1.v ${CDIR}/ip3_f2.v)
set(IP3 ${IP})

add_ip(ip4
VENDOR vendor
LIBRARY lib
VERSION 1.2.3
)
ip_sources(${IP} VERILOG ip4_f1.v ip4_f2.v)
ip_sources(${IP} VERILOG ${CDIR}/ip4_f1.v ${CDIR}/ip4_f2.v)
set(IP4 ${IP})

ip_link(${IP1} ${IP2} ${IP3})
Expand All @@ -62,17 +63,17 @@ function(${${TEST_NAME}})
get_ip_sources(V_SOURCES ${IP1} VERILOG)

list(SUBLIST V_SOURCES 0 2 CURRENT_V_FILES)
ct_assert_equal(CURRENT_V_FILES "ip4_f1.v;ip4_f2.v")
ct_assert_equal(CURRENT_V_FILES "${CDIR}/ip4_f1.v;${CDIR}/ip4_f2.v")

list(SUBLIST V_SOURCES 2 4 CURRENT_V_FILES)
if("${CURRENT_V_FILES}" STREQUAL "ip2_f1.v;ip2_f2.v;ip3_f1.v;ip3_f2.v")
if("${CURRENT_V_FILES}" STREQUAL "${CDIR}/ip2_f1.v;${CDIR}/ip2_f2.v;${CDIR}/ip3_f1.v;${CDIR}/ip3_f2.v")
ct_assert_true(TRUE)
elseif("${CURRENT_V_FILES}" STREQUAL "ip3_f1.v;ip3_f2.v;ip3_f1.v;ip3_f2.v")
elseif("${CURRENT_V_FILES}" STREQUAL "${CDIR}/ip3_f1.v;${CDIR}/ip3_f2.v;${CDIR}/ip3_f1.v;${CDIR}/ip3_f2.v")
ct_assert_true(TRUE)
else()
ct_assert_true(FALSE)
endif()

list(SUBLIST V_SOURCES 6 2 CURRENT_V_FILES)
ct_assert_equal(CURRENT_V_FILES "ip1_f1.v;ip1_f2.v")
ct_assert_equal(CURRENT_V_FILES "${CDIR}/ip1_f1.v;${CDIR}/ip1_f2.v")
endfunction()
27 changes: 14 additions & 13 deletions tests/tests/ip_sources/ip_sources.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include("${CMAKE_CURRENT_LIST_DIR}/../../../CMakeLists.txt")

set(TEST_NAME ip_sources)
set(CDIR ${CMAKE_CURRENT_LIST_DIR})

ct_add_test(NAME ${TEST_NAME})
function(${${TEST_NAME}})
Expand All @@ -11,48 +12,48 @@ function(${${TEST_NAME}})
)

ip_sources(${IP} SYSTEMVERILOG
svfile1.sv
svfile2.sv
${CDIR}/svfile1.sv
${CDIR}/svfile2.sv
)

ip_sources(${IP} VERILOG
vfile1.v
vfile2.v
${CDIR}/vfile1.v
${CDIR}/vfile2.v
)

# TEST SYSTEMVERILOG include directories

get_ip_sources(SV_SOURCES ${IP} SYSTEMVERILOG)
ct_assert_list(SV_SOURCES)
ct_assert_equal(SV_SOURCES "svfile1.sv;svfile2.sv")
ct_assert_equal(SV_SOURCES "${CDIR}/svfile1.sv;${CDIR}/svfile2.sv")

ip_sources(${IP} SYSTEMVERILOG
svfile3.sv
svfile4.sv
${CDIR}/svfile3.sv
${CDIR}/svfile4.sv
)

get_ip_sources(SV_SOURCES ${IP} SYSTEMVERILOG)
ct_assert_list(SV_SOURCES)
ct_assert_equal(SV_SOURCES "svfile1.sv;svfile2.sv;svfile3.sv;svfile4.sv")
ct_assert_equal(SV_SOURCES "${CDIR}/svfile1.sv;${CDIR}/svfile2.sv;${CDIR}/svfile3.sv;${CDIR}/svfile4.sv")


# TEST VERILOG include directories
get_ip_sources(V_SOURCES ${IP} VERILOG)
ct_assert_list(V_SOURCES)
ct_assert_equal(V_SOURCES "vfile1.v;vfile2.v")
ct_assert_equal(V_SOURCES "${CDIR}/vfile1.v;${CDIR}/vfile2.v")

ip_sources(${IP} VERILOG
vfile3.v
vfile4.v
${CDIR}/vfile3.v
${CDIR}/vfile4.v
)

get_ip_sources(V_SOURCES ${IP} VERILOG)
ct_assert_list(V_SOURCES)
ct_assert_equal(V_SOURCES "vfile1.v;vfile2.v;vfile3.v;vfile4.v")
ct_assert_equal(V_SOURCES "${CDIR}/vfile1.v;${CDIR}/vfile2.v;${CDIR}/vfile3.v;${CDIR}/vfile4.v")

# TEST Warning asserted on unknown language
ip_sources(${IP} FAKELANG
fakelang.fake
${CDIR}/fakelang.fake
)
ct_assert_prints("Language not supported: FAKELANG")

Expand Down
31 changes: 16 additions & 15 deletions tests/tests/ip_sources/ip_sources_get_multilang.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include("${CMAKE_CURRENT_LIST_DIR}/../../../CMakeLists.txt")

set(TEST_NAME ip_sources_get_multilang)
set(CDIR ${CMAKE_CURRENT_LIST_DIR})

ct_add_test(NAME ${TEST_NAME})
function(${${TEST_NAME}})
Expand All @@ -11,31 +12,31 @@ function(${${TEST_NAME}})
)

ip_sources(${IP} SYSTEMVERILOG
svfile1.sv
svfile2.sv
${CDIR}/svfile1.sv
${CDIR}/svfile2.sv
)

ip_sources(${IP} VERILOG
vfile1.v
vfile2.v
${CDIR}/vfile1.v
${CDIR}/vfile2.v
)

ip_sources(${IP} VHDL
vhdlfile1.vhdl
vhdlfile2.vhdl
${CDIR}/vhdlfile1.vhdl
${CDIR}/vhdlfile2.vhdl
)

get_ip_sources(SOURCES ${IP} VERILOG SYSTEMVERILOG)
ct_assert_list(SOURCES)
ct_assert_equal(SOURCES "vfile1.v;vfile2.v;svfile1.sv;svfile2.sv")
ct_assert_equal(SOURCES "${CDIR}/vfile1.v;${CDIR}/vfile2.v;${CDIR}/svfile1.sv;${CDIR}/svfile2.sv")

get_ip_sources(SOURCES ${IP} VHDL SYSTEMVERILOG)
ct_assert_list(SOURCES)
ct_assert_equal(SOURCES "vhdlfile1.vhdl;vhdlfile2.vhdl;svfile1.sv;svfile2.sv")
ct_assert_equal(SOURCES "${CDIR}/vhdlfile1.vhdl;${CDIR}/vhdlfile2.vhdl;${CDIR}/svfile1.sv;${CDIR}/svfile2.sv")

get_ip_sources(SOURCES ${IP} VERILOG VHDL)
ct_assert_list(SOURCES)
ct_assert_equal(SOURCES "vfile1.v;vfile2.v;vhdlfile1.vhdl;vhdlfile2.vhdl")
ct_assert_equal(SOURCES "${CDIR}/vfile1.v;${CDIR}/vfile2.v;${CDIR}/vhdlfile1.vhdl;${CDIR}/vhdlfile2.vhdl")

add_ip(ip2
VENDOR vendor
Expand All @@ -44,23 +45,23 @@ function(${${TEST_NAME}})
)

ip_sources(${IP} VERILOG
vfile3.v
vfile4.v
${CDIR}/vfile3.v
${CDIR}/vfile4.v
)

ip_sources(${IP} VHDL
vhdlfile3.vhdl
vhdlfile4.vhdl
${CDIR}/vhdlfile3.vhdl
${CDIR}/vhdlfile4.vhdl
)

ip_link(vendor::lib::ip::1.2.3 vendor::lib::ip2::1.2.4)

get_ip_sources(SOURCES vendor::lib::ip::1.2.3 VERILOG VHDL SYSTEMVERILOG)
ct_assert_list(SOURCES)
ct_assert_equal(SOURCES "vfile3.v;vfile4.v;vfile1.v;vfile2.v;vhdlfile3.vhdl;vhdlfile4.vhdl;vhdlfile1.vhdl;vhdlfile2.vhdl;svfile1.sv;svfile2.sv")
ct_assert_equal(SOURCES "${CDIR}/vfile3.v;${CDIR}/vfile4.v;${CDIR}/vfile1.v;${CDIR}/vfile2.v;${CDIR}/vhdlfile3.vhdl;${CDIR}/vhdlfile4.vhdl;${CDIR}/vhdlfile1.vhdl;${CDIR}/vhdlfile2.vhdl;${CDIR}/svfile1.sv;${CDIR}/svfile2.sv")

get_ip_sources(SOURCES vendor::lib::ip::1.2.3 VERILOG VHDL SYSTEMVERILOG NO_DEPS)
ct_assert_list(SOURCES)
ct_assert_equal(SOURCES "vfile1.v;vfile2.v;vhdlfile1.vhdl;vhdlfile2.vhdl;svfile1.sv;svfile2.sv")
ct_assert_equal(SOURCES "${CDIR}/vfile1.v;${CDIR}/vfile2.v;${CDIR}/vhdlfile1.vhdl;${CDIR}/vhdlfile2.vhdl;${CDIR}/svfile1.sv;${CDIR}/svfile2.sv")
endfunction()

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Test relative source file paths, the behaviour is matching the CMake behaviour of target_sources:
# Changed in version 3.13: Relative source file paths are interpreted as being relative to the current source directory (i.e. CMAKE_CURRENT_SOURCE_DIR). See policy CMP0076.

include("${CMAKE_CURRENT_LIST_DIR}/../../../../CMakeLists.txt")
set(THIS_DIR ${CMAKE_CURRENT_LIST_DIR})

set(TEST_NAME ip_sources_rel_path)
ct_add_test(NAME ${TEST_NAME})
function(${${TEST_NAME}})
execute_process(
COMMAND cmake -S ${THIS_DIR}/test
-B ${CMAKE_BINARY_DIR}/${TEST_NAME}/build
COMMAND_ERROR_IS_FATAL ANY
)
endfunction()
Loading
Loading