# Test the case of a simple app linked to a simple internal library to
# see whether compilation (not linking) of the app can happen before
# the internal library is built for parallel builds.

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(test_internal_library CXX)

file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

set(INTERNAL_LIBRARY_HEADERS
  ${CMAKE_BINARY_DIR}/lib/header1.h
  ${CMAKE_BINARY_DIR}/lib/header2.h
  ${CMAKE_BINARY_DIR}/lib/header3.h
  ${CMAKE_BINARY_DIR}/lib/header4.h
  )

add_custom_command(
  OUTPUT
  ${INTERNAL_LIBRARY_HEADERS}
  COMMAND sh ${CMAKE_SOURCE_DIR}/lib/include_generator.sh
  DEPENDS
  ${CMAKE_SOURCE_DIR}/lib/include_generator.sh
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/lib
  VERBATIM
  )

add_custom_target(generate_headers
  DEPENDS
  ${INTERNAL_LIBRARY_HEADERS}
  )

set(INTERNAL_LIBRARY_SOURCES
  ${CMAKE_BINARY_DIR}/lib/lib1.cpp
  ${CMAKE_BINARY_DIR}/lib/lib2.cpp
  ${CMAKE_BINARY_DIR}/lib/lib3.cpp
  ${CMAKE_BINARY_DIR}/lib/lib4.cpp
  )

add_custom_command(
  OUTPUT
  ${INTERNAL_LIBRARY_SOURCES}
  COMMAND sh ${CMAKE_SOURCE_DIR}/lib/lib_generator.sh
  DEPENDS
  ${CMAKE_SOURCE_DIR}/lib/lib_generator.sh
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/lib
  VERBATIM
  )

add_custom_target(generate_sources
  DEPENDS
  ${INTERNAL_LIBRARY_SOURCES}
  )

set_source_files_properties(
  ${INTERNAL_LIBRARY_SOURCES}
  PROPERTIES
  GENERATED ON
  )

add_custom_target(wait5
  COMMAND sleep 5
  )

add_library(internal_lib ${INTERNAL_LIBRARY_SOURCES})

# Headers must be generated before library is built.  Also wait for 5 seconds before the
# internal library build.
add_dependencies(internal_lib generate_headers wait5)

# Where to find these headers.
target_include_directories(internal_lib PUBLIC ${CMAKE_BINARY_DIR}/lib)

add_executable(app main.cpp)
target_link_libraries(app internal_lib)
