Skip to content

Commit

Permalink
CMake: add support for exporting and importing .bba files.
Browse files Browse the repository at this point in the history
This is useful for certain cross-compilation workloads, and to cache
rarely changing build products.

To use this functionality, build e.g. as follows:

    cmake . -B build-export -DEXPORT_BBA_FILES=../bba-files -DARCH=all
    cmake --build build-export -t nextpnr-all-bba

    cmake . -B build-import -DIMPORT_BBA_FILES=../bba-files -DARCH=all
    cmake --build build-import
  • Loading branch information
whitequark committed Jan 23, 2025
1 parent fac934b commit 90d746f
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ add_subdirectory(rust)

add_subdirectory(tests/gui)

add_custom_target(nextpnr-all-bba)

function(add_nextpnr_architecture target)
cmake_parse_arguments(arg "" "MAIN_SOURCE" "CORE_SOURCES;TEST_SOURCES;CURRENT_SOURCE_DIR;CURRENT_BINARY_DIR" ${ARGN})

Expand Down Expand Up @@ -325,6 +327,10 @@ function(add_nextpnr_architecture target)

# Chip database

add_library(nextpnr-${target}-bba INTERFACE)

add_dependencies(nextpnr-all-bba nextpnr-${target}-bba)

add_library(nextpnr-${target}-chipdb INTERFACE)

target_link_libraries(nextpnr-${target}-core INTERFACE nextpnr-${target}-chipdb)
Expand Down
83 changes: 70 additions & 13 deletions cmake/BBAsm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@ else()
set(BBASM_ENDIAN_FLAG "--le")
endif()

set(EXPORT_BBA_FILES "" CACHE PATH "Copy generated .bba files to this directory")
set(IMPORT_BBA_FILES "" CACHE PATH "Use pre-generated .bba files from this directory")
mark_as_advanced(EXPORT_BBA_FILES IMPORT_BBA_FILES)

if (EXPORT_BBA_FILES)
file(REAL_PATH ${EXPORT_BBA_FILES} EXPORT_BBA_FILES BASE_DIRECTORY ${CMAKE_BINARY_DIR})
message(STATUS "Exporting .bba files to ${EXPORT_BBA_FILES}")
endif()

if (IMPORT_BBA_FILES)
file(REAL_PATH ${IMPORT_BBA_FILES} IMPORT_BBA_FILES BASE_DIRECTORY ${CMAKE_BINARY_DIR})
message(STATUS "Importing .bba files from ${EXPORT_BBA_FILES}")
endif()

# Example usage (note the `.new`, used for atomic updates):
#
# add_bba_produce_command(
# TARGET nextpnr-ice40-bba
# COMMAND ${Python_EXECUTABLE}
# ${CMAKE_CURRENT_SOURCE_DIR}/chipdb.py
# -o ${CMAKE_CURRENT_BINARY_DIR}/chipdb-hx8k.bba.new
Expand All @@ -20,26 +35,68 @@ endif()
# Paths must be absolute.
#
function(add_bba_produce_command)
cmake_parse_arguments(arg "" "OUTPUT" "COMMAND;INPUTS" ${ARGN})
cmake_parse_arguments(arg "" "OUTPUT" "TARGET;COMMAND;INPUTS" ${ARGN})

cmake_path(GET arg_OUTPUT PARENT_PATH arg_OUTPUT_DIR)
cmake_path(GET arg_OUTPUT FILENAME arg_OUTPUT_NAME)
file(RELATIVE_PATH arg_OUTPUT_RELATIVE ${CMAKE_BINARY_DIR} ${arg_OUTPUT})
file(MAKE_DIRECTORY ${arg_OUTPUT_DIR})

list(GET arg_COMMAND 0 arg_EXECUTABLE)

add_custom_command(
OUTPUT
${arg_OUTPUT}
COMMAND
${arg_COMMAND}
COMMAND
${CMAKE_COMMAND} -E rename # atomic update
${arg_OUTPUT}.new
if (NOT IMPORT_BBA_FILES)

add_custom_command(
OUTPUT
${arg_OUTPUT}
COMMAND
${arg_COMMAND}
COMMAND
${CMAKE_COMMAND} -E rename # atomic update
${arg_OUTPUT}.new
${arg_OUTPUT}
DEPENDS
${arg_EXECUTABLE}
${arg_INPUTS}
VERBATIM
)

else()

add_custom_command(
OUTPUT
${arg_OUTPUT}
COMMAND
${CMAKE_COMMAND} -E copy
${IMPORT_BBA_FILES}/${arg_OUTPUT_RELATIVE}
${arg_OUTPUT}
DEPENDS
${IMPORT_BBA_FILES}/${arg_OUTPUT_RELATIVE}
COMMENT
"Importing ${arg_OUTPUT_NAME}"
VERBATIM
)

endif()

if (EXPORT_BBA_FILES)

add_custom_command(
OUTPUT
${arg_OUTPUT}
DEPENDS
${arg_EXECUTABLE}
${arg_INPUTS}
VERBATIM
COMMAND
${CMAKE_COMMAND} -E copy
${arg_OUTPUT}
${EXPORT_BBA_FILES}/${arg_OUTPUT_RELATIVE}
APPEND
VERBATIM
)

endif()

target_sources(
${arg_TARGET} PUBLIC
${arg_OUTPUT}
)

endfunction()
Expand Down
1 change: 1 addition & 0 deletions ecp5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ foreach (device ${ECP5_DEVICES})
endif()

add_bba_produce_command(
TARGET nextpnr-${family}-bba
COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/trellis_import.py
-L ${TRELLIS_LIBDIR}
Expand Down
1 change: 1 addition & 0 deletions gowin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ foreach (device ${GOWIN_DEVICES})
endif()

add_bba_produce_command(
TARGET nextpnr-${family}-bba
COMMAND ${GOWIN_BBA_EXECUTABLE}
-d ${device}
-i ${CMAKE_CURRENT_SOURCE_DIR}/constids.inc
Expand Down
4 changes: 4 additions & 0 deletions himbaechel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ else()

target_sources(nextpnr-himbaechel-core INTERFACE ${arg_CORE_SOURCES})

add_library(nextpnr-himbaechel-${microtarget}-bba INTERFACE)

add_dependencies(nextpnr-himbaechel-bba nextpnr-himbaechel-${microtarget}-bba)

add_library(nextpnr-himbaechel-${microtarget}-chipdb INTERFACE)

target_link_libraries(nextpnr-himbaechel-core INTERFACE nextpnr-himbaechel-${microtarget}-chipdb)
Expand Down
1 change: 1 addition & 0 deletions himbaechel/uarch/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ foreach (device ${HIMBAECHEL_EXAMPLE_DEVICES})
endif()

add_bba_produce_command(
TARGET nextpnr-himbaechel-example-bba
COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/example_arch_gen.py
${CMAKE_CURRENT_BINARY_DIR}/chipdb-${device}.bba.new
Expand Down
1 change: 1 addition & 0 deletions himbaechel/uarch/gowin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ foreach (device ${HIMBAECHEL_GOWIN_DEVICES})
endif()

add_bba_produce_command(
TARGET nextpnr-himbaechel-gowin-bba
COMMAND ${apycula_Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/gowin_arch_gen.py
-d ${device}
Expand Down
1 change: 1 addition & 0 deletions himbaechel/uarch/ng-ultra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ foreach (device ${HIMBAECHEL_NGULTRA_DEVICES})
string(TOUPPER ${device} device_upper)

add_bba_produce_command(
TARGET nextpnr-himbaechel-ng-ultra-bba
COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/gen/arch_gen.py
--db ${HIMBAECHEL_PRJBEYOND_DB}
Expand Down
1 change: 1 addition & 0 deletions himbaechel/uarch/xilinx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ message(STATUS "Enabled Himbaechel-Xilinx devices: ${HIMBAECHEL_XILINX_DEVICES}"

foreach (device ${HIMBAECHEL_XILINX_DEVICES})
add_bba_produce_command(
TARGET nextpnr-himbaechel-xilinx-bba
COMMAND /usr/bin/pypy3 ${CMAKE_CURRENT_SOURCE_DIR}/gen/xilinx_gen.py
--xray ${HIMBAECHEL_PRJXRAY_DB}/artix7
--device ${device}
Expand Down
1 change: 1 addition & 0 deletions ice40/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ foreach (device ${ICE40_DEVICES})
endif()

add_bba_produce_command(
TARGET nextpnr-${family}-bba
COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/chipdb.py
-p ${CMAKE_CURRENT_SOURCE_DIR}/constids.inc
Expand Down
1 change: 1 addition & 0 deletions machxo2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ foreach (device ${MACHXO2_DEVICES})
endif()

add_bba_produce_command(
TARGET nextpnr-${family}-bba
COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/facade_import.py
-L ${TRELLIS_LIBDIR}
Expand Down
1 change: 1 addition & 0 deletions nexus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ foreach (subfamily ${NEXUS_FAMILIES})
endif()

add_bba_produce_command(
TARGET nextpnr-${family}-bba
COMMAND ${PRJOXIDE_TOOL}
bba-export ${subfamily}
${CMAKE_CURRENT_SOURCE_DIR}/constids.inc
Expand Down

0 comments on commit 90d746f

Please sign in to comment.