راهنمای قوانین ساخت، راهنمای قوانین ساخت
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
مقدمه
Sandboxed API (SAPI) را می توان با سیستم ساخت 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 : کتابخانه Sandboxed، cc_library معمولی را به عنوان هدف کد میزبان جایگزین میکند. از وابستگی های
zlib_sapi.bin
و sandbox تشکیل شده است. - name.interface : رابط کتابخانه تولید شده.
- name .embed:
cc_embed_data()
هدف برای جاسازی Sandboxee در باینری استفاده می شود. به bazel/embed_data.bzl مراجعه کنید.- name .bin: باینری Sandboxee، شامل یک خرده ارتباطی کوچک و کتابخانهای است که در حال بستهبندی میشود.
استدلال ها
صفات | نام | نام؛ مورد نیاز است یک نام منحصر به فرد برای این هدف. با این کار کتابخانه C/C++ جعبه شنی شناسایی می شود، هدف خروجی name-sapi را ببینید. |
deps | لیست برچسب ها ؛ اختیاری فهرستی از کتابخانه های دیگر که باید به کتابخانه سندباکس C/C++ پیوند داده شوند. |
srcs | لیست برچسب ها ؛ اختیاری فهرستی از فایلهای C و C++ که برای ایجاد کتابخانه C/C++ sandbox پردازش میشوند. اینها فایلهای منبع و هدر C/C++ هستند، یا تولید نشده (کد منبع عادی) یا تولید شدهاند. برای اطلاعات بیشتر، به توضیح ویژگی srcs در مستندات cc_library مراجعه کنید. |
hdrs | لیست برچسب ها ؛ اختیاری فهرستی از فایلهای هدر که برای ایجاد کتابخانه C/C++ جعبهشناختی پردازش میشوند. اینجا جایی است که تعریف sandbox (sandbox.h) باید برود. اگر از کتابخانه SAPI تعبیه شده استفاده می شود، خالی بگذارید و سیاست پیش فرض جعبه ایمنی کافی است. |
جاسازی کنید | بولی؛ اختیاری؛ پیش فرض True است اگر درست است، کتابخانه sandboxed باید در داخل کد میزبان تعبیه شود. این اجازه می دهد تا جعبه ایمنی SAPI با سازنده ::sapi::Sandbox::Sandbox(FileToc*) مقداردهی اولیه شود. |
توابع | لیست نام توابع؛ اختیاری فهرستی از توابع از کتابخانه C/C++ که یک نسخه sandbox برای آنها تولید شده است و سپس میتواند در کد میزبان استفاده شود. یک لیست خالی سعی می کند همه توابع موجود در کتابخانه را صادر و بسته بندی کند. |
lib | رشته؛ مورد نیاز است نام هدف کتابخانه C/C++ که قرار است کتابخانه sandbox شده باشد. این انتظار دارد که شما یک قانون ساخت cc_library برای کتابخانه C/C++ در پروژه داشته باشید. |
lib_name | رشته؛ مورد نیاز است نام شی SAPI که برای پراکسی کردن توابع کتابخانه از ویژگی توابع استفاده می شود. هر فراخوانی به تابع در کتابخانه sandboxed از طریق شی SAPI انجام می شود. |
input_files | لیست برچسب ها ؛ اختیاری لیستی از فایل های C و C++ که در طول اجرای داخلی قانون sapi_interface پردازش می شوند. مولد این فایل ها را برای اعلان عملکرد کتابخانه C/C++ اسکن می کند. این بیشتر مورد نیاز نیست زیرا سرصفحه های صادر شده کتابخانه C/C++ همیشه اسکن می شوند. |
فضای نام | رشته؛ اختیاری؛ پیش فرض sapigen است یک شناسه فضای نام C++ برای قرار دادن شی SAPI تعریف شده توسط lib_name در آن. فضای نام پیش فرض sapigen است. |
رشته؛ اختیاری نام فایل هدر برای استفاده به جای فایل هدر تولید شده. اگر می خواهید کد را به صورت خودکار تولید کنید، از این ویژگی استفاده نکنید |
add_default_deps | بولی؛ اختیاری؛ پیش فرض True است منسوخ شده |
limit_scan_depth | بولی؛ اختیاری؛ پیش فرض نادرست است برای کتابخانه های پیچیده، ممکن است تعداد فایل برای Bazel به دست آید و فرآیند ساخت موفق نخواهد شد. این ویژگی یک دریچه فرار برای این موقعیت های پیچیده است. مگر در موارد ضروری استفاده نکنید . |
برچسب ها | برای برچسب ها به مستندات Bazel مراجعه کنید. |
دید | برای مشاهده مستندات 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",
)
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-24 بهوقت ساعت هماهنگ جهانی."],[[["\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 )"]]