# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

import os


def find_tool(tool_name: str) -> str:
    '''Return a string describing how to invoke the given RISC-V tool

    Try to resolve the tool in the following way, stopping after the first
    match:

    1. Use the path set in the RV32_TOOL_<tool_name> environment variable.
    2. Use the path set in $TOOLCHAIN_PATH/bin/riscv32-unknown-elf-<tool_name>.
    3. Use riscv32-unknown-elf-<tool_name> and expect it to be in PATH.

    In cases (1) and (2), the file path is checked if it exists, and if not, an
    error message is printed.
    '''
    tool_env_var = 'RV32_TOOL_' + tool_name.upper()
    configured_tool_path = os.environ.get(tool_env_var)
    if configured_tool_path is not None:
        if not os.path.exists(configured_tool_path):
            raise RuntimeError('No such file: {!r} (derived from the '
                               '{!r} environment variable when trying '
                               'to find the {!r} tool).'.format(
                                   configured_tool_path, tool_env_var,
                                   tool_name))
        return configured_tool_path

    expanded = 'riscv32-unknown-elf-' + tool_name
    toolchain_path = os.environ.get('TOOLCHAIN_PATH')
    if toolchain_path is None:
        return expanded

    tool_path = os.path.join(toolchain_path, 'bin', expanded)
    if not os.path.exists(tool_path):
        raise RuntimeError('No such file: {!r} (derived from the '
                           'TOOLCHAIN_PATH environment variable when trying '
                           'to find the {!r} tool).'
                           .format(tool_path, tool_name))

    return tool_path

@@ -511,19 +511,24 @@
 %0A    3. 
-Use
+Look for
  riscv32
@@ -556,49 +556,116 @@
 me%3E 
-and expect it to be in PATH.%0A%0A    In case
+in the system PATH.%0A    4. Look in the default toolchain install location, /tools/riscv/bin.%0A%0A    For method
 s (1
@@ -678,91 +678,201 @@
 (2),
+ if
  the 
-file path is checked if it exists, and if not, an%0A    error message is printed
+expected environment variable is set but%0A    the tool isn't found, an error is printed. An error is also printed if%0A    neither environment variables is set and methods (3) and (4) fail
 .%0A  
@@ -1571,16 +1571,20 @@
 path is 
+not 
 None:%0A  
@@ -1589,29 +1589,8 @@
     
-    return expanded%0A%0A
     
@@ -1647,16 +1647,20 @@
 panded)%0A
+    
     if n
@@ -1689,32 +1689,36 @@
 _path):%0A        
+    
 raise RuntimeErr
@@ -1787,16 +1787,20 @@
         
+    
 'TOOLCHA
@@ -1861,32 +1861,36 @@
                 
+    
 'to find the %7B!r
@@ -1899,16 +1899,20 @@
 tool).'%0A
+    
         
@@ -1965,26 +1965,608 @@
 e))%0A
-%0A    return tool_path
+        return tool_path%0A%0A    default_location = '/tools/riscv/bin'%0A    paths = os.get_exec_path() + %5Bdefault_location%5D%0A    for exec_path in paths:%0A        tool_path = os.path.join(exec_path, expanded)%0A        if os.path.exists(tool_path):%0A            return tool_path%0A%0A    raise RuntimeError('Unable to find %7B!r%7D in PATH or in %7B!r%7D. Set the %7B!r%7D '%0A                       'or TOOLCHAIN_PATH environment variable if you '%0A                       'installed your RISC-V toolchain in an alternate '%0A                       'location.'%0A                       .format(expanded, default_location, tool_env_var))
 %0A

Update RISC-V toolchain search to look in default location.