├── cuda ├── main.h ├── main.cc ├── cuda.cc └── BUILD ├── .gitignore ├── README.md ├── WORKSPACE ├── tests ├── BUILD └── tests.cc ├── gtest.BUILD ├── cuda.BUILD ├── crosstool ├── crosstool_ar ├── BUILD ├── osx_wrapper.sh ├── CROSSTOOL └── crosstool_wrapper_driver_is_not_gcc └── .travis.yml /cuda/main.h: -------------------------------------------------------------------------------- 1 | void test(); 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | bazel* 3 | -------------------------------------------------------------------------------- /cuda/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include"main.h" 3 | 4 | int main() { 5 | test(); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /cuda/cuda.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #ifdef useCUDA 3 | #include 4 | __global__ void kernel() { 5 | printf("in kernel\n"); 6 | } 7 | #endif 8 | void test() { 9 | #ifdef useCUDA 10 | kernel<<<1,1>>>(); 11 | cudaDeviceSynchronize(); 12 | #endif 13 | } 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [![Build Status](https://travis-ci.org/Hibbert-pku/bazel_nvcc.svg?branch=master)](https://travis-ci.org/Hibbert-pku/bazel_nvcc) 3 | 4 | # bazel_nvcc 5 | use nvcc compiler in bazel 6 | 7 | # how to build 8 | 9 | bazel build cuda:all 10 | 11 | bazel test tests:all 12 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | new_http_archive( 2 | name = "gtest", 3 | url = "https://googletest.googlecode.com/files/gtest-1.7.0.zip", 4 | sha256 = "247ca18dd83f53deb1328be17e4b1be31514cedfc1e3424f672bf11fd7e0d60d", 5 | build_file = "gtest.BUILD", 6 | ) 7 | 8 | new_local_repository( 9 | name = "cuda", 10 | path = "/usr/local/cuda", 11 | build_file = "cuda.BUILD", 12 | ) 13 | 14 | -------------------------------------------------------------------------------- /tests/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | # TODO(xibai): how to use bazel test instead of bazel build & run tests? 4 | 5 | cc_test( 6 | name = "all_tests", 7 | srcs = ["tests.cc"], 8 | copts = ["-Iexternal/gtest/gtest-1.7.0/include", "-DuseCUDA"], 9 | linkopts = ["-L/usr/local/cuda/lib64 -L/usr/local/cuda/lib -lcuda -lcudart"], 10 | deps = ["@gtest//:main", "@cuda//:main"], 11 | ) 12 | -------------------------------------------------------------------------------- /gtest.BUILD: -------------------------------------------------------------------------------- 1 | cc_library( 2 | name = "main", 3 | srcs = glob( 4 | ["gtest-1.7.0/src/*.cc"], 5 | exclude = ["gtest-1.7.0/src/gtest-all.cc"] 6 | ), 7 | hdrs = glob([ 8 | "gtest-1.7.0/include/**/*.h", 9 | "gtest-1.7.0/src/*.h" 10 | ]), 11 | copts = [ 12 | "-Iexternal/gtest/gtest-1.7.0/include", 13 | "-Iexternal/gtest/gtest-1.7.0" 14 | ], 15 | linkopts = ["-pthread"], 16 | visibility = ["//visibility:public"], 17 | ) 18 | -------------------------------------------------------------------------------- /cuda/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | config_setting( 4 | name = "useCUDA", 5 | values = { 6 | "copt": "-DuseCUDA", 7 | }, 8 | ) 9 | 10 | cc_library( 11 | name = "cuda", 12 | srcs = select({":useCUDA": ["cuda.cc"], 13 | "//conditions:default": [] 14 | }) + ["main.h"], 15 | deps = ["@cuda//:main"], 16 | ) 17 | 18 | cc_binary( 19 | name = "main", 20 | srcs = ["main.cc"], 21 | deps = [":cuda"], 22 | ) 23 | -------------------------------------------------------------------------------- /cuda.BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | cc_library( 4 | name = "main", 5 | srcs = [ 6 | "lib64/libcublas.so.7.0", 7 | "lib64/libcudart.so.7.0", 8 | "lib64/libcufft.so.7.0", 9 | "lib64/libcurand.so.7.0", 10 | ], 11 | hdrs = glob([ 12 | "include/**/*.h", 13 | "include/**/*.hpp", 14 | "include/**/*.inl", 15 | ]), 16 | includes = ["include/"], 17 | linkopts = ["-Wl,-rpath,/usr/local/cuda/lib64"], 18 | ) 19 | -------------------------------------------------------------------------------- /crosstool/crosstool_ar: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from argparse import ArgumentParser 4 | import os 5 | import subprocess 6 | import re 7 | import sys 8 | import pipes 9 | 10 | CPU_COMPILER = ('/usr/bin/ar') 11 | 12 | def main(): 13 | 14 | cpu_compiler_flags = [flag for flag in sys.argv[1:]] 15 | for index, flag in enumerate(cpu_compiler_flags): 16 | if 'rcsD' in flag: 17 | cpu_compiler_flags[index] = 'rcs' 18 | return subprocess.call([CPU_COMPILER] + cpu_compiler_flags) 19 | 20 | if __name__ == '__main__': 21 | sys.exit(main()) 22 | -------------------------------------------------------------------------------- /tests/tests.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | #include "gtest/gtest.h" 9 | 10 | __global__ void kernel(int* x) { 11 | x[0]=1; 12 | } 13 | 14 | class CUDATest: public testing::Test { 15 | }; 16 | 17 | TEST_F(CUDATest, cuda) { 18 | int *x; 19 | cudaStream_t stream_; 20 | cudaStreamCreate(&stream_); 21 | cudaError_t err = cudaMalloc(&x, sizeof(int)*10); 22 | if (err != cudaSuccess) { 23 | exit(-1); 24 | } 25 | kernel<<<1,1,0,stream_>>>(x); 26 | cudaStreamSynchronize(stream_); 27 | cudaStreamDestroy(stream_); 28 | cudaFree(x); 29 | return; 30 | } 31 | -------------------------------------------------------------------------------- /crosstool/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | config_setting( 4 | name = "is_mac", 5 | values = { 6 | "cpu": "darwin", 7 | } 8 | ) 9 | 10 | cc_toolchain( 11 | name = "cc-compiler-local", 12 | all_files = ":empty", 13 | compiler_files = ":empty", 14 | cpu = "k8", 15 | dwp_files = ":empty", 16 | dynamic_runtime_libs = [":empty"], 17 | linker_files = ":empty", 18 | objcopy_files = ":empty", 19 | static_runtime_libs = [":empty"], 20 | strip_files = ":empty", 21 | supports_param_files = 0, 22 | ) 23 | 24 | filegroup( 25 | name = "crosstool", 26 | srcs = ["CROSSTOOL"], 27 | ) 28 | 29 | filegroup( 30 | name = "empty", 31 | srcs = [], 32 | ) 33 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | before_install: 3 | - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test 4 | - sudo apt-add-repository -y ppa:sao/backports 5 | - sudo apt-add-repository -y ppa:fcitx-team/nightly 6 | - sudo apt-add-repository -y ppa:asolovets/backports 7 | - sudo add-apt-repository ppa:apokluda/boost1.53 --yes 8 | - wget 'https://github.com/bazelbuild/bazel/releases/download/0.2.1/bazel-0.2.1-jdk7-installer-linux-x86_64.sh' 9 | - chmod +x bazel-0.2.1-jdk7-installer-linux-x86_64.sh 10 | - ./bazel-0.2.1-jdk7-installer-linux-x86_64.sh --user 11 | - wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1204/x86_64/cuda-repo-ubuntu1204_7.0-28_amd64.deb 12 | - sudo dpkg -i cuda-repo-ubuntu1204_7.0-28_amd64.deb 13 | - sudo apt-get update -qq 14 | - sudo apt-get install -qq libgflags-dev libgoogle-glog-dev libboost1.53-all-dev g++-4.9 cuda && sudo ldconfig 15 | install: 16 | - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 99 17 | - sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 99 18 | - g++ --version 19 | 20 | before_script: 21 | - export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH 22 | - export LD_LIBRARY_PATH=/usr/local/cuda/nvvm/lib64:$LD_LIBRARY_PATH 23 | - export PATH=/usr/local/cuda/bin:$PATH 24 | script: 25 | - bazel build tests:all --spawn_strategy=standalone --crosstool_top=//crosstool:crosstool --copt=-DuseCUDA 26 | - bazel build cuda:all --spawn_strategy=standalone --crosstool_top=//crosstool:crosstool --copt=-DuseCUDA 27 | -------------------------------------------------------------------------------- /crosstool/osx_wrapper.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Copyright 2015 Google Inc. All rights reserved. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | # OS X rpath is not really working. This is a wrapper script around gcc 18 | # to simulate rpath behavior. 19 | # 20 | # This wrapper uses install_name_tool to replace all paths in the binary 21 | # (bazel-out/.../path/to/original/library.so) by the paths relative to 22 | # the binary. It parses the command line to behaves as rpath is supposed 23 | # to work. 24 | # 25 | # See https://blogs.oracle.com/dipol/entry/dynamic_libraries_rpath_and_mac 26 | # on how to set those paths for Mach-O binaries. 27 | # 28 | set -eu 29 | 30 | GCC="/usr/bin/g++" 31 | INSTALL_NAME_TOOL="/usr/bin/install_name_tool" 32 | 33 | LIBS= 34 | LIB_DIRS= 35 | RPATH= 36 | OUTPUT= 37 | # let parse the option list 38 | for i in "$@"; do 39 | if [[ "${OUTPUT}" = "1" ]]; then 40 | OUTPUT=$i 41 | elif [[ "$i" =~ ^-l(.*)$ ]]; then 42 | # lib 43 | LIBS="${BASH_REMATCH[1]} $LIBS" 44 | elif [[ "$i" =~ ^-L(.*)$ ]]; then 45 | # lib 46 | LIB_DIRS="${BASH_REMATCH[1]} $LIB_DIRS" 47 | elif [[ "$i" =~ ^-Wl,-rpath,\$ORIGIN/(.*)$ ]]; then 48 | # rpath 49 | RPATH=${BASH_REMATCH[1]} 50 | elif [[ "$i" = "-o" ]]; then 51 | # output is coming 52 | OUTPUT=1 53 | fi 54 | done 55 | 56 | # Call gcc 57 | ${GCC} "$@" 58 | 59 | function get_library_path() { 60 | for libdir in ${LIB_DIRS}; do 61 | if [ -f ${libdir}/lib$1.so ]; then 62 | echo "${libdir}/lib$1.so" 63 | fi 64 | done 65 | } 66 | 67 | # A convenient method to return the actual path even for non symlinks 68 | # and multi-level symlinks. 69 | function get_realpath() { 70 | local previous="$1" 71 | local next=$(readlink "${previous}") 72 | while [ -n "${next}" ]; do 73 | previous="${next}" 74 | next=$(readlink "${previous}") 75 | done 76 | echo "${previous}" 77 | } 78 | 79 | # Get the path of a lib inside a tool 80 | function get_otool_path() { 81 | # the lib path is the path of the original lib relative to the workspace 82 | get_realpath $1 | sed 's|^.*/bazel-out/|bazel-out/|' 83 | } 84 | 85 | # Do replacements in the output 86 | if [ -n "${RPATH}" ]; then 87 | for lib in ${LIBS}; do 88 | libpath=$(get_library_path ${lib}) 89 | if [ -n "${libpath}" ]; then 90 | if [[ $lib == *"lite"* ]]; then 91 | ${INSTALL_NAME_TOOL} -change $(get_otool_path "${libpath}") "@loader_path/${RPATH}/google/protobuf/libprotobuf_lite.so" "${OUTPUT}" 92 | elif [[ $lib == *"protobuf"* ]]; then 93 | ${INSTALL_NAME_TOOL} -change $(get_otool_path "${libpath}") "@loader_path/${RPATH}/google/protobuf/libprotobuf.so" "${OUTPUT}" 94 | elif [[ $lib == *"proto"* ]]; then 95 | ${INSTALL_NAME_TOOL} -change $(get_otool_path "${libpath}") "@loader_path/${RPATH}/proto/liball.so" "${OUTPUT}" 96 | elif [[ $lib == *"extractors"* ]]; then 97 | ${INSTALL_NAME_TOOL} -change $(get_otool_path "${libpath}") "@loader_path/${RPATH}/crf/extractors/liball.so" "${OUTPUT}" 98 | elif [[ $lib == *"gtest"* ]]; then 99 | ${INSTALL_NAME_TOOL} -change $(get_otool_path "${libpath}") "@loader_path/${RPATH}/external/gtest/libmain.so" "${OUTPUT}" 100 | elif [[ $lib == *"util"* ]]; then 101 | ${INSTALL_NAME_TOOL} -change $(get_otool_path "${libpath}") "@loader_path/${RPATH}/util/liball.so" "${OUTPUT}" 102 | fi 103 | fi 104 | done 105 | fi 106 | -------------------------------------------------------------------------------- /crosstool/CROSSTOOL: -------------------------------------------------------------------------------- 1 | major_version: "local" 2 | minor_version: "" 3 | default_target_cpu: "same_as_host" 4 | 5 | default_toolchain { 6 | cpu: "k8" 7 | toolchain_identifier: "local_linux" 8 | } 9 | default_toolchain { 10 | cpu: "piii" 11 | toolchain_identifier: "local_linux" 12 | } 13 | default_toolchain { 14 | cpu: "darwin" 15 | toolchain_identifier: "local_darwin" 16 | } 17 | 18 | toolchain { 19 | abi_version: "local" 20 | abi_libc_version: "local" 21 | builtin_sysroot: "" 22 | compiler: "compiler" 23 | host_system_name: "local" 24 | needsPic: true 25 | supports_gold_linker: false 26 | supports_incremental_linker: false 27 | supports_fission: false 28 | supports_interface_shared_objects: false 29 | supports_normalizing_ar: false 30 | supports_start_end_lib: false 31 | supports_thin_archives: false 32 | target_libc: "local" 33 | target_cpu: "local" 34 | target_system_name: "local" 35 | toolchain_identifier: "local_linux" 36 | 37 | tool_path { name: "ar" path: "/usr/bin/ar" } 38 | tool_path { name: "compat-ld" path: "/usr/bin/ld" } 39 | tool_path { name: "cpp" path: "/usr/bin/cpp" } 40 | tool_path { name: "dwp" path: "/usr/bin/dwp" } 41 | # As part of the TensorFlow release, we place some cuda-related compilation 42 | # files in third_party/gpus/crosstool/clang/bin, and this relative 43 | # path, combined with the rest of our Bazel configuration causes our 44 | # compilation to use those files. 45 | tool_path { name: "gcc" path: "crosstool_wrapper_driver_is_not_gcc" } 46 | # Use "-std=c++11" for nvcc. For consistency, force both the host compiler 47 | # and the device compiler to use "-std=c++11". 48 | cxx_flag: "-std=c++11" 49 | linker_flag: "-lstdc++" 50 | linker_flag: "-B/usr/bin/" 51 | cxx_builtin_include_directory: "/usr/local/cuda/include" 52 | cxx_builtin_include_directory: "/usr/local/cuda/targets/x86_64-linux/include" 53 | 54 | # TODO(bazel-team): In theory, the path here ought to exactly match the path 55 | # used by gcc. That works because bazel currently doesn't track files at 56 | # absolute locations and has no remote execution, yet. However, this will need 57 | # to be fixed, maybe with auto-detection? 58 | cxx_builtin_include_directory: "/usr/lib/gcc/" 59 | cxx_builtin_include_directory: "/usr/local/include" 60 | cxx_builtin_include_directory: "/usr/include" 61 | tool_path { name: "gcov" path: "/usr/bin/gcov" } 62 | 63 | # C(++) compiles invoke the compiler (as that is the one knowing where 64 | # to find libraries), but we provide LD so other rules can invoke the linker. 65 | tool_path { name: "ld" path: "/usr/bin/ld" } 66 | 67 | tool_path { name: "nm" path: "/usr/bin/nm" } 68 | tool_path { name: "objcopy" path: "/usr/bin/objcopy" } 69 | objcopy_embed_flag: "-I" 70 | objcopy_embed_flag: "binary" 71 | tool_path { name: "objdump" path: "/usr/bin/objdump" } 72 | tool_path { name: "strip" path: "/usr/bin/strip" } 73 | 74 | # Anticipated future default. 75 | unfiltered_cxx_flag: "-no-canonical-prefixes" 76 | 77 | # Make C++ compilation deterministic. Use linkstamping instead of these 78 | # compiler symbols. 79 | unfiltered_cxx_flag: "-Wno-builtin-macro-redefined" 80 | 81 | # Security hardening on by default. 82 | # Conservative choice; -D_FORTIFY_SOURCE=2 may be unsafe in some cases. 83 | # We need to undef it before redefining it as some distributions now have 84 | # it enabled by default. 85 | compiler_flag: "-fstack-protector" 86 | compiler_flag: "-fPIE" 87 | linker_flag: "-pie" 88 | linker_flag: "-Wl,-z,relro,-z,now" 89 | 90 | # Enable coloring even if there's no attached terminal. Bazel removes the 91 | # escape sequences if --nocolor is specified. This isn't supported by gcc 92 | # on Ubuntu 14.04. 93 | compiler_flag: "-fdiagnostics-color=always" 94 | 95 | # All warnings are enabled. Maybe enable -Werror as well? 96 | compiler_flag: "-Wall" 97 | # Enable a few more warnings that aren't part of -Wall. 98 | # But disable some that are problematic. 99 | 100 | # Keep stack frames for debugging, even in opt mode. 101 | compiler_flag: "-fno-omit-frame-pointer" 102 | 103 | # Anticipated future default. 104 | linker_flag: "-no-canonical-prefixes" 105 | # Have gcc return the exit code from ld. 106 | linker_flag: "-pass-exit-codes" 107 | # Stamp the binary with a unique identifier. 108 | linker_flag: "-Wl,--build-id=md5" 109 | linker_flag: "-Wl,--hash-style=gnu" 110 | # Gold linker only? Can we enable this by default? 111 | # linker_flag: "-Wl,--warn-execstack" 112 | # linker_flag: "-Wl,--detect-odr-violations" 113 | 114 | compilation_mode_flags { 115 | mode: DBG 116 | # Enable debug symbols. 117 | compiler_flag: "-g" 118 | } 119 | compilation_mode_flags { 120 | mode: OPT 121 | 122 | # No debug symbols. 123 | # Maybe we should enable https://gcc.gnu.org/wiki/DebugFission for opt or 124 | # even generally? However, that can't happen here, as it requires special 125 | # handling in Bazel. 126 | compiler_flag: "-g0" 127 | 128 | # Conservative choice for -O 129 | # -O3 can increase binary size and even slow down the resulting binaries. 130 | # Profile first and / or use FDO if you need better performance than this. 131 | compiler_flag: "-O2" 132 | 133 | # Disable assertions 134 | compiler_flag: "-DNDEBUG" 135 | 136 | # Removal of unused code and data at link time (can this increase binary size in some cases?). 137 | compiler_flag: "-ffunction-sections" 138 | compiler_flag: "-fdata-sections" 139 | } 140 | } 141 | 142 | toolchain { 143 | abi_version: "local" 144 | abi_libc_version: "local" 145 | builtin_sysroot: "" 146 | compiler: "compiler" 147 | host_system_name: "local" 148 | needsPic: true 149 | supports_gold_linker: false 150 | supports_incremental_linker: false 151 | supports_fission: false 152 | supports_interface_shared_objects: false 153 | supports_normalizing_ar: false 154 | supports_start_end_lib: false 155 | supports_thin_archives: false 156 | target_libc: "local" 157 | target_cpu: "local" 158 | target_system_name: "local" 159 | toolchain_identifier: "local_darwin" 160 | 161 | tool_path { name: "ar" path: "crosstool_ar" } 162 | tool_path { name: "compat-ld" path: "/usr/bin/ld" } 163 | tool_path { name: "cpp" path: "/usr/bin/cpp" } 164 | tool_path { name: "dwp" path: "/usr/bin/dwp" } 165 | # As part of the TensorFlow release, we place some cuda-related compilation 166 | # files in third_party/gpus/crosstool/clang/bin, and this relative 167 | # path, combined with the rest of our Bazel configuration causes our 168 | # compilation to use those files. 169 | tool_path { name: "gcc" path: "crosstool_wrapper_driver_is_not_gcc" } 170 | # Use "-std=c++11" for nvcc. For consistency, force both the host compiler 171 | # and the device compiler to use "-std=c++11". 172 | cxx_flag: "-std=c++11" 173 | linker_flag: "-lstdc++" 174 | linker_flag: "-B/usr/bin/" 175 | 176 | # TODO(bazel-team): In theory, the path here ought to exactly match the path 177 | # used by gcc. That works because bazel currently doesn't track files at 178 | # absolute locations and has no remote execution, yet. However, this will need 179 | # to be fixed, maybe with auto-detection? 180 | cxx_builtin_include_directory: "/usr/lib/gcc/" 181 | cxx_builtin_include_directory: "/usr/local/include" 182 | cxx_builtin_include_directory: "/usr/include" 183 | tool_path { name: "gcov" path: "/usr/bin/gcov" } 184 | 185 | # C(++) compiles invoke the compiler (as that is the one knowing where 186 | # to find libraries), but we provide LD so other rules can invoke the linker. 187 | tool_path { name: "ld" path: "/usr/bin/ld" } 188 | 189 | tool_path { name: "nm" path: "/usr/bin/nm" } 190 | tool_path { name: "objcopy" path: "/usr/bin/objcopy" } 191 | objcopy_embed_flag: "-I" 192 | objcopy_embed_flag: "binary" 193 | tool_path { name: "objdump" path: "/usr/bin/objdump" } 194 | tool_path { name: "strip" path: "/usr/bin/strip" } 195 | 196 | # Anticipated future default. 197 | unfiltered_cxx_flag: "-no-canonical-prefixes" 198 | 199 | # Make C++ compilation deterministic. Use linkstamping instead of these 200 | # compiler symbols. 201 | 202 | # Security hardening on by default. 203 | # Conservative choice; -D_FORTIFY_SOURCE=2 may be unsafe in some cases. 204 | # We need to undef it before redefining it as some distributions now have 205 | # it enabled by default. 206 | compiler_flag: "-fstack-protector" 207 | compiler_flag: "-fPIE" 208 | 209 | # Enable coloring even if there's no attached terminal. Bazel removes the 210 | # escape sequences if --nocolor is specified. This isn't supported by gcc 211 | # on Ubuntu 14.04. 212 | compiler_flag: "-fcolor-diagnostics" 213 | 214 | # All warnings are enabled. Maybe enable -Werror as well? 215 | compiler_flag: "-Wall" 216 | # Enable a few more warnings that aren't part of -Wall. 217 | # But disable some that are problematic. 218 | 219 | # Keep stack frames for debugging, even in opt mode. 220 | compiler_flag: "-fno-omit-frame-pointer" 221 | 222 | # Anticipated future default. 223 | linker_flag: "-no-canonical-prefixes" 224 | # Have gcc return the exit code from ld. 225 | # Stamp the binary with a unique identifier. 226 | # Gold linker only? Can we enable this by default? 227 | # linker_flag: "-Wl,--warn-execstack" 228 | # linker_flag: "-Wl,--detect-odr-violations" 229 | 230 | compilation_mode_flags { 231 | mode: DBG 232 | # Enable debug symbols. 233 | compiler_flag: "-g" 234 | } 235 | compilation_mode_flags { 236 | mode: OPT 237 | 238 | # No debug symbols. 239 | # Maybe we should enable https://gcc.gnu.org/wiki/DebugFission for opt or 240 | # even generally? However, that can't happen here, as it requires special 241 | # handling in Bazel. 242 | compiler_flag: "-g0" 243 | 244 | # Conservative choice for -O 245 | # -O3 can increase binary size and even slow down the resulting binaries. 246 | # Profile first and / or use FDO if you need better performance than this. 247 | compiler_flag: "-O2" 248 | 249 | # Disable assertions 250 | compiler_flag: "-DNDEBUG" 251 | 252 | # Removal of unused code and data at link time (can this increase binary size in some cases?). 253 | compiler_flag: "-ffunction-sections" 254 | compiler_flag: "-fdata-sections" 255 | } 256 | } 257 | -------------------------------------------------------------------------------- /crosstool/crosstool_wrapper_driver_is_not_gcc: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from argparse import ArgumentParser 4 | from sys import platform as _platform 5 | import os 6 | import subprocess 7 | import re 8 | import sys 9 | import pipes 10 | 11 | CPU_COMPILER = ('/usr/bin/g++') 12 | GCC_COMPILER = ('/usr/bin/gcc') 13 | OTHER_COMPILER = ('crosstool/osx_wrapper.sh') 14 | NVCC_PATH = '/usr/local/cuda/bin/nvcc' 15 | GPP_PATH = '/usr/bin/g++' 16 | GCC_HOST_COMPILER_PATH = ('/usr/bin/gcc') 17 | LLVM_HOST_COMPILER_PATH = ('/usr/bin/gcc') 18 | PREFIX_DIR = os.path.dirname(GCC_HOST_COMPILER_PATH) 19 | 20 | def Log(s): 21 | print 'gpus/crosstool: {0}'.format(s) 22 | 23 | def GetOptionValue(argv, option): 24 | parser = ArgumentParser() 25 | parser.add_argument('-' + option, nargs='*', action='append') 26 | args, _ = parser.parse_known_args(argv) 27 | if not args or not vars(args)[option]: 28 | return [] 29 | else: 30 | return sum(vars(args)[option], []) 31 | 32 | def GetHostCompilerOptions(argv): 33 | parser = ArgumentParser() 34 | parser.add_argument('-isystem', nargs='*', action='append') 35 | parser.add_argument('-iquote', nargs='*', action='append') 36 | parser.add_argument('--sysroot', nargs=1) 37 | parser.add_argument('-g', nargs='*', action='append') 38 | 39 | args, _ = parser.parse_known_args(argv) 40 | 41 | opts = '' 42 | if args.g: 43 | opts += ' -g' + ' -g'.join(sum(args.g, [])) 44 | if args.sysroot: 45 | opts += ' --sysroot ' + args.sysroot[0] 46 | 47 | return opts 48 | 49 | def GetNvccOptions(argv): 50 | parser = ArgumentParser() 51 | parser.add_argument('-nvcc_options', nargs='*', action='append') 52 | 53 | args, _ = parser.parse_known_args(argv) 54 | 55 | if args.nvcc_options: 56 | return ' '.join(['--'+a for a in sum(args.nvcc_options, [])]) 57 | return '' 58 | 59 | def InvokeGpp(argv, log=False): 60 | host_compiler_options = GetHostCompilerOptions(argv) 61 | nvcc_compiler_options = GetNvccOptions(argv) 62 | opt_option = GetOptionValue(argv, 'O') 63 | f_options = GetOptionValue(argv, 'f') 64 | f_options = ''.join([' -f' + f for f in f_options]) 65 | include_options = GetOptionValue(argv, 'I') 66 | out_file = GetOptionValue(argv, 'o') 67 | depfiles = GetOptionValue(argv, 'MF') 68 | defines = GetOptionValue(argv, 'D') 69 | defines = ''.join([' -D' + define for define in defines]) 70 | undefines = GetOptionValue(argv, 'U') 71 | undefines = ''.join([' -U' + define for define in undefines]) 72 | std_options = GetOptionValue(argv, 'std') 73 | # currently only c++11 is supported by Cuda 7.0 std argument 74 | nvcc_allowed_std_options = ["c++11"] 75 | std_options = ''.join([' -std=' + define 76 | for define in std_options if define in nvcc_allowed_std_options]) 77 | 78 | # The list of source files get passed after the -c option. I don't know of 79 | # any other reliable way to just get the list of source files to be compiled. 80 | src_files = GetOptionValue(argv, 'c') 81 | if len(src_files) == 0: 82 | return 1 83 | if len(out_file) != 1: 84 | return 1 85 | opt = (' -O3' if (len(opt_option) > 0 and int(opt_option[0]) > 0) 86 | else ' -g') 87 | includes = (' -I ' + ' -I '.join(include_options) 88 | if len(include_options) > 0 89 | else '') 90 | # Unfortunately, there are other options that have -c prefix too. 91 | # So allowing only those look like C/C++ files. 92 | src_files = [f for f in src_files if 93 | re.search('\.cpp$|\.cc$|\.c$|\.cxx$|\.C$', f)] 94 | srcs = ' '.join(src_files) 95 | out = ' -o ' + out_file[0] 96 | # "configure" uses the specific format to substitute the following string. 97 | # If you change it, make sure you modify "configure" as well. 98 | supported_cuda_compute_capabilities = [ "3.5", "5.2" ] 99 | nvccopts = '' 100 | for capability in supported_cuda_compute_capabilities: 101 | capability = capability.replace('.', '') 102 | # nvccopts += r'-gencode=arch=compute_%s,\"code=sm_%s,compute_%s\" ' % ( 103 | # capability, capability, capability) 104 | #nvccopts += ' ' + nvcc_compiler_options 105 | #nvccopts += undefines 106 | nvccopts += defines 107 | nvccopts += std_options 108 | nvccopts += f_options 109 | nvccopts += " -I/usr/local/include -isystem /include -I../external/googletest -fPIC" 110 | if _platform != 'darwin': 111 | nvccopts += " -iquote bazel-out/local_linux-fastbuild/genfiles -iquote bazel-out/local_linux-dbg/genfiles -iquote bazel-out/local_linux-opt/genfiles" 112 | else: 113 | nvccopts += " -iquote bazel-out/local_darwin-fastbuild/genfiles -iquote bazel-out/local_darwin-dbg/genfiles -iquote bazel-out/local_darwin-opt/genfiles" 114 | if depfiles: 115 | # Generate the dependency file 116 | depfile = depfiles[0] 117 | cmd = (GPP_PATH + ' ' + nvccopts + 118 | ' -I .' + 119 | includes + ' ' + srcs + ' -M -o ' + depfile) 120 | if log: Log(cmd) 121 | exit_status = os.system(cmd) 122 | if exit_status != 0: 123 | return exit_status 124 | 125 | cmd = (GPP_PATH + ' ' + nvccopts + 126 | ' -I .' + 127 | opt + includes + ' -c ' + srcs + out) 128 | 129 | cmd = 'PATH=' + PREFIX_DIR + ' ' + cmd 130 | if log: Log(cmd) 131 | return os.system(cmd) 132 | 133 | def InvokeNvcc(argv, log=False): 134 | host_compiler_options = GetHostCompilerOptions(argv) 135 | nvcc_compiler_options = GetNvccOptions(argv) 136 | opt_option = GetOptionValue(argv, 'O') 137 | m_options = GetOptionValue(argv, 'm') 138 | m_options = ''.join([' -m' + m for m in m_options if m in ['32', '64']]) 139 | include_options = GetOptionValue(argv, 'I') 140 | out_file = GetOptionValue(argv, 'o') 141 | depfiles = GetOptionValue(argv, 'MF') 142 | defines = GetOptionValue(argv, 'D') 143 | defines = ''.join([' -D' + define for define in defines]) 144 | undefines = GetOptionValue(argv, 'U') 145 | undefines = ''.join([' -U' + define for define in undefines]) 146 | std_options = GetOptionValue(argv, 'std') 147 | # currently only c++11 is supported by Cuda 7.0 std argument 148 | nvcc_allowed_std_options = ["c++11"] 149 | std_options = ''.join([' -std=' + define 150 | for define in std_options if define in nvcc_allowed_std_options]) 151 | 152 | # The list of source files get passed after the -c option. I don't know of 153 | # any other reliable way to just get the list of source files to be compiled. 154 | src_files = GetOptionValue(argv, 'c') 155 | if len(src_files) == 0: 156 | return 1 157 | if len(out_file) != 1: 158 | return 1 159 | opt = (' -O3' if (len(opt_option) > 0 and int(opt_option[0]) > 0) 160 | else ' -g -G') 161 | includes = (' -I ' + ' -I '.join(include_options) 162 | if len(include_options) > 0 163 | else '') 164 | # Unfortunately, there are other options that have -c prefix too. 165 | # So allowing only those look like C/C++ files. 166 | src_files = [f for f in src_files if 167 | re.search('\.cpp$|\.cc$|\.c$|\.cxx$|\.C$', f)] 168 | srcs = ' '.join(src_files) 169 | out = ' -o ' + out_file[0] 170 | # "configure" uses the specific format to substitute the following string. 171 | # If you change it, make sure you modify "configure" as well. 172 | supported_cuda_compute_capabilities = [ "3.5", "5.2" ] 173 | nvccopts = '' 174 | for capability in supported_cuda_compute_capabilities: 175 | capability = capability.replace('.', '') 176 | # nvccopts += r'-gencode=arch=compute_%s,\"code=sm_%s,compute_%s\" ' % ( 177 | # capability, capability, capability) 178 | #nvccopts += ' ' + nvcc_compiler_options 179 | #nvccopts += undefines 180 | nvccopts += defines 181 | nvccopts += std_options 182 | #nvccopts += m_options 183 | nvccopts += " --disable-warnings -lineinfo -I/usr/local/include -isystem /include" 184 | if _platform != 'darwin': 185 | nvccopts += " -Ibazel-out/local_linux-fastbuild/genfiles -Ibazel-out/local_linux-dbg/genfiles -Ibazel-out/local_linux-opt/genfiles" 186 | else: 187 | nvccopts += " -Ibazel-out/local_darwin-fastbuild/genfiles -Ibazel-out/local_darwin-dbg/genfiles -Ibazel-out/local_darwin-opt/genfiles" 188 | if depfiles: 189 | # Generate the dependency file 190 | depfile = depfiles[0] 191 | cmd = (NVCC_PATH + ' ' + nvccopts + 192 | ' --compiler-options "' + host_compiler_options + '"' + 193 | ' --compiler-bindir=' + GCC_HOST_COMPILER_PATH + 194 | ' -I .' + 195 | ' -x cu ' + includes + ' ' + srcs + ' -M -o ' + depfile) 196 | if log: Log(cmd) 197 | exit_status = os.system(cmd) 198 | if exit_status != 0: 199 | return exit_status 200 | 201 | cmd = (NVCC_PATH + ' ' + nvccopts + 202 | ' --compiler-options "' + host_compiler_options + ' -fPIC"' + 203 | ' --compiler-bindir=' + GCC_HOST_COMPILER_PATH + 204 | ' -I .' + 205 | ' -x cu ' + opt + includes + ' -c ' + srcs + out) 206 | 207 | cmd = 'PATH=' + PREFIX_DIR + ' ' + cmd + ' --ftz=true --prec-div=false --prec-sqrt=false --fmad=true --use_fast_math' 208 | if log: Log(cmd) 209 | return os.system(cmd) 210 | 211 | def main(): 212 | origin_cmd = [flag for flag in sys.argv[1:]] 213 | origin_flag = 0 214 | # compile & link protobuf src 215 | for index, flag in enumerate(origin_cmd): 216 | if '.o' in flag: 217 | if 'tools' in flag: 218 | origin_flag = 1 219 | if 'tests' in flag: 220 | origin_flag = 1 221 | if 'cuda' in flag: 222 | origin_flag = 1 223 | if 'util/vlist' in flag: 224 | origin_flag = 1 225 | if 'crf' in flag: 226 | origin_flag = 1 227 | src_files = GetOptionValue(origin_cmd, 'c') 228 | if origin_flag == 0: 229 | if len(src_files) == 0 and _platform == 'darwin': 230 | origin_cmd.append('-L/usr/local/lib') 231 | origin_cmd.append('-lglog') 232 | return subprocess.call([GCC_COMPILER] + origin_cmd) 233 | 234 | # compile 235 | if len(src_files) != 0: 236 | # cuda 237 | is_cuda = GetOptionValue(origin_cmd, 'D') 238 | for index, flag in enumerate(is_cuda): 239 | if 'useCUDA' in flag: 240 | return InvokeNvcc(origin_cmd) 241 | return InvokeGpp(origin_cmd) 242 | 243 | # link 244 | 245 | com_flag = 0 246 | for index, flag in enumerate(origin_cmd): 247 | if 'all_tests' in flag: 248 | com_flag = 1 249 | break 250 | origin_cmd.append('-L/usr/local/lib') 251 | origin_cmd.append('-lglog') 252 | if com_flag and _platform == 'darwin': 253 | origin_cmd.append('-Wl,-rpath,$ORIGIN/../../bin') 254 | return subprocess.call([OTHER_COMPILER] + origin_cmd) 255 | else: 256 | return subprocess.call([CPU_COMPILER] + origin_cmd) 257 | 258 | if __name__ == '__main__': 259 | sys.exit(main()) 260 | --------------------------------------------------------------------------------