Chromium Chronicle #15: ターゲットの公開設定の制限

エピソード 15: ジョー メイソン(PQ、モントリオール、2020 年 11 月)
前のエピソード

Chrome は多くのサブシステムがある大規模なプロジェクトです。1 つのコンポーネント用に作成されたコードの中には、他の場所では役に立つかもしれませんが、隠れた制限があるものもあります。安全のため、危険な機能への外部アクセスを制限してください。たとえば、特定のパフォーマンス ニーズに合わせてチューニングされたカスタム関数があるとします。

// Blazing fast for 2-char strings, O(n^3) otherwise.
std::string ConcatShortStringsFast(const std::string& a, const std::string& b);

アクセスを制限する方法はいくつかあります。GN 可視性ルールは、コンポーネント外のコードがターゲットに依存しないようにするデフォルトでは、ターゲットはすべてのユーザーに表示されますが、必要に応じて変更できます。

# In components/restricted_component/BUILD.gn
visibility = [
  # Applies to all targets in this file.
  # Only the given targets can depend on them.
  "//components/restricted_component:*",
  "//components/authorized_other_component:a_single_target",
]
source_set("internal") {
  # This dangerous target should be locked down even more.
  visibility = [ "//components/restricted_component:privileged_target" ]
}

可視性の宣言は、すべての GN ビルドの一部として実行される gn check で検証されます。

もう 1 つのメカニズムは、ヘッダー ファイルへのアクセスを制限する DEPS include_rules です。すべてのディレクトリは親から include_rules を継承し、それぞれの DEPS ファイルでこれらのルールを変更できます。外部ディレクトリからインクルードされたすべてのヘッダー ファイルは、include_rules で許可される必要があります。

# In //components/authorized_other_component/DEPS
include_rules = [
  # Common directories like //base are inherited from
  # //components/DEPS or //DEPS. Also allow includes from
  # restricted_component, but not restricted_component/internal.
  "+components/restricted_component",
  "-components/restricted_component/internal",
  # But do allow a single header from internal, for testing.
  "+components/restricted_component/internal/test_support.h",
]

これらの依存関係が適切であることを確認するために、ディレクトリを include_rules に追加する変更は、そのディレクトリの OWNERS によって承認される必要がありますinclude_rules を使用してディレクトリを制限するために承認は必要ありません。include_rule を禁止することで、コンポーネントを変更するすべてのユーザーが、特定のヘッダーを使用しないようにすることができます。

include_rules は presubmit によってチェックされるため、変更をアップロードするまでエラーは表示されません。アップロードせずに include_rules をテストするには、buildtools/checkdeps/checkdeps.py <directory> を実行します。

関連情報