# $Id: CMakeLists.macro.asn.txt 662048 2023-01-24 18:45:51Z gotvyans $

NCBI_begin_lib(macro)
    NCBI_dataspecs(macro.asn)
    NCBI_uses_toolkit_libraries(seq)
    NCBI_project_watchers(stakhovv kans)

    if (NOT CMAKE_CROSSCOMPILING AND NCBI_FSM_COMPILER_ENABLED AND NCBI_PYTHON_EXECUTABLE)
        NCBI_custom_target_dependencies(macro_fsm_files)
    endif()

NCBI_end_lib()

if (NOT CMAKE_CROSSCOMPILING AND NCBI_FSM_COMPILER_ENABLED AND NCBI_PYTHON_EXECUTABLE)

    NCBI_begin_custom_target(macro_fsm_files)
        NCBI_custom_target_definition(define_macro_fsm_files_target)
        NCBI_custom_target_dependencies(multipattern)
    NCBI_end_custom_target()

endif()


function(define_macro_fsm_files_target variable access value current_list_file stack)
    set(_prt_modules
        weasel.txt
    )

    define_multipatern_inc_target(macro_fsm_files ${current_list_file} multipattern "${_prt_modules}" "-A")
endfunction()

function(define_multipatern_inc_target up_target current_list_file compiler modules more_args)

    cmake_path(GET current_list_file PARENT_PATH _source_dir)

    set (_compiler ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${compiler}${CMAKE_EXECUTABLE_SUFFIX})

    set(_target_dir ${_source_dir})

    set(_make_fsm_file ${NCBI_SRC_ROOT}/objects/macro/make_fsm_file.py)
    #set(_make_fsm_file ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/make_fsm_file.py)

    add_custom_target(${up_target}
        COMMENT "Generate FSM files"
        SOURCES ${_make_fsm_file}
        )

    foreach(_module ${modules})

        # remove suffix
        string(REGEX REPLACE "\\..*" "" _tmp_module_name "${_module}")

        set(_prt_file ${_module})
        set(_inc_file ${_tmp_module_name}.inc)
        set(_target fsm_${_tmp_module_name}_inc)

        add_custom_command(
            OUTPUT ${_target_dir}/${_inc_file}
            COMMAND ${NCBI_PYTHON_EXECUTABLE} ${_make_fsm_file} ${_inc_file} ${_compiler} -i ${_prt_file} ${more_args}
            WORKING_DIRECTORY ${_source_dir}
            COMMENT "Running make_fsm_file.py for ${_prt_file}"
            DEPENDS ${current_list_file} ${_source_dir}/${_prt_file} ${_make_fsm_file} ${_compiler}
            # ${CMAKE_CURRENT_FUNCTION_LIST_FILE}
            )

        add_custom_target(
            ${_target}
            DEPENDS ${_target_dir}/${_inc_file}
        )
        add_dependencies(${up_target} ${_target})
    endforeach()

    unset(_inc_file)
    unset(_prt_file)
    unset(_target)
    unset(_target_dir)
    unset(_source_dir)
    unset(_compiler)
endfunction()
