AfterBuildHandler.bat 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. md build\keil\Execute
  2. @echo off
  3. :: enter .bat folder
  4. cd %~dp0
  5. :: 批处理所在路径
  6. set bat_path=%0
  7. :: MDK $J 这里传入的是KEIL 编译器头文件路径,利用这个路径找到编译器相关工具链地址
  8. set tool_chain_inc=%1
  9. :: MDK #L 这里传入的是KEIL生成的axf文件的完整路径
  10. set axf_full_path=%2
  11. :: 获取axf文件的名字,不含后缀
  12. set axf_name=%~n2
  13. if %tool_chain_inc:~-1,1% == \ (
  14. :: 删除路径最后的\
  15. set tool_chain_inc=%tool_chain_inc:~,-1%
  16. )
  17. :: call .bin generate function
  18. call :binGenerate %tool_chain_inc% %axf_full_path%
  19. if %errorlevel% == 1 (
  20. echo Failed 1: fromelf generate .bin failed!
  21. goto :EOF
  22. )
  23. :: call 文件复制
  24. call :doFileCopy %axf_full_path%
  25. :: 对hex文件进行Patch并生成patch后的.bin和.hex
  26. call :doFilePatch %axf_name%
  27. if %errorlevel% == 1 (
  28. echo Failed 2: Patch failed!
  29. goto :EOF
  30. )
  31. exit /b %errorlevel%
  32. :: Function Definiations ------------------------------------------------
  33. :: .bin generate function
  34. :binGenerate
  35. :: 通过头文件路径,获取工具链的根目录
  36. set tool_chain_root=%~dp1
  37. :: 获取axf的路径
  38. set axf_path=%~dp2
  39. :: 获取axf的名字
  40. set axf_name=%~n2
  41. :: echo %axf_path%
  42. :: echo %axf_name%
  43. :: echo %tool_chain_root%
  44. :: 执行fromelf 生成bin文件
  45. %tool_chain_root%bin\fromelf --bin %2 --output %axf_path%\%axf_name%.bin
  46. exit /b %errorlevel%
  47. :: 将axf/hex/bin文件复制到Execute文件夹下
  48. :doFileCopy
  49. :: 获取axf的名字
  50. set axf_name=%~n1
  51. copy /Y .\build\keil\Obj\%axf_name%.axf .\build\keil\Execute\%axf_name%.axf
  52. copy /Y .\build\keil\Obj\%axf_name%.hex .\build\keil\Execute\%axf_name%.hex
  53. copy /Y .\build\keil\Obj\%axf_name%.bin .\build\keil\Execute\%axf_name%.bin
  54. :: 根据用户的配置,可能hex不生成,不管这个,直接返回成功
  55. exit /b 0
  56. :: 对文件进行Patch
  57. :doFilePatch
  58. set target_name=%1
  59. Patcher.exe .\build\keil\Execute\%target_name%.hex
  60. exit /b %errorlevel%