@echo off
:: Получаем аргументы из командной строки
:: %1 - includes_USER
:: %2 - code_USER
:: %3 - режим (например, debug)

:: Аргументы:
:: %1 — includes строка (в кавычках)
:: %2 — sources строка
:: %3 — defines строка
:: %4 — режим компиляции (debug/release)

:: Сохраняем как переменные
set filename=%~1
set includes_USER=%~2
set code_USER=%~3
set defines_USER=%~4
set defines_CONFIG=%~5
set compil_mode=%~6

:: Заменяем __EQ__ на =
set defines_USER=%defines_USER:__EQ__==%
set defines_CONFIG=%defines_CONFIG:__EQ__==%


set defines_WRAPPER=-D"MATLAB"^ -D"__sizeof_ptr=8"
:: -------------------------USERS PATHS AND CODE---------------------------
::-------------------------------------------------------------------------


:: -------------------------WRAPPER PATHS AND CODE---------------------------
:: оболочка, которая будет моделировать работу МК в симулинке
:: WRAPPER BAT START
set code_WRAPPER=.\MCU_Wrapper\MCU.c^
 .\MCU_Wrapper\mcu_wrapper.c

set includes_WRAPPER= -I".\MCU_Wrapper\"
:: WRAPPER BAT END

:: APP WRAPPER BAT START
set code_APP_WRAPPER=.\app_wrapper\app_wrapper.c^
 .\app_wrapper\app_init.c^
 .\app_wrapper\app_io.c

set includes_APP_WRAPPER= -I".\app_wrapper\"
:: APP WRAPPER BAT END

set includes_WRAPPER= %includes_WRAPPER% %includes_APP_WRAPPER%
set code_WRAPPER= %code_WRAPPER% %code_APP_WRAPPER%


:: PERIPH BAT START
set code_PERIPH=.\MCU_STM32_Matlab\stm32_matlab_conf.c^
 .\MCU_STM32_Matlab\Drivers\STM32_SIMULINK\stm32_matlab_gpio.c^
 .\MCU_STM32_Matlab\Drivers\STM32_SIMULINK\stm32_matlab_dma.c^
 .\MCU_STM32_Matlab\Drivers\STM32_SIMULINK\stm32_periph_registers.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\BasicMathFunctions\BasicMathFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\BasicMathFunctions\BasicMathFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\BayesFunctions\BayesFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\BayesFunctions\BayesFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\CommonTables\CommonTables.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\CommonTables\CommonTablesF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\ComplexMathFunctions\ComplexMathFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\ComplexMathFunctions\ComplexMathFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\ControllerFunctions\ControllerFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\DistanceFunctions\DistanceFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\FastMathFunctions\FastMathFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\FastMathFunctions\FastMathFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\FilteringFunctions\FilteringFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\FilteringFunctions\FilteringFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\InterpolationFunctions\InterpolationFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\InterpolationFunctions\InterpolationFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\MatrixFunctions\MatrixFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\MatrixFunctions\MatrixFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\QuaternionMathFunctions\QuaternionMathFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\StatisticsFunctions\StatisticsFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\StatisticsFunctions\StatisticsFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\SupportFunctions\SupportFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\SupportFunctions\SupportFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\SVMFunctions\SVMFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\SVMFunctions\SVMFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\TransformFunctions\TransformFunctions.c^
 .\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\TransformFunctions\TransformFunctionsF16.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_adc.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_adc_ex.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_cortex.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_dma.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_exti.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_gpio.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_iwdg.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_pwr.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_pwr_ex.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_rcc.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_rcc_ex.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_tim.c^
 .\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_tim_ex.c^
 .\MCU_STM32_Matlab\Drivers\STM32_SIMULINK\stm32_matlab_tim.c^
 .\MCU_STM32_Matlab\Drivers\STM32_SIMULINK\stm32_matlab_adc.c

set includes_PERIPH=-I".\MCU_STM32_Matlab\."^
 -I".\MCU_STM32_Matlab\Drivers\STM32_SIMULINK"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\Device\STM32F4xx"^
 -I".\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Inc"^
 -I".\MCU_STM32_Matlab\Drivers\STM32F4xx_HAL_Driver\Inc\Legacy"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Include"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\PrivateInclude"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\BasicMathFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\BayesFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\CommonTables"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\ComplexMathFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\ControllerFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\DistanceFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\FastMathFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\FilteringFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\InterpolationFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\MatrixFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\QuaternionMathFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\StatisticsFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\SupportFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\SVMFunctions"^
 -I".\MCU_STM32_Matlab\Drivers\CMSIS\DSP\Source\TransformFunctions"
:: PERIPH BAT END
::-------------------------------------------------------------------------


:: ---------------------SET PARAMS FOR MEX COMPILING-----------------------
:: -------------ALL------------
set includes= %includes_WRAPPER% %includes_PERIPH% %includes_USER%
set codes= %code_USER% %code_WRAPPER% %code_PERIPH% 
set defines= %defines_WRAPPER% %defines_CONFIG% %defines_USER%
:: -------OUTPUT FOLDER--------
set output= -outdir "." -output %filename%

:: если нужен дебаг, до запускаем run_mex с припиской debug
IF %compil_mode%==debug (set debug= -g)
::-------------------------------------------------------------------------


::------START COMPILING-------    
if "%7"=="echo_enable" (
    echo Compiling...

    echo ===========================
    echo =========INCLUDES==========
    echo USER:
    for %%f in (%includes_USER%) do (
        echo   %%f
    )
    echo INTERNAL:
    for %%f in (%includes_WRAPPER%) do (
        echo   %%f
    )
    echo PERIPH:
    for %%f in (%includes_PERIPH%) do (
        echo   %%f
    )

    echo ===========================
    echo ==========SOURCES==========
    echo USER:
    for %%f in (%code_USER%) do (
        echo   %%f
    )
    echo INTERNAL:
    for %%f in (%code_WRAPPER%) do (
        echo   %%f
    )
    echo PERIPH:
    for %%f in (%code_PERIPH%) do (
        echo   %%f
    )

    echo ===========================
    echo ==========DEFINES==========
    echo USER:
    for %%d in (%defines_USER%) do (
        echo   %%d
    )
    echo CONFIG:
    for %%f in (%defines_CONFIG%) do (
        echo   %%f
    )
    echo INTERNAL:
    for %%f in (%defines_WRAPPER%) do (
        echo   %%f
    )
)
echo ===========================
echo MODE: %compil_mode%
echo ===========================
:: 1. ПРЕЖДЕ ЧЕМ КОМПИЛИРОВАТЬ - ВЫГРУЗИТЬ СТАРЫЙ ФАЙЛ

:: 2. Компиляция с флагами для MSVC
:: set LINK_EMBEDDED=/BASE:0x00000000 /FIXED:NO /FILEALIGN:0x1000 /FORCE:MULTIPLE /DYNAMICBASE:NO
set C_EMBEDDED="-w"
mex %output% %defines% %includes% %codes% %debug% CFLAGS="$CFLAGS %C_EMBEDDED%" LINKFLAGS="$LINKFLAGS %LINK_EMBEDDED%"
echo %DATE% %TIME%

exit /b %ERRORLEVEL%