blob: 157c3cb053a0254e28d29f222e04f5d7923f38b0 [file] [log] [blame]
Joshua Haberman823eb092021-04-05 12:26:41 -07001# Copyright (c) 2009-2021, Google LLC
Joshua Habermane59d2c82021-04-05 10:47:53 -07002# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6# * Redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer.
8# * Redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution.
11# * Neither the name of Google LLC nor the
12# names of its contributors may be used to endorse or promote products
13# derived from this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18# DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
19# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
Joshua Haberman7ff16622022-04-01 12:07:07 -070026"""Bazel support functions related to CMake support."""
27
Mike Kruskal65e7d4a2023-02-03 14:49:40 -080028def staleness_test(name, outs, generated_pattern, target_files = None, tags = [], **kwargs):
Joshua Habermane3f41de2020-10-17 13:15:58 -070029 """Tests that checked-in file(s) match the contents of generated file(s).
30
31 The resulting test will verify that all output files exist and have the
32 correct contents. If the test fails, it can be invoked with --fix to
33 bring the checked-in files up to date.
34
35 Args:
36 name: Name of the rule.
37 outs: the checked-in files that are copied from generated files.
38 generated_pattern: the pattern for transforming each "out" file into a
39 generated file. For example, if generated_pattern="generated/%s" then
40 a file foo.txt will look for generated file generated/foo.txt.
Joshua Habermane41a2d72023-01-11 21:25:34 -080041 target_files: A glob representing all of the files that should be
42 covered by this rule. Files in this glob but not generated will
43 be deleted. (Not currently implemented in OSS).
Joshua Haberman7ff16622022-04-01 12:07:07 -070044 **kwargs: Additional keyword arguments to pass through to py_test().
Joshua Habermane3f41de2020-10-17 13:15:58 -070045 """
46
47 script_name = name + ".py"
Adam Cozzette4a9e1ba2022-09-29 22:14:05 +000048 script_src = Label("//cmake:staleness_test.py")
Joshua Habermane3f41de2020-10-17 13:15:58 -070049
50 # Filter out non-existing rules so Blaze doesn't error out before we even
51 # run the test.
52 existing_outs = native.glob(include = outs)
53
54 # The file list contains a few extra bits of information at the end.
55 # These get unpacked by the Config class in staleness_test_lib.py.
56 file_list = outs + [generated_pattern, native.package_name() or ".", name]
57
58 native.genrule(
59 name = name + "_makescript",
60 outs = [script_name],
61 srcs = [script_src],
62 testonly = 1,
Adam Cozzette4a9e1ba2022-09-29 22:14:05 +000063 cmd = "cp $< $@; " +
Joshua Habermane3f41de2020-10-17 13:15:58 -070064 "sed -i.bak -e 's|INSERT_FILE_LIST_HERE|" + "\\\n ".join(file_list) + "|' $@",
65 )
66
67 native.py_test(
68 name = name,
69 srcs = [script_name],
70 data = existing_outs + [generated_pattern % file for file in outs],
Joshua Haberman71837802021-08-24 07:58:37 -070071 python_version = "PY3",
Joshua Habermane3f41de2020-10-17 13:15:58 -070072 deps = [
Adam Cozzette4a9e1ba2022-09-29 22:14:05 +000073 Label("//cmake:staleness_test_lib"),
Joshua Habermane3f41de2020-10-17 13:15:58 -070074 ],
Mike Kruskal65e7d4a2023-02-03 14:49:40 -080075 tags = ["staleness_test"] + tags,
Joshua Haberman7ff16622022-04-01 12:07:07 -070076 **kwargs
Joshua Habermane3f41de2020-10-17 13:15:58 -070077 )