# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

toolchain("clang") {
  c_command = "clang"
  cpp_command = "clang++"

  lib_switch = "-l"
  lib_dir_switch = "-L"

  tool("cc") {
    depfile = "{{output}}.d"
    command = "$c_command -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
    depsformat = "gcc"
    description = "CC {{output}}"
    outputs =
        [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
  }

  tool("cxx") {
    depfile = "{{output}}.d"
    command = "$cpp_command -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
    depsformat = "gcc"
    description = "CXX {{output}}"
    outputs =
        [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
  }

  tool("asm") {
    depfile = "{{output}}.d"
    command = "$c_command -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
    depsformat = "gcc"
    description = "ASM {{output}}"
    outputs =
        [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
  }

  tool("objc") {
    depfile = "{{output}}.d"
    precompiled_header_type = "gcc"
    command = "$c_command -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_objc}} -c {{source}} -o {{output}}"
    depsformat = "gcc"
    description = "OBJC {{output}}"
    outputs =
        [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
  }

  tool("objcxx") {
    depfile = "{{output}}.d"
    precompiled_header_type = "gcc"
    command = "$cpp_command -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_objcc}} -c {{source}} -o {{output}}"
    depsformat = "gcc"
    description = "OBJCXX {{output}}"
    outputs =
        [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
  }

  tool("alink") {
    # Note about -filelist: Apple's linker reads the file list file and
    # interprets each newline-separated chunk of text as a file name. It
    # doesn't do the things one would expect from the shell like unescaping
    # or handling quotes. In contrast, when Ninja finds a file name with
    # spaces, it single-quotes them in $inputs_newline as it would normally
    # do for command-line arguments. Thus any source names with spaces, or
    # label names with spaces (which GN bases the output paths on) will be
    # corrupted by this process. Don't use spaces for source files or labels.
    rspfile = "{{output}}.rsp"
    rspfile_content = "{{inputs_newline}}"
    command = "rm -f {{output}} && libtool -static {{arflags}} -o {{output}} -filelist $rspfile"
    description = "LIBTOOL-STATIC {{output}}"
    outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]
    default_output_dir = "{{target_out_dir}}"
    default_output_extension = ".a"
    output_prefix = "lib"
  }

  tool("solink") {
    soname = "{{target_output_name}}{{output_extension}}"
    dylib = "{{output_dir}}/$soname"  # e.g. "libfoo.dylib"
    rspfile = soname + ".rsp"

    # These variables are not built into GN but are helpers that implement
    # (1) linking to produce a .dylib, (2) extracting the symbols from that
    # file to a temporary file, (3) if the temporary file has differences from
    # the existing .TOC file, overwrite it, otherwise, don't change it.
    #
    # As a special case, if the library reexports symbols from other dynamic
    # libraries, we always update the .TOC and skip the temporary file and
    # diffing steps, since that library always needs to be re-linked.
    tocname = dylib + ".TOC"
    temporary_tocname = dylib + ".tmp"

    does_reexport_command = "[ ! -e \"$dylib\" -o ! -e \"$tocname\" ] || otool -l \"$dylib\" | grep -q LC_REEXPORT_DYLIB"
    link_command = "$cpp_command -shared -Wl,-install_name,@rpath/\"$soname\" {{ldflags}} -o \"$dylib\" -Wl,-filelist,\"$rspfile\" {{libs}} {{solibs}} {{frameworks}}"
    replace_command = "if ! cmp -s \"$temporary_tocname\" \"$tocname\"; then mv \"$temporary_tocname\" \"$tocname\""
    extract_toc_command = "{ otool -l \"$dylib\" | grep LC_ID_DYLIB -A 5; nm -gP \"$dylib\" | cut -f1-2 -d' ' | grep -v U\$\$; true; }"
    command = "if $does_reexport_command ; then $link_command && $extract_toc_command > \"$tocname\"; else $link_command && $extract_toc_command > \"$temporary_tocname\" && $replace_command; fi; fi"

    # See comment in tool("alink") about content of the rspfile.
    rspfile_content = "{{inputs_newline}}"

    description = "DYLIB {{output}}"

    # Use this for {{output_extension}} expansions unless a target manually
    # overrides it (in which case {{output_extension}} will be what the target
    # specifies).
    default_output_extension = ".dylib"

    # Use this for {{output_dir}} expansions unless a target manually overrides
    # it (in which case {{output_dir}} will be what the target specifies).
    default_output_dir = "{{root_out_dir}}"

    # Link to the dylib but have GN use the tocname for dependency management.
    # Tell GN about the output files. It will link to the dylib but use the
    # tocname for dependency management.
    outputs = [
      dylib,
      tocname,
    ]
    link_output = dylib
    depend_output = tocname
    output_prefix = "lib"

    # Since the above commands only updates the .TOC file when it changes, ask
    # Ninja to check if the timestamp actually changed to know if downstream
    # dependencies should be recompiled.
    restat = true
  }

  tool("link") {
    outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
    rspfile = "$outfile.rsp"

    # These extra ldflags allow an executable to search for shared libraries in
    # the current working directory.
    additional_executable_ldflags = "-Wl,-rpath,@loader_path/."
    command = "$cpp_command {{ldflags}} $additional_executable_ldflags -o \"$outfile\" -Wl,-filelist,\"$rspfile\" {{solibs}} {{libs}} {{frameworks}}"
    description = "LINK $outfile"
    default_output_dir = "{{root_out_dir}}"
    rspfile_content = "{{inputs_newline}}"
    outputs = [ outfile ]
  }

  tool("stamp") {
    command = "touch {{output}}"
    description = "STAMP {{output}}"
  }

  tool("copy") {
    command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
    description = "COPY {{source}} {{output}}"
  }
}
