summaryrefslogtreecommitdiffstats
path: root/components/policy/BUILD.gn
blob: b373cc3e5ce079af9f794ba20bcc549d495d01de (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/features.gni")
import("resources/policy_templates.gni")
import("//third_party/protobuf/proto_library.gni")
import("//tools/grit/grit_rule.gni")

# About the policy component:
#
# There should really be two components. One called "policy" that includes all
# generated code, and one "policy/proto" liek we have now. The proto needs to
# be separate for cases like SesssionManagerClient which uses the protos to
# communicate with the Chrome OS system layer, but doesn't need the rest of the
# policy stuff).
#
# The reason the rest of the targets exist are artifacts of the way the GYP
# build works. The current "policy" target which is really just some generated
# code (which should be folded into the new "policy" component described above)
# exists so code outside which depends on the generated headers can depend on
# the hard policy target without having to make all policy targets hard
# dependencies (GN will do the right thing without this extra target).
if (is_component_build) {
  component("policy_component") {
    deps = [
      "//components/policy/core/browser",
      "//components/policy/core/common",
    ]
  }
  group("policy_component_browser") {
    deps = [
      ":policy_component",
    ]
  }
  group("policy_component_common") {
    deps = [
      ":policy_component",
    ]
  }
} else {  # Compile to separate libraries.
  group("policy_component") {
    deps = [
      ":policy_component_browser",
      ":policy_component_common",
    ]
  }
  component("policy_component_browser") {
    deps = [
      "//components/policy/core/browser",
    ]
  }
  component("policy_component_common") {
    deps = [
      "//components/policy/core/common",
    ]
  }
}

if (enable_configuration_policy) {
  # TODO(brettw) this component should use target_gen_dir instead but the GYP
  # build puts everything into the following directory. We do the same for now.
  policy_gen_dir = "$root_gen_dir/policy"

  # This protobuf is equivalent to chrome_settings.proto but shares messages
  # for policies of the same type, so that less classes have to be generated
  # and compiled.
  cloud_policy_proto_path = "$policy_gen_dir/cloud_policy.proto"

  # This is the "full" protobuf, which defines one protobuf message per
  # policy. It is also the format currently used by the server.
  chrome_settings_proto_path = "$policy_gen_dir/chrome_settings.proto"

  constants_header_path = "$policy_gen_dir/policy_constants.h"
  constants_source_path = "$policy_gen_dir/policy_constants.cc"
  protobuf_decoder_path = "$policy_gen_dir/cloud_policy_generated.cc"
  app_restrictions_path = "$policy_gen_dir/app_restrictions.xml"

  action("cloud_policy_code_generate") {
    script = "tools/generate_policy_source.py"
    chrome_version_abspath = "//chrome/VERSION"
    chrome_version_path = rebase_path(chrome_version_abspath, root_build_dir)

    if (is_chromeos) {
      chromeos_flag = "1"
    } else {
      chromeos_flag = "0"
    }

    inputs = [
      chrome_version_abspath,
      "resources/policy_templates.json",
    ]
    outputs = [
      constants_header_path,
      constants_source_path,
      protobuf_decoder_path,
      chrome_settings_proto_path,
      cloud_policy_proto_path,
      app_restrictions_path,
    ]

    if (target_os != "android") {
      outputs -= [ app_restrictions_path ]
    }

    args = [
      "--policy-constants-header=" +
          rebase_path(constants_header_path, root_build_dir),
      "--policy-constants-source=" +
          rebase_path(constants_source_path, root_build_dir),
      "--chrome-settings-protobuf=" +
          rebase_path(chrome_settings_proto_path, root_build_dir),
      "--cloud-policy-protobuf=" +
          rebase_path(cloud_policy_proto_path, root_build_dir),
      "--cloud-policy-decoder=" +
          rebase_path(protobuf_decoder_path, root_build_dir),
      "--app-restrictions-definition=" +
          rebase_path(app_restrictions_path, root_build_dir),
      chrome_version_path,
      target_os,
      chromeos_flag,
      rebase_path("resources/policy_templates.json", root_build_dir),
    ]
  }

  grit("policy_templates") {
    source = "resources/policy_templates.grd"
    use_qualified_include = true
    output_dir = "$root_gen_dir/chrome"
    outputs = policy_templates_doc_outputs
    if (is_android) {
      outputs += policy_templates_android_outputs
    }
    if (is_linux) {
      outputs += policy_templates_linux_outputs
    }
    if (is_mac) {
      outputs += policy_templates_mac_outputs

      # TODO(knn) : Move this out once more targets use this.
      if (is_chrome_branded) {
        mac_bundle_id = "com.google.Chrome"
      } else {
        mac_bundle_id = "org.chromium.Chromium"
      }
      defines = [ "mac_bundle_id=$mac_bundle_id" ]
    }
    if (is_win) {
      outputs += policy_templates_windows_outputs
    }
  }

  # Run the proto compiler over the generated file and make it a component.
  component("cloud_policy_proto_generated_compile") {
    public_deps = [
      ":cloud_policy_proto_generated_compile_proto",
    ]
  }
  proto_library("cloud_policy_proto_generated_compile_proto") {
    visibility = [ ":cloud_policy_proto_generated_compile" ]
    sources = [
      cloud_policy_proto_path,
    ]

    proto_out_dir = "policy/proto"
    cc_generator_options = "dllexport_decl=POLICY_PROTO_EXPORT:"
    cc_include = "components/policy/policy_proto_export.h"
    defines = [ "POLICY_PROTO_COMPILATION" ]

    deps = [
      ":cloud_policy_code_generate",
    ]
  }

  # This target builds the "full" protobuf, used for tests only.
  proto_library("chrome_settings_proto_generated_compile") {
    sources = [
      chrome_settings_proto_path,
    ]
    proto_out_dir = "policy/proto"

    deps = [
      ":cloud_policy_code_generate",
      ":cloud_policy_proto_generated_compile",
    ]
  }

  # The dependencies here are kind of messed up.
  #
  # //components/policy/core/browser and .../common depend on this target,
  # and require it to be compiled with POLICY_COMPONENT_IMPLEMENTATION to
  # export the necessary symbols in a component build. But other targets like
  # //chrome/browser also depend on this target, which in component build
  # ends up in another shared library, but with the IMPLEMENTATION flag set
  # incorrectly.
  #
  # This only reason this works is that this target is a shared library and
  # the compilation units that this depends on in policy/core/common happen to
  # not be brought in in the "bad" case. This needs serious untangling, maybe
  # these files can just be put into policy/core/common?
  static_library("policy") {
    sources = [
      constants_header_path,
      constants_source_path,
      protobuf_decoder_path,
    ]

    defines = [ "POLICY_COMPONENT_IMPLEMENTATION" ]

    deps = [
      ":cloud_policy_code_generate",
      ":cloud_policy_proto_generated_compile",
      "//base",
      "//third_party/protobuf:protobuf_lite",
    ]
  }

  group("test_support") {
    deps = [
      ":chrome_settings_proto_generated_compile",
      ":policy_component",
    ]
  }

  static_library("policy_component_test_support") {
    testonly = true
    sources = [
      "core/browser/configuration_policy_pref_store_test.cc",
      "core/browser/configuration_policy_pref_store_test.h",
      "core/common/cloud/mock_cloud_external_data_manager.cc",
      "core/common/cloud/mock_cloud_external_data_manager.h",
      "core/common/cloud/mock_cloud_policy_client.cc",
      "core/common/cloud/mock_cloud_policy_client.h",
      "core/common/cloud/mock_cloud_policy_store.cc",
      "core/common/cloud/mock_cloud_policy_store.h",
      "core/common/cloud/mock_device_management_service.cc",
      "core/common/cloud/mock_device_management_service.h",
      "core/common/cloud/mock_user_cloud_policy_store.cc",
      "core/common/cloud/mock_user_cloud_policy_store.h",
      "core/common/cloud/policy_builder.cc",
      "core/common/cloud/policy_builder.h",
      "core/common/configuration_policy_provider_test.cc",
      "core/common/configuration_policy_provider_test.h",
      "core/common/fake_async_policy_loader.cc",
      "core/common/fake_async_policy_loader.h",
      "core/common/mock_configuration_policy_provider.cc",
      "core/common/mock_configuration_policy_provider.h",
      "core/common/mock_policy_service.cc",
      "core/common/mock_policy_service.h",
      "core/common/policy_test_utils.cc",
      "core/common/policy_test_utils.h",
      "core/common/preferences_mock_mac.cc",
      "core/common/preferences_mock_mac.h",
      "core/common/remote_commands/test_remote_command_job.cc",
      "core/common/remote_commands/test_remote_command_job.h",
      "core/common/remote_commands/testing_remote_commands_server.cc",
      "core/common/remote_commands/testing_remote_commands_server.h",
    ]

    if (is_chromeos) {
      sources -= [
        "core/common/cloud/mock_user_cloud_policy_store.cc",
        "core/common/cloud/mock_user_cloud_policy_store.h",
      ]
    }

    deps = [
      ":policy_component",
      ":test_support",
      "//components/policy/proto",
      "//testing/gmock",
      "//testing/gtest",
    ]
  }

  if (is_android) {
    import("//build/config/android/rules.gni")
    group("app_restrictions_resources") {
      resources_zip = "res.java/$target_name.zip"
      generated_resources_dir = "$root_gen_dir/chrome/app/policy/android"

      copy("${target_name}__consolidate_resources") {
        sources = [
          "$root_gen_dir/policy/app_restrictions.xml",
        ]
        outputs = [
          "$generated_resources_dir/xml-v21/app_restrictions.xml",
        ]
        deps = [
          ":policy_templates",
        ]
      }

      # TODO(471115): The write_build_config template is in
      # //build/config/android/internal_rules.gni and is not meant for external
      # use. The public rules (in rules.gni) should be updated to support what
      # we are doing here.
      build_config_path = "$target_gen_dir/$target_name.build_config"
      write_build_config("${target_name}__build_config") {
        build_config = build_config_path
        resources_zip = "$root_build_dir/$resources_zip"
        type = "android_resources"
      }

      action("${target_name}__create_zip") {
        script = "//build/android/gn/zip.py"
        outputs = [
          "$root_build_dir/$resources_zip",
        ]
        generated_resources =
            [ "$generated_resources_dir/xml-v21/app_restrictions.xml" ] +
            policy_templates_android_outputs
        inputs = generated_resources
        rebased_inputs = rebase_path(generated_resources, root_build_dir)
        rebased_resources_dir =
            rebase_path(generated_resources_dir, root_build_dir)
        args = [
          "--inputs=$rebased_inputs",
          "--output=$resources_zip",
          "--base-dir=$rebased_resources_dir",
        ]
      }

      deps = [
        ":${target_name}__consolidate_resources",
        ":${target_name}__build_config",
        ":${target_name}__create_zip",
      ]
    }
  }
}
#TODO(GYP) chrome_manifest_bundle