ビルドルールガイド
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
はじめに
サンドボックス API(SAPI)は、Google の Bazel ビルドシステム、または一般的な CMake メタビルドシステムで使用できます。このページでは Bazel に焦点を当てていますが、同じ機能が CMake でも利用できます。Bazel は推奨されるビルドシステムであり、最も簡単に統合できます。
BUILD.bazel ファイルには、ホストコードをビルドするビルドルールがあります。ホストコードでライブラリのサンドボックス バージョンを使用するには、ホストコードで使用するビルド ターゲットを準備する必要があります。
SAPI ビルドルール
sapi_library
sapi_library(name, deps, srcs, hdrs, embed, functions, lib, lib_name, input_files, namespace, header, add_default_deps, limit_scan_depth, visibility)
出力ターゲット
sapi_library()
ビルドルールは、次のターゲットを生成します。
- name-sapi: サンドボックス化されたライブラリ。ホストコード ターゲットとして通常の cc_library を置き換えます。
zlib_sapi.bin
とサンドボックスの依存関係で構成されます。
- name.interface: 生成されたライブラリ インターフェース。
- name.embed: バイナリに Sandboxee を埋め込むために使用される
cc_embed_data()
ターゲット。bazel/embed_data.bzl をご覧ください。
- name.bin: Sandboxee バイナリ。小さな通信スタブとサンドボックス化されるライブラリで構成されます。
引数
属性 |
name |
名前(必須)
このターゲットの一意の名前。これにより、サンドボックス化された C/C++ ライブラリが特定されます。name-sapi 出力ターゲットをご覧ください。
|
deps |
ラベルのリスト。省略可
サンドボックス化された C/C++ ライブラリにリンクされる他のライブラリのリスト。
|
srcs |
ラベルのリスト。省略可
サンドボックス化された C/C++ ライブラリを作成するために処理される C ファイルと C++ ファイルのリスト。これらは、生成されていない(通常のソースコード)か生成された C/C++ ソースファイルとヘッダー ファイルです。
詳細については、
cc_library ドキュメントの srcs 属性の説明をご覧ください。
|
hdrs |
ラベルのリスト。省略可
サンドボックス化された C/C++ ライブラリを作成するために処理されるヘッダー ファイルのリスト。
サンドボックス定義(sandbox.h)はここに配置します。埋め込み SAPI ライブラリが使用され、デフォルトのサンドボックス ポリシーで十分な場合は、空のままにします。
|
埋め込む |
ブール値。省略可。デフォルトは True です。
True の場合、サンドボックス化されたライブラリはホストコード内に埋め込まれます。これにより、SAPI サンドボックスを ::sapi::Sandbox::Sandbox(FileToc*) コンストラクタで初期化できます。
|
関数 |
関数名のリスト(省略可)
サンドボックス バージョンが生成され、ホストコードで使用できる C/C++ ライブラリの関数のリスト。
リストが空の場合、ライブラリで見つかったすべての関数をエクスポートしてラップしようとします。
|
lib |
文字列。必須
サンドボックス化されたライブラリになる C/C++ ライブラリ ターゲットの名前。
これは、プロジェクトに C/C++ ライブラリの cc_library ビルドルールがあることを前提としています。
|
lib_name |
文字列。必須
functions 属性からライブラリ関数をプロキシするために使用される SAPI オブジェクトの名前。サンドボックス ライブラリ内の関数への呼び出しは、SAPI オブジェクトを介して行われます。
|
input_files |
ラベルのリスト。省略可
sapi_interface ルールの内部実行中に処理される C ファイルと C++ ファイルのリスト。ジェネレータは、これらのファイルで C/C++ ライブラリの関数宣言をスキャンします。
C/C++ ライブラリのエクスポートされたヘッダーは常にスキャンされるため、ほとんどの場合、これは必要ありません。
|
namespace |
文字列。省略可。デフォルトは sapigen
lib_name で定義された SAPI オブジェクトを配置する C++ 名前空間識別子。
デフォルトの Namespace は sapigen です。
|
文字列。省略可
生成されたヘッダー ファイルの代わりに使用するヘッダー ファイルの名前。
コードを自動生成する場合は、この属性を使用しないでください。
|
add_default_deps |
ブール値。省略可。デフォルトは True です。
非推奨
|
limit_scan_depth |
ブール値。省略可。デフォルトは False です。
複雑なライブラリの場合、Bazel のファイル数の上限に達し、ビルドプロセスが成功しないことがあります。この属性は、このような複雑な状況を回避するためのものです。必要な場合を除き、使用しないでください。
|
tags |
タグについては、Bazel のドキュメントをご覧ください。
|
visibility |
可視性については、Bazel のドキュメントをご覧ください。
|
使用例
zlib の例は、sapi_library ビルドルールがどのように使用されるかを示す優れたリファレンス プロジェクトです。
load(
"//sandboxed_api/tools/generator:sapi_generator.bzl",
"sapi_library",
)
sapi_library(
name = "zlib-sapi",
srcs = [], # Extra code compiled with the SAPI library
hdrs = [], # Leave empty if embedded SAPI libraries are used, and the
# default sandbox policy is sufficient.
embed = True, # This is the default
functions = [
"deflateInit_",
"deflate",
"deflateEnd",
],
lib = "@zlib//:zlibonly",
lib_name = "Zlib",
namespace = "sapi::zlib",
)
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-26 UTC。"],[[["\u003cp\u003eSandboxed API (SAPI) integrates with Bazel and CMake, with Bazel being the recommended build system.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003esapi_library\u003c/code\u003e rule in Bazel is used to build sandboxed versions of libraries.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003esapi_library\u003c/code\u003e generates targets like \u003ccode\u003ename-sapi\u003c/code\u003e for the sandboxed library and \u003ccode\u003ename.embed\u003c/code\u003e for embedding the Sandboxee.\u003c/p\u003e\n"],["\u003cp\u003eSeveral arguments in \u003ccode\u003esapi_library\u003c/code\u003e allow customization such as \u003ccode\u003efunctions\u003c/code\u003e to specify functions to sandbox and \u003ccode\u003elib\u003c/code\u003e to define the library target.\u003c/p\u003e\n"],["\u003cp\u003eAn example using zlib demonstrates the practical application of the \u003ccode\u003esapi_library\u003c/code\u003e rule.\u003c/p\u003e\n"]]],[],null,["Introduction\n\nSandboxed API (SAPI) can be used with Google's [Bazel](https://bazel.build/)\nbuild system, or with the popular [CMake](https://cmake.org/) meta build system.\nThis page focuses on Bazel, but the same features are available for CMake. Bazel\nis the recommended build system and the easiest to integrate with.\n\nIn your BUILD.bazel file you will have a build rule to build your Host Code. For\nthe Host Code to use the sandboxed version of a library, you need to prepare a\nbuild target that your Host Code will make use of.\n\nSAPI Build Rules\n\n- sapi_library\n\nsapi_library \n\n```\nsapi_library(name, deps, srcs, hdrs, embed, functions, lib, lib_name, input_files, namespace, header, add_default_deps, limit_scan_depth, visibility)\n```\n\nOutput Targets\n\nThe `sapi_library()` build rule generates the following targets:\n\n- *name-sapi* : Sandboxed library, substitutes the normal cc_library as the Host Code target. Consists of `zlib_sapi.bin` and sandbox dependencies.\n- *name*.interface: Generated library interface.\n- *name* .embed: `cc_embed_data()` target used to embed the Sandboxee in the binary. See [bazel/embed_data.bzl](https://github.com/google/sandboxed-api/blob/main/sandboxed_api/bazel/embed_data.bzl).\n - *name*.bin: Sandboxee binary, consists of a small communication stub and the library that is being sandboxed.\n\nArguments\n\n| Attributes ||\n|------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| name | [Name](https://docs.bazel.build/versions/main/build-ref.html#name); required A unique name for this target. This will identify the sandboxed C/C++ library, see the name-sapi output target. |\n| deps | List of [labels](https://docs.bazel.build/versions/main/build-ref.html#labels); optional A list of other libraries to be linked into the sandboxed C/C++ library. |\n| srcs | List of [labels](https://docs.bazel.build/versions/main/build-ref.html#labels); optional A list of C and C++ files that are processed to create the sandboxed C/C++ library. These are C/C++ source and header files, either non-generated (normal source code) or generated. For more information, see the explanation of the attribute srcs in the [cc_library documentation](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.srcs). |\n| hdrs | List of [labels](https://docs.bazel.build/versions/main/build-ref.html#labels); optional A list of header files that are processed to create the sandboxed C/C++ library. This is where the sandbox definition (sandbox.h) should go; leave empty if the embedded SAPI library is used, and the default sandbox policy is sufficient. |\n| embed | Boolean; optional; default is True If True, the sandboxed library should be embedded inside the host code. This allows the SAPI Sandbox to be initialized with the `::sapi::Sandbox::Sandbox(FileToc*)` constructor. |\n| functions | List of function names; optional A list of functions from the C/C++ library for which a sandboxed version is generated and that can then be used in the Host Code. An empty list will try to export and wrap all functions found in the library. |\n| lib | String; required The name of the C/C++ library target which is going to be the sandboxed library. This expects that you have a cc_library build rule for the C/C++ library in the project. |\n| lib_name | String; required The name of the SAPI object which is used to proxy the library functions from the functions attribute. Any call to the function in the sandboxed library will happen via the SAPI Object. |\n| input_files | List of [labels](https://docs.bazel.build/versions/main/build-ref.html#labels); optional A list of C and C++ files that are processed during the internal run of the sapi_interface rule. The generator scans these files for the C/C++ library's function declarations. This is mostly not needed as the C/C++ library's exported headers are always scanned. |\n| namespace | String; optional; default is sapigen A C++ namespace identifier to place the SAPI object defined by *lib_name* into. The default namespace is *sapigen*. |\n| header | String; optional The name of the header file to use instead of the generated header file. If you want to auto-generate the code, **do not use** this attribute |\n| add_default_deps | Boolean; optional; default is True **DEPRECATED** |\n| limit_scan_depth | Boolean; optional; default is False For complex libraries, the file-count for Bazel might be reached and the build process will not succeed. This attribute is an escape hatch for these complex situations. **Do not use** unless necessary. |\n| tags | See Bazel documentation for [tags](https://docs.bazel.build/versions/main/be/common-definitions.html#common.tags). |\n| visibility | See Bazel documentation for [visibility](https://docs.bazel.build/versions/main/be/common-definitions.html#common.visibility) |\n\nExample Use\n\nThe zlib example is a good reference project demonstrating how the sapi_library\nbuild rule is used: \n\n load(\n \"//sandboxed_api/tools/generator:sapi_generator.bzl\",\n \"sapi_library\",\n )\n\n sapi_library(\n name = \"zlib-sapi\",\n srcs = [], # Extra code compiled with the SAPI library\n hdrs = [], # Leave empty if embedded SAPI libraries are used, and the\n # default sandbox policy is sufficient.\n embed = True, # This is the default\n functions = [\n \"deflateInit_\",\n \"deflate\",\n \"deflateEnd\",\n ],\n lib = \"@zlib//:zlibonly\",\n lib_name = \"Zlib\",\n namespace = \"sapi::zlib\",\n )"]]