1# Copyright (c) 2013 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 5import("//build/config/clang/clang.gni") 6import("//build/config/mac/mac_sdk.gni") 7import("//build/config/mac/symbols.gni") 8 9assert(host_os == "mac") 10 11import("//build/toolchain/cc_wrapper.gni") 12import("//build/toolchain/clang_static_analyzer.gni") 13import("//build/toolchain/toolchain.gni") 14 15# When implementing tools using Python scripts, a TOOL_VERSION=N env 16# variable is placed in front of the command. The N should be incremented 17# whenever the script is changed, so that the build system rebuilds all 18# edges that utilize the script. Ideally this should be changed to use 19# proper input-dirty checking, but that could be expensive. Instead, use a 20# script to get the tool scripts' modification time to use as the version. 21# This won't cause a re-generation of GN files when the tool script changes 22# but it will cause edges to be marked as dirty if the ninja files are 23# regenerated. See https://crbug.com/619083 for details. A proper fix 24# would be to have inputs to tools (https://crbug.com/621119). 25tool_versions = 26 exec_script("get_tool_mtime.py", 27 rebase_path([ 28 "//build/toolchain/mac/filter_libtool.py", 29 "//build/toolchain/mac/linker_driver.py", 30 ], 31 root_build_dir), 32 "trim scope") 33 34# Shared toolchain definition. Invocations should set current_os to set the 35# build args in this definition. 36template("mac_toolchain") { 37 toolchain(target_name) { 38 if (use_system_xcode) { 39 env_wrapper = "" 40 } else { 41 env_wrapper = "export DEVELOPER_DIR=$hermetic_xcode_path; " 42 } 43 44 # When invoking this toolchain not as the default one, these args will be 45 # passed to the build. They are ignored when this is the default toolchain. 46 assert(defined(invoker.toolchain_args), 47 "Toolchains must declare toolchain_args") 48 toolchain_args = { 49 # Populate toolchain args from the invoker. 50 forward_variables_from(invoker.toolchain_args, "*") 51 52 # The host toolchain value computed by the default toolchain's setup 53 # needs to be passed through unchanged to all secondary toolchains to 54 # ensure that it's always the same, regardless of the values that may be 55 # set on those toolchains. 56 host_toolchain = host_toolchain 57 } 58 59 # Supports building with the version of clang shipped with Xcode when 60 # targeting iOS by not respecting clang_base_path. 61 if (toolchain_args.current_os == "ios" && use_xcode_clang) { 62 prefix = "" 63 } else { 64 prefix = rebase_path("$clang_base_path/bin/", root_build_dir) 65 } 66 67 _cc = "${prefix}clang" 68 _cxx = "${prefix}clang++" 69 _ar = "${prefix}llvm-ar" 70 71 # When the invoker has explicitly overridden use_goma or cc_wrapper in the 72 # toolchain args, use those values, otherwise default to the global one. 73 # This works because the only reasonable override that toolchains might 74 # supply for these values are to force-disable them. 75 if (defined(toolchain_args.cc_wrapper)) { 76 toolchain_cc_wrapper = toolchain_args.cc_wrapper 77 } else { 78 toolchain_cc_wrapper = cc_wrapper 79 } 80 81 # Compute the compiler prefix. 82 if (toolchain_cc_wrapper != "") { 83 compiler_prefix = toolchain_cc_wrapper + " " 84 } else { 85 compiler_prefix = "" 86 } 87 88 cc = compiler_prefix + _cc 89 cxx = compiler_prefix + _cxx 90 ld = _cxx 91 92 if (use_clang_static_analyzer) { 93 analyzer_wrapper = 94 rebase_path("//build/toolchain/clang_static_analyzer_wrapper.py", 95 root_build_dir) + " --mode=clang" 96 cc = analyzer_wrapper + " ${cc}" 97 cxx = analyzer_wrapper + " ${cxx}" 98 ld = cxx 99 } 100 101 linker_driver = 102 "TOOL_VERSION=${tool_versions.linker_driver} " + 103 rebase_path("//build/toolchain/mac/linker_driver.py", root_build_dir) 104 105 # On iOS, the final applications are assembled using lipo (to support fat 106 # builds). The correct flags are passed to the linker_driver.py script 107 # directly during the lipo call. 108 _save_unstripped_output = false 109 110 # Make these apply to all tools below. 111 lib_switch = "-l" 112 lib_dir_switch = "-L" 113 114 # Object files go in this directory. Use label_name instead of 115 # target_output_name since labels will generally have no spaces and will be 116 # unique in the directory. 117 object_subdir = "{{source_out_dir}}/{{label_name}}" 118 119 if (_save_unstripped_output) { 120 _unstripped_output = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.unstripped" 121 } 122 123 tool("cc") { 124 depfile = "{{output}}.d" 125 precompiled_header_type = "gcc" 126 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}" 127 depsformat = "gcc" 128 description = "CC {{output}}" 129 outputs = [ "$object_subdir/{{source_name_part}}.o" ] 130 } 131 132 tool("cxx") { 133 depfile = "{{output}}.d" 134 precompiled_header_type = "gcc" 135 command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}" 136 depsformat = "gcc" 137 description = "CXX {{output}}" 138 outputs = [ "$object_subdir/{{source_name_part}}.o" ] 139 } 140 141 tool("asm") { 142 # For GCC we can just use the C compiler to compile assembly. 143 depfile = "{{output}}.d" 144 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}" 145 depsformat = "gcc" 146 description = "ASM {{output}}" 147 outputs = [ "$object_subdir/{{source_name_part}}.o" ] 148 } 149 150 tool("objc") { 151 depfile = "{{output}}.d" 152 precompiled_header_type = "gcc" 153 command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{framework_dirs}} {{cflags}} {{cflags_objc}} -c {{source}} -o {{output}}" 154 depsformat = "gcc" 155 description = "OBJC {{output}}" 156 outputs = [ "$object_subdir/{{source_name_part}}.o" ] 157 } 158 159 tool("objcxx") { 160 depfile = "{{output}}.d" 161 precompiled_header_type = "gcc" 162 command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{framework_dirs}} {{cflags}} {{cflags_objcc}} -c {{source}} -o {{output}}" 163 depsformat = "gcc" 164 description = "OBJCXX {{output}}" 165 outputs = [ "$object_subdir/{{source_name_part}}.o" ] 166 } 167 168 tool("alink") { 169 rspfile = "{{output}}.rsp" 170 rspfile_content = "{{inputs}}" 171 command = "$_ar {{arflags}} -r -c -s -D {{output}} \"@$rspfile\"" 172 command = "rm -f {{output}} && $command" 173 description = "AR {{output}}" 174 outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ] 175 default_output_dir = "{{target_out_dir}}" 176 default_output_extension = ".a" 177 output_prefix = "lib" 178 } 179 180 tool("solink") { 181 dylib = "{{output_dir}}/{{target_output_name}}{{output_extension}}" # eg 182 # "./libfoo.dylib" 183 rspfile = dylib + ".rsp" 184 pool = "//build/toolchain:link_pool($default_toolchain)" 185 186 # These variables are not built into GN but are helpers that implement 187 # (1) linking to produce a .dylib, (2) extracting the symbols from that 188 # file to a temporary file, (3) if the temporary file has differences from 189 # the existing .TOC file, overwrite it, otherwise, don't change it. 190 # 191 # As a special case, if the library reexports symbols from other dynamic 192 # libraries, we always update the .TOC and skip the temporary file and 193 # diffing steps, since that library always needs to be re-linked. 194 tocname = dylib + ".TOC" 195 temporary_tocname = dylib + ".tmp" 196 197 does_reexport_command = "[ ! -e \"$dylib\" -o ! -e \"$tocname\" ] || otool -l \"$dylib\" | grep -q LC_REEXPORT_DYLIB" 198 199 link_command = "$linker_driver $ld -shared " 200 if (is_component_build) { 201 link_command += " -Wl,-install_name,@rpath/\"{{target_output_name}}{{output_extension}}\" " 202 } 203 link_command += "{{ldflags}} -o \"$dylib\" -Wl,-filelist,\"$rspfile\" {{libs}} {{solibs}} {{frameworks}}" 204 205 replace_command = "if ! cmp -s \"$temporary_tocname\" \"$tocname\"; then mv \"$temporary_tocname\" \"$tocname\"" 206 extract_toc_command = "{ otool -l \"$dylib\" | grep LC_ID_DYLIB -A 5; nm -gP \"$dylib\" | cut -f1-2 -d' ' | grep -v U\$\$; true; }" 207 208 command = "$env_wrapper if $does_reexport_command ; then $link_command && $extract_toc_command > \"$tocname\"; else $link_command && $extract_toc_command > \"$temporary_tocname\" && $replace_command ; fi; fi" 209 210 rspfile_content = "{{inputs_newline}}" 211 212 description = "SOLINK {{output}}" 213 214 # Use this for {{output_extension}} expansions unless a target manually 215 # overrides it (in which case {{output_extension}} will be what the target 216 # specifies). 217 default_output_dir = "{{root_out_dir}}" 218 default_output_extension = ".dylib" 219 220 output_prefix = "lib" 221 222 # Since the above commands only updates the .TOC file when it changes, ask 223 # Ninja to check if the timestamp actually changed to know if downstream 224 # dependencies should be recompiled. 225 restat = true 226 227 # Tell GN about the output files. It will link to the dylib but use the 228 # tocname for dependency management. 229 outputs = [ 230 dylib, 231 tocname, 232 ] 233 link_output = dylib 234 depend_output = tocname 235 236 if (_save_unstripped_output) { 237 outputs += [ _unstripped_output ] 238 } 239 } 240 241 tool("solink_module") { 242 sofile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" # eg 243 # "./libfoo.so" 244 rspfile = sofile + ".rsp" 245 pool = "//build/toolchain:link_pool($default_toolchain)" 246 247 link_command = "$env_wrapper $linker_driver $ld -bundle {{ldflags}} -o \"$sofile\" -Wl,-filelist,\"$rspfile\"" 248 if (is_component_build) { 249 link_command += " -Wl,-install_name,@rpath/{{target_output_name}}{{output_extension}}" 250 } 251 link_command += " {{solibs}} {{libs}}" 252 command = link_command 253 254 rspfile_content = "{{inputs_newline}}" 255 256 description = "SOLINK_MODULE {{output}}" 257 258 # Use this for {{output_extension}} expansions unless a target manually 259 # overrides it (in which case {{output_extension}} will be what the target 260 # specifies). 261 default_output_dir = "{{root_out_dir}}" 262 default_output_extension = ".so" 263 264 outputs = [ sofile ] 265 266 if (_save_unstripped_output) { 267 outputs += [ _unstripped_output ] 268 } 269 } 270 271 tool("link") { 272 outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" 273 rspfile = "$outfile.rsp" 274 pool = "//build/toolchain:link_pool($default_toolchain)" 275 276 # Note about -filelist: Apple's linker reads the file list file and 277 # interprets each newline-separated chunk of text as a file name. It 278 # doesn't do the things one would expect from the shell like unescaping 279 # or handling quotes. In contrast, when Ninja finds a file name with 280 # spaces, it single-quotes them in $inputs_newline as it would normally 281 # do for command-line arguments. Thus any source names with spaces, or 282 # label names with spaces (which GN bases the output paths on) will be 283 # corrupted by this process. Don't use spaces for source files or labels. 284 command = "$env_wrapper $linker_driver $ld {{ldflags}} -o \"$outfile\" -Wl,-filelist,\"$rspfile\" {{solibs}} {{libs}} {{frameworks}}" 285 description = "LINK $outfile" 286 rspfile_content = "{{inputs_newline}}" 287 outputs = [ outfile ] 288 289 if (_save_unstripped_output) { 290 outputs += [ _unstripped_output ] 291 } 292 293 default_output_dir = "{{root_out_dir}}" 294 } 295 296 # These two are really entirely generic, but have to be repeated in 297 # each toolchain because GN doesn't allow a template to be used here. 298 # See //build/toolchain/toolchain.gni for details. 299 tool("stamp") { 300 command = stamp_command 301 description = stamp_description 302 } 303 tool("copy") { 304 command = copy_command 305 description = copy_description 306 } 307 308 tool("action") { 309 pool = "//build/toolchain:action_pool($default_toolchain)" 310 } 311 } 312} 313 314mac_toolchain("clang_x64") { 315 toolchain_args = { 316 current_cpu = "x64" 317 current_os = "mac" 318 } 319} 320 321mac_toolchain("clang_x86") { 322 toolchain_args = { 323 current_cpu = "x86" 324 current_os = "mac" 325 } 326} 327 328mac_toolchain("clang_arm64") { 329 toolchain_args = { 330 current_cpu = "arm64" 331 current_os = "mac" 332 } 333} 334