brettw | f760b44 | 2016-06-29 18:57:28 | [diff] [blame] | 1 | # Copyright 2016 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 | |
| 5 | template("split_static_library") { |
| 6 | assert(defined(invoker.split_count), |
| 7 | "Must define split_count for split_static_library") |
| 8 | |
| 9 | # In many conditions the number of inputs will be 1 (because the count will |
| 10 | # be conditional on platform or configuration) so optimize that. |
| 11 | if (invoker.split_count == 1) { |
| 12 | static_library(target_name) { |
| 13 | forward_variables_from(invoker, "*") |
| 14 | } |
| 15 | } else { |
| 16 | group_name = target_name |
| 17 | |
| 18 | generated_static_libraries = [] |
| 19 | current_library_index = 0 |
| 20 | foreach(current_sources, split_list(invoker.sources, invoker.split_count)) { |
| 21 | current_name = "${target_name}_$current_library_index" |
| 22 | assert( |
| 23 | current_sources != [], |
| 24 | "Your values for splitting a static library generate one that has no sources.") |
| 25 | generated_static_libraries += [ ":$current_name" ] |
| 26 | |
| 27 | static_library(current_name) { |
| 28 | # Generated static library shard gets everything but sources (which |
| 29 | # we're redefining) and visibility (which is set to be the group |
| 30 | # below). |
| 31 | forward_variables_from(invoker, |
| 32 | "*", |
| 33 | [ |
brettw | b3e6014 | 2016-09-23 19:55:01 | [diff] [blame] | 34 | "check_includes", |
brettw | f760b44 | 2016-06-29 18:57:28 | [diff] [blame] | 35 | "sources", |
| 36 | "visibility", |
| 37 | ]) |
| 38 | sources = current_sources |
| 39 | visibility = [ ":$group_name" ] |
brettw | 251c5518 | 2016-07-15 18:21:26 | [diff] [blame] | 40 | |
brettw | b3e6014 | 2016-09-23 19:55:01 | [diff] [blame] | 41 | # When splitting a target's sources up into a series of static |
| 42 | # libraries, those targets will naturally include headers from each |
| 43 | # other arbitrarily. We could theoretically generate a web of |
| 44 | # dependencies and allow_circular_includes_from between all pairs of |
| 45 | # targets, but that's very cumbersome. Typical usage in Chrome is that |
| 46 | # only official Windows builds use split static libraries due to the |
| 47 | # Visual Studio size limits, and this means we'll still get header |
| 48 | # checking coverage for the other configurations. |
| 49 | check_includes = false |
| 50 | |
brettw | 251c5518 | 2016-07-15 18:21:26 | [diff] [blame] | 51 | # Uniquify the output name if one is specified. |
| 52 | if (defined(invoker.output_name)) { |
| 53 | output_name = "${invoker.output_name}_$current_library_index" |
| 54 | } |
brettw | f760b44 | 2016-06-29 18:57:28 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | current_library_index = current_library_index + 1 |
| 58 | } |
| 59 | |
| 60 | group(group_name) { |
| 61 | public_deps = generated_static_libraries |
| 62 | forward_variables_from(invoker, |
| 63 | [ |
| 64 | "testonly", |
| 65 | "visibility", |
| 66 | ]) |
| 67 | } |
| 68 | } |
| 69 | } |
brettw | edb6ecc | 2016-07-14 23:37:03 | [diff] [blame] | 70 | |
| 71 | set_defaults("split_static_library") { |
| 72 | configs = default_compiler_configs |
| 73 | } |