diff --git a/.bazelrc b/.bazelrc index e571676ad6..15327ed801 100644 --- a/.bazelrc +++ b/.bazelrc @@ -6,22 +6,35 @@ # editor's search path. build --symlink_prefix=dist/ -# Use our custom-configured c++ toolchain. -build:wasm --crosstool_top=//toolchain:emscripten +# These compile flags are active no matter which build mode we are in +# (dbg vs opt). For flags specific to build mode, see cc_toolchain_config.bzl. +build --cxxopt="-std=c++11" +build --cxxopt="-fno-rtti" +build --cxxopt="-fno-exceptions" +build --cxxopt="-fomit-frame-pointer" -# Use --cpu as a differentiator. -build:wasm --cpu=wasm +# Remote cache config. Users can add credentials in their .bazelrc.user files. +build:remote --remote_cache=https://storage.googleapis.com/bazel-remote-cache-tfjs -# Use the default C++ toolchain to build the tools used during the build. -build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain +# Config for Google Cloud continuous integration that uses default credentials. +build:ci --config=remote +build:ci --google_default_credentials -# These compile flags are active no matter which build mode we are in -# (dbg vs opt). For flags specific to build mode, see cc_toolchain_config.bzl. -build:wasm --cxxopt="-std=c++11" -build:wasm --cxxopt="-fno-rtti" -build:wasm --cxxopt="-fno-exceptions" -build:wasm --cxxopt="-fomit-frame-pointer" +# This flag is needed to prevent the bazel cache from being invalidated when +# running bazel via `yarn bazel`. +# See https://github.com/angular/angular/issues/27514. +build --incompatible_strict_action_env +run --incompatible_strict_action_env + +# Don't use a sandboxed build since it hurts performance. +# When sandboxed, the wasm backend takes hours to build on a 32 core machine. +build --spawn_strategy=worker,local -# Disable sandbox environment because emsdk caches files by writing to -# home directory. -build:wasm --spawn_strategy=local +# Load any settings specific to the current user. +# .bazelrc.user should appear in .gitignore so that settings are not shared with +# team members. This needs to be last statement in this config, as the user +# configuration should be able to overwrite flags from this file. +# See https://docs.bazel.build/versions/master/best-practices.html#bazelrc +# (Note that we use .bazelrc.user so the file appears next to .bazelrc in +# directory listing, rather than user.bazelrc as suggested in the Bazel docs). +try-import %workspace%/.bazelrc.user diff --git a/.gitignore b/.gitignore index ac42a7a5a6..20e852c787 100644 --- a/.gitignore +++ b/.gitignore @@ -59,4 +59,7 @@ tfjs-backend-wasm/wasm-out/*.js tfjs-backend-wasm/wasm-out/*.wasm yalc.lock yarn-error.log -cloudbuild_generated.yml \ No newline at end of file +cloudbuild_generated.yml + +# User-specific .bazelrc +.bazelrc.user \ No newline at end of file diff --git a/WORKSPACE b/WORKSPACE index c2c22aaf04..d5e19bcb9b 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -12,18 +12,28 @@ yarn_install( yarn_lock = "//:yarn.lock", ) -# Make all files under $HOME/emsdk/* visible to the toolchain. The files are -# available as external/emsdk/emsdk/* -load("//toolchain:cc_toolchain_config.bzl", "emsdk_configure") -emsdk_configure(name = "emsdk") +# Emscripten toolchain +http_archive( + name = "emsdk", + strip_prefix = "emsdk-c1589b55641787d55d53e883852035beea9aec3f/bazel", + url = "https://github.com/emscripten-core/emsdk/archive/c1589b55641787d55d53e883852035beea9aec3f.tar.gz", + sha256 = "7a58a9996b113d3e0675df30b5f17e28aa47de2e684a844f05394fe2f6f12e8e", +) + +load("@emsdk//:deps.bzl", emsdk_deps = "deps") +emsdk_deps() + +load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps") +emsdk_emscripten_deps() + load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") # xnnpack used for fast vectorized wasm operations git_repository( name = "xnnpack", - commit = "55d53a4e7079d38e90acd75dd9e4f9e781d2da35", + commit = "3bfbdaf00211b313b143af39279bb6bf1f7effc0", remote = "https://github.com/google/XNNPACK.git", - shallow_since = "1614036677 -0800", + shallow_since = "1617056836 -0700", ) # The libraries below are transitive dependencies of XNNPACK that we need to diff --git a/tfjs-backend-wasm/scripts/build-ci.sh b/tfjs-backend-wasm/scripts/build-ci.sh index c154f902d7..a503705f4c 100755 --- a/tfjs-backend-wasm/scripts/build-ci.sh +++ b/tfjs-backend-wasm/scripts/build-ci.sh @@ -17,5 +17,4 @@ set -e yarn tsc - -./scripts/build-wasm.sh +BAZEL_REMOTE="--config=ci" ./scripts/build-wasm.sh diff --git a/tfjs-backend-wasm/scripts/build-wasm.sh b/tfjs-backend-wasm/scripts/build-wasm.sh index 6262b93696..73e950a2d5 100755 --- a/tfjs-backend-wasm/scripts/build-wasm.sh +++ b/tfjs-backend-wasm/scripts/build-wasm.sh @@ -24,25 +24,28 @@ set -e set -x # Default build. -yarn bazel build -c opt //tfjs-backend-wasm/src/cc:tfjs-backend-wasm.js --config=wasm +yarn bazel build $BAZEL_REMOTE -c opt //tfjs-backend-wasm/src/cc:tfjs-backend-wasm # The typescript code and karma config expect the output of emscripten to be in # wasm-out/ so we copy the bazel output there. -cp -f ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm.js \ - ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm.wasm \ +cp -f ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm/tfjs-backend-wasm.js \ + ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm/tfjs-backend-wasm.wasm \ ../wasm-out/ if [[ "$1" != "--dev" ]]; then - # SIMD build. - yarn bazel build -c opt //tfjs-backend-wasm/src/cc:tfjs-backend-wasm-simd.js --config=wasm --copt="-msimd128" - cp -f ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm-simd.wasm \ - ../wasm-out/ - - # Threaded + SIMD build. - yarn bazel build -c opt //tfjs-backend-wasm/src/cc:tfjs-backend-wasm-threaded-simd.js --config=wasm --copt="-pthread" --copt="-msimd128" - cp -f ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm-threaded-simd.js \ - ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm-threaded-simd.worker.js \ - ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm-threaded-simd.wasm \ - ../wasm-out/ + # SIMD and threaded + SIMD builds. + yarn bazel build $BAZEL_REMOTE -c opt --copt="-msimd128" //tfjs-backend-wasm/src/cc:tfjs-backend-wasm-simd \ + //tfjs-backend-wasm/src/cc:tfjs-backend-wasm-threaded-simd + # Copy SIMD + cp -f ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm-simd/tfjs-backend-wasm.wasm \ + ../wasm-out/tfjs-backend-wasm-simd.wasm + + # Copy threaded + cp -f ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm-threaded-simd/tfjs-backend-wasm.js \ + ../wasm-out/tfjs-backend-wasm-threaded-simd.js + cp -f ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm-threaded-simd/tfjs-backend-wasm.worker.js \ + ../wasm-out/tfjs-backend-wasm-threaded-simd.worker.js + cp -f ../../dist/bin/tfjs-backend-wasm/src/cc/tfjs-backend-wasm-threaded-simd/tfjs-backend-wasm.wasm \ + ../wasm-out/tfjs-backend-wasm-threaded-simd.wasm node ./create-worker-module.js node ./patch-threaded-simd-module.js diff --git a/tfjs-backend-wasm/scripts/create-worker-module.js b/tfjs-backend-wasm/scripts/create-worker-module.js index b8b9fe47ca..a264ace177 100755 --- a/tfjs-backend-wasm/scripts/create-worker-module.js +++ b/tfjs-backend-wasm/scripts/create-worker-module.js @@ -26,5 +26,6 @@ const BASE_PATH = '../wasm-out/'; const WORKER_PATH = `${BASE_PATH}tfjs-backend-wasm-threaded-simd.worker.js`; const workerContents = fs.readFileSync(WORKER_PATH, "utf8"); +fs.chmodSync(WORKER_PATH, 0o644); fs.writeFileSync(`${WORKER_PATH}`, `export const wasmWorkerContents = '${workerContents.trim()}';`); diff --git a/tfjs-backend-wasm/src/cc/BUILD b/tfjs-backend-wasm/src/cc/BUILD index 1a2566418f..b2ce1552d6 100644 --- a/tfjs-backend-wasm/src/cc/BUILD +++ b/tfjs-backend-wasm/src/cc/BUILD @@ -1,3 +1,4 @@ +load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary") load(":build_defs.bzl", "tfjs_cc_library", "tfjs_unit_test") # Emcripten produces a much larger wasm bundle unless the cc_binary has srcs @@ -36,54 +37,22 @@ cc_binary( ], ) -# This build rule generates tfjs-backend-wasm-simd.{js,wasm}. -# -# We only need the .wasm file, not the .js file, because it will be loadsed by -# the tfjs-backend-wasm.js file (generated from the previous build rule). -# -# See scripts/build-wasm.sh where we only copy the .wasm file to wasm-out. -cc_binary( - name = "tfjs-backend-wasm-simd.js", - srcs = ["backend.cc"] + KERNELS_WITH_KEEPALIVE, - linkopts = [ - "-s ALLOW_MEMORY_GROWTH=1", - "-s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=[]", - "-s DISABLE_EXCEPTION_CATCHING=1", - "-s FILESYSTEM=0", - "-s EXIT_RUNTIME=0", - "-s EXPORTED_FUNCTIONS='[\"_malloc\", \"_free\"]'", - "-s EXTRA_EXPORTED_RUNTIME_METHODS='[\"cwrap\"]'", - "-s MALLOC=emmalloc", - ], - deps = [ - ":all_kernels", - ":backend", - ], +wasm_cc_binary( + name = "tfjs-backend-wasm", + cc_target = ":tfjs-backend-wasm.js", ) -# This build rule generates tfjs-backend-wasm-threaded-simd.{js,wasm} and -# tfjs-backend-wasm-threaded-simd.worker.js. -cc_binary( - name = "tfjs-backend-wasm-threaded-simd.js", - srcs = ["backend.cc"] + KERNELS_WITH_KEEPALIVE, - linkopts = [ - "-s ALLOW_MEMORY_GROWTH=1", - "-s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=[]", - "-s DISABLE_EXCEPTION_CATCHING=1", - "-s FILESYSTEM=0", - "-s EXIT_RUNTIME=0", - "-s EXPORTED_FUNCTIONS='[\"_malloc\", \"_free\"]'", - "-s EXTRA_EXPORTED_RUNTIME_METHODS='[\"cwrap\"]'", - "-s MODULARIZE=1", - "-s EXPORT_NAME=WasmBackendModuleThreadedSimd", - "-s MALLOC=emmalloc", - "-s USE_PTHREADS=1", - "-s PROXY_TO_PTHREAD=1", - ], - deps = [ - ":all_kernels", - ":backend", - ], +wasm_cc_binary( + name = "tfjs-backend-wasm-simd", + cc_target = ":tfjs-backend-wasm.js", + simd = True, +) + +wasm_cc_binary( + name = "tfjs-backend-wasm-threaded-simd", + cc_target = ":tfjs-backend-wasm.js", + simd = True, + threads = "emscripten", ) test_suite( diff --git a/tfjs-backend-wasm/src/cc/binary.cc b/tfjs-backend-wasm/src/cc/binary.cc index 538d1c5f65..2d76ae3c79 100644 --- a/tfjs-backend-wasm/src/cc/binary.cc +++ b/tfjs-backend-wasm/src/cc/binary.cc @@ -61,7 +61,6 @@ void binary_xnn_f32(const size_t a_id, const size_t* a_shape_ptr, } else { binary_op = cache_result->second; } - const size_t batch_size = out_info.size; xnn_status status = setup_op(binary_op, a_shape_len, a_shape_ptr, b_shape_len, b_shape_ptr, a_buf, b_buf, out_buf, tfjs::backend::threadpool); diff --git a/tfjs-backend-wasm/src/cc/kernels/All.cc b/tfjs-backend-wasm/src/cc/kernels/All.cc index 474f2ea34e..5bd1c7f69a 100644 --- a/tfjs-backend-wasm/src/cc/kernels/All.cc +++ b/tfjs-backend-wasm/src/cc/kernels/All.cc @@ -33,7 +33,6 @@ void All(const size_t x_id, const size_t reduce_size, const size_t out_id) { auto& out_info = backend::get_tensor_info_out(out_id); const bool* x_buf = x_info.b(); - const size_t x_size = x_info.size; bool* out_buf = out_info.b_write(); const size_t out_size = out_info.size; diff --git a/tfjs-backend-wasm/src/cc/kernels/Any.cc b/tfjs-backend-wasm/src/cc/kernels/Any.cc index ac09d7eb34..6e27d22a99 100644 --- a/tfjs-backend-wasm/src/cc/kernels/Any.cc +++ b/tfjs-backend-wasm/src/cc/kernels/Any.cc @@ -33,7 +33,6 @@ void Any(const size_t x_id, const size_t reduce_size, const size_t out_id) { auto& out_info = backend::get_tensor_info_out(out_id); const bool* x_buf = x_info.b(); - const size_t x_size = x_info.size; bool* out_buf = out_info.b_write(); const size_t out_size = out_info.size; diff --git a/tfjs-backend-wasm/src/cc/kernels/CropAndResize.cc b/tfjs-backend-wasm/src/cc/kernels/CropAndResize.cc index dd2d4a0e11..0bb95b43cb 100644 --- a/tfjs-backend-wasm/src/cc/kernels/CropAndResize.cc +++ b/tfjs-backend-wasm/src/cc/kernels/CropAndResize.cc @@ -91,16 +91,12 @@ void CropAndResize(size_t images_id, size_t boxes_id, size_t box_ind_id, auto& out_info = backend::get_tensor_info_out(out_id); const float* images_buf = images_info.f32(); - const size_t images_size = images_info.size; const float* boxes_buf = boxes_info.f32(); - const size_t boxes_size = boxes_info.size; const int* box_ind_buf = box_ind_info.i32(); - const size_t box_ind_size = box_ind_info.size; float* out_buf = out_info.f32_write(); - const size_t out_size = out_info.size; const size_t batch = images_shape[0]; const size_t image_height = images_shape[1]; diff --git a/tfjs-backend-wasm/src/cc/kernels/FloorDiv.cc b/tfjs-backend-wasm/src/cc/kernels/FloorDiv.cc index ba4cf9c800..2c1fbd0b38 100644 --- a/tfjs-backend-wasm/src/cc/kernels/FloorDiv.cc +++ b/tfjs-backend-wasm/src/cc/kernels/FloorDiv.cc @@ -35,7 +35,6 @@ void FloorDiv(const size_t a_id, const size_t* a_shape_ptr, const size_t a_shape_len, const size_t b_id, const size_t* b_shape_ptr, const size_t b_shape_len, const DType dtype, const size_t out_id) { - auto& a_info = backend::get_tensor_info(a_id); switch (dtype) { case DType::float32: binary_f32(a_id, b_id, out_id, diff --git a/tfjs-backend-wasm/src/cc/kernels/Max.cc b/tfjs-backend-wasm/src/cc/kernels/Max.cc index 7d91daaa2b..4d791e593b 100644 --- a/tfjs-backend-wasm/src/cc/kernels/Max.cc +++ b/tfjs-backend-wasm/src/cc/kernels/Max.cc @@ -33,7 +33,6 @@ void Max(const size_t x_id, const size_t reduce_size, const size_t out_id) { auto& out_info = backend::get_tensor_info_out(out_id); const float* x_buf = x_info.f32(); - const size_t x_size = x_info.size; float* out_buf = out_info.f32_write(); const size_t out_size = out_info.size; diff --git a/tfjs-backend-wasm/src/cc/kernels/Mean.cc b/tfjs-backend-wasm/src/cc/kernels/Mean.cc index 3230b2c291..f1328c410a 100644 --- a/tfjs-backend-wasm/src/cc/kernels/Mean.cc +++ b/tfjs-backend-wasm/src/cc/kernels/Mean.cc @@ -34,7 +34,6 @@ void Mean(const size_t x_id, const size_t reduce_size, const size_t out_id) { auto& out_info = backend::get_tensor_info_out(out_id); const float* x_buf = x_info.f32(); - const size_t x_size = x_info.size; float* out_buf = out_info.f32_write(); const size_t out_size = out_info.size; @@ -42,7 +41,6 @@ void Mean(const size_t x_id, const size_t reduce_size, const size_t out_id) { const float* x_offset = x_buf; for (size_t i = 0; i < out_size; ++i) { - const size_t offset = i * reduce_size; float sum = 0; const float* x_iter_end = x_offset + reduce_size; diff --git a/tfjs-backend-wasm/src/cc/kernels/Min.cc b/tfjs-backend-wasm/src/cc/kernels/Min.cc index bffa69cac7..d0e05748ba 100644 --- a/tfjs-backend-wasm/src/cc/kernels/Min.cc +++ b/tfjs-backend-wasm/src/cc/kernels/Min.cc @@ -33,7 +33,6 @@ void Min(const size_t x_id, const size_t reduce_size, const size_t out_id) { auto& out_info = backend::get_tensor_info_out(out_id); const float* x_buf = x_info.f32(); - const size_t x_size = x_info.size; float* out_buf = out_info.f32_write(); const size_t out_size = out_info.size; diff --git a/tfjs-backend-wasm/src/cc/kernels/Pow.cc b/tfjs-backend-wasm/src/cc/kernels/Pow.cc index f778382cc1..1436d4ce23 100644 --- a/tfjs-backend-wasm/src/cc/kernels/Pow.cc +++ b/tfjs-backend-wasm/src/cc/kernels/Pow.cc @@ -27,8 +27,13 @@ template inline T power(T a, T b) { return pow(a, b); } + +inline bool power_bool(bool a, bool b) { + return static_cast(pow(static_cast(a), static_cast(b))); +} } // namespace + namespace tfjs { namespace wasm { // We use C-style API to interface with Javascript. @@ -48,7 +53,7 @@ void Pow(const size_t a_id, const size_t* a_shape_ptr, const size_t a_shape_len, binary_i32(a_id, b_id, out_id, power); break; case DType::boolean: - binary_bool(a_id, b_id, out_id, power); + binary_bool(a_id, b_id, out_id, power_bool); break; default: util::warn("Pow for tensor ids %d and %d failed. Unknown dtype %d", a_id, diff --git a/tfjs-backend-wasm/src/cc/kernels/Prod.cc b/tfjs-backend-wasm/src/cc/kernels/Prod.cc index 28016385c4..e4effe5313 100644 --- a/tfjs-backend-wasm/src/cc/kernels/Prod.cc +++ b/tfjs-backend-wasm/src/cc/kernels/Prod.cc @@ -29,7 +29,6 @@ void prod(const size_t x_id, const size_t reduce_size, const size_t out_id) { auto& out_info = tfjs::backend::get_tensor_info_out(out_id); const T* x_buf = reinterpret_cast(x_info.memory_offset); - const size_t x_size = x_info.size; T* out_buf = reinterpret_cast(out_info.memory_offset); const size_t out_size = out_info.size; @@ -37,7 +36,6 @@ void prod(const size_t x_id, const size_t reduce_size, const size_t out_id) { const T* x_offset = x_buf; for (size_t i = 0; i < out_size; ++i) { - const size_t offset = i * reduce_size; T product = 1; const T* x_iter_end = x_offset + reduce_size; diff --git a/tfjs-backend-wasm/src/cc/kernels/Sum.cc b/tfjs-backend-wasm/src/cc/kernels/Sum.cc index 83a26d1063..4a21e9c9f1 100644 --- a/tfjs-backend-wasm/src/cc/kernels/Sum.cc +++ b/tfjs-backend-wasm/src/cc/kernels/Sum.cc @@ -34,7 +34,6 @@ void Sum(const size_t x_id, const size_t reduce_size, const size_t out_id) { auto& out_info = backend::get_tensor_info_out(out_id); const float* x_buf = x_info.f32(); - const size_t x_size = x_info.size; float* out_buf = out_info.f32_write(); const size_t out_size = out_info.size; @@ -42,7 +41,6 @@ void Sum(const size_t x_id, const size_t reduce_size, const size_t out_id) { const float* x_offset = x_buf; for (size_t i = 0; i < out_size; ++i) { - const size_t offset = i * reduce_size; float sum = 0; const float* x_iter_end = x_offset + reduce_size; diff --git a/tfjs-backend-wasm/src/cc/non_max_suppression_impl.cc b/tfjs-backend-wasm/src/cc/non_max_suppression_impl.cc index a23f6c4b29..2aeda8d276 100644 --- a/tfjs-backend-wasm/src/cc/non_max_suppression_impl.cc +++ b/tfjs-backend-wasm/src/cc/non_max_suppression_impl.cc @@ -116,7 +116,7 @@ const NonMaxSuppressionResult* non_max_suppression_impl( std::vector selected_indices; std::vector selected_scores; Candidate candidate; - float iou, original_score; + float original_score; while (selected_indices.size() < max_out_size && !candidate_priority_queue.empty()) { diff --git a/tfjs-backend-wasm/yarn.lock b/tfjs-backend-wasm/yarn.lock index ddc7bb697f..5298f092d8 100644 --- a/tfjs-backend-wasm/yarn.lock +++ b/tfjs-backend-wasm/yarn.lock @@ -984,18 +984,11 @@ "@tensorflow/tfjs-backend-cpu@link:../tfjs-backend-cpu": version "0.0.0" - dependencies: - "@types/seedrandom" "2.4.27" - seedrandom "2.4.3" + uid "" "@tensorflow/tfjs-core@link:../tfjs-core": version "0.0.0" - dependencies: - "@types/offscreencanvas" "~2019.3.0" - "@types/seedrandom" "2.4.27" - "@types/webgl-ext" "0.0.30" - node-fetch "~2.6.1" - seedrandom "2.4.3" + uid "" "@types/emscripten@~0.0.34": version "0.0.34" diff --git a/toolchain/BUILD b/toolchain/BUILD deleted file mode 100644 index c92e688e8e..0000000000 --- a/toolchain/BUILD +++ /dev/null @@ -1,35 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -load(":cc_toolchain_config.bzl", "cc_toolchain_config") - -cc_toolchain_config(name = "wasm_toolchain_config") - -cc_toolchain_suite( - name = "emscripten", - toolchains = { - "wasm": ":wasm_toolchain", - }, -) - -filegroup( - name = "all", - srcs = [ - "emar.sh", - "emcc.sh", - "false.sh", - "@emsdk//:all", - ], -) - -cc_toolchain( - name = "wasm_toolchain", - all_files = ":all", - compiler_files = ":all", - dwp_files = ":empty", - linker_files = ":all", - objcopy_files = ":empty", - strip_files = ":empty", - supports_param_files = 0, - toolchain_config = ":wasm_toolchain_config", - toolchain_identifier = "wasm-toolchain", -) diff --git a/toolchain/cc_toolchain_config.bzl b/toolchain/cc_toolchain_config.bzl deleted file mode 100644 index 4c12bd1ff7..0000000000 --- a/toolchain/cc_toolchain_config.bzl +++ /dev/null @@ -1,212 +0,0 @@ -load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") -load( - "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", - "feature", - "flag_group", - "flag_set", - "tool_path", - "with_feature_set", -) - -def _impl(ctx): - tool_paths = [ - tool_path( - name = "gcc", - path = "emcc.sh", - ), - tool_path( - name = "ld", - path = "emcc.sh", - ), - tool_path( - name = "ar", - path = "emar.sh", - ), - tool_path( - name = "cpp", - path = "false.sh", - ), - tool_path( - name = "gcov", - path = "false.sh", - ), - tool_path( - name = "nm", - path = "NOT_USED", - ), - tool_path( - name = "objdump", - path = "false.sh", - ), - tool_path( - name = "strip", - path = "NOT_USED", - ), - ] - preprocessor_compile_actions = [ - ACTION_NAMES.c_compile, - ACTION_NAMES.cpp_compile, - ACTION_NAMES.linkstamp_compile, - ACTION_NAMES.preprocess_assemble, - ACTION_NAMES.cpp_header_parsing, - ACTION_NAMES.cpp_module_compile, - ACTION_NAMES.clif_match, - ] - - all_link_actions = [ - ACTION_NAMES.cpp_link_executable, - ACTION_NAMES.cpp_link_dynamic_library, - ACTION_NAMES.cpp_link_nodeps_dynamic_library, - ] - - all_compile_actions = [ - ACTION_NAMES.c_compile, - ACTION_NAMES.cpp_compile, - ACTION_NAMES.linkstamp_compile, - ACTION_NAMES.assemble, - ACTION_NAMES.preprocess_assemble, - ACTION_NAMES.cpp_header_parsing, - ACTION_NAMES.cpp_module_compile, - ACTION_NAMES.cpp_module_codegen, - ACTION_NAMES.clif_match, - ACTION_NAMES.lto_backend, - ] - toolchain_include_directories_feature = feature( - name = "toolchain_include_directories", - enabled = True, - flag_sets = [ - flag_set( - actions = all_compile_actions, - flag_groups = [ - flag_group( - flags = [ - # The clang compiler comes with a definition of - # max_align_t struct in $emsdk/upstream/lib/clang/13.0.0/include/__stddef_max_align_t.h. - # It conflicts with the one defined in - # $emsdk/upstream/emscripten/cache/sysroot/include/bits/alltypes.h. - # We need both include paths to make things work. - # - # To workaround this, we are defining the following - # symbol through compiler flag so that the max_align_t - # defined in clang's header file will be skipped. - "-D", - "__CLANG_MAX_ALIGN_T_DEFINED", - # We are using emscripten 2.0.14 for this build. It - # comes with clang 13.0.0. Future emscripten release - # might change the clang version number below. - # - # Also need to change the version number in - # cxx_cxx_builtin_include_directories below. - "-isystem", - "external/emsdk/emsdk/upstream/lib/clang/13.0.0/include", - ], - ), - ], - ), - ], - ) - - crosstool_default_flag_sets = [ - # Opt. - flag_set( - actions = preprocessor_compile_actions, - flag_groups = [flag_group(flags = ["-DNDEBUG"])], - with_features = [with_feature_set(features = ["opt"])], - ), - flag_set( - actions = all_compile_actions + all_link_actions, - flag_groups = [flag_group(flags = ["-g0", "-O3"])], - with_features = [with_feature_set(features = ["opt"])], - ), - # Fastbuild. - flag_set( - actions = all_compile_actions + all_link_actions, - flag_groups = [flag_group(flags = ["-O2"])], - with_features = [with_feature_set(features = ["fastbuild"])], - ), - # Dbg. - flag_set( - actions = all_compile_actions + all_link_actions, - flag_groups = [flag_group(flags = ["-g2", "-O0"])], - with_features = [with_feature_set(features = ["dbg"])], - ), - ] - - features = [ - toolchain_include_directories_feature, - # These 3 features will be automatically enabled by blaze in the - # corresponding build mode. - feature( - name = "opt", - provides = ["variant:crosstool_build_mode"], - ), - feature( - name = "dbg", - provides = ["variant:crosstool_build_mode"], - ), - feature( - name = "fastbuild", - provides = ["variant:crosstool_build_mode"], - ), - feature( - name = "crosstool_default_flags", - enabled = True, - flag_sets = crosstool_default_flag_sets, - ), - ] - - cxx_builtin_include_directories = [ - "external/emsdk/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1", - "external/emsdk/emsdk/upstream/emscripten/cache/sysroot/include/compat", - "external/emsdk/emsdk/upstream/emscripten/cache/sysroot/include", - # We are using emscripten 2.0.14 for this build. It comes with clang - # 13.0.0. Future emscripten release might change the clang version - # number below. - # - # Also need to change the version number in - # toolchain_include_directories_feature above. - "external/emsdk/emsdk/upstream/lib/clang/13.0.0/include", - ] - - builtin_sysroot = "external/emsdk/emsdk/upstream/emscripten/cache/sysroot" - - return cc_common.create_cc_toolchain_config_info( - ctx = ctx, - toolchain_identifier = "wasm-toolchain", - host_system_name = "i686-unknown-linux-gnu", - target_system_name = "wasm-unknown-emscripten", - target_cpu = "wasm", - target_libc = "musl/js", - compiler = "emscripten", - abi_version = "emscripten_syscalls", - abi_libc_version = "default", - tool_paths = tool_paths, - features = features, - builtin_sysroot = builtin_sysroot, - cxx_builtin_include_directories = cxx_builtin_include_directories, - ) - -cc_toolchain_config = rule( - implementation = _impl, - attrs = {}, - provides = [CcToolchainConfigInfo], -) - -def _emsdk_impl(ctx): - if "EMSDK" not in ctx.os.environ or ctx.os.environ["EMSDK"].strip() == "": - fail("The environment variable EMSDK is not found. " + - "Did you run source ./emsdk_env.sh ?") - path = ctx.os.environ["EMSDK"] - ctx.symlink(path, "emsdk") - ctx.file("BUILD", """ -filegroup( - name = "all", - srcs = glob(["emsdk/**"]), - visibility = ["//visibility:public"], -) -""") - -emsdk_configure = repository_rule( - implementation = _emsdk_impl, - local = True, -) diff --git a/toolchain/emar.sh b/toolchain/emar.sh deleted file mode 100755 index 65eed16b42..0000000000 --- a/toolchain/emar.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env bash -# Copyright 2019 Google LLC. 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. -# ============================================================================= - -set -euo pipefail - -external/emsdk/emsdk/upstream/emscripten/emar "$@" diff --git a/toolchain/emcc.sh b/toolchain/emcc.sh deleted file mode 100755 index 85044e097e..0000000000 --- a/toolchain/emcc.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env bash -# Copyright 2019 Google LLC. 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. -# ============================================================================= - -set -euo pipefail - -external/emsdk/emsdk/upstream/emscripten/emcc "$@" diff --git a/toolchain/false.sh b/toolchain/false.sh deleted file mode 100644 index b6ce9a0456..0000000000 --- a/toolchain/false.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env bash -# Copyright 2019 Google LLC. 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. -# ============================================================================= - -exit 1