##### macro to build ocaml sources

macro( tde_add_ocaml_source _target _ml )

    get_filename_component( basename "${_ml}" NAME_WE )

    if( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${basename}.mli )
      add_custom_command(
        OUTPUT
            ${CMAKE_CURRENT_BINARY_DIR}/${basename}.cmi

        COMMENT "Compile ${basename}.mli"

        COMMAND ${OCAMLC_EXECUTABLE}
            -o ${CMAKE_CURRENT_BINARY_DIR}/${basename}.cmi
            -I ${FACILE_INCLUDE_DIR}
            -c ${CMAKE_CURRENT_SOURCE_DIR}/${basename}.mli

        DEPENDS
            ${CMAKE_CURRENT_SOURCE_DIR}/${basename}.ml
            ${ARGN}

        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
      )

      set( _build_cmi ${CMAKE_CURRENT_BINARY_DIR}/${basename}.cmi )
    else( )
      set( _build_cmi "" )
    endif( )

    add_custom_command(
        OUTPUT
            ${CMAKE_CURRENT_BINARY_DIR}/${basename}.cmx

        COMMENT "Compile ${basename}.ml"

        COMMAND ${OCAMLOPT_EXECUTABLE}
            -o ${CMAKE_CURRENT_BINARY_DIR}/${basename}.cmx
            -I ${FACILE_INCLUDE_DIR}
            -c ${CMAKE_CURRENT_SOURCE_DIR}/${basename}.ml

        DEPENDS
            ${CMAKE_CURRENT_SOURCE_DIR}/${basename}.ml
            ${_build_cmi}
            ${ARGN}

        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    )

    list( APPEND ${_target}-ocaml
        ${CMAKE_CURRENT_BINARY_DIR}/${basename}.cmx
    )
endmacro( )


# build kalzium ocaml solver

tde_add_ocaml_source( kalzium_solver chemset.ml )

tde_add_ocaml_source( kalzium_solver datastruct.ml
    ${CMAKE_CURRENT_BINARY_DIR}/chemset.cmi
)

tde_add_ocaml_source( kalzium_solver chem.ml
    ${CMAKE_CURRENT_BINARY_DIR}/chemset.cmi
    ${CMAKE_CURRENT_BINARY_DIR}/datastruct.cmi
)

tde_add_ocaml_source( kalzium_solver parser.ml
    ${CMAKE_CURRENT_BINARY_DIR}/chemset.cmi
)

tde_add_ocaml_source( kalzium_solver lexer.ml
    ${CMAKE_CURRENT_BINARY_DIR}/chemset.cmi
    ${CMAKE_CURRENT_BINARY_DIR}/parser.cmi
)

tde_add_ocaml_source( kalzium_solver calc.ml
    ${CMAKE_CURRENT_BINARY_DIR}/chem.cmi
    ${CMAKE_CURRENT_BINARY_DIR}/chemset.cmi
    ${CMAKE_CURRENT_BINARY_DIR}/datastruct.cmi
    ${CMAKE_CURRENT_BINARY_DIR}/parser.cmi
    ${CMAKE_CURRENT_BINARY_DIR}/lexer.cmx
)

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/modwrap.o

    COMMAND ${OCAMLOPT_EXECUTABLE}
        -I ${FACILE_INCLUDE_DIR}
        -c ${CMAKE_CURRENT_SOURCE_DIR}/modwrap.c

    DEPENDS
        ${CMAKE_CURRENT_SOURCE_DIR}/modwrap.c
        ${CMAKE_CURRENT_BINARY_DIR}/solver.o

    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/solver.o

    COMMAND ${OCAMLOPT_EXECUTABLE} -output-obj
        -o ${CMAKE_CURRENT_BINARY_DIR}/solver.o
        ${FACILE_INCLUDE_DIR}/facile.cmxa
        ${kalzium_solver-ocaml}

    DEPENDS
        ${kalzium_solver-ocaml}

    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

add_custom_target( kalzium_solver
    DEPENDS
        ${CMAKE_CURRENT_BINARY_DIR}/modwrap.o
        ${CMAKE_CURRENT_BINARY_DIR}/solver.o
)
