2019-11-10 22:39:42 -05:00
|
|
|
# Sphinx extension to integrate IDF build system information
|
|
|
|
# into the Sphinx Build
|
|
|
|
#
|
|
|
|
# Runs early in the Sphinx process, runs CMake to generate the dummy IDF project
|
|
|
|
# in this directory - including resolving paths, etc.
|
|
|
|
#
|
|
|
|
# Then emits the new 'idf-info' event which has information read from IDF
|
|
|
|
# build system, that other extensions can use to generate relevant data.
|
2021-01-25 21:49:01 -05:00
|
|
|
import json
|
2019-11-10 22:39:42 -05:00
|
|
|
import os.path
|
2020-02-06 23:36:14 -05:00
|
|
|
import shutil
|
2019-11-10 22:39:42 -05:00
|
|
|
import subprocess
|
2021-01-25 21:49:01 -05:00
|
|
|
import sys
|
2019-11-10 22:39:42 -05:00
|
|
|
|
|
|
|
# this directory also contains the dummy IDF project
|
|
|
|
project_path = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
2020-10-28 21:57:09 -04:00
|
|
|
# Targets which needs --preview to build
|
2021-05-24 03:08:00 -04:00
|
|
|
PREVIEW_TARGETS = ['esp32s3']
|
2020-10-28 21:57:09 -04:00
|
|
|
|
2019-11-28 16:56:53 -05:00
|
|
|
|
2019-11-10 22:39:42 -05:00
|
|
|
def setup(app):
|
2019-11-28 16:56:53 -05:00
|
|
|
# Setup some common paths
|
|
|
|
|
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
build_dir = os.environ['BUILDDIR'] # TODO see if we can remove this
|
2019-11-28 16:56:53 -05:00
|
|
|
except KeyError:
|
|
|
|
build_dir = os.path.dirname(app.doctreedir.rstrip(os.sep))
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.mkdir(build_dir)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.mkdir(os.path.join(build_dir, 'inc'))
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Fill in a default IDF_PATH if it's missing (ie when Read The Docs is building the docs)
|
|
|
|
try:
|
|
|
|
idf_path = os.environ['IDF_PATH']
|
|
|
|
except KeyError:
|
|
|
|
idf_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
app.add_config_value('docs_root', os.path.join(idf_path, 'docs'), 'env')
|
2019-11-28 16:56:53 -05:00
|
|
|
app.add_config_value('idf_path', idf_path, 'env')
|
|
|
|
app.add_config_value('build_dir', build_dir, 'env') # not actually an IDF thing
|
2019-11-10 22:39:42 -05:00
|
|
|
app.add_event('idf-info')
|
|
|
|
|
|
|
|
# we want this to run early in the docs build but unclear exactly when
|
2019-12-06 04:22:55 -05:00
|
|
|
app.connect('config-inited', generate_idf_info)
|
2019-11-10 22:39:42 -05:00
|
|
|
|
2019-11-28 16:56:53 -05:00
|
|
|
return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '0.1'}
|
|
|
|
|
2019-11-28 00:00:24 -05:00
|
|
|
|
2019-12-06 04:22:55 -05:00
|
|
|
def generate_idf_info(app, config):
|
2021-01-25 21:49:01 -05:00
|
|
|
print('Running CMake on dummy project to get build info...')
|
2019-11-12 05:42:03 -05:00
|
|
|
build_dir = os.path.dirname(app.doctreedir.rstrip(os.sep))
|
2021-01-25 21:49:01 -05:00
|
|
|
cmake_build_dir = os.path.join(build_dir, 'build_dummy_project')
|
|
|
|
idf_py_path = os.path.join(app.config.idf_path, 'tools', 'idf.py')
|
|
|
|
print('Running idf.py...')
|
2019-11-12 05:42:03 -05:00
|
|
|
idf_py = [sys.executable,
|
|
|
|
idf_py_path,
|
2021-01-25 21:49:01 -05:00
|
|
|
'-B',
|
2019-11-12 05:42:03 -05:00
|
|
|
cmake_build_dir,
|
2021-01-25 21:49:01 -05:00
|
|
|
'-C',
|
2020-02-06 23:36:14 -05:00
|
|
|
project_path,
|
2021-01-25 21:49:01 -05:00
|
|
|
'-D',
|
|
|
|
'SDKCONFIG={}'.format(os.path.join(build_dir, 'dummy_project_sdkconfig'))
|
2020-02-06 23:36:14 -05:00
|
|
|
]
|
2019-11-12 22:46:16 -05:00
|
|
|
|
2020-02-06 23:36:14 -05:00
|
|
|
# force a clean idf.py build w/ new sdkconfig each time
|
|
|
|
# (not much slower than 'reconfigure', avoids any potential config & build versioning problems
|
|
|
|
shutil.rmtree(cmake_build_dir, ignore_errors=True)
|
2021-01-25 21:49:01 -05:00
|
|
|
print('Starting new dummy IDF project... ')
|
2020-10-28 21:57:09 -04:00
|
|
|
|
|
|
|
if (app.config.idf_target in PREVIEW_TARGETS):
|
2021-01-25 21:49:01 -05:00
|
|
|
subprocess.check_call(idf_py + ['--preview', 'set-target', app.config.idf_target])
|
2020-10-28 21:57:09 -04:00
|
|
|
else:
|
2021-01-25 21:49:01 -05:00
|
|
|
subprocess.check_call(idf_py + ['set-target', app.config.idf_target])
|
2020-10-28 21:57:09 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
print('Running CMake on dummy project...')
|
|
|
|
subprocess.check_call(idf_py + ['reconfigure'])
|
2020-02-06 23:36:14 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
with open(os.path.join(cmake_build_dir, 'project_description.json')) as f:
|
2019-11-10 22:39:42 -05:00
|
|
|
project_description = json.load(f)
|
2021-01-25 21:49:01 -05:00
|
|
|
if project_description['target'] != app.config.idf_target:
|
2019-11-12 22:46:16 -05:00
|
|
|
# this shouldn't really happen unless someone has been moving around directories inside _build, as
|
|
|
|
# the cmake_build_dir path should be target-specific
|
2021-01-25 21:49:01 -05:00
|
|
|
raise RuntimeError(('Error configuring the dummy IDF project for {}. ' +
|
|
|
|
'Target in project description is {}. ' +
|
|
|
|
'Is build directory contents corrupt?')
|
|
|
|
.format(app.config.idf_target, project_description['target']))
|
2019-11-10 22:39:42 -05:00
|
|
|
app.emit('idf-info', project_description)
|
|
|
|
|
|
|
|
return []
|