diff --git a/foreign_cc/built_tools/private/built_tools_framework.bzl b/foreign_cc/built_tools/private/built_tools_framework.bzl index d3eef4d5..aa1b1763 100644 --- a/foreign_cc/built_tools/private/built_tools_framework.bzl +++ b/foreign_cc/built_tools/private/built_tools_framework.bzl @@ -3,7 +3,7 @@ load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") load("//foreign_cc/private:detect_root.bzl", "detect_root") load("//foreign_cc/private:framework.bzl", "wrap_outputs") -load("//foreign_cc/private/framework:helpers.bzl", "convert_shell_script") +load("//foreign_cc/private/framework:helpers.bzl", "convert_shell_script", "shebang") # Common attributes for all built_tool rules FOREIGN_CC_BUILT_TOOLS_ATTRS = { @@ -48,12 +48,12 @@ def built_tool_rule_impl(ctx, script_lines, out_dir, mnemonic): "cd $$BUILD_TMPDIR$$", ] - script.append("set -x") + script.append("##enable_tracing##") script.extend(script_lines) - script.append("set +x") + script.append("##disable_tracing##") script_text = "\n".join([ - "#!/usr/bin/env bash", + shebang(ctx), convert_shell_script(ctx, script), "", ]) diff --git a/foreign_cc/private/cmake_script.bzl b/foreign_cc/private/cmake_script.bzl index 21f5c74b..bde2a0e3 100644 --- a/foreign_cc/private/cmake_script.bzl +++ b/foreign_cc/private/cmake_script.bzl @@ -88,7 +88,7 @@ def create_cmake_script( directory = "$$EXT_BUILD_ROOT$$/" + root - script.append("set -x") + script.append("##enable_tracing##") # Configure the CMake generate command script.append(" ".join([ @@ -102,7 +102,7 @@ def create_cmake_script( script.extend(cmake_commands) - script.append("set +x") + script.append("##disable_tracing##") return params.commands + script diff --git a/foreign_cc/private/configure_script.bzl b/foreign_cc/private/configure_script.bzl index 138a22ab..9e15cf6b 100644 --- a/foreign_cc/private/configure_script.bzl +++ b/foreign_cc/private/configure_script.bzl @@ -70,9 +70,9 @@ def create_configure_script( user_options = " ".join(user_options), )) - script.append("set -x") + script.append("##enable_tracing##") script.extend(make_commands) - script.append("set +x") + script.append("##disable_tracing##") return script diff --git a/foreign_cc/private/framework.bzl b/foreign_cc/private/framework.bzl index b736766d..41de8545 100644 --- a/foreign_cc/private/framework.bzl +++ b/foreign_cc/private/framework.bzl @@ -11,6 +11,8 @@ load( "convert_shell_script", "create_function", "os_name", + "script_extension", + "shebang", ) load( "//toolchains/native_tools:tool_access.bzl", @@ -360,7 +362,7 @@ def cc_external_rule_impl(ctx, attrs): ] script_text = "\n".join([ - "#!/usr/bin/env bash", + shebang(ctx), convert_shell_script(ctx, script_lines), ]) wrapped_outputs = wrap_outputs(ctx, lib_name, attrs.configure_name, script_text) @@ -443,9 +445,10 @@ WrappedOutputs = provider( # buildifier: disable=function-docstring def wrap_outputs(ctx, lib_name, configure_name, script_text, build_script_file = None): + extension = script_extension(ctx) build_log_file = ctx.actions.declare_file("{}_foreign_cc/{}.log".format(lib_name, configure_name)) - build_script_file = ctx.actions.declare_file("{}_foreign_cc/build_script.sh".format(lib_name)) - wrapper_script_file = ctx.actions.declare_file("{}_foreign_cc/wrapper_build_script.sh".format(lib_name)) + build_script_file = ctx.actions.declare_file("{}_foreign_cc/build_script{}".format(lib_name, extension)) + wrapper_script_file = ctx.actions.declare_file("{}_foreign_cc/wrapper_build_script{}".format(lib_name, extension)) ctx.actions.write( output = build_script_file, @@ -492,7 +495,7 @@ def wrap_outputs(ctx, lib_name, configure_name, script_text, build_script_file = "##redirect_out_err## $$BUILD_SCRIPT$$ $$BUILD_LOG$$", ] build_command = "\n".join([ - "#!/usr/bin/env bash", + shebang(ctx), convert_shell_script(ctx, build_command_lines), "", ]) diff --git a/foreign_cc/private/framework/helpers.bzl b/foreign_cc/private/framework/helpers.bzl index f7de8ddc..f0c3f212 100644 --- a/foreign_cc/private/framework/helpers.bzl +++ b/foreign_cc/private/framework/helpers.bzl @@ -37,6 +37,28 @@ load("//foreign_cc/private/framework/toolchains:commands.bzl", "PLATFORM_COMMAND def os_name(ctx): return call_shell(create_context(ctx), "os_name") +def script_extension(ctx): + """A helper method for getting the script extension of the current foreign_cc framework toolchain + + Args: + ctx (ctx): The current rule's context object + + Returns: + str: A script extension. eg: `.sh` + """ + return call_shell(create_context(ctx), "script_extension") + +def shebang(ctx): + """A helper method for getting the shebang of the current foreign_cc framework toolchain + + Args: + ctx (ctx): The current rule's context object + + Returns: + str: A shebang for a script. eg: `#!/usr/bin/env bash` + """ + return call_shell(create_context(ctx), "shebang") + def create_function(ctx, name, text): return call_shell(create_context(ctx), "define_function", name, text) diff --git a/foreign_cc/private/framework/toolchains/commands.bzl b/foreign_cc/private/framework/toolchains/commands.bzl index a624c74e..71b3b017 100644 --- a/foreign_cc/private/framework/toolchains/commands.bzl +++ b/foreign_cc/private/framework/toolchains/commands.bzl @@ -90,10 +90,18 @@ PLATFORM_COMMANDS = { ], doc = "Defines a function with 'text' as the function body.", ), + "disable_tracing": _command_info( + arguments = [], + doc = "Disable script tracing. eg: `set +x`", + ), "echo": _command_info( arguments = [_argument_info(name = "text", data_type = type(""), doc = "Text to output")], doc = "Outputs 'text' to stdout", ), + "enable_tracing": _command_info( + arguments = [], + doc = "Enable script tracing. eg: `set -x`", + ), "env": _command_info( arguments = [], doc = "Print all environment variables", @@ -173,10 +181,18 @@ PLATFORM_COMMANDS = { ], doc = "Replaces all occurrences of 'from_' to 'to_' recursively in the directory 'dir'.", ), + "script_extension": _command_info( + arguments = [], + doc = "Return the extension for the current set of commands (`.sh` for bash, `.ps1` for powershell)", + ), "script_prelude": _command_info( arguments = [], doc = "Function for setting necessary environment variables for the platform", ), + "shebang": _command_info( + arguments = [], + doc = "The shebang for the current shell executable", + ), "symlink_contents_to_dir": _command_info( arguments = [ _argument_info( diff --git a/foreign_cc/private/framework/toolchains/linux_commands.bzl b/foreign_cc/private/framework/toolchains/linux_commands.bzl index dab5f919..6505902e 100644 --- a/foreign_cc/private/framework/toolchains/linux_commands.bzl +++ b/foreign_cc/private/framework/toolchains/linux_commands.bzl @@ -7,6 +7,12 @@ _REPLACE_VALUE = "\\${EXT_BUILD_DEPS}" def os_name(): return "linux" +def shebang(): + return "#!/usr/bin/env bash" + +def script_extension(): + return ".sh" + def pwd(): return "$(pwd)" @@ -31,6 +37,12 @@ def path(expression): def touch(path): return "touch " + path +def enable_tracing(): + return "set -x" + +def disable_tracing(): + return "set +x" + def mkdirs(path): return "mkdir -p " + path diff --git a/foreign_cc/private/framework/toolchains/macos_commands.bzl b/foreign_cc/private/framework/toolchains/macos_commands.bzl index 64d03d13..7fa9ae0d 100644 --- a/foreign_cc/private/framework/toolchains/macos_commands.bzl +++ b/foreign_cc/private/framework/toolchains/macos_commands.bzl @@ -7,6 +7,12 @@ _REPLACE_VALUE = "\\${EXT_BUILD_DEPS}" def os_name(): return "macos" +def shebang(): + return "#!/usr/bin/env bash" + +def script_extension(): + return ".sh" + def pwd(): return "$(pwd)" @@ -31,6 +37,12 @@ def path(expression): def touch(path): return "touch " + path +def enable_tracing(): + return "set -x" + +def disable_tracing(): + return "set +x" + def mkdirs(path): return "mkdir -p " + path diff --git a/foreign_cc/private/framework/toolchains/windows_commands.bzl b/foreign_cc/private/framework/toolchains/windows_commands.bzl index 7fc9f1ec..038ba7e5 100644 --- a/foreign_cc/private/framework/toolchains/windows_commands.bzl +++ b/foreign_cc/private/framework/toolchains/windows_commands.bzl @@ -7,6 +7,12 @@ _REPLACE_VALUE = "\\${EXT_BUILD_DEPS}" def os_name(): return "windows" +def shebang(): + return "#!/usr/bin/env bash" + +def script_extension(): + return ".sh" + def pwd(): return "$(type -t cygpath > /dev/null && cygpath $(pwd) -w || pwd -W)" @@ -31,6 +37,12 @@ def path(expression): def touch(path): return "touch " + path +def enable_tracing(): + return "set -x" + +def disable_tracing(): + return "set +x" + def mkdirs(path): return "mkdir -p " + path diff --git a/foreign_cc/private/make_script.bzl b/foreign_cc/private/make_script.bzl index 45d3fcbe..0c503de1 100644 --- a/foreign_cc/private/make_script.bzl +++ b/foreign_cc/private/make_script.bzl @@ -10,9 +10,9 @@ def create_make_script( script.append("##symlink_contents_to_dir## $$EXT_BUILD_ROOT$$/{} $$BUILD_TMPDIR$$".format(root)) - script.append("set -x") + script.append("##enable_tracing##") script.extend(make_commands) - script.append("set +x") + script.append("##disable_tracing##") return script # buildifier: disable=function-docstring-args diff --git a/foreign_cc/private/run_shell_file_utils.bzl b/foreign_cc/private/run_shell_file_utils.bzl index 8ffe4ff0..89b5e336 100644 --- a/foreign_cc/private/run_shell_file_utils.bzl +++ b/foreign_cc/private/run_shell_file_utils.bzl @@ -1,12 +1,19 @@ -# buildifier: disable=module-docstring -# buildifier: disable=name-conventions -CreatedByScript = provider( - doc = "Structure to keep declared file or directory and creating script.", - fields = dict( - file = "Declared file or directory", - script = "Script that creates that file or directory", - ), -) +"""A module defining some sketchy solutions for managing outputs of foreign_cc rules""" + +def _created_by_script(file, script): + """Structure to keep declared file or directory and creating script. + + Args: + file (File): Declared file or directory + script (str): Script that creates that file or directory + + Returns: + struct: A struct of script info + """ + return struct( + file = file, + script = script, + ) def fictive_file_in_genroot(actions, target_name): """Creates a fictive file under the build root. @@ -20,7 +27,7 @@ def fictive_file_in_genroot(actions, target_name): # we need this fictive file in the genroot to get the path of the root in the script empty = actions.declare_file("empty_{}.txt".format(target_name)) - return CreatedByScript( + return _created_by_script( file = empty, script = "##touch## $$EXT_BUILD_ROOT$$/" + empty.path, ) @@ -36,7 +43,7 @@ def copy_directory(actions, orig_path, copy_path): copy_path: target directory, relative to the build root """ dir_copy = actions.declare_directory(copy_path) - return CreatedByScript( + return _created_by_script( file = dir_copy, script = "\n".join([ "##mkdirs## $$EXT_BUILD_ROOT$$/" + dir_copy.path, diff --git a/test/cmake_text_tests.bzl b/test/cmake_text_tests.bzl index a4c7de4f..b36513b2 100644 --- a/test/cmake_text_tests.bzl +++ b/test/cmake_text_tests.bzl @@ -257,9 +257,9 @@ def _merge_flag_values_no_toolchain_file_test(ctx): expected = r"""export CC="/usr/bin/gcc" export CXX="/usr/bin/gcc" export CXXFLAGS="foo=\\\"bar\\\" -Fbat" -set -x +##enable_tracing## cmake -DCMAKE_AR="/usr/bin/ar" -DCMAKE_BUILD_TYPE="RelWithDebInfo" -DCMAKE_INSTALL_PREFIX="test_rule" -DCMAKE_PREFIX_PATH="$$EXT_BUILD_DEPS$$" -DCMAKE_RANLIB="" -G 'Unix Makefiles' $$EXT_BUILD_ROOT$$/external/test_rule -set +x +##disable_tracing## """ asserts.equals(env, expected.splitlines(), script) @@ -307,9 +307,9 @@ export CXX="/usr/bin/gcc" export CFLAGS="-U_FORTIFY_SOURCE -fstack-protector -Wall" export CXXFLAGS="-U_FORTIFY_SOURCE -fstack-protector -Wall" export ASMFLAGS="-U_FORTIFY_SOURCE -fstack-protector -Wall" -set -x +##enable_tracing## cmake -DCMAKE_AR="/usr/bin/ar" -DCMAKE_SHARED_LINKER_FLAGS="-shared -fuse-ld=gold" -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=gold -Wl -no-as-needed" -DNOFORTRAN="on" -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_INSTALL_PREFIX="test_rule" -DCMAKE_PREFIX_PATH="$$EXT_BUILD_DEPS$$;/abc/def" -DCMAKE_RANLIB="" --debug-output -Wdev -G 'Ninja' $$EXT_BUILD_ROOT$$/external/test_rule -set +x +##disable_tracing## """ asserts.equals(env, expected.splitlines(), script) @@ -360,9 +360,9 @@ export CXX="/usr/bin/gcc" export CFLAGS="-U_FORTIFY_SOURCE -fstack-protector -Wall" export CXXFLAGS="-U_FORTIFY_SOURCE -fstack-protector -Wall" export ASMFLAGS="-U_FORTIFY_SOURCE -fstack-protector -Wall" -set -x +##enable_tracing## cmake -DCMAKE_AR="/usr/bin/ar" -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=gold -Wl -no-as-needed" -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_INSTALL_PREFIX="test_rule" -DCMAKE_PREFIX_PATH="$$EXT_BUILD_DEPS$$;/abc/def" -DCMAKE_RANLIB="" --debug-output -Wdev -G 'Ninja' $$EXT_BUILD_ROOT$$/external/test_rule -set +x +##disable_tracing## """ asserts.equals(env, expected.splitlines(), script) @@ -415,9 +415,9 @@ set(CMAKE_EXE_LINKER_FLAGS_INIT "-fuse-ld=gold -Wl -no-as-needed") set(CMAKE_SHARED_LINKER_FLAGS_INIT "-shared -fuse-ld=gold") EOF -set -x +##enable_tracing## cmake -DNOFORTRAN="on" -DCMAKE_TOOLCHAIN_FILE="crosstool_bazel.cmake" -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_INSTALL_PREFIX="test_rule" -DCMAKE_PREFIX_PATH="$$EXT_BUILD_DEPS$$" -DCMAKE_RANLIB="" --debug-output -Wdev -G 'Ninja' $$EXT_BUILD_ROOT$$/external/test_rule -set +x +##disable_tracing## """ asserts.equals(env, expected.splitlines(), script) @@ -479,9 +479,9 @@ export CFLAGS="-cc-flag -gcc_toolchain cc-toolchain --from-env --additional-flag export CXXFLAGS="--quoted=\\\"abc def\\\" --sysroot=/abc/sysroot --gcc_toolchain cxx-toolchain" export ASMFLAGS="assemble assemble-user" export CUSTOM_ENV="YES" -set -x +##enable_tracing## cmake -DCMAKE_AR="/cxx_linker_static" -DCMAKE_CXX_LINK_EXECUTABLE="became" -DCMAKE_SHARED_LINKER_FLAGS="shared1 shared2" -DCMAKE_EXE_LINKER_FLAGS="executable" -DCMAKE_BUILD_TYPE="user_type" -DCUSTOM_CACHE="YES" -DCMAKE_INSTALL_PREFIX="test_rule" -DCMAKE_PREFIX_PATH="$$EXT_BUILD_DEPS$$" -DCMAKE_RANLIB="" --debug-output -Wdev -G 'Ninja' $$EXT_BUILD_ROOT$$/external/test_rule -set +x +##disable_tracing## """ asserts.equals(env, expected.splitlines(), script) @@ -552,9 +552,9 @@ set(CMAKE_SYSROOT "/abc/sysroot") EOF export CUSTOM_ENV="YES" -set -x +##enable_tracing## cmake -DCUSTOM_CACHE="YES" -DCMAKE_TOOLCHAIN_FILE="crosstool_bazel.cmake" -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_INSTALL_PREFIX="test_rule" -DCMAKE_PREFIX_PATH="$$EXT_BUILD_DEPS$$" -DCMAKE_RANLIB="" --debug-output -Wdev -G 'Ninja' $$EXT_BUILD_ROOT$$/external/test_rule -set +x +##disable_tracing## """ asserts.equals(env, expected.splitlines(), script)