test_build.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import logging
  4. import os
  5. import stat
  6. import sys
  7. import textwrap
  8. from pathlib import Path
  9. from typing import List, Union
  10. import pytest
  11. from test_build_system_helpers import (APP_BINS, BOOTLOADER_BINS, PARTITION_BIN, IdfPyFunc, append_to_file,
  12. file_contains, get_idf_build_env, replace_in_file, run_cmake_and_build)
  13. def assert_built(paths: Union[List[str], List[Path]]) -> None:
  14. for path in paths:
  15. assert os.path.exists(path)
  16. def test_build_alternative_directories(idf_py: IdfPyFunc, session_work_dir: Path, test_app_copy: Path) -> None:
  17. logging.info('Moving BUILD_DIR_BASE out of tree')
  18. alt_build_dir = session_work_dir / 'alt_build'
  19. idf_py('-B', str(alt_build_dir), 'build')
  20. assert os.listdir(alt_build_dir) != [], 'No files found in new build directory!'
  21. default_build_dir = test_app_copy / 'build'
  22. if default_build_dir.exists():
  23. assert os.listdir(default_build_dir) == [], f'Some files were incorrectly put into the default build directory: {default_build_dir}'
  24. logging.info('BUILD_DIR_BASE inside default build directory')
  25. build_subdir_inside_build_dir = default_build_dir / 'subdirectory'
  26. idf_py('-B', str(build_subdir_inside_build_dir), 'build')
  27. assert os.listdir(build_subdir_inside_build_dir) != [], 'No files found in new build directory!'
  28. @pytest.mark.usefixtures('test_app_copy')
  29. def test_build_cmake_ninja() -> None:
  30. logging.info('Can build with Ninja (no idf.py)')
  31. run_cmake_and_build('-G', 'Ninja', '..')
  32. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN)
  33. @pytest.mark.skipif(sys.platform == 'win32', reason="GNU Make doesn't work on Windows")
  34. @pytest.mark.usefixtures('test_app_copy')
  35. def test_build_cmake_makefile() -> None:
  36. logging.info('Can build with GNU Make (no idf.py)')
  37. run_cmake_and_build('-G', 'Unix Makefiles', '..')
  38. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN)
  39. @pytest.mark.usefixtures('test_app_copy')
  40. def test_build_with_generator_ninja(idf_py: IdfPyFunc) -> None:
  41. logging.info('idf.py can build with Ninja')
  42. idf_py('-G', 'Ninja', 'build')
  43. cmake_cache_file = Path('build', 'CMakeCache.txt')
  44. assert_built([cmake_cache_file])
  45. assert file_contains(cmake_cache_file, 'CMAKE_GENERATOR:INTERNAL=Ninja')
  46. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN)
  47. @pytest.mark.skipif(sys.platform == 'win32', reason="GNU Make doesn't work on Windows")
  48. @pytest.mark.usefixtures('test_app_copy')
  49. def test_build_with_generator_makefile(idf_py: IdfPyFunc) -> None:
  50. logging.info('idf.py can build with Unix Makefiles')
  51. idf_py('-G', 'Unix Makefiles', 'build')
  52. cmake_cache_file = Path('build', 'CMakeCache.txt')
  53. assert_built([cmake_cache_file])
  54. assert file_contains(cmake_cache_file, 'CMAKE_GENERATOR:INTERNAL=Unix Makefiles')
  55. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN)
  56. def test_build_with_cmake_and_idf_path_unset(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  57. idf_path = Path(os.environ['IDF_PATH'])
  58. env = get_idf_build_env(idf_path)
  59. env.pop('IDF_PATH')
  60. logging.info('Can build with IDF_PATH set via cmake cache not environment')
  61. replace_in_file('CMakeLists.txt', 'ENV{IDF_PATH}', '{IDF_PATH}')
  62. run_cmake_and_build('-G', 'Ninja', '..', '-DIDF_PATH={}'.format(idf_path), env=env)
  63. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN)
  64. idf_py('fullclean')
  65. logging.info('Can build with IDF_PATH unset and inferred by cmake when Kconfig needs it to be set')
  66. # working with already changed CMakeLists.txt
  67. kconfig_file = test_app_copy / 'main' / 'Kconfig.projbuild'
  68. kconfig_file.write_text('source "$IDF_PATH/examples/wifi/getting_started/station/main/Kconfig.projbuild"')
  69. run_cmake_and_build('-G', 'Ninja', '..', '-DIDF_PATH={}'.format(idf_path), env=env)
  70. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN)
  71. kconfig_file.unlink() # remove file to not affect following sub-test
  72. idf_py('fullclean')
  73. logging.info('Can build with IDF_PATH unset and inferred by build system')
  74. # replacing {IDF_PATH} not ENV{IDF_PATH} since CMakeLists.txt was already changed in this test
  75. replace_in_file('CMakeLists.txt', '{IDF_PATH}', '{ci_idf_path}')
  76. run_cmake_and_build('-G', 'Ninja', '-D', 'ci_idf_path={}'.format(idf_path), '..', env=env)
  77. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN)
  78. def test_build_skdconfig_phy_init_data(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  79. logging.info('can build with phy_init_data')
  80. (test_app_copy / 'sdkconfig.defaults').touch()
  81. (test_app_copy / 'sdkconfig.defaults').write_text('CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION=y')
  82. idf_py('reconfigure')
  83. idf_py('build')
  84. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN + ['build/phy_init_data.bin'])
  85. def test_build_compiler_flag_in_source_file(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  86. logging.info('Compiler flags on build command line are taken into account')
  87. compiler_flag = textwrap.dedent("""
  88. #ifndef USER_FLAG
  89. #error "USER_FLAG is not defined!"
  90. #endif
  91. """)
  92. append_to_file((test_app_copy / 'main' / 'build_test_app.c'), compiler_flag)
  93. idf_py('build', '-DCMAKE_C_FLAGS=-DUSER_FLAG')
  94. @pytest.mark.usefixtures('test_app_copy')
  95. def test_build_compiler_flags_no_overwriting(idf_py: IdfPyFunc) -> None:
  96. logging.info('Compiler flags cannot be overwritten')
  97. # If the compiler flags are overriden, the following build command will
  98. # cause issues at link time.
  99. idf_py('build', '-DCMAKE_C_FLAGS=', '-DCMAKE_CXX_FLAGS=')
  100. def test_build_with_sdkconfig_build_abspath(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  101. build_path = test_app_copy / 'build_tmp'
  102. sdkconfig_path = build_path / 'sdkconfig'
  103. idf_py('-D', f'SDKCONFIG={sdkconfig_path}', '-B', str(build_path), 'build')
  104. def test_build_fail_on_build_time(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  105. logging.info('Fail on build time works')
  106. append_to_file(test_app_copy / 'CMakeLists.txt', '\n'.join(['',
  107. 'if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/hello.txt")',
  108. 'fail_at_build_time(test_file "hello.txt does not exists")',
  109. 'endif()']))
  110. ret = idf_py('build', check=False)
  111. assert ret.returncode != 0, 'Build should fail if requirements are not satisfied'
  112. (test_app_copy / 'hello.txt').touch()
  113. idf_py('build')
  114. @pytest.mark.usefixtures('test_app_copy')
  115. def test_build_dfu(idf_py: IdfPyFunc) -> None:
  116. logging.info('DFU build works')
  117. ret = idf_py('dfu', check=False)
  118. assert 'command "dfu" is not known to idf.py and is not a Ninja target' in ret.stderr, 'DFU build should fail for default chip target'
  119. idf_py('set-target', 'esp32s2')
  120. ret = idf_py('dfu')
  121. assert 'build/dfu.bin" has been written. You may proceed with DFU flashing.' in ret.stdout, 'DFU build should succeed for esp32s2'
  122. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN + ['build/dfu.bin'])
  123. @pytest.mark.usefixtures('test_app_copy')
  124. def test_build_uf2(idf_py: IdfPyFunc) -> None:
  125. logging.info('UF2 build works')
  126. ret = idf_py('uf2')
  127. assert 'build/uf2.bin, ready to be flashed with any ESP USB Bridge' in ret.stdout, 'UF2 build should work for esp32'
  128. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN + ['build/uf2.bin'])
  129. ret = idf_py('uf2-app')
  130. assert 'build/uf2-app.bin, ready to be flashed with any ESP USB Bridge' in ret.stdout, 'UF2 build should work for application binary'
  131. assert_built(['build/uf2-app.bin'])
  132. idf_py('set-target', 'esp32s2')
  133. ret = idf_py('uf2')
  134. assert 'build/uf2.bin, ready to be flashed with any ESP USB Bridge' in ret.stdout, 'UF2 build should work for esp32s2'
  135. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN + ['build/uf2.bin'])
  136. def test_build_loadable_elf(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  137. logging.info('Loadable ELF build works')
  138. (test_app_copy / 'sdkconfig').write_text('\n'.join(['CONFIG_APP_BUILD_TYPE_RAM=y',
  139. 'CONFIG_VFS_SUPPORT_TERMIOS=n',
  140. 'CONFIG_NEWLIB_NANO_FORMAT=y',
  141. 'CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y',
  142. 'CONFIG_ESP_ERR_TO_NAME_LOOKUP=n']))
  143. idf_py('reconfigure')
  144. assert (test_app_copy / 'build' / 'flasher_args.json').exists(), 'flasher_args.json should be generated in a loadable ELF build'
  145. idf_py('build')
  146. @pytest.mark.skipif(sys.platform == 'win32', reason='Windows does not support stat commands')
  147. def test_build_with_crlf_files(idf_py: IdfPyFunc, test_app_copy: Path, idf_copy: Path) -> None:
  148. def change_files_to_crlf(path: Path) -> None:
  149. for root, _, files in os.walk(path):
  150. for filename in files:
  151. file_path = os.path.join(root, filename)
  152. # Do not modify .git directory and executable files, as Linux will fail to execute them
  153. if '.git' in file_path or os.stat(file_path).st_mode & stat.S_IEXEC:
  154. continue
  155. with open(file_path, 'rb') as f:
  156. data = f.read()
  157. crlf_data = data.replace(b'\n', b'\r\n')
  158. with open(file_path, 'wb') as f:
  159. f.write(crlf_data)
  160. logging.info('Can still build if all text files are CRLFs')
  161. change_files_to_crlf(test_app_copy)
  162. change_files_to_crlf(idf_copy)
  163. idf_py('build')
  164. assert_built(BOOTLOADER_BINS + APP_BINS + PARTITION_BIN)