From 0aab4e65c5a79987d0a44e863a77f5828aeb7b1b Mon Sep 17 00:00:00 2001 From: irengrig Date: Tue, 11 Sep 2018 11:43:13 +0200 Subject: [PATCH] Win changes (#85) * Allow execution on Windows, use utils_win.sh, copy instead of symlink * Prepare for execution on Windows: set up environment, convert path also rename TMPDIR since it can clash with widely used variable --- .bazelrc | 2 +- for_workspace/cmake_build.bzl | 6 +- for_workspace/utils_osx.sh | 7 ++ for_workspace/utils_unix.sh | 7 ++ for_workspace/utils_win.bat | 0 for_workspace/utils_win.sh | 123 +++++++++++++++++++++++++++++++++ tools/build_defs/cmake.bzl | 2 +- tools/build_defs/framework.bzl | 21 +++--- workspace_definitions.bzl | 3 +- 9 files changed, 155 insertions(+), 16 deletions(-) delete mode 100644 for_workspace/utils_win.bat create mode 100644 for_workspace/utils_win.sh diff --git a/.bazelrc b/.bazelrc index 95201f30..0858f381 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1 +1 @@ -build --experimental_cc_skylark_api_enabled_packages=@rules_foreign_cc//tools/build_defs,tools/build_defs \ No newline at end of file +build --experimental_cc_skylark_api_enabled_packages=@rules_foreign_cc//tools/build_defs,tools/build_defs --action_env=PATH --test_env=PATH \ No newline at end of file diff --git a/for_workspace/cmake_build.bzl b/for_workspace/cmake_build.bzl index 3d458158..23fb7785 100644 --- a/for_workspace/cmake_build.bzl +++ b/for_workspace/cmake_build.bzl @@ -8,10 +8,10 @@ def _cmake_tool(ctx): cmake = ctx.actions.declare_directory("cmake") script_text = "\n".join([ "BUILD_DIR=$(pwd)", - "export TMPDIR=$(mktemp -d)", - "cp -R ./{}/. $TMPDIR".format(root), + "export BUILD_TMPDIR=$(mktemp -d)", + "cp -R ./{}/. $BUILD_TMPDIR".format(root), "mkdir " + cmake.path, - "pushd $TMPDIR", + "pushd $BUILD_TMPDIR", "./bootstrap --prefix=install", "make install", "cp -a ./install/. $BUILD_DIR/" + cmake.path, diff --git a/for_workspace/utils_osx.sh b/for_workspace/utils_osx.sh index 81acdea2..95051f78 100644 --- a/for_workspace/utils_osx.sh +++ b/for_workspace/utils_osx.sh @@ -2,6 +2,7 @@ # default placeholder value REPLACE_VALUE='BAZEL_GEN_ROOT' +export BUILD_PWD=$(pwd) # Echo variables in the format # var1_name="var1_value" @@ -124,3 +125,9 @@ function define_absolute_paths() { function replace_absolute_paths() { replace_in_files $1 $2 $REPLACE_VALUE } + +# function for setting necessary environment variables for the platform +function set_platform_env_vars() { + # empty for Mac OS + return 0 +} \ No newline at end of file diff --git a/for_workspace/utils_unix.sh b/for_workspace/utils_unix.sh index 0dc81d13..df090558 100644 --- a/for_workspace/utils_unix.sh +++ b/for_workspace/utils_unix.sh @@ -2,6 +2,7 @@ # default placeholder value REPLACE_VALUE='BAZEL_GEN_ROOT' +export BUILD_PWD=$(pwd) # Echo variables in the format # var1_name="var1_value" @@ -123,3 +124,9 @@ function define_absolute_paths() { function replace_absolute_paths() { replace_in_files $1 $2 $REPLACE_VALUE } + +# function for setting necessary environment variables for the platform +function set_platform_env_vars() { + # empty for Linux + return 0 +} \ No newline at end of file diff --git a/for_workspace/utils_win.bat b/for_workspace/utils_win.bat deleted file mode 100644 index e69de29b..00000000 diff --git a/for_workspace/utils_win.sh b/for_workspace/utils_win.sh new file mode 100644 index 00000000..8ddd258e --- /dev/null +++ b/for_workspace/utils_win.sh @@ -0,0 +1,123 @@ +#!/usr/bin/env bash + +# default placeholder value +REPLACE_VALUE='BAZEL_GEN_ROOT' +export BUILD_PWD=$(cygpath -m $(pwd)) + +# Echo variables in the format +# var1_name="var1_value" +# ... +# vark_name="vark_value" +# +# arguments: the names of the variables +function echo_vars() { + for arg in "$@" + do + echo_var "$arg" + done +} + +# Echo variable in the format var_name="var_value" +# $1 the name of the variable +function echo_var() { + local name="$1" + local value=${!name} + echo "$name: \"${value}\"" +} + +# Wrap the function execution in the echo lines: +# --- START $1 +# (anything printed by $2) +# --- END $1 +# $1 parameter to be printed next to start/end text +# $2 function to call in between START and END +function wrap() { + echo "--- START $1:" + $2 + echo "--- END $1" +} + +# Append string to PATH variable +# $1 string to append +function path() { + export PATH="$1:$PATH" +} + +# Replace string in all files in directory +# $1 directory to search recursively, absolute path +# $2 string to replace +# $3 replace target +function replace_in_files() { + if [ -d "$1" ]; then + find $1 -type f -exec sed -i 's@'"$2"'@'"$3"'@g' {} ';' + fi +} + +# copies contents of the directory to target directory (create the target directory if needed) +# $1 source directory, immediate children of which are copied +# $2 target directory +function copy_dir_contents_to_dir() { + local children=$(find $1 -maxdepth 1 -mindepth 1) + local target="$2" + mkdir -p ${target} + for child in $children; do + cp -R $child ${target} + done +} + +# Symlink contents of the directory to target directory (create the target directory if needed). +# If file is passed, symlink it into the target directory. +# $1 source directory, immediate children of which are symlinked, or file to be symlinked. +# $2 target directory +function symlink_contents_to_dir() { + local target="$2" + mkdir -p ${target} + if [[ -f $1 ]]; then + symlink_to_dir $1 ${target} + return 0 + fi + local children=$(find $1 -maxdepth 1 -mindepth 1) + for child in $children; do + symlink_to_dir $child ${target} + done +} + +# COPIES all files from source directory to target directory (create the target directory if needed) +# (Windows complains about long filenames with symlink command, so copy them) +# NB symlinks from the source directory are copied +# $1 source directory +# $2 target directory +function symlink_to_dir() { + cp $1 $2 +} + +# Copy all files from source directory to target directory (create the target directory if needed), +# and add target paths on to path +# $1 source directory +# $2 target directory +function copy_and_add_to_path() { + copy_to_dir $1 $2 + path $2/bin + path $2 +} + +# replace placeholder with absolute path in all files in a directory +# $1 directory +# $2 absolute path +function define_absolute_paths() { + replace_in_files $1 $REPLACE_VALUE $2 +} + +# replace the absolute path with a placeholder value in all files in a directory +# $1 directory +# $2 absolute path to replace +function replace_absolute_paths() { + replace_in_files $1 $2 $REPLACE_VALUE +} + +# function for setting necessary environment variables for the platform +function set_platform_env_vars() { + export MSYS_NO_PATHCONV=1 + export MSYS2_ARG_CONV_EXCL="*" + export SYSTEMDRIVE="C:" +} \ No newline at end of file diff --git a/tools/build_defs/cmake.bzl b/tools/build_defs/cmake.bzl index 984d6560..bda6649a 100644 --- a/tools/build_defs/cmake.bzl +++ b/tools/build_defs/cmake.bzl @@ -40,7 +40,7 @@ def _cmake_external(ctx): dict(ctx.attr.env_vars), ctx.attr.cmake_options, ) - copy_results = "copy_dir_contents_to_dir $TMPDIR/{} $INSTALLDIR".format(install_prefix) + copy_results = "copy_dir_contents_to_dir $BUILD_TMPDIR/{} $INSTALLDIR".format(install_prefix) tools_deps = ctx.attr.tools_deps + ([ctx.attr._cmake_dep] if hasattr(ctx.attr, "_cmake_dep") else []) attrs = create_attrs( diff --git a/tools/build_defs/framework.bzl b/tools/build_defs/framework.bzl index 84342a99..c9bc88b0 100644 --- a/tools/build_defs/framework.bzl +++ b/tools/build_defs/framework.bzl @@ -169,20 +169,23 @@ def cc_external_rule_impl(ctx, attrs): shell_utils = ctx.attr._utils.files.to_list()[0].path script_lines = [ - "set -e", - "export EXT_BUILD_ROOT=$(pwd)", - "source " + shell_utils, "echo \"Building external library '{}'\"".format(lib_name), - "export TMPDIR=$(mktemp -d)", - "trap \"{ rm -rf $TMPDIR $EXT_BUILD_ROOT/bazel_foreign_cc_deps; }\" EXIT", + "set -e", + "source " + shell_utils, + "set_platform_env_vars", + "export EXT_BUILD_ROOT=$BUILD_PWD", + "export BUILD_TMPDIR=$(mktemp -d)", "export EXT_BUILD_DEPS=$EXT_BUILD_ROOT/bazel_foreign_cc_deps", "mkdir -p $EXT_BUILD_DEPS", - "\n".join(_copy_deps_and_tools(inputs)), - "define_absolute_paths $EXT_BUILD_ROOT/bin $EXT_BUILD_ROOT/bin", "export INSTALLDIR=$EXT_BUILD_ROOT/" + outputs.installdir.path, "mkdir -p $INSTALLDIR", - "echo_vars INSTALLDIR EXT_BUILD_DEPS EXT_BUILD_ROOT PATH", - "pushd $TMPDIR", + "echo \"Environment:______________\"", + "env", + "echo \"__________________________\"", + "trap \"{ rm -rf $BUILD_TMPDIR $EXT_BUILD_ROOT/bazel_foreign_cc_deps; }\" EXIT", + "\n".join(_copy_deps_and_tools(inputs)), + "define_absolute_paths $EXT_BUILD_ROOT/bin $EXT_BUILD_ROOT/bin", + "pushd $BUILD_TMPDIR", attrs.configure_script, "\n".join(attrs.make_commands), _value(attrs.postfix_script, ""), diff --git a/workspace_definitions.bzl b/workspace_definitions.bzl index 5a568e14..79a351fb 100644 --- a/workspace_definitions.bzl +++ b/workspace_definitions.bzl @@ -28,8 +28,7 @@ def _shell_utils_text(rctx, host_os): if host_os.is_osx: utils_name = "utils_osx.sh" if host_os.is_win: - utils_name = "utils_win.bat" - fail("Not supported yet!") + utils_name = "utils_win.sh" path = rctx.path(Label("//for_workspace:" + utils_name)) rctx.template(utils_name, path, executable = True)