blob: ffbfcc4f99ea843351747911bc1c1d8bd88626a8 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001# Copyright 2019 The Dawn & Tint Authors
Corentin Wallezabc753c2019-03-06 23:17:39 +00002#
Austin Engcc2516a2023-10-17 20:57:54 +00003# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are met:
Corentin Wallezabc753c2019-03-06 23:17:39 +00005#
Austin Engcc2516a2023-10-17 20:57:54 +00006# 1. Redistributions of source code must retain the above copyright notice, this
7# list of conditions and the following disclaimer.
Corentin Wallezabc753c2019-03-06 23:17:39 +00008#
Austin Engcc2516a2023-10-17 20:57:54 +00009# 2. Redistributions in binary form must reproduce the above copyright notice,
10# this list of conditions and the following disclaimer in the documentation
11# and/or other materials provided with the distribution.
12#
13# 3. Neither the name of the copyright holder nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Corentin Wallezabc753c2019-03-06 23:17:39 +000027
Corentin Wallez67314642019-11-28 09:40:54 +000028import("//build_overrides/build.gni")
Corentin Wallezabc753c2019-03-06 23:17:39 +000029import("dawn_features.gni")
Corentin Wallez67314642019-11-28 09:40:54 +000030import("dawn_overrides_with_defaults.gni")
Corentin Wallezabc753c2019-03-06 23:17:39 +000031
32###############################################################################
33# Template to produce a component for one of Dawn's libraries.
34###############################################################################
35
36# Template that produces static and shared versions of the same library as well
37# as a target similar to Chromium's component targets.
38# - The shared version exports symbols and has dependent import the symbols
Ben Claytonb01cf602022-02-04 17:53:55 +000039# as libdawn_${name}.so. If the target name matches the package directory
40# name, then the shared library target will be named 'shared', otherwise
Ben Clayton7d5badd2022-02-04 12:51:25 +000041# '${target_name}_shared'.
42# - The static library doesn't export symbols nor make dependents import them.
43# If the target name matches the package directory name, then the static
44# library target will be named 'static', otherwise '${target_name}_static'.
Corentin Wallezabc753c2019-03-06 23:17:39 +000045# - The libname target is similar to a Chromium component and is an alias for
46# either the static or the shared library.
47#
48# The DEFINE_PREFIX must be provided and must match the respective "_export.h"
49# file.
50#
51# Example usage:
52#
53# dawn_component("my_library") {
54# // my_library_export.h must use the MY_LIBRARY_IMPLEMENTATION and
55# // MY_LIBRARY_SHARED_LIBRARY macros.
56# DEFINE_PREFIX = "MY_LIBRARY"
57#
58# sources = [...]
59# deps = [...]
60# configs = [...]
61# }
62#
63# executable("foo") {
64# deps = [ ":my_library_shared" ] // or :my_library for the same effect
65# }
66template("dawn_component") {
67 # Copy the target_name in the local scope so it doesn't get shadowed in the
68 # definition of targets.
Corentin Wallez6574f922020-04-09 17:31:40 +000069 name = target_name
Corentin Wallezabc753c2019-03-06 23:17:39 +000070
Ben Clayton7d5badd2022-02-04 12:51:25 +000071 prefix = "${name}_"
72
73 # Remove prefix if the target name matches directory
74 if (get_label_info(get_label_info(":$target_name", "dir"), "name") == name) {
75 prefix = ""
76 }
77
Corentin Wallezabc753c2019-03-06 23:17:39 +000078 # The config that will apply to dependents of the shared library so they know
79 # they should "import" the symbols
Ben Clayton7d5badd2022-02-04 12:51:25 +000080 config("${prefix}shared_public_config") {
Corentin Wallezabc753c2019-03-06 23:17:39 +000081 defines = [ "${invoker.DEFINE_PREFIX}_SHARED_LIBRARY" ]
82
83 # Executable needs an rpath to find our shared libraries on OSX and Linux
84 if (is_mac) {
85 ldflags = [
86 "-rpath",
87 "@executable_path/",
88 ]
89 }
Hidehiko Abe948b3a02020-09-11 02:24:16 +000090 if ((is_linux || is_chromeos) && dawn_has_build) {
Corentin Wallezabc753c2019-03-06 23:17:39 +000091 configs = [ "//build/config/gcc:rpath_for_built_shared_libraries" ]
92 }
93 }
94
Ben Clayton7d5badd2022-02-04 12:51:25 +000095 shared_library("${prefix}shared") {
Corentin Wallez6574f922020-04-09 17:31:40 +000096 # The "tool" for creating shared libraries will automatically add the "lib" prefix
Ben Claytonb01cf602022-02-04 17:53:55 +000097 output_name = "dawn_${name}"
Corentin Wallezabc753c2019-03-06 23:17:39 +000098
99 # Copy all variables except "configs", which has a default value
100 forward_variables_from(invoker, "*", [ "configs" ])
101 if (defined(invoker.configs)) {
102 configs += invoker.configs
103 }
104
Avi Drissmanffd86812023-07-20 18:49:05 +0000105 # If a "build with ARC" config is present, remove it.
Avi Drissmanffd86812023-07-20 18:49:05 +0000106 if (filter_include(configs, [ "//build/config/compiler:enable_arc" ]) !=
107 []) {
108 configs -= [ "//build/config/compiler:enable_arc" ]
109 }
Avi Drissmanffd86812023-07-20 18:49:05 +0000110
Corentin Wallezabc753c2019-03-06 23:17:39 +0000111 # Tell dependents where to find this shared library
112 if (is_mac) {
113 ldflags = [
114 "-install_name",
Corentin Wallez6574f922020-04-09 17:31:40 +0000115 "@rpath/lib${name}.dylib",
Corentin Wallezabc753c2019-03-06 23:17:39 +0000116 ]
117 }
118
119 # Use the config that makes the ${DEFINE_PREFIX}_EXPORT macro do something
120 if (!defined(public_configs)) {
121 public_configs = []
122 }
Ben Clayton7d5badd2022-02-04 12:51:25 +0000123 public_configs += [ ":${prefix}shared_public_config" ]
Corentin Wallezabc753c2019-03-06 23:17:39 +0000124
125 # Tell sources of this library to export the symbols (and not import)
126 if (!defined(defines)) {
127 defines = []
128 }
129 defines += [ "${invoker.DEFINE_PREFIX}_IMPLEMENTATION" ]
Corentin Wallez67314642019-11-28 09:40:54 +0000130
131 # Chromium adds a config that uses a special linker script that removes
132 # all symbols except JNI ones. Remove this config so that our
133 # shared_library symbols are visible. This matches what Chromium's
134 # component template does.
135 if (build_with_chromium && is_android) {
136 configs -= [ "//build/config/android:hide_all_but_jni_onload" ]
137 }
Corentin Wallezabc753c2019-03-06 23:17:39 +0000138 }
139
Sunny Sachanandanib8e5a6a2023-11-15 16:20:13 +0000140 if (dawn_complete_static_libs) {
141 # Use static_library if explicitly requested - this works even if there are
142 # no sources in the target by linking in sources from all dependencies.
143 _static_target_type = "static_library"
144 } else if (!defined(invoker.sources) || invoker.sources == []) {
145 # When there are no sources defined, use a source set to avoid creating
146 # an empty static library (which generally don't work).
147 _static_target_type = "source_set"
148 } else {
149 # Use static libraries for the static build rather than source sets because
150 # many of of our test binaries link many large dependencies but often don't
151 # use large portions of them. The static libraries are much more efficient
152 # in this situation since only the necessary object files are linked.
153 _static_target_type = "static_library"
154 }
155
156 target(_static_target_type, "${prefix}static") {
Ben Claytonb01cf602022-02-04 17:53:55 +0000157 output_name = "dawn_${name}_static"
Corentin Wallezabc753c2019-03-06 23:17:39 +0000158
Sunny Sachanandanib8e5a6a2023-11-15 16:20:13 +0000159 if (dawn_complete_static_libs) {
160 assert(_static_target_type == "static_library")
161 complete_static_lib = true
162 }
Corentin Wallezabc753c2019-03-06 23:17:39 +0000163
164 # Copy all variables except "configs", which has a default value
165 forward_variables_from(invoker, "*", [ "configs" ])
166 if (defined(invoker.configs)) {
167 configs += invoker.configs
168 }
Avi Drissmanffd86812023-07-20 18:49:05 +0000169
170 # If a "build with ARC" config is present, remove it.
Avi Drissmanffd86812023-07-20 18:49:05 +0000171 if (filter_include(configs, [ "//build/config/compiler:enable_arc" ]) !=
172 []) {
173 configs -= [ "//build/config/compiler:enable_arc" ]
174 }
Corentin Wallezabc753c2019-03-06 23:17:39 +0000175 }
176
Corentin Wallez6574f922020-04-09 17:31:40 +0000177 group(name) {
Corentin Wallezabc753c2019-03-06 23:17:39 +0000178 if (is_component_build) {
Ben Clayton7d5badd2022-02-04 12:51:25 +0000179 public_deps = [ ":${prefix}shared" ]
Corentin Wallezabc753c2019-03-06 23:17:39 +0000180 } else {
Ben Clayton7d5badd2022-02-04 12:51:25 +0000181 public_deps = [ ":${prefix}static" ]
Corentin Wallezabc753c2019-03-06 23:17:39 +0000182 }
183 }
184}