#!/usr/bin/env python3

"""
 *
 * This file is part of AdaptiveCpp, an implementation of SYCL and C++ standard
 * parallelism for CPUs and GPUs.
 *
 * Copyright The AdaptiveCpp Contributors
 *
 * AdaptiveCpp is released under the BSD 2-Clause "Simplified" License.
 * See file LICENSE in the project root for full license details.
 *
 SPDX-License-Identifier: BSD-2-Clause
 """

import subprocess
import sys
import os

CXX_COMPILER_ARG = '--launcher-cxx-compiler='
SYCLCC_ARG = '--launcher-syclcc='

if __name__ == '__main__':
  syclcc_specific_args = []
  command_offset = None
  cxx_compiler_exe = None
  syclcc_exe = None
  for command_offset, arg in enumerate(sys.argv[1:], 1):
    if arg.startswith(CXX_COMPILER_ARG):
      cxx_compiler_exe = arg[len(CXX_COMPILER_ARG):]
    elif arg.startswith(SYCLCC_ARG):
      # Split by non-path char, as on Windows it is required to run python path/to/syclcc,
      # instead of just syclcc. As we need "python" and "syclcc" two different array elements
      # in the config, a '*' is used to seperate those two elements.
      syclcc_exe = arg[len(SYCLCC_ARG):].split("*")
    elif arg == '--':
      command_offset += 1
      break
    elif arg.startswith('-'):
      syclcc_specific_args.append(arg)
    else:
      break

  if cxx_compiler_exe is None or syclcc_exe is None or command_offset is None or command_offset + 1 >= len(sys.argv) \
      or '--help' in syclcc_specific_args:
    print('Usage: {} {}<path> {}<path> [syclcc-specific-args...] '
          'command [command-args...]'.format(sys.argv[0], CXX_COMPILER_ARG, SYCLCC_ARG), file=sys.stderr)
    sys.exit(1)

  # If this is a compilation step, attempt to find the expected compiler (e.g. clang++).
  # This may not be the first argument, in case additional CMAKE_CXX_COMPILER_LAUNCHERs are set.
  compiler_offset = command_offset
  while compiler_offset < len(sys.argv) and (
      not os.path.isfile(sys.argv[compiler_offset]) or
      not os.path.samefile(cxx_compiler_exe, sys.argv[compiler_offset])):
    compiler_offset += 1
  is_compilation_step = compiler_offset < len(sys.argv)

  # When invoked with a command line for expected compiler, replace with a syclcc invocation.
  if is_compilation_step:
    launcher_commands = sys.argv[command_offset:compiler_offset]
    compiler_args = sys.argv[compiler_offset + 1:]
    command_line = [*launcher_commands, *syclcc_exe, *syclcc_specific_args, *compiler_args]
  # Otherwise, e.g. for invocations of `ar` for linking static libraries, just continue with the command as-is.
  else:
    command_line = sys.argv[command_offset:]

  sys.exit(subprocess.run(command_line).returncode)
