Skip to content

Commit 7ea4728

Browse files
committed
more fixes
1 parent 718610f commit 7ea4728

11 files changed

Lines changed: 617 additions & 53 deletions

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module(
55
compatibility_level = 1,
66
)
77

8+
bazel_dep(name = "rules_cc", version = "0.2.8")
89
bazel_dep(name = "platforms", version = "0.0.10")
910
bazel_dep(name = "bazel_features", version = "1.20.0")
1011
bazel_dep(name = "bazel_skylib", version = "1.5.0")

apt/extensions.bzl

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,27 @@ def _parse_source(src):
2727
components = components,
2828
)
2929

30-
def _distroless_extension(module_ctx):
30+
def _distroless_extension(mctx):
3131
root_direct_deps = []
3232
root_direct_dev_deps = []
3333
reproducible = False
3434

35-
for mod in module_ctx.modules:
36-
deb_repo = deb_repository.new(module_ctx)
35+
for mod in mctx.modules:
36+
37+
lockf = lockfile.empty(mctx)
38+
39+
# if mod.is_root:
40+
41+
if len(mod.tags.lock):
42+
lock = mod.tags.lock[0]
43+
lockf = lockfile.from_json(mctx, mctx.read(lock.into))
44+
45+
deb_repo = deb_repository.new(mctx, lockf.facts())
3746
resolver = dependency_resolver.new(deb_repo)
38-
lockf = lockfile.empty(module_ctx)
47+
3948

4049
for sl in mod.tags.sources_list:
50+
continue
4151
uris = [uri.removeprefix("mirror+") for uri in sl.uris]
4252
architectures = sl.architectures
4353

@@ -58,6 +68,7 @@ def _distroless_extension(module_ctx):
5868
sources = lockf.sources()
5969
dependency_sets = lockf.dependency_sets()
6070
for install in mod.tags.install:
71+
continue
6172
dependency_set = dependency_sets.setdefault(install.dependency_set, {
6273
"sets": {},
6374
})
@@ -77,8 +88,8 @@ def _distroless_extension(module_ctx):
7788
arch = architectures.pop()
7889
resolved_count = 0
7990

80-
module_ctx.report_progress("Resolving %s:%s" % (dep_constraint, arch))
81-
(package, dependencies, unmet_dependencies) = resolver.resolve_all(
91+
mctx.report_progress("Resolving %s:%s" % (dep_constraint, arch))
92+
(package, dependencies, unmet_dependencies, warnings) = resolver.resolve_all(
8293
name = constraint["name"],
8394
version = constraint["version"],
8495
arch = arch,
@@ -93,9 +104,12 @@ def _distroless_extension(module_ctx):
93104
" 3 - Ensure that an apt.source_list added for the specified architecture.",
94105
)
95106

107+
for warning in warnings:
108+
util.warning(mctx, warning)
109+
110+
96111
if len(unmet_dependencies):
97-
# buildifier: disable=print
98-
util.warning(module_ctx, "Following dependencies could not be resolved for %s: %s" % (constraint["name"], ",".join([up[0] for up in unmet_dependencies])))
112+
util.warning(mctx, "Following dependencies could not be resolved for %s: %s" % (constraint["name"], ",".join([up[0] for up in unmet_dependencies])))
99113

100114
lockf.add_package(package)
101115

@@ -110,15 +124,15 @@ def _distroless_extension(module_ctx):
110124
arch_set[lockfile.short_package_key(package)] = package["Version"]
111125

112126
# For cases where architecture for the package is not specified we need
113-
# to first find out which source contains the package. and in order to do
127+
# to first find out which source contains the package. in order to do
114128
# that we first need to resolve the package for amd64 architecture.
115129
# Once the repository is found, then resolve the package for all the
116130
# architectures the repository supports.
117131
if not constraint["arch"] and arch == "amd64":
118132
source = sources[package["Dist"]]
119133
architectures = [a for a in source["architectures"] if a != "amd64"]
120134

121-
module_ctx.report_progress("Resolved %d packages for %s" % (resolved_count, arch))
135+
mctx.report_progress("Resolved %d packages for %s" % (resolved_count, arch))
122136

123137
# Generate a hub repo for every dependency set
124138
lock_content = lockf.as_json()
@@ -131,23 +145,36 @@ def _distroless_extension(module_ctx):
131145

132146
# Generate a repo per package which will be aliased by hub repo.
133147
for (package_key, package) in lockf.packages().items():
148+
# dependent_packages = None
149+
# if package["name"].endswith("-dev")
150+
# packages = lockf.packages()
151+
# dependent_packages = json.encode([
152+
153+
# ])
154+
134155
deb_import(
135156
name = util.sanitize(package_key),
157+
target_name = util.sanitize(package_key),
136158
urls = [
137159
uri + "/" + package["filename"]
138160
for uri in sources[package["suite"]]["uris"]
139161
],
140162
sha256 = package["sha256"],
141163
mergedusr = False,
142-
depends_on = ["@" + util.sanitize(dep_key) for dep_key in package["depends_on"]],
164+
depends_on = package["depends_on"],
165+
package_name = package["name"]
143166
)
144167

145-
lock_tmp = module_ctx.path("apt.lock.json")
146-
lockf.write(lock_tmp)
147-
lockf_wksp = module_ctx.path(Label("@//:apt.lock.json"))
148-
module_ctx.execute(
149-
["mv", lock_tmp, lockf_wksp],
150-
)
168+
169+
170+
171+
for lock in mod.tags.lock:
172+
lock_tmp = mctx.path("apt.lock.json")
173+
lockf.write(lock_tmp)
174+
lockf_wksp = mctx.path(lock.into)
175+
mctx.execute(
176+
["cp", "-f", lock_tmp, lockf_wksp],
177+
)
151178

152179
_doc = """
153180
Module extension to create Debian repositories.
@@ -261,11 +288,21 @@ install = tag_class(
261288
},
262289
)
263290

291+
292+
lock = tag_class(
293+
attrs = {
294+
"into": attr.label(
295+
mandatory = True,
296+
),
297+
},
298+
)
299+
264300
apt = module_extension(
265301
doc = _doc,
266302
implementation = _distroless_extension,
267303
tag_classes = {
268304
"install": install,
269305
"sources_list": sources_list,
306+
"lock": lock,
270307
},
271308
)

apt/private/apt_cursed_symlink.bzl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
load("@bazel_skylib//lib:paths.bzl", "paths")
2+
3+
def _apt_cursed_symlink(ctx):
4+
own_path = ctx.attr.own_path.removeprefix(".")
5+
own_dirname = own_path[:own_path.rfind("/")+1]
6+
candidate_full_path = own_dirname + ctx.attr.candidate_path
7+
8+
found = None
9+
10+
for file in ctx.files.candidates:
11+
if file.path.endswith(candidate_full_path):
12+
found = file
13+
break
14+
15+
if not found:
16+
fail("Failed to find the candidate so library. file an issue.")
17+
18+
ctx.actions.symlink(
19+
output = ctx.outputs.out,
20+
target_file = file
21+
)
22+
return DefaultInfo(
23+
files = depset([ctx.outputs.out])
24+
)
25+
26+
apt_cursed_symlink = rule(
27+
implementation = _apt_cursed_symlink,
28+
attrs = {
29+
"candidates": attr.label_list(),
30+
"candidate_path": attr.string(),
31+
"own_path": attr.string(),
32+
"out": attr.output()
33+
}
34+
)

apt/private/apt_deb_repository.bzl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def _get_auth(ctx, urls):
1212
netrc = read_user_netrc(ctx)
1313
return use_netrc(netrc, urls, {})
1414

15-
def _fetch_package_index(rctx, urls, dist, comp, arch, integrity):
15+
def _fetch_package_index(mctx, urls, dist, comp, arch, integrity):
1616
target_triple = "{dist}/{comp}/{arch}".format(dist = dist, comp = comp, arch = arch)
1717

1818
# See https://linux.die.net/man/1/xz , https://linux.die.net/man/1/gzip , and https://linux.die.net/man/1/bzip2
@@ -30,7 +30,7 @@ def _fetch_package_index(rctx, urls, dist, comp, arch, integrity):
3030
failed_attempts = []
3131

3232
url = None
33-
base_auth = _get_auth(rctx, urls)
33+
base_auth = _get_auth(mctx, urls)
3434
for url in urls:
3535
download = None
3636
for (ext, cmd) in supported_extensions:
@@ -39,7 +39,7 @@ def _fetch_package_index(rctx, urls, dist, comp, arch, integrity):
3939
auth = {}
4040
if url in base_auth:
4141
auth = {dist_url: base_auth[url]}
42-
download = rctx.download(
42+
download = mctx.download(
4343
url = dist_url,
4444
output = output,
4545
integrity = integrity,
@@ -163,6 +163,7 @@ def _package(state, name, version, arch):
163163

164164
def _fetch_and_parse_sources(state):
165165
mctx = state.mctx
166+
facts = state.facts
166167
for source in state.sources:
167168
(urls, dist, components, architectures) = source
168169

@@ -175,19 +176,25 @@ def _fetch_and_parse_sources(state):
175176
# on misconfigured HTTP servers)
176177
urls = [url.rstrip("/") for url in urls]
177178

179+
fact_key = dist + "/" + comp + "/" + arch
180+
fact_value = facts.get(fact_key, "")
181+
178182
# TODO: make parallel
179183
mctx.report_progress("Fetching package index: {}/{} for {}".format(dist, comp, arch))
180-
(output, _, _) = _fetch_package_index(mctx, urls, dist, comp, arch, "")
184+
(output, url, integrity) = _fetch_package_index(mctx, urls, dist, comp, arch, fact_value)
185+
186+
facts[fact_key] = integrity
181187

182188
mctx.report_progress("Parsing package index: {}/{} for {}".format(dist, comp, arch))
183189
_parse_repository(state, mctx.read(output), urls, dist)
184190

185-
def _create(mctx):
191+
def _create(mctx, facts):
186192
state = struct(
187193
mctx = mctx,
188194
sources = list(),
189195
packages = dict(),
190196
virtual_packages = dict(),
197+
facts = facts
191198
)
192199

193200
return struct(

apt/private/apt_dep_resolver.bzl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def _resolve_package(state, name, version, arch):
1717
)
1818
]
1919

20+
warning = None
21+
2022
if len(candidates) == 1:
2123
return candidates[0]
2224

@@ -36,11 +38,10 @@ def _resolve_package(state, name, version, arch):
3638

3739
# Otherwise, we can't disambiguate the virtual package providers so
3840
# choose none and warn.
39-
# buildifier: disable=print
40-
print("\nMultiple candidates for virtual package '{}': {}".format(
41+
warning = "Multiple candidates for virtual package '{}': {}".format(
4142
name,
42-
[package["Package"] for package in candidates],
43-
))
43+
", ".join([package["Package"] for package in candidates]),
44+
)
4445

4546
# Get available versions of the package
4647
versions_by_arch = state.repository.package_versions(name = name, arch = arch)
@@ -68,7 +69,7 @@ def _resolve_package(state, name, version, arch):
6869
if not package:
6970
package = state.repository.package(name = name, version = selected_version, arch = "all")
7071

71-
return package
72+
return (package, warning)
7273

7374
_ITERATION_MAX_ = 2147483646
7475

@@ -85,6 +86,8 @@ def _resolve_all(state, name, version, arch, include_transitive = True):
8586
dependency_group = []
8687
stack = [(name, version, -1)]
8788

89+
warnings = []
90+
8891
for i in range(0, _ITERATION_MAX_ + 1):
8992
if not len(stack):
9093
break
@@ -98,7 +101,9 @@ def _resolve_all(state, name, version, arch, include_transitive = True):
98101
continue
99102

100103
# TODO: only resolve in specified suites
101-
package = _resolve_package(state, name, version, arch)
104+
(package, warning) = _resolve_package(state, name, version, arch)
105+
if warning:
106+
warnings.append(warning)
102107

103108
# If this package is not found and is part of a dependency group, then just skip it.
104109
if not package and dependency_group_idx > -1:
@@ -158,7 +163,7 @@ def _resolve_all(state, name, version, arch, include_transitive = True):
158163
if not met:
159164
unmet_dependencies.append((dep, None))
160165

161-
return (root_package, dependencies, unmet_dependencies)
166+
return (root_package, dependencies, unmet_dependencies, warnings)
162167

163168
def _create_resolution(repository):
164169
state = struct(repository = repository)

apt/private/deb_cc_export.bzl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"normalization rules"
2+
3+
# buildifier: disable=function-docstring-args
4+
def deb_cc_export(name, src, outs, **kwargs):
5+
"""Private. DO NOT USE."""
6+
if len(outs) == 0:
7+
native.filegroup(name = name, srcs = [], **kwargs)
8+
return
9+
toolchains = ["@bsd_tar_toolchains//:resolved_toolchain"]
10+
11+
cmd = """
12+
$(BSDTAR_BIN) -xf "$<" -C $(RULEDIR) {} \
13+
""".format(
14+
" ".join(outs)
15+
)
16+
native.genrule(
17+
name = name,
18+
srcs = [src],
19+
outs = [out.removeprefix("./") for out in outs],
20+
cmd = cmd,
21+
toolchains = toolchains,
22+
output_to_bindir = True,
23+
**kwargs
24+
)

0 commit comments

Comments
 (0)