test_cmake.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import logging
  4. import os
  5. import re
  6. import shutil
  7. from pathlib import Path
  8. import pytest
  9. from test_build_system_helpers import EnvDict, IdfPyFunc, append_to_file, file_contains, run_cmake, run_cmake_and_build
  10. def test_build_custom_cmake_project(test_app_copy: Path) -> None:
  11. for target in ['esp32', 'esp32s3', 'esp32c6', 'esp32h2']:
  12. logging.info(f'Test build ESP-IDF as a library to a custom CMake projects for {target}')
  13. idf_path = Path(os.environ['IDF_PATH'])
  14. run_cmake_and_build(str(idf_path / 'examples' / 'build_system' / 'cmake' / 'idf_as_lib'),
  15. '-DCMAKE_TOOLCHAIN_FILE={}'.format(idf_path / 'tools' / 'cmake' / f'toolchain-{target}.cmake'), f'-DTARGET={target}')
  16. assert file_contains((test_app_copy / 'build' / 'compile_commands.json'), '"command"')
  17. shutil.rmtree(test_app_copy / 'build')
  18. def test_build_cmake_library_psram_workaround(test_app_copy: Path) -> None:
  19. logging.info('Building a project with CMake library imported and PSRAM workaround, all files compile with workaround')
  20. idf_path = Path(os.environ['IDF_PATH'])
  21. (test_app_copy / 'sdkconfig.defaults').write_text('\n'.join(['CONFIG_SPIRAM=y',
  22. 'CONFIG_SPIRAM_CACHE_WORKAROUND=y']))
  23. run_cmake('-G', 'Ninja', '-DSDKCONFIG_DEFAULTS={}'.format(test_app_copy / 'sdkconfig.defaults'),
  24. str(idf_path / 'examples' / 'build_system' / 'cmake' / 'import_lib'))
  25. with open((test_app_copy / 'build' / 'compile_commands.json'), 'r', encoding='utf-8') as f:
  26. data = f.read()
  27. res = re.findall(r'.*\"command\".*', data)
  28. for r in res:
  29. assert 'mfix-esp32-psram-cache-issue' in r, 'All commands in compile_commands.json should use PSRAM cache workaround'
  30. def test_build_cmake_library_psram_strategies(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
  31. for strategy in ['DUPLDST', 'NOPS', 'MEMW']:
  32. logging.info(f'Test for external libraries in custom CMake projects with PSRAM strategy {strategy}')
  33. (test_app_copy / 'sdkconfig.defaults').write_text('\n'.join(['CONFIG_SPIRAM=y',
  34. f'CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_{strategy}=y',
  35. 'CONFIG_SPIRAM_CACHE_WORKAROUND=y']))
  36. idf_py('reconfigure')
  37. with open((test_app_copy / 'build' / 'compile_commands.json'), 'r', encoding='utf-8') as f:
  38. data = f.read()
  39. res = re.findall(r'.*\"command\".*', data)
  40. for r in res:
  41. assert f'mfix-esp32-psram-cache-strategy={strategy.lower()}' in r, ('All commands in compile_commands.json '
  42. 'should use PSRAM cache workaround strategy')
  43. (test_app_copy / 'sdkconfig').unlink()
  44. @pytest.mark.usefixtures('test_app_copy')
  45. @pytest.mark.usefixtures('idf_copy')
  46. def test_defaults_for_unspecified_idf_build_process_args(default_idf_env: EnvDict) -> None:
  47. logging.info('Defaults set properly for unspecified idf_build_process args')
  48. idf_path = Path(default_idf_env.get('IDF_PATH'))
  49. idf_as_lib_path = idf_path / 'examples' / 'build_system' / 'cmake' / 'idf_as_lib'
  50. append_to_file(idf_as_lib_path / 'CMakeLists.txt', '\n'.join(['idf_build_get_property(project_dir PROJECT_DIR)',
  51. 'message("Project directory: ${project_dir}")']))
  52. ret = run_cmake('..',
  53. '-DCMAKE_TOOLCHAIN_FILE={}'.format(str(idf_path / 'tools' / 'cmake' / 'toolchain-esp32.cmake')),
  54. '-DTARGET=esp32',
  55. workdir=idf_as_lib_path)
  56. assert 'Project directory: {}'.format(str(idf_as_lib_path)) in ret.stderr
  57. def test_build_example_on_host(default_idf_env: EnvDict) -> None:
  58. logging.info('Test if it can build the example to run on host')
  59. idf_path = Path(default_idf_env.get('IDF_PATH'))
  60. idf_as_lib_path = Path(idf_path, 'examples', 'build_system', 'cmake', 'idf_as_lib')
  61. try:
  62. target = 'esp32'
  63. run_cmake('..',
  64. f'-DCMAKE_TOOLCHAIN_FILE={idf_path}/tools/cmake/toolchain-{target}.cmake',
  65. f'-DTARGET={target}',
  66. '-GNinja',
  67. workdir=idf_as_lib_path)
  68. run_cmake('--build',
  69. '.',
  70. workdir=idf_as_lib_path)
  71. finally:
  72. shutil.rmtree(idf_as_lib_path / 'build', ignore_errors=True)