The Chromium Chronicle #15: टारगेट विज़िबिलिटी को सीमित करना

एपिसोड 15: मॉन्ट्रियल में जो मेसन का बनाया हुआ, पीक्यू (नवंबर 2020)
पिछले एपिसोड

Chrome एक बड़ा प्रोजेक्ट है, जिसके कई सब-सिस्टम हैं. किसी एक कॉम्पोनेंट के लिए लिखा गया कोड मिलना आम बात है जो दूसरी जगहों पर भी काम का होगा. हालांकि, हो सकता है कि उसमें छिपी हुई पाबंदियां हों. सुरक्षा के लिए, खतरनाक सुविधाओं के ऐक्सेस पर पाबंदी लगाएं. उदाहरण के लिए, खास परफ़ॉर्मेंस की ज़रूरतों के हिसाब से बनाया गया कस्टम फ़ंक्शन:

// 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 check से की जाती है. यह हर GN बिल्ड के हिस्से के तौर पर काम करता है.

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 को पहले से सबमिट करके जांचा जाता है, इसलिए जब तक आप कोई बदलाव अपलोड करने की कोशिश नहीं करते, तब तक आपको कोई गड़बड़ी नहीं दिखेगी. अपलोड किए बिना include_rules की जांच करने के लिए, buildtools/checkdeps/checkdeps.py <directory> चलाएं.

संसाधन