1# Copyright (C) 2020 The Google Bazel Common 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
15"""A macro to configure Dagger deps within a workspace"""
16
17load("//:build_defs.bzl", "POM_VERSION", "POM_VERSION_ALPHA")
18
19_DAGGER_VERSION = POM_VERSION
20_HILT_VERSION = POM_VERSION_ALPHA
21
22DAGGER_ARTIFACTS = [
23    "com.google.dagger:dagger:" + _DAGGER_VERSION,
24    "com.google.dagger:dagger-compiler:" + _DAGGER_VERSION,
25    "com.google.dagger:dagger-producers:" + _DAGGER_VERSION,
26    "com.google.dagger:dagger-spi:" + _DAGGER_VERSION,
27]
28
29DAGGER_ANDROID_ARTIFACTS = [
30    "com.google.dagger:dagger-android-processor:" + _DAGGER_VERSION,
31    "com.google.dagger:dagger-android-support:" + _DAGGER_VERSION,
32    "com.google.dagger:dagger-android:" + _DAGGER_VERSION,
33]
34
35HILT_ANDROID_ARTIFACTS = [
36    "androidx.test:core:1.1.0",  # Export for ApplicationProvider
37    "javax.annotation:jsr250-api:1.0",  # Export for @Generated
38    "androidx.annotation:annotation:1.1.0",  # Export for @CallSuper/@Nullable
39    "com.google.dagger:dagger:" + _DAGGER_VERSION,
40    "com.google.dagger:dagger-compiler:" + _DAGGER_VERSION,
41    "com.google.dagger:hilt-android:" + _HILT_VERSION,
42    "com.google.dagger:hilt-android-testing:" + _HILT_VERSION,
43    "com.google.dagger:hilt-android-compiler:" + _HILT_VERSION,
44]
45
46DAGGER_REPOSITORIES = [
47    "https://maven.google.com",
48    "https://repo1.maven.org/maven2",
49]
50
51DAGGER_ANDROID_REPOSITORIES = DAGGER_REPOSITORIES
52
53HILT_ANDROID_REPOSITORIES = DAGGER_REPOSITORIES
54
55# https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unnamed-macro
56# buildifier: disable=unnamed-macro
57def dagger_rules(repo_name = "@maven"):
58    """Defines the Dagger targets with proper exported dependencies and plugins.
59
60    The targets will be of the form ":<artifact-id>".
61
62    Args:
63      repo_name: The name of the dependency repository (default is "@maven").
64    """
65    native.java_library(
66        name = "dagger",
67        exported_plugins = [":dagger-compiler"],
68        visibility = ["//visibility:public"],
69        exports = [
70            "%s//:com_google_dagger_dagger" % repo_name,
71            "%s//:javax_inject_javax_inject" % repo_name,
72        ],
73    )
74
75    native.java_plugin(
76        name = "dagger-compiler",
77        generates_api = 1,
78        processor_class = "dagger.internal.codegen.ComponentProcessor",
79        deps = [
80            "%s//:com_google_dagger_dagger_compiler" % repo_name,
81        ],
82    )
83
84    native.java_library(
85        name = "dagger-producers",
86        visibility = ["//visibility:public"],
87        exports = [
88            ":dagger",
89            "%s//:com_google_dagger_dagger_producers" % repo_name,
90        ],
91    )
92
93    native.java_library(
94        name = "dagger-spi",
95        visibility = ["//visibility:public"],
96        exports = [
97            "%s//:com_google_dagger_dagger_spi" % repo_name,
98        ],
99    )
100
101# https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unnamed-macro
102# buildifier: disable=unnamed-macro
103def dagger_android_rules(repo_name = "@maven"):
104    """Defines the Dagger Android targets with proper exported dependencies and plugins.
105
106    The targets will be of the form ":<artifact-id>".
107
108    Args:
109      repo_name: The name of the dependency repository (default is "@maven").
110    """
111
112    # https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#native-android
113    # buildifier: disable=native-android
114    native.android_library(
115        name = "dagger-android",
116        exported_plugins = [":dagger-android-processor"],
117        visibility = ["//visibility:public"],
118        exports = [
119            "%s//:com_google_dagger_dagger_android" % repo_name,
120        ],
121    )
122
123    # https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#native-android
124    # buildifier: disable=native-android
125    native.android_library(
126        name = "dagger-android-support",
127        exported_plugins = [":dagger-android-processor"],
128        visibility = ["//visibility:public"],
129        exports = [
130            ":dagger-android",
131            "%s//:com_google_dagger_dagger_android_support" % repo_name,
132        ],
133    )
134
135    native.java_plugin(
136        name = "dagger-android-processor",
137        generates_api = 1,
138        processor_class = "dagger.android.processor.AndroidProcessor",
139        deps = [
140            "%s//:com_google_dagger_dagger_android_processor" % repo_name,
141        ],
142    )
143
144# https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unnamed-macro
145# buildifier: disable=unnamed-macro
146def hilt_android_rules(repo_name = "@maven"):
147    """Defines the Hilt Android targets with proper exported dependencies and plugins.
148
149    The targets will be of the form ":<artifact-id>".
150
151    Args:
152      repo_name: The name of the dependency repository (default is "@maven").
153    """
154
155    # https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#native-android
156    # buildifier: disable=native-android
157    native.android_library(
158        name = "hilt-android",
159        exported_plugins = [
160            ":hilt_dagger_compiler",
161            ":hilt_android_entry_point_processor",
162            ":hilt_aggregated_deps_processor",
163            ":hilt_alias_of_processor",
164            ":hilt_define_component_processor",
165            ":hilt_generates_root_input_processor",
166            ":hilt_originating_element_processor",
167            ":hilt_root_processor",
168            ":hilt_view_model_processor",
169        ],
170        visibility = ["//visibility:public"],
171        exports = [
172            "%s//:com_google_dagger_dagger" % repo_name,  # For Dagger APIs
173            "%s//:javax_inject_javax_inject" % repo_name,  # For @Inject
174            "%s//:androidx_annotation_annotation" % repo_name,  # For @CallSuper
175            "%s//:com_google_dagger_hilt_android" % repo_name,
176            "%s//:javax_annotation_jsr250_api" % repo_name,  # For @Generated
177        ],
178    )
179
180    # This target is same as dagger-compiler, but we're redefining it here
181    # so that users don't have to call dagger_rules() first.
182    native.java_plugin(
183        name = "hilt_dagger_compiler",
184        generates_api = 1,
185        processor_class = "dagger.internal.codegen.ComponentProcessor",
186        deps = [
187            "%s//:com_google_dagger_dagger_compiler" % repo_name,
188        ],
189    )
190
191    native.java_plugin(
192        name = "hilt_android_entry_point_processor",
193        generates_api = 1,
194        processor_class = "dagger.hilt.android.processor.internal.androidentrypoint.AndroidEntryPointProcessor",
195        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
196    )
197
198    native.java_plugin(
199        name = "hilt_aggregated_deps_processor",
200        generates_api = 1,
201        processor_class = "dagger.hilt.processor.internal.aggregateddeps.AggregatedDepsProcessor",
202        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
203    )
204
205    native.java_plugin(
206        name = "hilt_alias_of_processor",
207        generates_api = 1,
208        processor_class = "dagger.hilt.processor.internal.aliasof.AliasOfProcessor",
209        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
210    )
211
212    native.java_plugin(
213        name = "hilt_define_component_processor",
214        generates_api = 1,
215        processor_class = "dagger.hilt.processor.internal.definecomponent.DefineComponentProcessor",
216        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
217    )
218
219    native.java_plugin(
220        name = "hilt_generates_root_input_processor",
221        generates_api = 1,
222        processor_class = "dagger.hilt.processor.internal.generatesrootinput.GeneratesRootInputProcessor",
223        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
224    )
225
226    native.java_plugin(
227        name = "hilt_originating_element_processor",
228        generates_api = 1,
229        processor_class = "dagger.hilt.processor.internal.originatingelement.OriginatingElementProcessor",
230        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
231    )
232
233    native.java_plugin(
234        name = "hilt_root_processor",
235        generates_api = 1,
236        processor_class = "dagger.hilt.processor.internal.root.RootProcessor",
237        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
238    )
239
240    native.java_plugin(
241        name = "hilt_view_model_processor",
242        generates_api = 1,
243        processor_class = "dagger.hilt.android.processor.internal.viewmodel.ViewModelProcessor",
244        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
245    )
246
247    # https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#native-android
248    # buildifier: disable=native-android
249    native.android_library(
250        name = "hilt-android-testing",
251        testonly = 1,
252        exported_plugins = [
253            ":hilt_bind_value_processor",
254            ":hilt_custom_test_application_processor",
255            ":hilt_testroot_processor",
256            ":hilt_uninstall_modules_processor",
257        ],
258        visibility = ["//visibility:public"],
259        exports = [
260            ":hilt-android",
261            "%s//:androidx_test_core" % repo_name,  # For ApplicationProvider
262            "%s//:com_google_dagger_hilt_android_testing" % repo_name,
263        ],
264    )
265
266    native.java_plugin(
267        name = "hilt_bind_value_processor",
268        generates_api = 1,
269        processor_class = "dagger.hilt.android.processor.internal.bindvalue.BindValueProcessor",
270        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
271    )
272
273    native.java_plugin(
274        name = "hilt_custom_test_application_processor",
275        generates_api = 1,
276        processor_class = "dagger.hilt.android.processor.internal.customtestapplication.CustomTestApplicationProcessor",
277        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
278    )
279
280    native.java_plugin(
281        name = "hilt_testroot_processor",
282        generates_api = 1,
283        processor_class = "dagger.hilt.android.processor.internal.testroot.TestRootProcessor",
284        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
285    )
286
287    native.java_plugin(
288        name = "hilt_uninstall_modules_processor",
289        generates_api = 1,
290        processor_class = "dagger.hilt.android.processor.internal.uninstallmodules.UninstallModulesProcessor",
291        deps = ["%s//:com_google_dagger_hilt_android_compiler" % repo_name],
292    )
293