Skip to content

Commit

Permalink
further debug
Browse files Browse the repository at this point in the history
  • Loading branch information
catvw committed Feb 13, 2024
1 parent a699dda commit d433365
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use_flake() {
watch_file flake.nix
watch_file flake.lock
eval "$(nix print-dev-env --profile "$(direnv_layout_dir)/flake-profile")"
}

use flake

watch_file './site_scons/env.py'
eval $(./site_scons/env.py --export)
3 changes: 3 additions & 0 deletions .github/actions/checkout/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ runs:
steps:
- uses: DeterminateSystems/nix-installer-action@v6
- uses: DeterminateSystems/magic-nix-cache-action@v2
- uses: HatsuneMiku3939/[email protected]
with:
direnvVersion: 2.32.3
6 changes: 3 additions & 3 deletions .github/workflows/mdbook-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ jobs:
with:
submodules: recursive

- name: another
run: echo $PATH

- name: Checkout `carrie` (we hope)
uses: ./.github/actions/checkout

- name: misc
run: nix build .

- name: Build docs
run: mdbook build

Expand Down
104 changes: 104 additions & 0 deletions site_scons/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env python3

import argparse
import json
import os
import shlex
import typing


_CARGO_HOME = '.cargo'
_IDF_BUILD = '.esp-idf'
_IDF_TOOLS_PATH = '.espressif'
_VIRTUAL_ENV = '.venv'


DIRENV_INSTALLED: typing.Final[dict[str, str]] = {
f'DIRENV_INSTALLED_{key}': f'{value}/.direnv.installed'
for key, value in {
'CRATES': _CARGO_HOME,
'IDF_TOOLS': _IDF_TOOLS_PATH,
'PYTHON_REQUIREMENTS': _VIRTUAL_ENV,
'SCONS_ESP_IDF_ENVIRONMENT': _IDF_BUILD,
}.items()
}


PATHS: typing.Final[dict[str, str]] = {
key: os.path.abspath(value)
for key, value in {
**DIRENV_INSTALLED,
'CARGO_HOME': _CARGO_HOME,
'IDF_BUILD': _IDF_BUILD,
'IDF_BUILD_SCONS_ESP32S3': f'{_IDF_BUILD}/scons/esp32s3.json',
'IDF_PATH': 'lib/esp-idf',
'IDF_TOOLS_PATH': _IDF_TOOLS_PATH,
'REPO_ROOT': '.',
'VIRTUAL_ENV': _VIRTUAL_ENV,
}.items()
}


ENV: typing.Final[dict[str, str]] = {
**PATHS,
'CMAKE_GENERATOR': 'Ninja',
'IDF_TARGETS': 'esp32s3',
'VIRTUAL_ENV_DISABLE_PROMPT': 'true',
}


def idf(env, target: str):
env = env.Clone()

with open(PATHS[f'IDF_BUILD_SCONS_{target.upper()}']) as f:
idf = json.load(f)

env.AppendUnique(
CPPPATH=idf['includes'],
CPPDEFINES=idf['defines'],
CCFLAGS=idf['options']
+ idf['optimization']
+ idf['debug']
+ idf['march']
+ idf['std'],
)

env.PrependENVPath('PATH', idf['path'])

prefix = idf['prefix']

env.Replace(
**{
key: f'{prefix}{val}'
for key, val in {
'AR': 'ar',
'AS': 'as',
'CC': 'gcc',
'CXX': 'g++',
'RANLIB': 'ranlib',
'SHLINK': 'ld',
}.items()
}
)

return env


if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='env.py',
description='generate environment variables',
)

parser.add_argument(
'-e',
'--export',
action='store_true',
)

args = parser.parse_args()

export = 'export ' if args.export else ''

for key, value in ENV.items():
print(f'{export}{key}={shlex.quote(value)}')
38 changes: 38 additions & 0 deletions site_scons/site_tools/Component.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import tomllib

from schema import (
Regex,
Schema,
)


schema = Schema(
{
'metadata': {
'name': Regex(r'^[a-z]+$'),
'description': str,
},
}
)


def Component(env, target, config):
with open(config.srcnode().abspath, 'rb') as f:
config = tomllib.load(f)

assert schema.validate(config) == config

name = config['metadata']['name']

return env.Alias(f'component:{name}', target, '')


def generate(env):
if env.Detect('Component'):
return

env.AddMethod(Component, 'Component')


def exists(env):
return env.Detect('Component')
71 changes: 71 additions & 0 deletions site_scons/site_tools/EspIdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os


OUTPUTS = [
'bootloader/config/sdkconfig.h',
'bootloader/.bin_timestamp',
'bootloader/bootloader.bin',
'bootloader/bootloader.elf',
'bootloader/bootloader.map',
'bootloader/prefix_map_gdbinit',
'config/sdkconfig.h',
'partition_table/partition-table.bin',
'.bin_timestamp',
'app-flash_args',
'bootloader-flash_args',
'firmware.bin',
'firmware.elf',
'firmware.map',
'flash_app_args',
'flash_args',
'flash_bootloader_args',
'flash_project_args',
'flasher_args.json',
'ldgen_libraries',
'partition-table-flash_args',
'prefix_map_gdbinit',
'x509_crt_bundle.S',
]


def EspIdf(env, library, target, *, outdir='esp-idf'):
build = f'{os.environ["IDF_BUILD"]}/march/{target}'
outdir = f'{os.path.dirname(library.abspath)}/{outdir}'

libprebuilt = (
f'{os.environ["REPO_ROOT"]}/lib/march/{target}/main/libprebuilt.a'
)

# since the esp-idf build is shared resource we need to
# lock it while we compile and link with out shared library
actions = [
f'exec {{LOCKFD}}> {build}/scons.lock',
'echo $$LOCKFD',
'flock --exclusive $$LOCKFD',
f'rm -f {libprebuilt}',
f'ln -s {library.abspath} {libprebuilt}',
f'ninja -C {build}',
f'mkdir -p {outdir}/bootloader',
f'mkdir -p {outdir}/partition_table',
*[f'cp {build}/{output} {outdir}/{output}' for output in OUTPUTS],
'exec {LOCKFD}>&-',
]

actions = ' && '.join(actions)

out = env.Command(
[f'{outdir}/{output}' for output in OUTPUTS], library, actions
)

return out


def generate(env):
if env.Detect('EspIdf'):
return

env.AddMethod(EspIdf, 'EspIdf')


def exists(env):
return env.Detect('EspIdf')
13 changes: 13 additions & 0 deletions site_scons/site_tools/Phony.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def Phony(env, target, action):
return env.AlwaysBuild(env.Alias(target, [], action))


def generate(env):
if env.Detect('Phony'):
return

env.AddMethod(Phony, 'Phony')


def exists(env):
return env.Detect('Phony')

0 comments on commit d433365

Please sign in to comment.