# One shared library, released twice. Both releases are built as
# libwidgets.so.1: the "1" is the SONAME, the name a program records when
# it is linked, and it stays the same while releases remain compatible.
# They go to separate directories, so a test can choose which one a
# program loads by setting LD_LIBRARY_PATH.
#
# widgets_release builds one release. It is a CMake function: target,
# dir, and version are its parameters, read below as ${target} and so on.
function(widgets_release target dir version)
  # SHARED: a shared library (.so), loaded when a program starts, instead of
  # code copied into each program. Each release has its own source file.
  add_library(${target} SHARED ${dir}/progress_bar.cpp)
  # PUBLIC: the release's header folder and export/ are also used by every
  # program that links with this target, so each client gets the right
  # widgets/progress_bar.hpp.
  target_include_directories(${target} PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/${dir} ${CMAKE_CURRENT_SOURCE_DIR}/export)
  # OUTPUT_NAME names the library "widgets", so its file is libwidgets.so.
  # VERSION gives the real file its full number, as in libwidgets.so.1.0.0.
  # SOVERSION is the SONAME's "1". The two visibility settings hide
  # everything not marked WIDGETS_API, and the last line puts each release
  # in its own folder of the build tree.
  set_target_properties(${target} PROPERTIES
    OUTPUT_NAME widgets
    VERSION ${version}
    SOVERSION 1
    CXX_VISIBILITY_PRESET hidden
    VISIBILITY_INLINES_HIDDEN ON
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${dir})
endfunction()

# The two releases: 1.0.0 from v1/ and 1.1.0 from v2/.
widgets_release(adv_widgets_1_0 v1 1.0.0)
widgets_release(adv_widgets_1_1 v2 1.1.0)

# The programs do not record where the library is, as an installed
# program would not; each test below says which release to load.
# Each client is compiled with the header of the release it links with.
add_executable(adv_widgets_old_client old_client.cpp)
target_link_libraries(adv_widgets_old_client PRIVATE adv_widgets_1_0)
add_executable(adv_widgets_new_client new_client.cpp)
target_link_libraries(adv_widgets_new_client PRIVATE adv_widgets_1_1)
set_target_properties(adv_widgets_old_client adv_widgets_new_client
  PROPERTIES SKIP_BUILD_RPATH ON)

# widgets_run registers a test that runs a client with one release and
# compares its output with a file. The test runs CMake in script mode
# (-P) on the site's compare script, which fails unless the program exits
# with status 0 and prints exactly the expected text. $<TARGET_FILE:...>
# becomes the path of the built program. LD_LIBRARY_PATH tells the loader
# which folder, and so which release, to load libwidgets.so.1 from.
function(widgets_run name client dir expected)
  add_test(NAME ${name}
    COMMAND ${CMAKE_COMMAND}
      -DEXE=$<TARGET_FILE:${client}>
      -DEXPECTED=${CMAKE_CURRENT_SOURCE_DIR}/${expected}
      -P ${LLD_COMPARE_SCRIPT})
  set_tests_properties(${name} PROPERTIES LABELS "example" TIMEOUT 120
    ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/${dir}")
endfunction()

# The compatibility promise, tested: a program built with 1.0 gives the
# same output with 1.1, apart from the version it reports.
widgets_run(adv_widgets_old_client_with_1_0.output adv_widgets_old_client v1
  expected_old_client_with_1_0.txt)
widgets_run(adv_widgets_old_client_with_1_1.output adv_widgets_old_client v2
  expected_old_client_with_1_1.txt)
widgets_run(adv_widgets_new_client_with_1_1.output adv_widgets_new_client v2
  expected_new_client_with_1_1.txt)

# And its limit: a program that uses set_label cannot run with 1.0.
# With PASS_REGULAR_EXPRESSION, CTest ignores the exit status and passes
# the test if the output matches: either the loader's message or the
# program's own. Output that matches neither, such as a normal run,
# fails it.
add_test(NAME adv_widgets_new_client_with_1_0 COMMAND adv_widgets_new_client)
set_tests_properties(adv_widgets_new_client_with_1_0 PROPERTIES LABELS "example" TIMEOUT 120
  ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/v1"
  PASS_REGULAR_EXPRESSION "undefined symbol|needs widgets 1.1")

# The unit tests, built against the 1.1 header and linked with 1.1.
# lld_add_test is the site's helper that builds and registers a test.
lld_add_test(adv_widgets_tests SOURCES tests/test_progress_bar.cpp)
target_link_libraries(adv_widgets_tests PRIVATE adv_widgets_1_1)
