1# Copyright 2018 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5toolchain("clang") { 6 c_command = "clang" 7 cpp_command = "clang++" 8 9 lib_switch = "-l" 10 lib_dir_switch = "-L" 11 12 tool("cc") { 13 depfile = "{{output}}.d" 14 command = "$c_command -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}" 15 depsformat = "gcc" 16 description = "CC {{output}}" 17 outputs = 18 [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] 19 } 20 21 tool("cxx") { 22 depfile = "{{output}}.d" 23 command = "$cpp_command -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}" 24 depsformat = "gcc" 25 description = "CXX {{output}}" 26 outputs = 27 [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] 28 } 29 30 tool("asm") { 31 depfile = "{{output}}.d" 32 command = "$c_command -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}" 33 depsformat = "gcc" 34 description = "ASM {{output}}" 35 outputs = 36 [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] 37 } 38 39 tool("objc") { 40 depfile = "{{output}}.d" 41 precompiled_header_type = "gcc" 42 command = "$c_command -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_objc}} -c {{source}} -o {{output}}" 43 depsformat = "gcc" 44 description = "OBJC {{output}}" 45 outputs = 46 [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] 47 } 48 49 tool("objcxx") { 50 depfile = "{{output}}.d" 51 precompiled_header_type = "gcc" 52 command = "$cpp_command -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_objcc}} -c {{source}} -o {{output}}" 53 depsformat = "gcc" 54 description = "OBJCXX {{output}}" 55 outputs = 56 [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] 57 } 58 59 tool("alink") { 60 # Note about -filelist: Apple's linker reads the file list file and 61 # interprets each newline-separated chunk of text as a file name. It 62 # doesn't do the things one would expect from the shell like unescaping 63 # or handling quotes. In contrast, when Ninja finds a file name with 64 # spaces, it single-quotes them in $inputs_newline as it would normally 65 # do for command-line arguments. Thus any source names with spaces, or 66 # label names with spaces (which GN bases the output paths on) will be 67 # corrupted by this process. Don't use spaces for source files or labels. 68 rspfile = "{{output}}.rsp" 69 rspfile_content = "{{inputs_newline}}" 70 command = "rm -f {{output}} && libtool -static {{arflags}} -o {{output}} -filelist $rspfile" 71 description = "LIBTOOL-STATIC {{output}}" 72 outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ] 73 default_output_dir = "{{target_out_dir}}" 74 default_output_extension = ".a" 75 output_prefix = "lib" 76 } 77 78 tool("solink") { 79 soname = "{{target_output_name}}{{output_extension}}" 80 dylib = "{{output_dir}}/$soname" # e.g. "libfoo.dylib" 81 rspfile = soname + ".rsp" 82 83 # These variables are not built into GN but are helpers that implement 84 # (1) linking to produce a .dylib, (2) extracting the symbols from that 85 # file to a temporary file, (3) if the temporary file has differences from 86 # the existing .TOC file, overwrite it, otherwise, don't change it. 87 # 88 # As a special case, if the library reexports symbols from other dynamic 89 # libraries, we always update the .TOC and skip the temporary file and 90 # diffing steps, since that library always needs to be re-linked. 91 tocname = dylib + ".TOC" 92 temporary_tocname = dylib + ".tmp" 93 94 does_reexport_command = "[ ! -e \"$dylib\" -o ! -e \"$tocname\" ] || otool -l \"$dylib\" | grep -q LC_REEXPORT_DYLIB" 95 link_command = "$cpp_command -shared -Wl,-install_name,@rpath/\"$soname\" {{ldflags}} -o \"$dylib\" -Wl,-filelist,\"$rspfile\" {{libs}} {{solibs}} {{frameworks}}" 96 replace_command = "if ! cmp -s \"$temporary_tocname\" \"$tocname\"; then mv \"$temporary_tocname\" \"$tocname\"" 97 extract_toc_command = "{ otool -l \"$dylib\" | grep LC_ID_DYLIB -A 5; nm -gP \"$dylib\" | cut -f1-2 -d' ' | grep -v U\$\$; true; }" 98 command = "if $does_reexport_command ; then $link_command && $extract_toc_command > \"$tocname\"; else $link_command && $extract_toc_command > \"$temporary_tocname\" && $replace_command; fi; fi" 99 100 # See comment in tool("alink") about content of the rspfile. 101 rspfile_content = "{{inputs_newline}}" 102 103 description = "DYLIB {{output}}" 104 105 # Use this for {{output_extension}} expansions unless a target manually 106 # overrides it (in which case {{output_extension}} will be what the target 107 # specifies). 108 default_output_extension = ".dylib" 109 110 # Use this for {{output_dir}} expansions unless a target manually overrides 111 # it (in which case {{output_dir}} will be what the target specifies). 112 default_output_dir = "{{root_out_dir}}" 113 114 # Link to the dylib but have GN use the tocname for dependency management. 115 # Tell GN about the output files. It will link to the dylib but use the 116 # tocname for dependency management. 117 outputs = [ 118 dylib, 119 tocname, 120 ] 121 link_output = dylib 122 depend_output = tocname 123 output_prefix = "lib" 124 125 # Since the above commands only updates the .TOC file when it changes, ask 126 # Ninja to check if the timestamp actually changed to know if downstream 127 # dependencies should be recompiled. 128 restat = true 129 } 130 131 tool("link") { 132 outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" 133 rspfile = "$outfile.rsp" 134 135 # These extra ldflags allow an executable to search for shared libraries in 136 # the current working directory. 137 additional_executable_ldflags = "-Wl,-rpath,@loader_path/." 138 command = "$cpp_command {{ldflags}} $additional_executable_ldflags -o \"$outfile\" -Wl,-filelist,\"$rspfile\" {{solibs}} {{libs}} {{frameworks}}" 139 description = "LINK $outfile" 140 default_output_dir = "{{root_out_dir}}" 141 rspfile_content = "{{inputs_newline}}" 142 outputs = [ outfile ] 143 } 144 145 tool("stamp") { 146 command = "touch {{output}}" 147 description = "STAMP {{output}}" 148 } 149 150 tool("copy") { 151 command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})" 152 description = "COPY {{source}} {{output}}" 153 } 154} 155