xref: /aosp_15_r20/external/libbrillo/BUILD.gn (revision 1a96fba65179ea7d3f56207137718607415c5953)
1# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//common-mk/deps.gni")
6import("//common-mk/pkg_config.gni")
7import("//common-mk/proto_library.gni")
8
9group("all") {
10  deps = [
11    ":libbrillo",
12    ":libbrillo-glib",
13    ":libbrillo-test",
14    ":libinstallattributes",
15    ":libpolicy",
16  ]
17  if (use.test) {
18    deps += [
19      ":libbrillo_tests",
20      ":libinstallattributes_tests",
21      ":libpolicy_tests",
22    ]
23  }
24  if (use.fuzzer) {
25    deps += [
26      ":libbrillo_data_encoding_fuzzer",
27      ":libbrillo_dbus_data_serialization_fuzzer",
28      ":libbrillo_http_form_data_fuzzer",
29    ]
30  }
31}
32
33default_pkg_deps = [ "libchrome-${libbase_ver}" ]
34pkg_config("target_defaults_pkg_deps") {
35  pkg_deps = default_pkg_deps
36}
37
38config("target_defaults") {
39  configs = [ ":target_defaults_pkg_deps" ]
40  include_dirs = [ "../libbrillo" ]
41  defines = [
42    "USE_DBUS=${use.dbus}",
43    "USE_RTTI_FOR_TYPE_TAGS",
44  ]
45}
46
47config("libbrillo_configs") {
48  include_dirs = [ "../libbrillo" ]
49}
50
51# Properties of shared libraries which libbrillo consists of.
52# Stored to variables once before actually declaring the targets, so that
53# another target can collect information for making the .pc and .so files.
54libbrillo_sublibs = [
55  {
56    # |library_name| is library file name without "lib" prefix. This is needed
57    # for composing -l*** flags in libbrillo-${libbasever}.so.
58    # (Current version of GN deployed to ChromeOS doesn't have string_replace.)
59    library_name = "brillo-core"
60    if (use.dbus) {
61      all_dependent_pkg_deps = [ "dbus-1" ]
62    }
63    libs = [ "modp_b64" ]
64    sources = [
65      "brillo/asynchronous_signal_handler.cc",
66      "brillo/backoff_entry.cc",
67      "brillo/daemons/daemon.cc",
68      "brillo/data_encoding.cc",
69      "brillo/errors/error.cc",
70      "brillo/errors/error_codes.cc",
71      "brillo/file_utils.cc",
72      "brillo/files/file_util.cc",
73      "brillo/files/safe_fd.cc",
74      "brillo/flag_helper.cc",
75      "brillo/key_value_store.cc",
76      "brillo/message_loops/base_message_loop.cc",
77      "brillo/message_loops/message_loop.cc",
78      "brillo/message_loops/message_loop_utils.cc",
79      "brillo/mime_utils.cc",
80      "brillo/osrelease_reader.cc",
81      "brillo/process.cc",
82      "brillo/process_information.cc",
83      "brillo/process_reaper.cc",
84      "brillo/scoped_umask.cc",
85      "brillo/secure_blob.cc",
86      "brillo/strings/string_utils.cc",
87      "brillo/syslog_logging.cc",
88      "brillo/timezone/tzif_parser.cc",
89      "brillo/type_name_undecorate.cc",
90      "brillo/url_utils.cc",
91      "brillo/userdb_utils.cc",
92      "brillo/value_conversion.cc",
93    ]
94    if (use.dbus) {
95      sources += [
96        "brillo/any.cc",
97        "brillo/daemons/dbus_daemon.cc",
98        "brillo/dbus/async_event_sequencer.cc",
99        "brillo/dbus/data_serialization.cc",
100        "brillo/dbus/dbus_connection.cc",
101        "brillo/dbus/dbus_method_invoker.cc",
102        "brillo/dbus/dbus_method_response.cc",
103        "brillo/dbus/dbus_object.cc",
104        "brillo/dbus/dbus_service_watcher.cc",
105        "brillo/dbus/dbus_signal.cc",
106        "brillo/dbus/exported_object_manager.cc",
107        "brillo/dbus/exported_property_set.cc",
108        "brillo/dbus/introspectable_helper.cc",
109        "brillo/dbus/utils.cc",
110      ]
111    }
112  },
113
114  {
115    library_name = "brillo-blockdeviceutils"
116    deps = [ ":libbrillo-core" ]
117    sources = [ "brillo/blkdev_utils/loop_device.cc" ]
118    if (use.device_mapper) {
119      pkg_deps = [ "devmapper" ]
120      sources += [
121        "brillo/blkdev_utils/device_mapper.cc",
122        "brillo/blkdev_utils/device_mapper_task.cc",
123      ]
124    }
125  },
126
127  {
128    library_name = "brillo-http"
129    deps = [
130      ":libbrillo-core",
131      ":libbrillo-streams",
132    ]
133    all_dependent_pkg_deps = [ "libcurl" ]
134    sources = [
135      "brillo/http/curl_api.cc",
136      "brillo/http/http_connection_curl.cc",
137      "brillo/http/http_form_data.cc",
138      "brillo/http/http_request.cc",
139      "brillo/http/http_transport.cc",
140      "brillo/http/http_transport_curl.cc",
141      "brillo/http/http_utils.cc",
142    ]
143    if (use.dbus) {
144      sources += [ "brillo/http/http_proxy.cc" ]
145    }
146  },
147
148  {
149    library_name = "brillo-streams"
150    deps = [ ":libbrillo-core" ]
151    all_dependent_pkg_deps = [ "openssl" ]
152    sources = [
153      "brillo/streams/file_stream.cc",
154      "brillo/streams/input_stream_set.cc",
155      "brillo/streams/memory_containers.cc",
156      "brillo/streams/memory_stream.cc",
157      "brillo/streams/openssl_stream_bio.cc",
158      "brillo/streams/stream.cc",
159      "brillo/streams/stream_errors.cc",
160      "brillo/streams/stream_utils.cc",
161      "brillo/streams/tls_stream.cc",
162    ]
163  },
164
165  {
166    library_name = "brillo-cryptohome"
167    all_dependent_pkg_deps = [ "openssl" ]
168    sources = [ "brillo/cryptohome.cc" ]
169  },
170
171  {
172    library_name = "brillo-namespaces"
173    deps = [ ":libbrillo-core" ]
174    sources = [
175      "brillo/namespaces/mount_namespace.cc",
176      "brillo/namespaces/platform.cc",
177      "brillo/scoped_mount_namespace.cc",
178    ]
179  },
180
181  {
182    library_name = "brillo-minijail"
183    all_dependent_pkg_deps = [ "libminijail" ]
184    sources = [ "brillo/minijail/minijail.cc" ]
185  },
186
187  {
188    library_name = "brillo-protobuf"
189    all_dependent_pkg_deps = [ "protobuf" ]
190    sources = [ "brillo/proto_file_io.cc" ]
191  },
192]
193
194if (use.udev) {
195  libbrillo_sublibs += [
196    {
197      library_name = "brillo-udev"
198      all_dependent_pkg_deps = [ "libudev" ]
199      sources = [
200        "brillo/udev/udev.cc",
201        "brillo/udev/udev_device.cc",
202        "brillo/udev/udev_enumerate.cc",
203        "brillo/udev/udev_list_entry.cc",
204        "brillo/udev/udev_monitor.cc",
205      ]
206    },
207  ]
208}
209
210# Generate shared libraries.
211foreach(attr, libbrillo_sublibs) {
212  shared_library("lib" + attr.library_name) {
213    sources = attr.sources
214    if (defined(attr.deps)) {
215      deps = attr.deps
216    }
217    if (defined(attr.libs)) {
218      libs = attr.libs
219    }
220    if (defined(attr.pkg_deps)) {
221      pkg_deps = attr.pkg_deps
222    }
223    if (defined(attr.public_pkg_deps)) {
224      public_pkg_deps = attr.public_pkg_deps
225    }
226    if (defined(attr.all_dependent_pkg_deps)) {
227      all_dependent_pkg_deps = attr.all_dependent_pkg_deps
228    }
229    if (defined(attr.cflags)) {
230      cflags = attr.cflags
231    }
232    if (defined(attr.configs)) {
233      configs += attr.configs
234    }
235    configs += [ ":target_defaults" ]
236  }
237}
238
239generate_pkg_config("libbrillo-${libbase_ver}_pc") {
240  name = "libbrillo"
241  output_name = "libbrillo-${libbase_ver}"
242  description = "brillo base library"
243  version = libbase_ver
244  requires_private = default_pkg_deps
245  foreach(sublib, libbrillo_sublibs) {
246    if (defined(sublib.pkg_deps)) {
247      requires_private += sublib.pkg_deps
248    }
249    if (defined(sublib.public_pkg_deps)) {
250      requires_private += sublib.public_pkg_deps
251    }
252    if (defined(sublib.all_dependent_pkg_deps)) {
253      requires_private += sublib.all_dependent_pkg_deps
254    }
255  }
256  defines = [ "USE_RTTI_FOR_TYPE_TAGS" ]
257  libs = [ "-lbrillo" ]
258}
259
260generate_pkg_config("libbrillo_pc") {
261  name = "libbrillo"
262  output_name = "libbrillo"
263  description = "brillo base library"
264  version = libbase_ver
265  requires_private = default_pkg_deps
266  foreach(sublib, libbrillo_sublibs) {
267    if (defined(sublib.pkg_deps)) {
268      requires_private += sublib.pkg_deps
269    }
270    if (defined(sublib.public_pkg_deps)) {
271      requires_private += sublib.public_pkg_deps
272    }
273    if (defined(sublib.all_dependent_pkg_deps)) {
274      requires_private += sublib.all_dependent_pkg_deps
275    }
276  }
277  defines = [ "USE_RTTI_FOR_TYPE_TAGS" ]
278  libs = [ "-lbrillo" ]
279}
280
281action("libbrillo") {
282  deps = [
283    ":libbrillo-${libbase_ver}_pc",
284    ":libbrillo_pc",
285  ]
286  foreach(sublib, libbrillo_sublibs) {
287    deps += [ ":lib" + sublib.library_name ]
288  }
289  script = "//common-mk/write_args.py"
290  outputs = [ "${root_out_dir}/lib/libbrillo.so" ]
291  args = [ "--output" ] + outputs + [ "--" ] + [
292           "GROUP",
293           "(",
294           "AS_NEEDED",
295           "(",
296         ]
297  foreach(sublib, libbrillo_sublibs) {
298    args += [ "-l" + sublib.library_name ]
299  }
300  args += [
301    ")",
302    ")",
303  ]
304}
305
306libbrillo_test_deps = [ "libbrillo-http" ]
307
308generate_pkg_config("libbrillo-test-${libbase_ver}_pc") {
309  name = "libbrillo-test"
310  output_name = "libbrillo-test-${libbase_ver}"
311  description = "brillo test library"
312  version = libbase_ver
313
314  # Because libbrillo-test is static, we have to depend directly on everything.
315  requires = [ "libbrillo" ] + default_pkg_deps
316  foreach(name, libbrillo_test_deps) {
317    foreach(t, libbrillo_sublibs) {
318      if ("lib" + t.library_name == name) {
319        if (defined(t.pkg_deps)) {
320          requires += t.pkg_deps
321        }
322        if (defined(t.public_pkg_deps)) {
323          requires += t.public_pkg_deps
324        }
325        if (defined(t.all_dependent_pkg_deps)) {
326          requires += t.all_dependent_pkg_deps
327        }
328      }
329    }
330  }
331  libs = [ "-lbrillo-test" ]
332}
333
334generate_pkg_config("libbrillo-test_pc") {
335  name = "libbrillo-test"
336  output_name = "libbrillo-test"
337  description = "brillo test library"
338  version = libbase_ver
339
340  # Because libbrillo-test is static, we have to depend directly on everything.
341  requires = [ "libbrillo" ] + default_pkg_deps
342  foreach(name, libbrillo_test_deps) {
343    foreach(t, libbrillo_sublibs) {
344      if ("lib" + t.library_name == name) {
345        if (defined(t.pkg_deps)) {
346          requires += t.pkg_deps
347        }
348        if (defined(t.public_pkg_deps)) {
349          requires += t.public_pkg_deps
350        }
351        if (defined(t.all_dependent_pkg_deps)) {
352          requires += t.all_dependent_pkg_deps
353        }
354      }
355    }
356  }
357  libs = [ "-lbrillo-test" ]
358}
359
360static_library("libbrillo-test") {
361  configs -= [ "//common-mk:use_thin_archive" ]
362  configs += [
363    "//common-mk:nouse_thin_archive",
364    ":target_defaults",
365  ]
366  deps = [
367    ":libbrillo-http",
368    ":libbrillo-test-${libbase_ver}_pc",
369    ":libbrillo-test_pc",
370  ]
371  foreach(name, libbrillo_test_deps) {
372    deps += [ ":" + name ]
373  }
374  sources = [
375    "brillo/blkdev_utils/loop_device_fake.cc",
376    "brillo/http/http_connection_fake.cc",
377    "brillo/http/http_transport_fake.cc",
378    "brillo/message_loops/fake_message_loop.cc",
379    "brillo/streams/fake_stream.cc",
380    "brillo/unittest_utils.cc",
381  ]
382  if (use.device_mapper) {
383    sources += [ "brillo/blkdev_utils/device_mapper_fake.cc" ]
384  }
385}
386
387shared_library("libinstallattributes") {
388  configs += [ ":target_defaults" ]
389  deps = [
390    ":libinstallattributes-includes",
391    "../common-mk/external_dependencies:install_attributes-proto",
392  ]
393  all_dependent_pkg_deps = [ "protobuf-lite" ]
394  sources = [ "install_attributes/libinstallattributes.cc" ]
395}
396
397shared_library("libpolicy") {
398  configs += [ ":target_defaults" ]
399  deps = [
400    ":libinstallattributes",
401    ":libpolicy-includes",
402    "../common-mk/external_dependencies:policy-protos",
403  ]
404  all_dependent_pkg_deps = [
405    "openssl",
406    "protobuf-lite",
407  ]
408  ldflags = [ "-Wl,--version-script,${platform2_root}/libbrillo/libpolicy.ver" ]
409  sources = [
410    "policy/device_policy.cc",
411    "policy/device_policy_impl.cc",
412    "policy/libpolicy.cc",
413    "policy/policy_util.cc",
414    "policy/resilient_policy_util.cc",
415  ]
416}
417
418libbrillo_glib_pkg_deps = [
419  "glib-2.0",
420  "gobject-2.0",
421]
422if (use.dbus) {
423  libbrillo_glib_pkg_deps += [
424    "dbus-1",
425    "dbus-glib-1",
426  ]
427}
428
429generate_pkg_config("libbrillo-glib-${libbase_ver}_pc") {
430  name = "libbrillo-glib"
431  output_name = "libbrillo-glib-${libbase_ver}"
432  description = "brillo glib wrapper library"
433  version = libbase_ver
434  requires_private = libbrillo_glib_pkg_deps
435  libs = [ "-lbrillo-glib" ]
436}
437
438generate_pkg_config("libbrillo-glib_pc") {
439  name = "libbrillo-glib"
440  output_name = "libbrillo-glib"
441  description = "brillo glib wrapper library"
442  version = libbase_ver
443  requires_private = libbrillo_glib_pkg_deps
444  libs = [ "-lbrillo-glib" ]
445}
446
447shared_library("libbrillo-glib") {
448  configs += [ ":target_defaults" ]
449  deps = [
450    ":libbrillo",
451    ":libbrillo-glib-${libbase_ver}_pc",
452    ":libbrillo-glib_pc",
453  ]
454  all_dependent_pkg_deps = libbrillo_glib_pkg_deps
455  if (use.dbus) {
456    sources = [
457      "brillo/glib/abstract_dbus_service.cc",
458      "brillo/glib/dbus.cc",
459    ]
460  }
461  cflags = [
462    # glib uses the deprecated "register" attribute in some header files.
463    "-Wno-deprecated-register",
464  ]
465}
466
467if (use.test) {
468  static_library("libbrillo_static") {
469    configs += [ ":target_defaults" ]
470    deps = [
471      ":libbrillo-${libbase_ver}_pc",
472      ":libbrillo_pc",
473      ":libinstallattributes",
474      ":libpolicy",
475    ]
476    foreach(sublib, libbrillo_sublibs) {
477      deps += [ ":lib" + sublib.library_name ]
478    }
479    public_configs = [ ":libbrillo_configs" ]
480  }
481  proto_library("libbrillo_tests_proto") {
482    proto_in_dir = "brillo/dbus"
483    proto_out_dir = "include/brillo/dbus"
484    sources = [ "${proto_in_dir}/test.proto" ]
485  }
486  executable("libbrillo_tests") {
487    configs += [
488      "//common-mk:test",
489      ":target_defaults",
490    ]
491    deps = [
492      ":libbrillo-glib",
493      ":libbrillo-test",
494      ":libbrillo_static",
495      ":libbrillo_tests_proto",
496    ]
497    pkg_deps = [ "libchrome-test-${libbase_ver}" ]
498    cflags = [ "-Wno-format-zero-length" ]
499
500    if (is_debug) {
501      cflags += [
502        "-fprofile-arcs",
503        "-ftest-coverage",
504        "-fno-inline",
505      ]
506      libs = [ "gcov" ]
507    }
508    sources = [
509      "brillo/asynchronous_signal_handler_test.cc",
510      "brillo/backoff_entry_test.cc",
511      "brillo/blkdev_utils/loop_device_test.cc",
512      "brillo/data_encoding_test.cc",
513      "brillo/enum_flags_test.cc",
514      "brillo/errors/error_codes_test.cc",
515      "brillo/errors/error_test.cc",
516      "brillo/file_utils_test.cc",
517      "brillo/files/file_util_test.cc",
518      "brillo/files/safe_fd_test.cc",
519      "brillo/flag_helper_test.cc",
520      "brillo/glib/object_test.cc",
521      "brillo/http/http_connection_curl_test.cc",
522      "brillo/http/http_form_data_test.cc",
523      "brillo/http/http_request_test.cc",
524      "brillo/http/http_transport_curl_test.cc",
525      "brillo/http/http_utils_test.cc",
526      "brillo/key_value_store_test.cc",
527      "brillo/map_utils_test.cc",
528      "brillo/message_loops/base_message_loop_test.cc",
529      "brillo/message_loops/fake_message_loop_test.cc",
530      "brillo/message_loops/message_loop_test.cc",
531      "brillo/mime_utils_test.cc",
532      "brillo/namespaces/mount_namespace_test.cc",
533      "brillo/osrelease_reader_test.cc",
534      "brillo/process_reaper_test.cc",
535      "brillo/process_test.cc",
536      "brillo/scoped_umask_test.cc",
537      "brillo/secure_blob_test.cc",
538      "brillo/streams/fake_stream_test.cc",
539      "brillo/streams/file_stream_test.cc",
540      "brillo/streams/input_stream_set_test.cc",
541      "brillo/streams/memory_containers_test.cc",
542      "brillo/streams/memory_stream_test.cc",
543      "brillo/streams/openssl_stream_bio_test.cc",
544      "brillo/streams/stream_test.cc",
545      "brillo/streams/stream_utils_test.cc",
546      "brillo/strings/string_utils_test.cc",
547      "brillo/timezone/tzif_parser_test.cc",
548      "brillo/unittest_utils.cc",
549      "brillo/url_utils_test.cc",
550      "brillo/value_conversion_test.cc",
551      "testrunner.cc",
552    ]
553    if (use.dbus) {
554      sources += [
555        "brillo/any_internal_impl_test.cc",
556        "brillo/any_test.cc",
557        "brillo/dbus/async_event_sequencer_test.cc",
558        "brillo/dbus/data_serialization_test.cc",
559        "brillo/dbus/dbus_method_invoker_test.cc",
560        "brillo/dbus/dbus_object_test.cc",
561        "brillo/dbus/dbus_param_reader_test.cc",
562        "brillo/dbus/dbus_param_writer_test.cc",
563        "brillo/dbus/dbus_signal_handler_test.cc",
564        "brillo/dbus/exported_object_manager_test.cc",
565        "brillo/dbus/exported_property_set_test.cc",
566        "brillo/http/http_proxy_test.cc",
567        "brillo/type_name_undecorate_test.cc",
568        "brillo/variant_dictionary_test.cc",
569      ]
570    }
571    if (use.device_mapper) {
572      sources += [ "brillo/blkdev_utils/device_mapper_test.cc" ]
573    }
574  }
575
576  executable("libinstallattributes_tests") {
577    configs += [
578      "//common-mk:test",
579      ":target_defaults",
580    ]
581    deps = [
582      ":libinstallattributes",
583      "../common-mk/external_dependencies:install_attributes-proto",
584      "../common-mk/testrunner:testrunner",
585    ]
586    sources = [ "install_attributes/tests/libinstallattributes_test.cc" ]
587  }
588
589  executable("libpolicy_tests") {
590    configs += [
591      "//common-mk:test",
592      ":target_defaults",
593    ]
594    deps = [
595      ":libinstallattributes",
596      ":libpolicy",
597      "../common-mk/external_dependencies:install_attributes-proto",
598      "../common-mk/external_dependencies:policy-protos",
599      "../common-mk/testrunner:testrunner",
600    ]
601    sources = [
602      "install_attributes/mock_install_attributes_reader.cc",
603      "policy/tests/device_policy_impl_test.cc",
604      "policy/tests/libpolicy_test.cc",
605      "policy/tests/policy_util_test.cc",
606      "policy/tests/resilient_policy_util_test.cc",
607    ]
608  }
609}
610
611if (use.fuzzer) {
612  executable("libbrillo_data_encoding_fuzzer") {
613    sources = [ "brillo/data_encoding_fuzzer.cc" ]
614
615    configs += [ "//common-mk/common_fuzzer:common_fuzzer" ]
616
617    pkg_deps = [ "libchrome-${libbase_ver}" ]
618
619    include_dirs = [ "../libbrillo" ]
620
621    deps = [ ":libbrillo-core" ]
622  }
623
624  executable("libbrillo_dbus_data_serialization_fuzzer") {
625    sources = [ "brillo/dbus/data_serialization_fuzzer.cc" ]
626
627    configs += [ "//common-mk/common_fuzzer:common_fuzzer" ]
628
629    pkg_deps = [ "libchrome-${libbase_ver}" ]
630
631    include_dirs = [ "../libbrillo" ]
632
633    deps = [ ":libbrillo-core" ]
634  }
635
636  executable("libbrillo_http_form_data_fuzzer") {
637    sources = [ "brillo/http/http_form_data_fuzzer.cc" ]
638
639    configs += [ "//common-mk/common_fuzzer:common_fuzzer" ]
640
641    pkg_deps = [ "libchrome-${libbase_ver}" ]
642
643    include_dirs = [ "../libbrillo" ]
644
645    deps = [
646      ":libbrillo-http",
647      ":libbrillo-streams",
648    ]
649  }
650}
651
652copy("libinstallattributes-includes") {
653  sources = [ "install_attributes/libinstallattributes.h" ]
654  outputs =
655      [ "${root_gen_dir}/include/install_attributes/{{source_file_part}}" ]
656}
657
658copy("libpolicy-includes") {
659  sources = [
660    "policy/device_policy.h",
661    "policy/device_policy_impl.h",
662    "policy/libpolicy.h",
663    "policy/mock_device_policy.h",
664    "policy/mock_libpolicy.h",
665    "policy/policy_util.h",
666    "policy/resilient_policy_util.h",
667  ]
668  outputs = [ "${root_gen_dir}/include/policy/{{source_file_part}}" ]
669}
670