blob: 8a69794e7729fad7b5a55c2f5475f658cdf6b1e2 [file] [log] [blame]
Corentin Wallezabc753c2019-03-06 23:17:39 +00001# Copyright 2019 The Dawn Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Corentin Wallez67314642019-11-28 09:40:54 +000015import("//build_overrides/build.gni")
Corentin Wallezabc753c2019-03-06 23:17:39 +000016import("dawn_features.gni")
Corentin Wallez67314642019-11-28 09:40:54 +000017import("dawn_overrides_with_defaults.gni")
Corentin Wallezabc753c2019-03-06 23:17:39 +000018
19###############################################################################
20# Template to produce a component for one of Dawn's libraries.
21###############################################################################
22
23# Template that produces static and shared versions of the same library as well
24# as a target similar to Chromium's component targets.
25# - The shared version exports symbols and has dependent import the symbols
Ben Claytonb01cf602022-02-04 17:53:55 +000026# as libdawn_${name}.so. If the target name matches the package directory
27# name, then the shared library target will be named 'shared', otherwise
Ben Clayton7d5badd2022-02-04 12:51:25 +000028# '${target_name}_shared'.
29# - The static library doesn't export symbols nor make dependents import them.
30# If the target name matches the package directory name, then the static
31# library target will be named 'static', otherwise '${target_name}_static'.
Corentin Wallezabc753c2019-03-06 23:17:39 +000032# - The libname target is similar to a Chromium component and is an alias for
33# either the static or the shared library.
34#
35# The DEFINE_PREFIX must be provided and must match the respective "_export.h"
36# file.
37#
38# Example usage:
39#
40# dawn_component("my_library") {
41# // my_library_export.h must use the MY_LIBRARY_IMPLEMENTATION and
42# // MY_LIBRARY_SHARED_LIBRARY macros.
43# DEFINE_PREFIX = "MY_LIBRARY"
44#
45# sources = [...]
46# deps = [...]
47# configs = [...]
48# }
49#
50# executable("foo") {
51# deps = [ ":my_library_shared" ] // or :my_library for the same effect
52# }
53template("dawn_component") {
54 # Copy the target_name in the local scope so it doesn't get shadowed in the
55 # definition of targets.
Corentin Wallez6574f922020-04-09 17:31:40 +000056 name = target_name
Corentin Wallezabc753c2019-03-06 23:17:39 +000057
Ben Clayton7d5badd2022-02-04 12:51:25 +000058 prefix = "${name}_"
59
60 # Remove prefix if the target name matches directory
61 if (get_label_info(get_label_info(":$target_name", "dir"), "name") == name) {
62 prefix = ""
63 }
64
Corentin Wallezabc753c2019-03-06 23:17:39 +000065 # The config that will apply to dependents of the shared library so they know
66 # they should "import" the symbols
Ben Clayton7d5badd2022-02-04 12:51:25 +000067 config("${prefix}shared_public_config") {
Corentin Wallezabc753c2019-03-06 23:17:39 +000068 defines = [ "${invoker.DEFINE_PREFIX}_SHARED_LIBRARY" ]
69
70 # Executable needs an rpath to find our shared libraries on OSX and Linux
71 if (is_mac) {
72 ldflags = [
73 "-rpath",
74 "@executable_path/",
75 ]
76 }
Hidehiko Abe948b3a02020-09-11 02:24:16 +000077 if ((is_linux || is_chromeos) && dawn_has_build) {
Corentin Wallezabc753c2019-03-06 23:17:39 +000078 configs = [ "//build/config/gcc:rpath_for_built_shared_libraries" ]
79 }
80 }
81
Ben Clayton7d5badd2022-02-04 12:51:25 +000082 shared_library("${prefix}shared") {
Corentin Wallez6574f922020-04-09 17:31:40 +000083 # The "tool" for creating shared libraries will automatically add the "lib" prefix
Ben Claytonb01cf602022-02-04 17:53:55 +000084 output_name = "dawn_${name}"
Corentin Wallezabc753c2019-03-06 23:17:39 +000085
86 # Copy all variables except "configs", which has a default value
87 forward_variables_from(invoker, "*", [ "configs" ])
88 if (defined(invoker.configs)) {
89 configs += invoker.configs
90 }
91
92 # Tell dependents where to find this shared library
93 if (is_mac) {
94 ldflags = [
95 "-install_name",
Corentin Wallez6574f922020-04-09 17:31:40 +000096 "@rpath/lib${name}.dylib",
Corentin Wallezabc753c2019-03-06 23:17:39 +000097 ]
98 }
99
100 # Use the config that makes the ${DEFINE_PREFIX}_EXPORT macro do something
101 if (!defined(public_configs)) {
102 public_configs = []
103 }
Ben Clayton7d5badd2022-02-04 12:51:25 +0000104 public_configs += [ ":${prefix}shared_public_config" ]
Corentin Wallezabc753c2019-03-06 23:17:39 +0000105
106 # Tell sources of this library to export the symbols (and not import)
107 if (!defined(defines)) {
108 defines = []
109 }
110 defines += [ "${invoker.DEFINE_PREFIX}_IMPLEMENTATION" ]
Corentin Wallez67314642019-11-28 09:40:54 +0000111
112 # Chromium adds a config that uses a special linker script that removes
113 # all symbols except JNI ones. Remove this config so that our
114 # shared_library symbols are visible. This matches what Chromium's
115 # component template does.
116 if (build_with_chromium && is_android) {
117 configs -= [ "//build/config/android:hide_all_but_jni_onload" ]
118 }
Corentin Wallezabc753c2019-03-06 23:17:39 +0000119 }
120
Ben Clayton7d5badd2022-02-04 12:51:25 +0000121 static_library("${prefix}static") {
Ben Claytonb01cf602022-02-04 17:53:55 +0000122 output_name = "dawn_${name}_static"
Corentin Wallezabc753c2019-03-06 23:17:39 +0000123
124 complete_static_lib = dawn_complete_static_libs
125
126 # Copy all variables except "configs", which has a default value
127 forward_variables_from(invoker, "*", [ "configs" ])
128 if (defined(invoker.configs)) {
129 configs += invoker.configs
130 }
Corentin Wallezabc753c2019-03-06 23:17:39 +0000131 }
132
Corentin Wallez6574f922020-04-09 17:31:40 +0000133 group(name) {
Corentin Wallezabc753c2019-03-06 23:17:39 +0000134 if (is_component_build) {
Ben Clayton7d5badd2022-02-04 12:51:25 +0000135 public_deps = [ ":${prefix}shared" ]
Corentin Wallezabc753c2019-03-06 23:17:39 +0000136 } else {
Ben Clayton7d5badd2022-02-04 12:51:25 +0000137 public_deps = [ ":${prefix}static" ]
Corentin Wallezabc753c2019-03-06 23:17:39 +0000138 }
139 }
140}