Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relative and deterministic DT comments #19333

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion scripts/dts/gen_defines.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# edtlib. This will keep this script simple.

import argparse
import os
from pathlib import Path
import sys

import edtlib
Expand All @@ -40,7 +42,20 @@ def main():

out_comment("Generated by gen_defines.py", blank_before=False)
out_comment("DTS input file: " + args.dts, blank_before=False)
out_comment("Directories with bindings: " + ", ".join(args.bindings_dirs),

zbase = Path(os.environ["ZEPHYR_BASE"])
shorter_bdirs = {}
for d in args.bindings_dirs:
try:
shorter_bdirs[d] = Path(d).relative_to(zbase)
except ValueError:
# leave as is when outside ZEPHYR_BASE
shorter_bdirs[d] = Path(d)

out_comment("Directories with bindings: " +
", ".join([ str("ZEPHYR_BASE" / d)
for d in shorter_bdirs.values()
]),
Copy link
Collaborator

@ulfalizer ulfalizer Sep 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poked around for a while and came up with a helper function that might be handy here:

    out_comment("Generated by gen_defines.py", blank_before=False)
    out_comment("DTS input file: " + args.dts, blank_before=False)
    out_comment("Directories with bindings: " +
                ", ".join(map(relativize, args.bindings_dirs)),
                blank_before=False)

Definition (I put it after parse_args()):

def relativize(path):
    # If 'path' is within $ZEPHYR_BASE, returns it relative to $ZEPHYR_BASE,
    # with a "$ZEPHYR_BASE/..." hint at the start of the string. Otherwise,
    # returns 'path' unchanged.

    if "ZEPHYR_BASE" not in os.environ:
        return path

    try:
        return str("$ZEPHYR_BASE" /
                   Path(path).relative_to(os.environ["ZEPHYR_BASE"]))
    except ValueError:
        # Not within ZEPHYR_BASE
        return path

Has the advantage that gen_defines.py won't crash if $ZEPHYR_BASE is unset, though I don't know if that'd come up.

Thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also do:

zbase = os.environ.get("ZEPHYR_BASE")
if not zbase:
   return path

... / Path(path).relative_to(zbase)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, bit neater.

blank_before=False)

active_compats = set()
Expand Down