# One static library per module. A module's include/ directory is its
# public interface and is visible to modules that link it; its src/
# directory is private and visible only to the module itself.
function(arch_module name)
  add_library(arch_modules_${name} STATIC ${ARGN})
  target_include_directories(arch_modules_${name}
    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/${name}/include
    PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${name}/src)
endfunction()

arch_module(students students/src/directory.cpp)

arch_module(enrollment enrollment/src/course.cpp enrollment/src/enrollment.cpp)
# PRIVATE: enrollment's public header only declares students::Directory.
target_link_libraries(arch_modules_enrollment PRIVATE arch_modules_students)

arch_module(notifications notifications/src/mailer.cpp)
# PUBLIC: the mailer's public header names enrollment's event types.
target_link_libraries(arch_modules_notifications PUBLIC arch_modules_enrollment)

arch_module(console console/src/console.cpp)
target_link_libraries(arch_modules_console PRIVATE arch_modules_students arch_modules_enrollment)

# main is the composition root, so it links every module.
lld_add_example(arch_enrollment_modules SOURCES main.cpp EXPECTED_OUTPUT expected_output.txt)
target_link_libraries(arch_enrollment_modules PRIVATE
  arch_modules_console arch_modules_notifications arch_modules_enrollment arch_modules_students)

lld_add_test(arch_modules_enrollment_tests SOURCES tests/test_enrollment.cpp)
target_link_libraries(arch_modules_enrollment_tests PRIVATE
  arch_modules_enrollment arch_modules_students)

lld_add_test(arch_modules_notifications_tests SOURCES tests/test_notifications.cpp)
target_link_libraries(arch_modules_notifications_tests PRIVATE arch_modules_notifications)

# Boundary checks. The first rejects includes that bypass include
# directories; the second shows that it catches one.
set(module_dirs students enrollment notifications console)
list(TRANSFORM module_dirs PREPEND ${CMAKE_CURRENT_SOURCE_DIR}/)
add_test(NAME arch_modules_include_check
  COMMAND ${CMAKE_COMMAND} "-DDIRS=${module_dirs}" -P ${CMAKE_CURRENT_SOURCE_DIR}/checks/check_includes.cmake)
add_test(NAME arch_modules_include_check_catches
  COMMAND ${CMAKE_COMMAND} -DDIRS=${CMAKE_CURRENT_SOURCE_DIR}/checks/fixtures
    -P ${CMAKE_CURRENT_SOURCE_DIR}/checks/check_includes.cmake)
set_tests_properties(arch_modules_include_check_catches PROPERTIES
  PASS_REGULAR_EXPRESSION "include check failed: 1 bad include")

# A file that includes enrollment's private header, compiled with only
# the include directories a module that links enrollment would get. It
# is not part of the normal build; the test builds it and expects the
# compiler to report a missing header.
add_executable(arch_modules_private_header_violation EXCLUDE_FROM_ALL checks/uses_private_header.cpp)
target_include_directories(arch_modules_private_header_violation PRIVATE
  $<TARGET_PROPERTY:arch_modules_enrollment,INTERFACE_INCLUDE_DIRECTORIES>)
add_test(NAME arch_modules_private_header_hidden
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target arch_modules_private_header_violation)
set_tests_properties(arch_modules_private_header_hidden PROPERTIES
  PASS_REGULAR_EXPRESSION "course.hpp.*(No such file or directory|file not found)")

set_tests_properties(arch_modules_include_check arch_modules_include_check_catches
  arch_modules_private_header_hidden PROPERTIES LABELS "test")
