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

CMake support #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
!board/*
!*.v
!*.cpp
!CMakeLists.txt
build/
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# The latest Verilator suggests cmake minimum version 3.12
cmake_minimum_required(VERSION 3.12)

project(nvboard)

# find SDL2, see https://wiki.libsdl.org/SDL2/README/cmake
find_package(SDL2 REQUIRED CONFIG REQUIRED COMPONENTS SDL2)
find_package(SDL2_image REQUIRED CONFIG REQUIRED COMPONENTS SDL2_image)
find_package(SDL2_ttf REQUIRED CONFIG REQUIRED COMPONENTS SDL2_ttf)

# add all *.cpp files under src to the source list
file(GLOB nvboard_srcs src/*.cpp)
# add the `libnvboard.a` static library
add_library(nvboard STATIC ${nvboard_srcs})
target_include_directories(nvboard PRIVATE include)
target_link_libraries(nvboard PRIVATE SDL2::SDL2 SDL2_image::SDL2_image SDL2_ttf::SDL2_ttf)
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ nvboard_quit();

### 编译链接

如果你使用Makefile作为构建系统:

在Makefile中
* 将生成的上述引脚绑定的C++文件加入源文件列表
* 将NVBoard的构建脚本包含进来
Expand All @@ -143,3 +145,13 @@ include $(NVBOARD_HOME)/scripts/nvboard.mk
* 在生成verilator仿真可执行文件(即`$(NVBOARD_ARCHIVE)`)将这个库文件加入链接过程,并添加链接选项`-lSDL2 -lSDL2_image`

可以参考示例项目中的Makefile文件,即`example/Makefile`

如果你使用CMake作为构建系统:

* 通过`add_subdirectory`添加NVBoard项目的目录
* 通过`add_custom_command`添加生成引脚绑定的命令,并将生成的文件添加到源文件列表
* 通过`target_include_dir`将NVBoard项目的`usr/include`目录添加到包含路径
* 通过`target_link_libraries`将你的程序链接到`nvboard`
* 在`CMakeLists.txt`中调用Verilator

可以参考示例项目中的CMakeLists.txt文件,即`example/CMakeLists.txt`,以及参考Verilator官方文档中关于在CMake中调用Verilator的部分。
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
!resource/*.hex
!constr/*.nxdc
!CMakeLists.txt
39 changes: 39 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# The latest Verilator suggests cmake minimum version 3.12
cmake_minimum_required(VERSION 3.12)

project(nvboard-example)

# specify the name of the top module here
set(topName top)
# in the main.cpp, the class name of the top module is specified with macro `TOP_NAME`
add_compile_definitions(TOP_NAME=V${topName})

# find verilator
find_package(verilator HINTS $ENV{VERILATOR_ROOT} ${VERILATOR_ROOT})

# the location of nvboard is specified by the environment variable $NVBOARD_HOME
# or you can just hardcode it here
set(nvboardDir $ENV{NVBOARD_HOME})
# build the `libnvboard.a` in subdirectory `nvboard-lib`
add_subdirectory(${nvboardDir} nvboard-lib)

# invoke the script to generate pin bindings
# specify the path to the nxdc file here
set(nxdcFile ${CMAKE_CURRENT_SOURCE_DIR}/constr/top.nxdc)
add_custom_command(
OUTPUT auto_bind.cpp
COMMAND ${nvboardDir}/scripts/auto_pin_bind.py ${nxdcFile} auto_bind.cpp
DEPENDS ${nxdcFile}
)

# add all *.cpp files under `csrc` to the source list
# change the following two lines if you place your c sources elsewhere
file(GLOB srcs csrc/*.cpp)
add_executable(nvboard-example ${srcs} auto_bind.cpp)
# add the include directory of nvboard
target_include_directories(nvboard-example PRIVATE ${nvboardDir}/usr/include)
# link the excutable against the nvboard static library
target_link_libraries(nvboard-example PUBLIC nvboard)
# invoke verilator, see https://verilator.org/guide/latest/verilating.html#cmake
# change the following line if you place your verilog sources elsewhere
verilate(nvboard-example SOURCES vsrc/top.v INCLUDE_DIRS vsrc TOP_MODULE ${topName})
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 示例工程

先设置环境变量`NVBOARD_HOME`为NVBoard项目的路径, 然后执行`make run`.
先设置环境变量`NVBOARD_HOME`为NVBoard项目的路径, 然后执行`make run`,或者使用CMake构建本项目。

该示例的演示效果如下:
1. 左边8个LED为流水灯效果
Expand Down