cmake_minimum_required(VERSION 3.12)
project(h5pp-examples CXX)
file(GLOB EXAMPLES "*.cpp")
add_custom_target(h5pp-example-all)

foreach (ex ${EXAMPLES})
    get_filename_component(example_src ${ex} NAME)
    get_filename_component(example_nwe ${ex} NAME_WE)
    add_executable(h5pp-${example_nwe} ${example_src})
    target_link_libraries(h5pp-${example_nwe} PRIVATE h5pp)
    if (MSVC)
        set_target_properties(h5pp-${example_nwe} PROPERTIES LINK_FLAGS "/ignore:4099")
    endif()
    add_dependencies(h5pp-example-all h5pp-${example_nwe})
    list(APPEND example_tgt_list h5pp-${example_nwe})
endforeach()


# Speed up compilation with precompiled headers
if (H5PP_ENABLE_PCH)
    if (COMMAND target_precompile_headers)
        foreach (tgt ${example_tgt_list})
            # Later CMake versions will only include system header paths for pch generation
            target_include_directories(${tgt} SYSTEM INTERFACE ../include)
            if (NOT TARGET ${first_tgt})
                target_precompile_headers(${tgt} PRIVATE <h5pp/h5pp.h>)
                set(first_tgt ${tgt})
            else()
                target_precompile_headers(${tgt} REUSE_FROM ${first_tgt})
            endif()
        endforeach()
    endif()
endif()

# Speed up compilation with ccache
if (H5PP_ENABLE_CCACHE)
    find_program(CCACHE_PROGRAM ccache)
    if (CCACHE_PROGRAM)
        foreach (tgt ${example_tgt_list})
            set_target_properties(${tgt} PROPERTIES CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
        endforeach()
        message(STATUS "h5pp examples: using ccache ${CCACHE_PROGRAM}")
        if (H5PP_ENABLE_PCH AND COMMAND target_precompile_headers)
            message(STATUS "h5pp examples: detected ccache + pch: remember to set --> sloppiness = include_file_mtime,pch_defines,time_macros <-- in your ccache.conf")
        endif()
    else()
        message(STATUS "ccache program could not be found")
    endif()
endif()