## ---------------------------------------------------------------------------
## PROPRIETARY CODE – Arthur de Araújo Farias 2026
## All rights reserved.  No part of this file may be reproduced, stored in a
## retrieval system, or transmitted in any form or by any means—electronic,
## mechanical, photocopying, recording, or otherwise—without the prior written
## permission of the copyright holder.
## ---------------------------------------------------------------------------

cmake_minimum_required(VERSION 3.25)

# VHDL comes from cmake-language-vhdl. Installed, find_package() locates it;
# with only a checkout at hand, point CMAKE_LANGUAGE_VHDL_SOURCE_DIR at it
# (the "checkout" preset does, for a clone next to this repository).
# Either way the modules must be on CMAKE_MODULE_PATH before project().
set(CMAKE_LANGUAGE_VHDL_SOURCE_DIR "" CACHE PATH "cmake-language-vhdl checkout to use instead of an installed package")
if(CMAKE_LANGUAGE_VHDL_SOURCE_DIR)
  list(PREPEND CMAKE_MODULE_PATH "${CMAKE_LANGUAGE_VHDL_SOURCE_DIR}/Modules")
else()
  find_package(cmake-language-vhdl 0.1 REQUIRED)
endif()
project(vhdl-example-cmake LANGUAGES VHDL)

include(CTest)

# The design is an INTERFACE source library; GHDL orders the analysis.
add_library(dds INTERFACE)
target_sources(dds INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/src/dds.vhdl")

# A native simulation executable. With CMAKE_BUILD_TYPE=Debug it is built with
# -g, which is what the debugger's "program" launch configuration needs.
add_executable(dds_tb tb/dds_tb.vhdl)
target_link_libraries(dds_tb PRIVATE dds)

# `cmake --build build --target waves` runs the simulation once and leaves both
# waveforms next to the executable. Open either file in VS Code to view it.
#   .ghw  every signal with its VHDL type, enumerations and records included
#   .vcd  bits, vectors, integers and reals; readable by any waveform tool
set(WAVES "${CMAKE_CURRENT_BINARY_DIR}/dds_tb.ghw" "${CMAKE_CURRENT_BINARY_DIR}/dds_tb.vcd")
add_custom_command(
  OUTPUT ${WAVES}
  COMMAND dds_tb --assert-level=error
          "--wave=${CMAKE_CURRENT_BINARY_DIR}/dds_tb.ghw"
          "--vcd=${CMAKE_CURRENT_BINARY_DIR}/dds_tb.vcd"
  DEPENDS dds_tb
  COMMENT "Simulating dds_tb"
  VERBATIM)
add_custom_target(waves DEPENDS ${WAVES})

if(BUILD_TESTING)
  add_test(NAME dds.behaviour COMMAND dds_tb --assert-level=error)
  set_tests_properties(dds.behaviour PROPERTIES
    PASS_REGULAR_EXPRESSION "all checks passed"
    FAIL_REGULAR_EXPRESSION "assertion error"
    TIMEOUT 60)
endif()
