| # Copyright 2016 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import("//build/toolchain/ccache.gni") |
| import("//build/toolchain/goma.gni") |
| import("//build/toolchain/variant.gni") |
| |
| declare_args() { |
| # The default clang toolchain provided by the buildtools. This variable is |
| # additionally consumed by the Go toolchain. |
| clang_prefix = |
| rebase_path("//buildtools/${host_platform}/clang/bin", root_build_dir) |
| } |
| |
| if (current_cpu == "arm64") { |
| clang_cpu = "aarch64" |
| } else if (current_cpu == "x64") { |
| clang_cpu = "x86_64" |
| } |
| if (defined(clang_cpu)) { |
| clang_target = "${clang_cpu}-${current_os}" |
| } else { |
| assert(current_cpu != target_cpu, "Unsupported architecture") |
| } |
| |
| # Defines a Clang-based toolchain. |
| # |
| # Parameters |
| # |
| # toolchain_cpu |
| # Required: Value of "current_cpu" inside this toolchain. |
| # |
| # toolchain_os |
| # Required: Value of "current_os" inside this toolchain. |
| # |
| # toolchain_args |
| # Optional: Scope specifying build arguments to override in this |
| # toolchain context. |
| # NOTE! Do not use this in the toolchain used as the default toolchain. |
| # It will have no effect, and you will be confused. |
| # |
| # use_strip |
| # Required: Whether to strip binaries, leaving unstripped ones |
| # in lib.unstripped and exe.unstripped subdirectories. |
| # |
| # prefix |
| # Optional: The path from which "cc", "cxx", "ar", "ld" and |
| # "strip" will be found (unless overridden). If not given, |
| # prefix defaults to $clang_prefix. |
| # |
| # cc |
| # cxx |
| # ar |
| # ld |
| # strip |
| # Optional: Override default tool names. |
| # |
| # use_ccache, use_goma |
| # Optional: Override the global setting, useful to opt out of |
| # ccache/goma in a particular toolchain. |
| # |
| # deps |
| # Optional: Dependencies of this toolchain. |
| # |
| template("clang_toolchain") { |
| toolchain(target_name) { |
| assert(defined(invoker.toolchain_cpu), |
| "clang_toolchain() must specify a \"toolchain_cpu\"") |
| assert(defined(invoker.toolchain_os), |
| "clang_toolchain() must specify a \"toolchain_os\"") |
| |
| compiler_prefix = "" |
| if (defined(invoker.use_goma)) { |
| use_goma = invoker.use_goma |
| } |
| if (use_goma) { |
| compiler_prefix = "$goma_dir/gomacc " |
| } else { |
| if (defined(invoker.use_ccache)) { |
| use_ccache = invoker.use_ccache |
| } |
| if (use_ccache) { |
| compiler_prefix = "ccache " |
| } |
| } |
| |
| prefix = clang_prefix |
| if (defined(invoker.prefix)) { |
| prefix = invoker.prefix |
| } |
| |
| # Set all tools to defaults |
| cc = compiler_prefix + "${prefix}/clang" |
| cxx = compiler_prefix + "${prefix}/clang++" |
| ar = "${prefix}/llvm-ar" |
| ld = cxx |
| |
| forward_variables_from(invoker, [ "use_strip" ]) |
| if (use_strip) { |
| clang_strip = "${prefix}/strip" |
| strip = clang_strip |
| } |
| |
| # Override any tools as requested by the invoker |
| if (defined(invoker.cc)) { |
| cc = compiler_prefix + invoker.cc |
| } |
| if (defined(invoker.cxx)) { |
| cxx = compiler_prefix + invoker.cxx |
| } |
| forward_variables_from(invoker, |
| [ |
| "ar", |
| "deps", |
| "ld", |
| "strip", |
| ]) |
| |
| # These library switches can apply to all tools below. |
| lib_switch = "-l" |
| lib_dir_switch = "-L" |
| |
| tool("cc") { |
| depfile = "{{output}}.d" |
| command = "$cc -MD -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 = "$cxx -MD -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 = "$cc {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}" |
| description = "ASM {{output}}" |
| outputs = [ |
| "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", |
| ] |
| } |
| |
| tool("objc") { |
| depfile = "{{output}}.d" |
| command = "$cc -MD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} {{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" |
| command = "$cxx -MD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} {{cflags_objcc}} -c {{source}} -o {{output}}" |
| depsformat = "gcc" |
| description = "OBJCXX {{output}}" |
| outputs = [ |
| "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o", |
| ] |
| } |
| |
| tool("alink") { |
| rspfile = "{{output}}.rsp" |
| command = |
| "rm -f {{output}} && $ar {{arflags}} rcsD {{output}} @\"$rspfile\"" |
| description = "AR {{output}}" |
| rspfile_content = "{{inputs}}" |
| outputs = [ |
| "{{output_dir}}/{{target_output_name}}{{output_extension}}", |
| ] |
| default_output_dir = "{{target_out_dir}}" |
| default_output_extension = ".a" |
| output_prefix = "lib" |
| } |
| |
| tool("solink") { |
| outname = "{{target_output_name}}{{output_extension}}" |
| outfile = "{{output_dir}}/$outname" |
| rspfile = "$outfile.rsp" |
| unstripped_outfile = outfile |
| if (use_strip) { |
| unstripped_outfile = "{{root_out_dir}}/lib.unstripped/{{target_output_name}}{{output_extension}}" |
| } |
| if (invoker.toolchain_os == "mac") { |
| command = "$ld -shared {{ldflags}} -Wl,-install_name,@rpath/\"{{target_output_name}}{{output_extension}}\" -o \"$unstripped_outfile\" -Wl,-filelist,\"$rspfile\" {{libs}} {{solibs}}" |
| rspfile_content = "{{inputs_newline}}" |
| default_output_extension = ".dylib" |
| } else { |
| command = "$ld -shared {{ldflags}} -o \"$unstripped_outfile\" -Wl,-soname=\"$outname\" @\"$rspfile\"" |
| rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}" |
| default_output_extension = ".so" |
| } |
| if (use_strip) { |
| command += " && $strip -s -o \"$outfile\" \"$unstripped_outfile\"" |
| } |
| description = "SOLINK $outfile" |
| default_output_dir = "{{root_out_dir}}" |
| output_prefix = "lib" |
| outputs = [ |
| outfile, |
| ] |
| if (outfile != unstripped_outfile) { |
| outputs += [ unstripped_outfile ] |
| } |
| } |
| |
| tool("solink_module") { |
| outname = "{{target_output_name}}{{output_extension}}" |
| outfile = "{{output_dir}}/$outname" |
| rspfile = "$outfile.rsp" |
| if (use_strip) { |
| unstripped_outfile = "{{root_out_dir}}/lib.unstripped/{{target_output_name}}{{output_extension}}" |
| } else { |
| unstripped_outfile = outfile |
| } |
| if (invoker.toolchain_os == "mac") { |
| command = "$ld -shared {{ldflags}} -o \"$unstripped_outfile\" -Wl,-filelist,\"$rspfile\" {{libs}} {{solibs}}" |
| rspfile_content = "{{inputs_newline}}" |
| default_output_extension = ".dylib" |
| } else { |
| command = "$ld -shared {{ldflags}} -o \"$unstripped_outfile\" -Wl,-soname=\"$outname\" @\"$rspfile\"" |
| rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}" |
| default_output_extension = ".so" |
| } |
| if (use_strip) { |
| command += " && $strip -s -o \"$outfile\" \"$unstripped_outfile\"" |
| } |
| description = "SOLINK $outfile" |
| default_output_dir = "{{root_out_dir}}" |
| outputs = [ |
| outfile, |
| ] |
| if (outfile != unstripped_outfile) { |
| outputs += [ unstripped_outfile ] |
| } |
| } |
| |
| tool("link") { |
| outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" |
| rspfile = "$outfile.rsp" |
| unstripped_outfile = outfile |
| if (use_strip) { |
| unstripped_outfile = "{{root_out_dir}}/exe.unstripped/{{target_output_name}}{{output_extension}}" |
| } |
| if (invoker.toolchain_os == "mac") { |
| command = "$ld {{ldflags}} -o \"$unstripped_outfile\" -Wl,-filelist,\"$rspfile\" {{solibs}} {{libs}}" |
| rspfile_content = "{{inputs_newline}}" |
| } else { |
| command = "$ld {{ldflags}} -o \"$unstripped_outfile\" -Wl,--start-group @\"$rspfile\" {{solibs}} -Wl,--end-group {{libs}}" |
| rspfile_content = "{{inputs}}" |
| } |
| if (use_strip) { |
| command += " && $strip -s -o \"$outfile\" \"$unstripped_outfile\"" |
| } |
| description = "LINK $outfile" |
| default_output_dir = "{{root_out_dir}}" |
| outputs = [ |
| outfile, |
| ] |
| if (outfile != unstripped_outfile) { |
| outputs += [ unstripped_outfile ] |
| } |
| } |
| |
| tool("stamp") { |
| command = "touch {{output}}" |
| description = "STAMP {{output}}" |
| } |
| |
| tool("copy") { |
| # We use link instead of copy; the way "copy" tool is being used is |
| # compatible with links since Ninja is tracking changes to the source. |
| command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})" |
| description = "COPY {{source}} {{output}}" |
| } |
| |
| # When invoking this toolchain not as the default one, these args will be |
| # passed to the build. They are ignored when this is the default toolchain. |
| toolchain_args = { |
| current_cpu = invoker.toolchain_cpu |
| current_os = invoker.toolchain_os |
| |
| # These values need to be passed through unchanged. |
| target_os = target_os |
| target_cpu = target_cpu |
| |
| if (defined(invoker.toolchain_args)) { |
| # The invoker isn't allowed to fiddle with the essential settings. |
| forward_variables_from(invoker.toolchain_args, |
| "*", |
| [ |
| "current_cpu", |
| "current_os", |
| "target_os", |
| "target_cpu", |
| ]) |
| } |
| } |
| } |
| } |
| |
| # Defines a standard suite of Clang-based toolchains. |
| # |
| # Parameters |
| # |
| # variants: |
| # Optional: Pre-set with the default list of supported variants from |
| # the `known_variants` and `extra_variants` build arguments, which see. |
| # The invoker scope can add remove to the list or remove variants from |
| # the list to control the set of variants available for this suite of |
| # toolchains. Each variant (element of this list) is a list of configs. |
| # |
| # with_shared: |
| # Optional: If this toolchain will have a "-shared" companion. |
| # (Each variant toolchain has its own "-shared" companion.) |
| # Pre-set to true. Set this to false if either no shared libraries |
| # are used in this toolchain, or it's used to build nothing but |
| # shared libraries anyway. |
| # |
| # See clang_toolchain for others. Most parameters are just forwarded there. |
| # |
| template("clang_toolchain_suite") { |
| # Exclude the arguments that are for us or are verboten. |
| invoker_clang_toolchain_args = { |
| forward_variables_from(invoker, |
| "*", |
| [ |
| "toolchain_args", |
| "toolchain_variant", |
| "with_shared", |
| "variants", |
| ]) |
| } |
| |
| foreach(variant, [ [] ] + invoker.variants) { |
| # Each variant toolchain's name is formed by taking the base toolchain |
| # name and successively appending "-$name" for each config listed in |
| # the variant, where name is `get_label_name(config, "name")`. |
| # NOTE: This must match the method used in |
| # //build/config/BUILDCONFIG.gn:current_variant |
| suffix = "" |
| foreach(subvariant, variant) { |
| suffix += "-" + get_label_info(subvariant, "name") |
| } |
| |
| toolchain_args_for_clang = { |
| } |
| toolchain_args_for_clang = { |
| toolchain_variant = variant |
| current_base_toolchain = |
| get_label_info(":$target_name", "label_no_toolchain") |
| if (defined(invoker.toolchain_args)) { |
| forward_variables_from(invoker.toolchain_args, "*") |
| } |
| } |
| |
| clang_toolchain("${target_name}${suffix}") { |
| forward_variables_from(invoker_clang_toolchain_args, "*") |
| toolchain_args = toolchain_args_for_clang |
| } |
| |
| if (invoker.with_shared) { |
| assert(!invoker.is_pic_default, |
| "If setting is_pic_default, use with_shared=false") |
| clang_toolchain("${target_name}${suffix}-shared") { |
| forward_variables_from(invoker_clang_toolchain_args, "*") |
| toolchain_args = { |
| is_pic_default = true |
| forward_variables_from(toolchain_args_for_clang, "*") |
| } |
| } |
| } |
| } |
| } |
| |
| set_defaults("clang_toolchain_suite") { |
| variants = known_variants + extra_variants |
| with_shared = true |
| } |