{"id":1804,"date":"2026-05-24T15:12:10","date_gmt":"2026-05-24T22:12:10","guid":{"rendered":"https:\/\/nanzhou.cc\/?p=1804"},"modified":"2026-05-24T15:12:10","modified_gmt":"2026-05-24T22:12:10","slug":"bazel-platforms","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2026\/05\/24\/bazel-platforms\/","title":{"rendered":"Bazel: platforms"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Summary Table<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><td><strong>Concept<\/strong><\/td><td><strong>What it is<\/strong><\/td><td><strong>Analogy<\/strong><\/td><\/tr><\/thead><tbody><tr><td><strong>Constraint Setting<\/strong><\/td><td>A property category<\/td><td>&#8220;Vehicle Type&#8221;<\/td><\/tr><tr><td><strong>Constraint Value<\/strong><\/td><td>A specific option<\/td><td>&#8220;Car&#8221;, &#8220;Truck&#8221;, &#8220;Motorcycle&#8221;<\/td><\/tr><tr><td><strong>Platform<\/strong><\/td><td>A group of settings<\/td><td>&#8220;Vehicle configured for highway use&#8221; (Car + 4 Tires)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">The Core Concepts<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To understand how they relate, think of them in terms of a property-based system:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>constraint_setting<\/code><\/strong>: Defines a <strong>category<\/strong> or type of property that distinguishes machines (e.g., &#8220;CPU architecture&#8221;, &#8220;Operating System&#8221;).<\/li>\n\n\n\n<li><strong><code>constraint_value<\/code><\/strong>: Defines a specific <strong>choice<\/strong> within a category (e.g., within the &#8220;CPU architecture&#8221; setting, choices could be &#8220;x86&#8221;, &#8220;ARM&#8221;, &#8220;RISC-V&#8221;).<\/li>\n\n\n\n<li><strong><code>platform<\/code><\/strong>: A <strong>collection<\/strong> of <code>constraint_values<\/code> that defines a complete environment (e.g., a &#8220;Linux-x86&#8221; platform is a collection of <code>OS:Linux<\/code> and <code>CPU:x86<\/code>).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example: Defining Custom Constraints<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose you are building an embedded system and you need to distinguish between different <strong>GPU types<\/strong> and <strong>glibc versions<\/strong>. You would define these in your own <code>BUILD<\/code> file.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Define the &#8220;Categories&#8221; (Settings)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create a <code>BUILD<\/code> file (e.g., <code>\/\/tools\/platforms:BUILD<\/code>) to hold your custom logic.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># \/\/tools\/platforms\/BUILD\n\n# A new dimension for GPU hardware\nconstraint_setting(name = \"gpu_type\")\n\n# A new dimension for C library versions\nconstraint_setting(name = \"glibc_version\")\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. Define the &#8220;Choices&#8221; (Values)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Now, define the specific values that belong to those categories.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># \/\/tools\/platforms\/BUILD\n\n# Values for gpu_type\nconstraint_value(name = \"nvidia_rtx\", constraint_setting = \":gpu_type\")\nconstraint_value(name = \"amd_radeon\", constraint_setting = \":gpu_type\")\n\n# Values for glibc_version\nconstraint_value(name = \"glibc_2_25\", constraint_setting = \":glibc_version\")\nconstraint_value(name = \"glibc_2_28\", constraint_setting = \":glibc_version\")\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. Bundle them into a Platform<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you have defined these custom constraints, you can use them alongside standard ones to define your custom platform.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># \/\/tools\/platforms\/BUILD\n\nplatform(\n    name = \"custom_embedded_gpu_linux\",\n    constraint_values = [\n        \"@platforms\/\/os:linux\",          # Standard constraint\n        \"@platforms\/\/cpu:aarch64\",       # Standard constraint\n        \":nvidia_rtx\",                   # YOUR custom constraint\n        \":glibc_2_28\",                   # YOUR custom constraint\n    ],\n)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How to use these in your build<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once you&#8217;ve defined these, you can use <code>select()<\/code> in your rules to change behavior based on these custom constraints.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"># \/\/myapp\/BUILD\n\ncc_library(\n    name = \"gpu_driver\",\n    srcs = select({\n        \"\/\/tools\/platforms:nvidia_rtx\": [\"driver_nvidia.cc\"],\n        \"\/\/tools\/platforms:amd_radeon\": [\"driver_amd.cc\"],\n        \"\/\/conditions:default\": [\"driver_stub.cc\"],\n    }),\n)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Why this approach is powerful:<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Strict Typing:<\/strong> Bazel ensures that <code>nvidia_rtx<\/code> is only used for the <code>gpu_type<\/code> setting. You can\u2019t accidentally assign it to an OS setting.<\/li>\n\n\n\n<li><strong>Organization:<\/strong> You keep your project-specific needs (like &#8220;is this a high-security build?&#8221;) inside your own repository, while using <code>@platforms<\/code> for universal things like <code>cpu<\/code> and <code>os<\/code>.<\/li>\n\n\n\n<li><strong>Cross-Project Compatibility:<\/strong> If another team in your company wants to build for the same <code>custom_embedded_gpu_linux<\/code> platform, they can import the label <code>\/\/tools\/platforms:custom_embedded_gpu_linux<\/code> and get all the same settings.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Summary Table Concept What it is Analogy Constraint Setting A property category &#8220;Vehicle Type&#8221; Constraint Value A specific option &#8220;Car&#8221;, &#8220;Truck&#8221;, &#8220;Motorcycle&#8221; Platform A group of settings &#8220;Vehicle configured for highway use&#8221; (Car + 4 Tires) The Core Concepts To understand how they relate, think of them in terms of a property-based system: Example: Defining&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"0","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[],"class_list":["post-1804","post","type-post","status-publish","format-standard","hentry","category-Toolchain"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=1804"}],"version-history":[{"count":1,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1804\/revisions"}],"predecessor-version":[{"id":1806,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1804\/revisions\/1806"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=1804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=1804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=1804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}