srcdir.py 823 B

1234567891011121314151617181920212223242526272829
  1. # srcdir.py
  2. #
  3. # Helper script to locate the relative source folder for a given object folder.
  4. # For example:
  5. # objdir = /path/to/out/stm32f105/prod/floppy/usb
  6. # srcdir = ../../../../../src/usb
  7. #
  8. # Written & released by Keir Fraser <keir.xen@gmail.com>
  9. #
  10. # This is free and unencumbered software released into the public domain.
  11. # See the file COPYING for more details, or visit <http://unlicense.org>.
  12. import sys, re
  13. # /out/<mcu>/<level>/<target>
  14. NR_LEVELS = 4
  15. objdir = sys.argv[1]
  16. # stem = /out/<mcu>/<level>/target[/<rest_of_path>]
  17. stem = objdir[objdir.rfind('/out'):]
  18. # stem = [/<rest_of_path>]
  19. m = re.match('/[^/]*'*NR_LEVELS+'(/.*)?', stem)
  20. stem = '' if m.group(1) is None else m.group(1)
  21. # srcdir = path to sources, relative to objdir
  22. srcdir = '../'*(NR_LEVELS+stem.count('/')) + 'src' + stem
  23. print(srcdir)