@echo off  
setlocal  
  
:: 设置输出文件夹名  
set "OUTPUT_DIR=wasm"  
  
:: 如果输出文件夹不存在，则创建它  
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"  
  
:: 设置输出文件名  
set "OUTPUT_FILE=%OUTPUT_DIR%\aplm8000sdk.js"  
  
:: 设置编译器选项（如果有需要的话，可以添加优化选项、定义等）  
set "COMPILER_OPTIONS1=-O3 -s WASM=1 -Wall -Wextra"  
set "COMPILER_OPTIONS2=-sINITIAL_MEMORY=1024mb -sTOTAL_STACK=67108864 -sSTACK_SIZE=10485760"

::系统入口模块
set "MAIN=main.c"

::数据交换模块
set "EXCHANGE=src\exchange\exchange.c" 

::::设置业务模块文件
::1、解调模块
set "DEMODU_MAIN=src\modules\demodulation\dmdl_main.c"
set "DEMODU_DDC=src\modules\demodulation\dmdl_ddc.c"
set "DEMODU_OVERLAP=src\modules\demodulation\dmdl_overlap.c"
set "DEMODU_FIR_REAL=src\modules\demodulation\dmdl_filter390_real.c"
set "DEMODU_FIR_IMAG=src\modules\demodulation\dmdl_filter390_imag.c"
set "DEMODU_MODULUS=src\modules\demodulation\dmdl_modulus.c"
set "DEMODULATION=%DEMODU_MAIN% %DEMODU_DDC% %DEMODU_OVERLAP% %DEMODU_FIR_REAL% %DEMODU_FIR_IMAG% %DEMODU_MODULUS% "

::2、基站模块
set "BTS_MAIN=src\modules\basestation\bts_main.c"
set "BTS_AMPL=src\modules\basestation\bts_ampl.c"
set "BTS_SYNC=src\modules\basestation\bts_sync.c"
::set "BTS_ID=src\modules\basestation\bts_id.c"
::set "BTS_IDCHECK=src\modules\basestation\bts_idcheck.c"
set "BTS_TRACK=src\modules\basestation\bts_track.c"
set "BASESTATION=%BTS_MAIN% %BTS_AMPL% %BTS_SYNC% %BTS_TRACK%"

::3、定位模块
set "LCT_MAIN=src\modules\location\lct_main.c"
set "LCT_FIRSTPATH=src\modules\location\lct_firstpath.c"
set "LCT_COORDCAL=src\modules\location\lct_coordcal.c"
set "LCT_PREDICTION=src\modules\location\lct_prediction.c"
set "LCT_COORDCAL_ONE=src\modules\location\lct_coordcal_one.c"
set "LCT_COORDCAL_TWO=src\modules\location\lct_coordcal_two.c"
set "LCT_COORDSEL=src\modules\location\lct_coordsel.c"
set "LCT_SINC=src\modules\location\lct_sinc.c"
set "LOCATION=%LCT_MAIN% %LCT_FIRSTPATH% %LCT_COORDCAL% %LCT_PREDICTION% %LCT_COORDCAL_ONE% %LCT_COORDCAL_TWO% %LCT_COORDSEL% %LCT_SINC%"

::4、路由模块
set "ROUTE_MAIN=src\modules\route\route_main.c"
set "ROUTE_BLUETOOTH=src\modules\route\route_bluetooth.c"
set "ROUTE_WIFI=src\modules\route\route_wifi.c"
set "ROUTE=%ROUTE_MAIN% %ROUTE_BLUETOOTH% %ROUTE_WIFI%"

::5、更新定位数据模块
set "LCT_DATA=src\modules\lctdata\lctdata.c"

::6、IMU模块
set "LCT_IMU=src\modules\imu\imu.c"


::系统主控模块
set "GLOBALDATA=src\sysmain\globaldata.c" 
set "SYS_MAIN=src\sysmain\sysmain.c" 
set "SYSMAIN=%GLOBALDATA% %SYS_MAIN%"

::工具模块
set "COMPLEX=src\utils\complex.c"
set "DISTANCE=src\utils\distance.c"
set "SORT=src\utils\sort.c"
set "FFT=src\utils\fft.c"
::set "FILE=src\utils\file.c"
set "LOOPLIST=src\utils\looplist.c"
set "UTILS=%COMPLEX% %DISTANCE% %SORT% %FFT% %LOOPLIST%"

::设置所有编译文件
set "SOURCE_FILES=%MAIN% %EXCHANGE% %DEMODULATION% %BASESTATION% %LOCATION% %ROUTE% %LCT_DATA% %LCT_IMU% %SYSMAIN% %UTILS%"

:: 使用emcc编译源文件  
emcc  %SOURCE_FILES% -o %OUTPUT_FILE%  %COMPILER_OPTIONS2%


:: 检查编译是否成功，%errorlevel%是一个特殊的变量，保存了emcc命令的退出代码，要是不为0则标识失败
if %errorlevel% neq 0 (  
    echo Compilation failed
    exit /b %errorlevel%  
)  
  
echo Compilation successful
  
endlocal