1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import, print_function
6
7import hashlib
8import json
9import os
10
11from mozpack.files import FileFinder
12import mozpack.path as mozpath
13
14
15def sha512_digest(data):
16    """
17    Generate the SHA-512 digest of `data` and return it as a hex string.
18    """
19    return hashlib.sha512(data).hexdigest()
20
21
22def get_filename_with_digest(name, contents):
23    """
24    Return the filename that will be used to store the generated file
25    in the S3 bucket, consisting of the SHA-512 digest of `contents`
26    joined with the relative path `name`.
27    """
28    digest = sha512_digest(contents)
29    return mozpath.join(digest, name)
30
31
32def get_generated_sources():
33    """
34    Yield tuples of `(objdir-rel-path, file)` for generated source files
35    in this objdir, where `file` is either an absolute path to the file or
36    a `mozpack.File` instance.
37    """
38    import buildconfig
39
40    # First, get the list of generated sources produced by the build backend.
41    gen_sources = os.path.join(buildconfig.topobjdir, "generated-sources.json")
42    with open(gen_sources, "r") as f:
43        data = json.load(f)
44    for f in data["sources"]:
45        # Exclute symverscript
46        if mozpath.basename(f) != "symverscript":
47            yield f, mozpath.join(buildconfig.topobjdir, f)
48    # Next, return all the files in $objdir/ipc/ipdl/_ipdlheaders.
49    base = "ipc/ipdl/_ipdlheaders"
50    finder = FileFinder(mozpath.join(buildconfig.topobjdir, base))
51    for p, f in finder.find("**/*.h"):
52        yield mozpath.join(base, p), f
53    # Next, return any source files that were generated into the Rust
54    # object directory.
55    rust_build_kind = "debug" if buildconfig.substs.get("MOZ_DEBUG_RUST") else "release"
56    base = mozpath.join(buildconfig.substs["RUST_TARGET"], rust_build_kind, "build")
57    finder = FileFinder(mozpath.join(buildconfig.topobjdir, base))
58    for p, f in finder:
59        if p.endswith((".rs", ".c", ".h", ".cc", ".cpp")):
60            yield mozpath.join(base, p), f
61
62
63def get_s3_region_and_bucket():
64    """
65    Return a tuple of (region, bucket) giving the AWS region and S3
66    bucket to which generated sources should be uploaded.
67    """
68    region = "us-west-2"
69    level = os.environ.get("MOZ_SCM_LEVEL", "1")
70    bucket = {
71        "1": "gecko-generated-sources-l1",
72        "2": "gecko-generated-sources-l2",
73        "3": "gecko-generated-sources",
74    }[level]
75    return (region, bucket)
76