diff options
author | 2016-03-11 22:57:08 +0000 | |
---|---|---|
committer | 2016-03-14 13:53:08 +0000 | |
commit | da258a005c47570097d3862722b492d5bd0770c7 (patch) | |
tree | eba88516797a4e0f2857374d04cea0b5ba64fc52 | |
parent | 82741ecec2d7cedbb0ab11b07f6ef3a6ef0737f2 (diff) |
Use repository_ctx for each usage of repository context.
This will avoid confusion with the normal Skylark rule context.
Also fixed indentation of cc_configure.bzl
--
MOS_MIGRATED_REVID=117011107
5 files changed, 107 insertions, 102 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java index 34fdda4b2e..48a9fea963 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java @@ -80,7 +80,8 @@ public class SkylarkRepositoryContext { } /** - * Create a new context (ctx) object for a skylark repository rule ({@code rule} argument). + * Create a new context (repository_ctx) object for a skylark repository rule ({@code rule} + * argument). */ SkylarkRepositoryContext( Rule rule, Path outputDirectory, Environment environment, Map<String, String> env) { diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryModule.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryModule.java index 252ecdaac3..3332accfb8 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryModule.java @@ -60,8 +60,8 @@ public class SkylarkRepositoryModule { type = BaseFunction.class, doc = "the function implementing this rule, has to have exactly one parameter: " - + "<code>ctx</code>. The function is called during analysis phase for each " - + "instance of the rule." + + "<code>repository_ctx</code>. The function is called during analysis phase for " + + "each instance of the rule." ) }, optionalNamedOnly = { diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryIntegrationTest.java index 329c30cfb1..8f692515b6 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryIntegrationTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryIntegrationTest.java @@ -152,8 +152,8 @@ public class SkylarkRepositoryIntegrationTest extends BuildViewTestCase { scratch.file("/repo2/BUILD", "filegroup(name='bar', srcs=['bar.txt'], path='foo')"); scratch.file( "def.bzl", - "def _impl(ctx):", - " ctx.symlink(ctx.attr.path, '')", + "def _impl(repository_ctx):", + " repository_ctx.symlink(repository_ctx.attr.path, '')", "", "repo = repository_rule(", " implementation=_impl,", @@ -177,9 +177,9 @@ public class SkylarkRepositoryIntegrationTest extends BuildViewTestCase { scratch.file("/repo2/WORKSPACE"); scratch.file( "def.bzl", - "def _impl(ctx):", - " ctx.symlink(Label('@repo2//:bar.txt'), 'BUILD')", - " ctx.file('foo.txt', 'foo')", + "def _impl(repository_ctx):", + " repository_ctx.symlink(Label('@repo2//:bar.txt'), 'BUILD')", + " repository_ctx.file('foo.txt', 'foo')", "", "repo = repository_rule(", " implementation=_impl,", @@ -203,9 +203,10 @@ public class SkylarkRepositoryIntegrationTest extends BuildViewTestCase { scratch.file("/repo2/WORKSPACE"); scratch.file( "def.bzl", - "def _impl(ctx):", - " ctx.template('BUILD', Label('@repo2//:bar.txt'), {'{target}': 'bar', '{path}': 'foo'})", - " ctx.file('foo.txt', 'foo')", + "def _impl(repository_ctx):", + " repository_ctx.template('BUILD', Label('@repo2//:bar.txt'), " + + "{'{target}': 'bar', '{path}': 'foo'})", + " repository_ctx.file('foo.txt', 'foo')", "", "repo = repository_rule(", " implementation=_impl,", @@ -230,9 +231,10 @@ public class SkylarkRepositoryIntegrationTest extends BuildViewTestCase { scratch.file("/repo2/WORKSPACE"); scratch.file( "def.bzl", - "def _impl(ctx):", - " ctx.template('BUILD', Label('@repo2//:bar.txt'), {'{path}': ctx.name})", - " ctx.file('foo.txt', 'foo')", + "def _impl(repository_ctx):", + " repository_ctx.template('BUILD', Label('@repo2//:bar.txt'), " + + "{'{path}': repository_ctx.name})", + " repository_ctx.file('foo.txt', 'foo')", "", "repo = repository_rule(", " implementation=_impl,", diff --git a/src/test/shell/bazel/skylark_repository_test.sh b/src/test/shell/bazel/skylark_repository_test.sh index 8207c5ccc6..c69981f08b 100755 --- a/src/test/shell/bazel/skylark_repository_test.sh +++ b/src/test/shell/bazel/skylark_repository_test.sh @@ -232,8 +232,8 @@ EOF # Our custom repository rule cat >test.bzl <<EOF -def _impl(ctx): - ctx.symlink(ctx.path(ctx.attr.path), ctx.path("")) +def _impl(repository_ctx): + repository_ctx.symlink(repository_ctx.path(repository_ctx.attr.path), repository_ctx.path("")) repo = repository_rule( implementation=_impl, @@ -277,21 +277,21 @@ function test_skylark_repository_which_and_execute() { # Our custom repository rule cat >test.bzl <<EOF -def _impl(ctx): - bash = ctx.which("bash") +def _impl(repository_ctx): + bash = repository_ctx.which("bash") if bash == None: fail("Bash not found!") - bin = ctx.which("bin.sh") + bin = repository_ctx.which("bin.sh") if bin == None: fail("bin.sh not found!") - result = ctx.execute([bash, "--version"]) + result = repository_ctx.execute([bash, "--version"]) if result.return_code != 0: fail("Non-zero return code from bash: " + result.return_code) if result.stderr != "": fail("Non-empty error output: " + result.stderr) print(result.stdout) # Symlink so a repository is created - ctx.symlink(ctx.path("$repo2"), ctx.path("")) + repository_ctx.symlink(repository_ctx.path("$repo2"), repository_ctx.path("")) repo = repository_rule(implementation=_impl, local=True) EOF @@ -303,15 +303,15 @@ function test_skylark_repository_execute_stderr() { setup_skylark_repository cat >test.bzl <<EOF -def _impl(ctx): - result = ctx.execute([str(ctx.which("bash")), "-c", "echo erf >&2; exit 1"]) +def _impl(repository_ctx): + result = repository_ctx.execute([str(repository_ctx.which("bash")), "-c", "echo erf >&2; exit 1"]) if result.return_code != 1: fail("Incorrect return code from bash (should be 1): " + result.return_code) if result.stdout != "": fail("Non-empty output: %s (stderr was %s)" % (result.stdout, result.stderr)) print(result.stderr) # Symlink so a repository is created - ctx.symlink(ctx.path("$repo2"), ctx.path("")) + repository_ctx.symlink(repository_ctx.path("$repo2"), repository_ctx.path("")) repo = repository_rule(implementation=_impl, local=True) EOF @@ -324,10 +324,10 @@ function test_skylark_repository_environ() { # Our custom repository rule cat >test.bzl <<EOF -def _impl(ctx): - print(ctx.os.environ["FOO"]) +def _impl(repository_ctx): + print(repository_ctx.os.environ["FOO"]) # Symlink so a repository is created - ctx.symlink(ctx.path("$repo2"), ctx.path("")) + repository_ctx.symlink(repository_ctx.path("$repo2"), repository_ctx.path("")) repo = repository_rule(implementation=_impl, local=False) EOF @@ -351,10 +351,10 @@ EOF # Test modifying test.bzl invalidate the repository cat >test.bzl <<EOF -def _impl(ctx): - print(ctx.os.environ["BAR"]) +def _impl(repository_ctx): + print(repository_ctx.os.environ["BAR"]) # Symlink so a repository is created - ctx.symlink(ctx.path("$repo2"), ctx.path("")) + repository_ctx.symlink(repository_ctx.path("$repo2"), repository_ctx.path("")) repo = repository_rule(implementation=_impl, local=True) EOF BAR=BEZ bazel build @foo//:bar >& $TEST_log || fail "Failed to build" @@ -363,10 +363,10 @@ EOF # Shutdown and modify again bazel shutdown cat >test.bzl <<EOF -def _impl(ctx): - print(ctx.os.environ["BAZ"]) +def _impl(repository_ctx): + print(repository_ctx.os.environ["BAZ"]) # Symlink so a repository is created - ctx.symlink(ctx.path("$repo2"), ctx.path("")) + repository_ctx.symlink(repository_ctx.path("$repo2"), repository_ctx.path("")) repo = repository_rule(implementation=_impl, local=True) EOF BAZ=BOZ bazel build @foo//:bar >& $TEST_log || fail "Failed to build" @@ -378,11 +378,11 @@ function test_skylark_repository_executable_flag() { # Our custom repository rule cat >test.bzl <<EOF -def _impl(ctx): - ctx.file("test.sh", "exit 0") - ctx.file("BUILD", "sh_binary(name='bar',srcs=['test.sh'])", False) - ctx.template("test2", Label("//:bar"), {}, False) - ctx.template("test2.sh", Label("//:bar"), {}, True) +def _impl(repository_ctx): + repository_ctx.file("test.sh", "exit 0") + repository_ctx.file("BUILD", "sh_binary(name='bar',srcs=['test.sh'])", False) + repository_ctx.template("test2", Label("//:bar"), {}, False) + repository_ctx.template("test2.sh", Label("//:bar"), {}, True) repo = repository_rule(implementation=_impl, local=True) EOF cat >bar diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl index 5ad70a9b66..f3876cbfd1 100644 --- a/tools/cpp/cc_configure.bzl +++ b/tools/cpp/cc_configure.bzl @@ -44,15 +44,15 @@ def _build_tool_path(d): return "\n".join(lines) -def _which(ctx, cmd, default): - """A wrapper around ctx.which() to provide a fallback value.""" - result = ctx.which(cmd) +def _which(repository_ctx, cmd, default): + """A wrapper around repository_ctx.which() to provide a fallback value.""" + result = repository_ctx.which(cmd) return default if result == None else str(result) -def _get_tool_paths(ctx, darwin, cc): +def _get_tool_paths(repository_ctx, darwin, cc): """Compute the path to the various tools.""" - return {k: _which(ctx, k, "/usr/bin/" + k) + return {k: _which(repository_ctx, k, "/usr/bin/" + k) for k in [ "ld", "cpp", @@ -65,16 +65,16 @@ def _get_tool_paths(ctx, darwin, cc): ]} + { "gcc": cc, "ar": "/usr/bin/libtool" - if darwin else _which(ctx, "ar", "/usr/bin/ar") + if darwin else _which(repository_ctx, "ar", "/usr/bin/ar") } -def _ld_library_paths(ctx): +def _ld_library_paths(repository_ctx): """Use ${LD_LIBRARY_PATH} to compute the list -Wl,rpath flags.""" - if "LD_LIBRARY_PATH" in ctx.os.environ: + if "LD_LIBRARY_PATH" in repository_ctx.os.environ: result = [] - for p in ctx.os.environ["LD_LIBRARY_PATH"].split(":"): - p = str(ctx.path(p)) # Normalize the path + for p in repository_ctx.os.environ["LD_LIBRARY_PATH"].split(":"): + p = str(repository_ctx.path(p)) # Normalize the path result.append("-Wl,rpath," + p) result.append("-L" + p) return result @@ -82,18 +82,18 @@ def _ld_library_paths(ctx): return [] -def _get_cpu_value(ctx): +def _get_cpu_value(repository_ctx): """Compute the cpu_value based on the OS name.""" - return "darwin" if ctx.os.name.lower().startswith("mac os") else "k8" + return "darwin" if repository_ctx.os.name.lower().startswith("mac os") else "k8" _INC_DIR_MARKER_BEGIN = "#include <...> search starts here:" _INC_DIR_MARKER_END = "End of search list." -def _get_cxx_inc_directories(ctx, cc): +def _get_cxx_inc_directories(repository_ctx, cc): """Compute the list of default C++ include directories.""" - result = ctx.execute([cc, "-E", "-xc++", "-", "-v"]) + result = repository_ctx.execute([cc, "-E", "-xc++", "-", "-v"]) index1 = result.stderr.find(_INC_DIR_MARKER_BEGIN) if index1 == -1: return [] @@ -101,23 +101,23 @@ def _get_cxx_inc_directories(ctx, cc): if index2 == -1: return [] inc_dirs = result.stderr[index1 + len(_INC_DIR_MARKER_BEGIN):index2].strip() - return [ctx.path(p.strip()) for p in inc_dirs.split("\n")] + return [repository_ctx.path(p.strip()) for p in inc_dirs.split("\n")] -def _add_option_if_supported(ctx, cc, option): +def _add_option_if_supported(repository_ctx, cc, option): """Checks that `option` is supported by the C compiler.""" - result = ctx.execute([ - cc, - option, - "-o", - "/dev/null", - "-c", - str(ctx.path("tools/cpp/empty.cc")) + result = repository_ctx.execute([ + cc, + option, + "-o", + "/dev/null", + "-c", + str(repository_ctx.path("tools/cpp/empty.cc")) ]) return [option] if result.stderr.find(option) == -1 else [] -def _crosstool_content(ctx, cc, cpu_value, darwin): +def _crosstool_content(repository_ctx, cc, cpu_value, darwin): """Return the content for the CROSSTOOL file, in a dictionary.""" return { "abi_version": "local", @@ -140,32 +140,32 @@ def _crosstool_content(ctx, cc, cpu_value, darwin): "linker_flag": [ "-lstdc++", # Anticipated future default. - ] + _add_option_if_supported(ctx, cc, "-no-canonical-prefixes") + ( - ["-undefined", "dynamic_lookup"] if darwin else [ - "-B" + str(ctx.path(cc).dirname), - # Have gcc return the exit code from ld. - "-pass-exit-codes", - # Stamp the binary with a unique identifier. - "-Wl,--build-id=md5", - "-Wl,--hash-style=gnu" - # Gold linker only? Can we enable this by default? - # "-Wl,--warn-execstack", - # "-Wl,--detect-odr-violations" - ]) + _ld_library_paths(ctx), + ] + _add_option_if_supported(repository_ctx, cc, "-no-canonical-prefixes") + ( + ["-undefined", "dynamic_lookup"] if darwin else [ + "-B" + str(repository_ctx.path(cc).dirname), + # Have gcc return the exit code from ld. + "-pass-exit-codes", + # Stamp the binary with a unique identifier. + "-Wl,--build-id=md5", + "-Wl,--hash-style=gnu" + # Gold linker only? Can we enable this by default? + # "-Wl,--warn-execstack", + # "-Wl,--detect-odr-violations" + ]) + _ld_library_paths(repository_ctx), "ar_flag": ["-static", "-s", "-o"] if darwin else [], - "cxx_builtin_include_directory": _get_cxx_inc_directories(ctx, cc), + "cxx_builtin_include_directory": _get_cxx_inc_directories(repository_ctx, cc), "objcopy_embed_flag": ["-I", "binary"], "unfiltered_cxx_flag": # Anticipated future default. - _add_option_if_supported(ctx, cc, "-no-canonical-prefixes") + - _add_option_if_supported(ctx, cc, "-fno-canonical-system-headers") + [ - # Make C++ compilation deterministic. Use linkstamping instead of these - # compiler symbols. - "-Wno-builtin-macro-redefined", - "-D__DATE__=\\\"redacted\\\"", - "-D__TIMESTAMP__=\\\"redacted\\\"", - "-D__TIME__=\\\"redacted\\\"" - ], + _add_option_if_supported(repository_ctx, cc, "-no-canonical-prefixes") + + _add_option_if_supported(repository_ctx, cc, "-fno-canonical-system-headers") + [ + # Make C++ compilation deterministic. Use linkstamping instead of these + # compiler symbols. + "-Wno-builtin-macro-redefined", + "-D__DATE__=\\\"redacted\\\"", + "-D__TIMESTAMP__=\\\"redacted\\\"", + "-D__TIME__=\\\"redacted\\\"" + ], "compiler_flag": [ # Security hardening on by default. # Conservative choice; -D_FORTIFY_SOURCE=2 may be unsafe in some cases. @@ -183,10 +183,10 @@ def _crosstool_content(ctx, cc, cpu_value, darwin): "-Wl,-z,-relro,-z,now" ]) + ( # has false positives - _add_option_if_supported(ctx, cc, "-Wno-free-nonheap-object") + + _add_option_if_supported(repository_ctx, cc, "-Wno-free-nonheap-object") + # Enable coloring even if there's no attached terminal. Bazel removes the # escape sequences if --nocolor is specified. - _add_option_if_supported(ctx, cc, "-fcolor-diagnostics")) + [ + _add_option_if_supported(repository_ctx, cc, "-fcolor-diagnostics")) + [ # Keep stack frames for debugging, even in opt mode. "-fno-omit-frame-pointer", ], @@ -225,12 +225,12 @@ def _dbg_content(): return {"compiler_flag": "-g"} -def _find_cc(ctx): +def _find_cc(repository_ctx): """Find the C++ compiler.""" - if "CC" in ctx.os.environ: - return ctx.path(ctx.os.environ["CC"]) + if "CC" in repository_ctx.os.environ: + return repository_ctx.path(repository_ctx.os.environ["CC"]) else: - cc = ctx.which("gcc") + cc = repository_ctx.which("gcc") if cc == None: fail( "Cannot find gcc, either correct your path or set the CC" + @@ -238,28 +238,30 @@ def _find_cc(ctx): return cc -def _tpl(ctx, tpl, substitutions={}): - ctx.template(tpl, Label("@bazel_tools//tools/cpp:%s.tpl" % tpl), - substitutions) +def _tpl(repository_ctx, tpl, substitutions={}): + repository_ctx.template( + tpl, + Label("@bazel_tools//tools/cpp:%s.tpl" % tpl), + substitutions) -def _impl(ctx): - ctx.file("tools/cpp/empty.cc") - cpu_value = _get_cpu_value(ctx) +def _impl(repository_ctx): + repository_ctx.file("tools/cpp/empty.cc") + cpu_value = _get_cpu_value(repository_ctx) darwin = cpu_value == "darwin" - cc = _find_cc(ctx) + cc = _find_cc(repository_ctx) crosstool_cc = "osx_cc_wrapper.sh" if darwin else str(cc) darwin = cpu_value == "darwin" - tool_paths = _get_tool_paths(ctx, darwin, crosstool_cc) - crosstool_content = _crosstool_content(ctx, cc, cpu_value, darwin) + tool_paths = _get_tool_paths(repository_ctx, darwin, crosstool_cc) + crosstool_content = _crosstool_content(repository_ctx, cc, cpu_value, darwin) opt_content = _opt_content(darwin) dbg_content = _dbg_content() - _tpl(ctx, "BUILD", { + _tpl(repository_ctx, "BUILD", { "%{name}": cpu_value, "%{supports_param_files}": "0" if darwin else "1" }) - _tpl(ctx, "osx_cc_wrapper.sh", {"%{cc}": str(cc)}) - _tpl(ctx, "CROSSTOOL", { + _tpl(repository_ctx, "osx_cc_wrapper.sh", {"%{cc}": str(cc)}) + _tpl(repository_ctx, "CROSSTOOL", { "%{cpu}": cpu_value, "%{content}": _build_crosstool(crosstool_content) + "\n" + _build_tool_path(tool_paths), |