quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

build_tree.py (5058B)


      1 """Mbed TLS build tree information and manipulation.
      2 """
      3 
      4 # Copyright The Mbed TLS Contributors
      5 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
      6 #
      7 
      8 import os
      9 import inspect
     10 import re
     11 from typing import Optional
     12 
     13 def looks_like_tf_psa_crypto_root(path: str) -> bool:
     14     """Whether the given directory looks like the root of the PSA Crypto source tree."""
     15     try:
     16         with open(os.path.join(path, 'scripts', 'project_name.txt'), 'r') as f:
     17             return f.read() == "TF-PSA-Crypto\n"
     18     except FileNotFoundError:
     19         return False
     20 
     21 def looks_like_mbedtls_root(path: str) -> bool:
     22     """Whether the given directory looks like the root of the Mbed TLS source tree."""
     23     try:
     24         with open(os.path.join(path, 'scripts', 'project_name.txt'), 'r') as f:
     25             return f.read() == "Mbed TLS\n"
     26     except FileNotFoundError:
     27         return False
     28 
     29 def looks_like_root(path: str) -> bool:
     30     return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path)
     31 
     32 def crypto_core_directory(root: Optional[str] = None, relative: Optional[bool] = False) -> str:
     33     """
     34     Return the path of the directory containing the PSA crypto core
     35     for either TF-PSA-Crypto or Mbed TLS.
     36 
     37     Returns either the full path or relative path depending on the
     38     "relative" boolean argument.
     39     """
     40     if root is None:
     41         root = guess_project_root()
     42     if looks_like_tf_psa_crypto_root(root):
     43         if relative:
     44             return "core"
     45         return os.path.join(root, "core")
     46     elif looks_like_mbedtls_root(root):
     47         if is_mbedtls_3_6():
     48             path = "library"
     49         else:
     50             path = "tf-psa-crypto/core"
     51         if relative:
     52             return path
     53         return os.path.join(root, path)
     54     else:
     55         raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
     56 
     57 def crypto_library_filename(root: Optional[str] = None) -> str:
     58     """Return the crypto library filename for either TF-PSA-Crypto or Mbed TLS."""
     59     if root is None:
     60         root = guess_project_root()
     61     if looks_like_tf_psa_crypto_root(root):
     62         return "tfpsacrypto"
     63     elif looks_like_mbedtls_root(root):
     64         return "mbedcrypto"
     65     else:
     66         raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
     67 
     68 def check_repo_path():
     69     """Check that the current working directory is the project root, and throw
     70     an exception if not.
     71     """
     72     if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
     73         raise Exception("This script must be run from Mbed TLS root")
     74 
     75 def chdir_to_root() -> None:
     76     """Detect the root of the Mbed TLS or TF-PSA-Crypto source tree and change to it.
     77 
     78     The current directory must be up to two levels deep inside an Mbed TLS or
     79     TF-PSA-Crypto source tree.
     80     """
     81     for d in [os.path.curdir,
     82               os.path.pardir,
     83               os.path.join(os.path.pardir, os.path.pardir)]:
     84         if looks_like_root(d):
     85             os.chdir(d)
     86             return
     87     raise Exception('Mbed TLS or TF-PSA-Crypto source tree not found')
     88 
     89 def guess_project_root():
     90     """Guess project source code directory.
     91 
     92     Return the first possible project root directory.
     93     """
     94     dirs = set({})
     95     for frame in inspect.stack():
     96         path = os.path.dirname(frame.filename)
     97         for d in ['.', os.path.pardir] \
     98                  + [os.path.join(*([os.path.pardir]*i)) for i in range(2, 10)]:
     99             d = os.path.abspath(os.path.join(path, d))
    100             if d in dirs:
    101                 continue
    102             dirs.add(d)
    103             if looks_like_root(d):
    104                 return d
    105     raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found')
    106 
    107 def guess_mbedtls_root(root: Optional[str] = None) -> str:
    108     """Guess Mbed TLS source code directory.
    109 
    110     Return the first possible Mbed TLS root directory.
    111     Raise an exception if we are not in Mbed TLS.
    112     """
    113     if root is None:
    114         root = guess_project_root()
    115     if looks_like_mbedtls_root(root):
    116         return root
    117     else:
    118         raise Exception('Mbed TLS source tree not found')
    119 
    120 def guess_tf_psa_crypto_root(root: Optional[str] = None) -> str:
    121     """Guess TF-PSA-Crypto source code directory.
    122 
    123     Return the first possible TF-PSA-Crypto root directory.
    124     Raise an exception if we are not in TF-PSA-Crypto.
    125     """
    126     if root is None:
    127         root = guess_project_root()
    128     if looks_like_tf_psa_crypto_root(root):
    129         return root
    130     else:
    131         raise Exception('TF-PSA-Crypto source tree not found')
    132 
    133 def is_mbedtls_3_6() -> bool:
    134     """Whether the working tree is an Mbed TLS 3.6 one or not
    135 
    136     Return false if we are in TF-PSA-Crypto or in Mbed TLS but with a version
    137     different from 3.6.x.
    138     Raise an exception if we are neither in Mbed TLS nor in TF-PSA-Crypto.
    139     """
    140     root = guess_project_root()
    141     if not looks_like_mbedtls_root(root):
    142         return False
    143     with open(os.path.join(root, 'include', 'mbedtls', 'build_info.h'), 'r') as f:
    144         return re.search(r"#define MBEDTLS_VERSION_NUMBER.*0x0306", f.read()) is not None