Skip to content

Commit

Permalink
tensorboard: add crosstool files for osx
Browse files Browse the repository at this point in the history
  • Loading branch information
nehaljwani committed Aug 10, 2018
1 parent 461de6f commit 4b9b007
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 2 deletions.
28 changes: 26 additions & 2 deletions tensorboard/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,32 @@ rm -rf "${SP_DIR}/setuptools/script (dev).tmpl"

# build using bazel
mkdir -p ./bazel_output_base
export BAZEL_OPTS="--batch"
bazel ${BAZEL_OPTS} build //tensorboard/pip_package:build_pip_package
BAZEL_OPTS="--batch"
if [[ ${HOST} =~ .*darwin.* ]]; then
# set up bazel config file for conda provided clang toolchain
cp -r ${RECIPE_DIR}/custom_clang_toolchain .
cd custom_clang_toolchain
sed -e "s:\${CLANG}:${CLANG}:" \
-e "s:\${INSTALL_NAME_TOOL}:${INSTALL_NAME_TOOL}:" \
-e "s:\${CONDA_BUILD_SYSROOT}:${CONDA_BUILD_SYSROOT}:" \
cc_wrapper.sh.template > cc_wrapper.sh
chmod +x cc_wrapper.sh
sed -e "s:\${PREFIX}:${BUILD_PREFIX}:" \
-e "s:\${LD}:${LD}:" \
-e "s:\${NM}:${NM}:" \
-e "s:\${STRIP}:${STRIP}:" \
-e "s:\${LIBTOOL}:${LIBTOOL}:" \
-e "s:\${CONDA_BUILD_SYSROOT}:${CONDA_BUILD_SYSROOT}:" \
CROSSTOOL.template > CROSSTOOL
cd ..

# set build arguments
export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
BUILD_OPTS="
--crosstool_top=//custom_clang_toolchain:toolchain
--verbose_failures"
fi
bazel ${BAZEL_OPTS} build ${BUILD_OPTS:-} //tensorboard/pip_package:build_pip_package

# Adapted from: https://github.com/tensorflow/tensorboard/blob/1.9.0/tensorboard/pip_package/build_pip_package.sh
if [ "$(uname)" = "Darwin" ]; then
Expand Down
37 changes: 37 additions & 0 deletions tensorboard/custom_clang_toolchain/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package(default_visibility = ["//visibility:public"])

filegroup(
name = "empty",
srcs = [],
)

filegroup(
name = "cc_wrapper",
srcs = ["cc_wrapper.sh"],
)

filegroup(
name = "compiler_deps",
srcs = glob(["extra_tools/**"]) + [":cc_wrapper"],
)

cc_toolchain_suite(
name = "toolchain",
toolchains = {
"darwin|compiler": ":cc-compiler-darwin",
},
)

cc_toolchain(
name = "cc-compiler-darwin",
all_files = ":compiler_deps",
compiler_files = ":compiler_deps",
cpu = "darwin",
dwp_files = ":empty",
dynamic_runtime_libs = [":empty"],
linker_files = ":compiler_deps",
objcopy_files = ":empty",
static_runtime_libs = [":empty"],
strip_files = ":empty",
supports_param_files = 0,
)
82 changes: 82 additions & 0 deletions tensorboard/custom_clang_toolchain/CROSSTOOL.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
major_version: "local"
minor_version: ""
default_target_cpu: "same_as_host"

default_toolchain {
cpu: "darwin"
toolchain_identifier: "local"
}

toolchain {
toolchain_identifier: "local"
abi_version: "local"
abi_libc_version: "local"
builtin_sysroot: ""
compiler: "compiler"
host_system_name: "local"
needsPic: true
supports_gold_linker: false
supports_incremental_linker: false
supports_fission: false
supports_interface_shared_objects: false
supports_normalizing_ar: false
supports_start_end_lib: false
target_libc: "macosx"
target_cpu: "darwin"
target_system_name: "local"
cxx_flag: "-stdlib=libc++"
cxx_flag: "-fvisibility-inlines-hidden"
cxx_flag: "-std=c++14"
cxx_flag: "-fmessage-length=0"
linker_flag: "-Wl,-pie"
linker_flag: "-headerpad_max_install_names"
linker_flag: "-Wl,-dead_strip_dylibs"
linker_flag: "-lc++"
linker_flag: "-undefined"
linker_flag: "dynamic_lookup"
cxx_builtin_include_directory: "${PREFIX}/include/c++/v1"
cxx_builtin_include_directory: "${PREFIX}/lib/clang/4.0.1/include"
cxx_builtin_include_directory: "${CONDA_BUILD_SYSROOT}/usr/include"
cxx_builtin_include_directory: "${CONDA_BUILD_SYSROOT}/System/Library/Frameworks"
objcopy_embed_flag: "-I"
objcopy_embed_flag: "binary"
unfiltered_cxx_flag: "-no-canonical-prefixes"
unfiltered_cxx_flag: "-Wno-builtin-macro-redefined"
unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
compiler_flag: "-march=core2"
compiler_flag: "-mtune=haswell"
compiler_flag: "-mssse3"
compiler_flag: "-ftree-vectorize"
compiler_flag: "-fPIC"
compiler_flag: "-fPIE"
compiler_flag: "-fstack-protector-strong"
compiler_flag: "-O1"
compiler_flag: "-pipe"
tool_path {name: "ld" path: "${LD}" }
tool_path {name: "cpp" path: "/usr/bin/cpp" }
tool_path {name: "dwp" path: "/usr/bin/dwp" }
tool_path {name: "gcov" path: "/usr/bin/gcov" }
tool_path {name: "nm" path: "${NM}" }
tool_path {name: "objcopy" path: "/usr/bin/objcopy" }
tool_path {name: "objdump" path: "/usr/bin/objdump" }
tool_path {name: "strip" path: "${STRIP}" }
tool_path {name: "gcc" path: "cc_wrapper.sh" }
tool_path {name: "ar" path: "${LIBTOOL}" }

compilation_mode_flags {
mode: DBG
compiler_flag: "-g"
}
compilation_mode_flags {
mode: OPT
compiler_flag: "-g0"
compiler_flag: "-O1"
compiler_flag: "-D_FORTIFY_SOURCE=1"
compiler_flag: "-DNDEBUG"
compiler_flag: "-ffunction-sections"
compiler_flag: "-fdata-sections"
}
linking_mode_flags { mode: DYNAMIC }
}
1 change: 1 addition & 0 deletions tensorboard/custom_clang_toolchain/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspace(name = "local_config_cc")
106 changes: 106 additions & 0 deletions tensorboard/custom_clang_toolchain/cc_wrapper.sh.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/bin/bash
#
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# OS X relpath is not really working. This is a wrapper script around gcc
# to simulate relpath behavior.
#
# This wrapper uses install_name_tool to replace all paths in the binary
# (bazel-out/.../path/to/original/library.so) by the paths relative to
# the binary. It parses the command line to behave as rpath is supposed
# to work.
#
# See https://blogs.oracle.com/dipol/entry/dynamic_libraries_rpath_and_mac
# on how to set those paths for Mach-O binaries.
#
set -eu

INSTALL_NAME_TOOL="${INSTALL_NAME_TOOL}"

LIBS=
LIB_DIRS=
RPATHS=
OUTPUT=
# let parse the option list
for i in "$@"; do
if [[ "${OUTPUT}" = "1" ]]; then
OUTPUT=$i
elif [[ "$i" =~ ^-l(.*)$ ]]; then
LIBS="${BASH_REMATCH[1]} $LIBS"
elif [[ "$i" =~ ^-L(.*)$ ]]; then
LIB_DIRS="${BASH_REMATCH[1]} $LIB_DIRS"
elif [[ "$i" =~ ^-Wl,-rpath,\@loader_path/(.*)$ ]]; then
RPATHS="${BASH_REMATCH[1]} ${RPATHS}"
elif [[ "$i" = "-o" ]]; then
# output is coming
OUTPUT=1
fi
done

# Set-up the environment


# Call the C++ compiler
CONDA_BUILD_SYSROOT=${CONDA_BUILD_SYSROOT} ${CLANG} "$@"

function get_library_path() {
for libdir in ${LIB_DIRS}; do
if [ -f ${libdir}/lib$1.so ]; then
echo "${libdir}/lib$1.so"
elif [ -f ${libdir}/lib$1.dylib ]; then
echo "${libdir}/lib$1.dylib"
fi
done
}

# A convenient method to return the actual path even for non symlinks
# and multi-level symlinks.
function get_realpath() {
local previous="$1"
local next=$(readlink "${previous}")
while [ -n "${next}" ]; do
previous="${next}"
next=$(readlink "${previous}")
done
echo "${previous}"
}

# Get the path of a lib inside a tool
function get_otool_path() {
# the lib path is the path of the original lib relative to the workspace
get_realpath $1 | sed 's|^.*/bazel-out/|bazel-out/|'
}

# Do replacements in the output
for rpath in ${RPATHS}; do
for lib in ${LIBS}; do
unset libname
if [ -f "$(dirname ${OUTPUT})/${rpath}/lib${lib}.so" ]; then
libname="lib${lib}.so"
elif [ -f "$(dirname ${OUTPUT})/${rpath}/lib${lib}.dylib" ]; then
libname="lib${lib}.dylib"
fi
# ${libname-} --> return $libname if defined, or undefined otherwise. This is to make
# this set -e friendly
if [[ -n "${libname-}" ]]; then
libpath=$(get_library_path ${lib})
if [ -n "${libpath}" ]; then
${INSTALL_NAME_TOOL} -change $(get_otool_path "${libpath}") \
"@loader_path/${rpath}/${libname}" "${OUTPUT}"
fi
fi
done
done

0 comments on commit 4b9b007

Please sign in to comment.