subsys-build-feature.md 1.8 KB
Newer Older
A
Annie_wang 已提交
1 2 3
# Feature
### Configuration Rules

A
Annie_wang 已提交
4
This document describes how to declare, define, and use features.
A
Annie_wang 已提交
5 6 7

- Declare a feature

A
Annie_wang 已提交
8
  Declare the features of a component in **feature_list** of the **bundle.json** file of the component. Each feature must start with the **{component_name}**. 
A
Annie_wang 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

  The following is an example:

  ```
  {
    "name": "@ohos/xxx",
    "component": {
      "name": "partName",
      "subsystem": "subsystemName",
      "features": [
        "{partName}_feature_A"
      ]
    }
  }
  ```

   You can declare multiple features in **features** for a component.

- Define a feature

  You can define the default value of a feature as follows:

  ```
  declare_args() {
    {partName}_feature_A = true
  }
  ```

A
Annie_wang 已提交
37
  The value defined is the default value of the feature for this component. The product can overload the feature default values in the component list.
A
Annie_wang 已提交
38 39 40

  If a feature is used by multiple modules of a component, you are advised to define the feature in the global .gni file of the component and import the .gni file to the **BUILD.gn** file of each module.

A
Annie_wang 已提交
41
- Use a feature
A
Annie_wang 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55

  In the **BUILD.gn** file, determine the code or modules to build based on features.

  ```
  if ({partName}_feature_A) {
      sources += [ "xxx.c" ]
  }
  
  # Dependency introduced by a feature can be isolated by feature.
  if ({partName}_feature_A) {
      deps += [ "xxx" ]
      external_deps += [ "xxx" ]
  }
  
A
Annie_wang 已提交
56
  # The **bundle.json** file does not support the if statement. If the sub_component contained in the **bundle.json** file needs to be deleted, define group().
A
Annie_wang 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  group("testGroup") {
    deps = []
    if ({partName}_feature_A) {
      deps += [ "xxx" ]
    }
  }
  ```

  You can also define code macros for modules in the following way to implement differentiated configuration:

  ```
  if ({partName}_feature_A) {
      defines += ["FEATUREA_DEFINE"]
  }
  ```