@echo off

set defines=-D"STM32F407xx" -D"USE_HAL_DRIVER"^
 -D"MATLAB"^
 -D"__sizeof_ptr=8"
:: -------------------------USERS PATHS AND CODE---------------------------
::-----------заголовочные файлы---------------
:: не добавлять CMSIS и HAL, они кастомные
set includes_user= -I".\Core\Inc"

::-----------исходный код---------------
set code_user=.\Core\Src\main.c^
 .\Core\Src\tim.c^
 .\Core\Src\gpio.c^
 .\Core\Src\stm32f4xx_it.c^
 .\Core\Src\system_stm32f4xx.c

:: .\Core\Src\stm32f4xx_hal_msp.c^
::-------------------------------------------------------------------------


:: -----------------------MCU LIBRARIES & SIMULATOR------------------------
:: -------MCU INCLUDED PATHS-------
set includes_MCU= -I"."^
 -I".\MCU_STM32F407_Matlab"^
 -I".\MCU_STM32F407_Matlab\Drivers\STM32F4xx_SIMULINK"^
 -I".\MCU_STM32F407_Matlab\Drivers\CMSIS"^
 -I".\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Inc"^
 -I".\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Inc\Legacy"

:: --------MCU SIMULATOR-------
:: код, которая будет симулировать перефирию МК в симулинке
set code_mcu_sim= .\MCU_STM32F407_Matlab\Drivers\CMSIS\stm32f407xx_matlab.c^
 .\MCU_STM32F407_Matlab\STM32F407xx.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_SIMULINK\stm32f4xx_matlab_tim.c
  
:: ---------HAL CODE----------
set weak_folder= ".\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src"

set code_hal=.\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_tim.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_tim_ex.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_rcc.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_flash.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_flash_ex.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_flash_ramfunc.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_gpio.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_dma.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_pwr.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_pwr_ex.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_cortex.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal.c^
 .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_exti.c
:: .\MCU_STM32F407_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_rcc_ex.c^


:: --------SHELL CODE----------
:: оболочка, которая будет моделировать работу МК в симулинке
set code_shell= .\MCU_Simulink\MCU.c^
 .\MCU_Simulink\controller.c
 set asm_shell=asmjmp
:: оболочка, которая будет моделировать работу МК в симулинке
set includes_shell= -I".\MCU_Simulink"






:: ---------LIBRARIES---------
:: set params_obj=..\device_support_ml\source\C28x_FPU_FastRTS.obj ..\device_support_ml\source\DSP2833x_GlobalVariableDefs.obj

:: ---------------------SET PARAMS FOR MEX COMPILING-----------------------
:: --------ALL INCLUDES--------
set includes= %includes_user% %includes_MCU% %includes_shell%
:: -------OUTPUT FOLDER--------
set output= -outdir "."
set output_folder=.\MCU_Simulink\Outputs\

set asm_source=.\MCU_Simulink\%asm_shell%.asm
set asm_obj=%output_folder%%asm_shell%.obj

set weak_output_folder=%output_folder%WeaksLibs\
set weak_lib=%weak_output_folder%Weak_functions.lib

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

::------START COMPILING-------
set includes_defines= %defines% %includes%


IF [%2]==[UPDATE] (
del %output_folder%\*.obj

del %weak_output_folder%\*.obj
del %weak_output_folder%\*.lib

echo Create Obj with weak functions...
for %%f in (%code_hal%) do (
    mex -c -outdir %weak_output_folder% %includes_defines% %%f %debug% > nul
)
    
)
    
echo Create Obj with ASM context manager functions
set asm_exe= "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\ml64.exe"
set asm_command= /c /Fo %asm_obj% %asm_source%
echo ASM: %asm_command%
%asm_exe% %asm_command%


setlocal enabledelayedexpansion
set weak_obj_files=
for %%f in (%weak_output_folder%\*.obj) do (
    set weak_obj_files=!weak_obj_files! %%f
)
set lib_exe= "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64\lib.exe"
set lib_command= /out:%weak_lib% %weak_obj_files%
echo Create Libs with weak functions...
echo LIB: %lib_command%
%lib_exe% %lib_command%

mex %output% %includes_defines% %code_shell% %code_mcu_sim% %code_user% %asm_obj% %weak_lib% %debug%

::echo mex %hal_lib%
::mex %hal_lib%
:: если дебаг, то компилим из source
::IF [%1]==[debug] (set final_mex= mex %output% %debug% %defines% %includes_shell% %includes_MCU% %includes_user% %code_shell% %code_mcu_sim% %code_hal% %code_user% %params_obj%)
::ELSE (set final_mex= )

::echo mex %final_mex%
::mex %final_mex%
::-------------------------------------------------------------------------