project(coreschema)

# XSD really needed
find_package(XSD REQUIRED)

# Create lib files from xsd
file (GLOB_RECURSE XSD_INPUT_FILES *.xsd)

# generates headers from scratch
set (XSD_H)
set (XSD_SRCS)

if (XSD_COMMAND)
    set(XSD_COMMAND xsdcxx CACHE STRING "name of the XSD code synthesis compiler")
    # generates headers from scratch
    foreach(xsdFile ${XSD_INPUT_FILES})
        get_filename_component(xsdFileName ${xsdFile} NAME_WE)
        message(STATUS "Generating code from ${xsdFileName}")
        add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${xsdFileName}.hxx ${CMAKE_CURRENT_BINARY_DIR}/${xsdFileName}.cxx
                        COMMAND ${XSD_COMMAND}
                        ARGS cxx-tree --generate-polymorphic --generate-serialization --namespace-map =coreschema --polymorphic-type Method --polymorphic-type Criteria ${xsdFile}
                        DEPENDS ${xsdFile})
        set(XSD_H ${XSD_H} ${CMAKE_CURRENT_BINARY_DIR}/${xsdFileName}.hxx)
        set(XSD_SRCS ${XSD_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/${xsdFileName}.cxx)
    endforeach()
else()
    message(FATAL_ERROR "Cannot find xsdcxx command: cannot generate mml from scratch, try with MML_GENERATE_XSD_LIB set to OFF")
    set(MML_GENERATE_XSD_LIB OFF)
endif()

camitk_tool(STATIC
            SOURCES ${XSD_H} ${XSD_SRCS}
            NEEDS_XSD
)

#--------------
# installation
#--------------
export_headers(${XSD_H} COMPONENT coreschema)

# lib installation
install(TARGETS coreschema
        ARCHIVE DESTINATION lib/${CAMITK_SHORT_VERSION_STRING}
        COMPONENT coreschema
)