subsys-build-feature.md 1.8 KB
Newer Older
A
Annie_wang 已提交
1 2 3 4 5 6 7 8 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
# Feature
### Configuration Rules

This document describes how to declare, define, and configure features.

- Declare a feature

  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}**. 

  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
  }
  ```

  The value defined is the default value of the component. The product can overload the feature default values in the component list.

  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.

- Configure a feature

  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" ]
  }
  
  # 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().
  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"]
  }
  ```