diff --git a/tools/build_defs/framework.bzl b/tools/build_defs/framework.bzl index 42ba370d..717b5b47 100644 --- a/tools/build_defs/framework.bzl +++ b/tools/build_defs/framework.bzl @@ -80,9 +80,8 @@ CC_EXTERNAL_RULE_ATTRIBUTES = { # # link to the shell utilities used by the shell script in cc_external_rule_impl. "_utils": attr.label( - default = Label("//tools/build_defs:utils.sh"), + default = "@foreign_cc_platform_utils//:shell_utils", allow_single_file = True, - executable = True, cfg = "target", ), # we need to declare this attribute to access cc_toolchain @@ -165,7 +164,7 @@ def cc_external_rule_impl(ctx, attrs): "echo \"Building external library '{}'\"".format(lib_name), "export TMPDIR=$(mktemp -d)", "trap \"{ rm -rf $TMPDIR; }\" EXIT", - "export EXT_BUILD_DEPS=$(mktemp -d --tmpdir=$EXT_BUILD_ROOT)", + "export EXT_BUILD_DEPS=$(create_tmp_dir $EXT_BUILD_ROOT)", "\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, diff --git a/tools/build_defs/utils.sh b/tools/build_defs/utils_osx.sh similarity index 94% rename from tools/build_defs/utils.sh rename to tools/build_defs/utils_osx.sh index 0dc81d13..4e7ab231 100644 --- a/tools/build_defs/utils.sh +++ b/tools/build_defs/utils_osx.sh @@ -123,3 +123,10 @@ function define_absolute_paths() { function replace_absolute_paths() { replace_in_files $1 $2 $REPLACE_VALUE } + +# create temp directory in specific directory +# $1 directory where to create temp directory +function create_tmp_dir() { + local in_folder=$1 + mktemp -d -t ${in_folder} +} diff --git a/tools/build_defs/utils_unix.sh b/tools/build_defs/utils_unix.sh new file mode 100644 index 00000000..c3e1d1f7 --- /dev/null +++ b/tools/build_defs/utils_unix.sh @@ -0,0 +1,132 @@ +#!/usr/bin/env bash + +# default placeholder value +REPLACE_VALUE='BAZEL_GEN_ROOT' + +# 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 +} + +# Symlink all files from source directory to target directory (create the target directory if needed) +# NB symlinks from the source directory are copied +# $1 source directory +# $2 target directory +function symlink_to_dir() { + local target="$2" + mkdir -p ${target} + + if [[ -d $1 ]]; then + ln -s -t ${target} $1 + elif [[ -f $1 ]]; then + ln -s -t ${target} $1 + elif [[ -L $1 ]]; then + cp $1 ${target} + else + echo "Can not copy $1" + fi +} + +# 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 +} + +# create temp directory in specific directory +# $1 directory where to create temp directory +function create_tmp_dir() { + local in_folder=$1 + mktemp -d --tmpdir=${in_folder} +} \ No newline at end of file diff --git a/tools/build_defs/utils_win.bat b/tools/build_defs/utils_win.bat new file mode 100644 index 00000000..e69de29b diff --git a/workspace_definitions.bzl b/workspace_definitions.bzl index a8e7e34e..65e8e3e5 100644 --- a/workspace_definitions.bzl +++ b/workspace_definitions.bzl @@ -1,5 +1,28 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +def _define_shell_utils_impl(rctx): + os_name = rctx.os.name.lower() + utils_name = "utils_unix.sh" + if os_name.startswith("mac os"): + utils_name = "utils_osx.sh" + if os_name.find("windows") != -1: + utils_name = "utils_win.bat" + fail("Not supported yet!") + rctx.file("WORKSPACE", "workspace(name='foreign_cc_platform_utils')") + rctx.file("BUILD.bazel", """ +sh_library( + name = "shell_utils", + srcs = ["{}"], + visibility = ["//visibility:public"] +) +""".format(utils_name)) + path = rctx.path(Label("//tools/build_defs:" + utils_name)) + rctx.template(utils_name, path, executable = True) + +_define_shell_utils = repository_rule( + implementation = _define_shell_utils_impl, +) + def rules_foreign_cc_dependencies(): _all_content = """filegroup(name = "all", srcs = glob(["**"]), visibility = ["//visibility:public"])""" @@ -13,3 +36,5 @@ def rules_foreign_cc_dependencies(): "https://github.com/bazelbuild/bazel-skylib/archive/0.5.0.tar.gz", ], ) + + _define_shell_utils(name = "foreign_cc_platform_utils")