-
Notifications
You must be signed in to change notification settings - Fork 742
[Bazel] Support generating a secondary cache #1405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
592171e
Implement emscripten_cache
allsey87 ea4386a
Remove hard-coded @emscripten_bin_linux// reference
allsey87 e3ae46e
Add emscripten_cache to test_external's workspace
allsey87 f77fcf4
Add tests that generate and use a secondary cache with LTO
allsey87 4856c76
Fix flags argument to emscripten_cache in test
allsey87 2818b5a
Fix error handling
allsey87 f3f2c7e
Fix various errors
allsey87 75656b1
Multiple fixes
allsey87 d5e2795
Make tests faster by not building the entire cache
allsey87 abfa9ce
Use BUILD.bazel to get the root directory of repositories
allsey87 311c587
Include the .exe extension on Windows for Node.js
allsey87 e6b94bc
Drop support for secondary caches on Windows
allsey87 0f584ea
Update .gitignore for secondary cache test
allsey87 c0242f8
Remove dependency on Node.js
allsey87 263d6b1
Hide output while building the cache
allsey87 cde46af
Fix typo in README.md
allsey87 312260f
Move cache declaration inside of the toolchain registration
allsey87 8f9e155
Remove trailing spaces
allsey87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
BUILD_FILE_CONTENT_TEMPLATE = """ | ||
package(default_visibility = ['//visibility:public']) | ||
exports_files(['emscripten_config']) | ||
""" | ||
|
||
EMBUILDER_CONFIG_TEMPLATE = """ | ||
CACHE = '{cache}' | ||
BINARYEN_ROOT = '{binaryen_root}' | ||
LLVM_ROOT = '{llvm_root}' | ||
""" | ||
|
||
def get_root_and_script_ext(repository_ctx): | ||
if repository_ctx.os.name.startswith('linux'): | ||
if 'amd64' in repository_ctx.os.arch or 'x86_64' in repository_ctx.os.arch: | ||
return (repository_ctx.path(Label("@emscripten_bin_linux//:BUILD.bazel")).dirname, '') | ||
elif 'aarch64' in repository_ctx.os.arch: | ||
return (repository_ctx.path(Label("@emscripten_bin_linux_arm64//:BUILD.bazel")).dirname, '') | ||
else: | ||
fail('Unsupported architecture for Linux') | ||
elif repository_ctx.os.name.startswith('mac'): | ||
if 'amd64' in repository_ctx.os.arch or 'x86_64' in repository_ctx.os.arch: | ||
return (repository_ctx.path(Label("@emscripten_bin_mac//:BUILD.bazel")).dirname, '') | ||
elif 'aarch64' in repository_ctx.os.arch: | ||
return (repository_ctx.path(Label("@emscripten_bin_mac_arm64//:BUILD.bazel")).dirname, '') | ||
else: | ||
fail('Unsupported architecture for MacOS') | ||
elif repository_ctx.os.name.startswith('windows'): | ||
return (repository_ctx.path(Label("@emscripten_bin_win//:BUILD.bazel")).dirname, '.bat') | ||
else: | ||
fail('Unsupported operating system') | ||
|
||
def _emscripten_cache_impl(repository_ctx): | ||
# Read the default emscripten configuration file | ||
default_config = repository_ctx.read( | ||
repository_ctx.path( | ||
Label("@emsdk//emscripten_toolchain:default_config") | ||
) | ||
) | ||
allsey87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if repository_ctx.attr.libraries or repository_ctx.attr.flags: | ||
root, script_ext = get_root_and_script_ext(repository_ctx) | ||
llvm_root = root.get_child("bin") | ||
cache = repository_ctx.path("cache") | ||
# Create configuration file | ||
embuilder_config_content = EMBUILDER_CONFIG_TEMPLATE.format( | ||
cache=cache, | ||
binaryen_root=root, | ||
llvm_root=llvm_root, | ||
) | ||
repository_ctx.file("embuilder_config", embuilder_config_content) | ||
embuilder_config_path = repository_ctx.path("embuilder_config") | ||
embuilder_path = "{}{}".format(root.get_child("emscripten").get_child("embuilder"), script_ext) | ||
# Prepare the command line | ||
if repository_ctx.attr.libraries: | ||
libraries = repository_ctx.attr.libraries | ||
else: | ||
# If no libraries are requested, build everything | ||
libraries = ["ALL"] | ||
flags = ["--em-config", embuilder_config_path] + repository_ctx.attr.flags | ||
embuilder_args = [embuilder_path] + flags + ["build"] + libraries | ||
# Run embuilder | ||
repository_ctx.report_progress("Building secondary cache") | ||
result = repository_ctx.execute( | ||
embuilder_args, | ||
quiet=True, | ||
environment = { | ||
"EM_IGNORE_SANITY": "1", | ||
"EM_NODE_JS": "empty", | ||
} | ||
) | ||
if result.return_code != 0: | ||
fail("Embuilder exited with a non-zero return code") | ||
# Override Emscripten's cache with the secondary cache | ||
default_config += "CACHE = '{}'\n".format(cache) | ||
|
||
# Create the configuration file for the toolchain and export | ||
repository_ctx.file('emscripten_config', default_config) | ||
repository_ctx.file('BUILD.bazel', BUILD_FILE_CONTENT_TEMPLATE) | ||
|
||
_emscripten_cache = repository_rule( | ||
implementation = _emscripten_cache_impl, | ||
attrs = { | ||
"flags": attr.string_list(), | ||
"libraries": attr.string_list(), | ||
}, | ||
local = True | ||
) | ||
|
||
def emscripten_cache(flags = [], libraries = []): | ||
_emscripten_cache( | ||
name = "emscripten_cache", | ||
flags = flags, | ||
libraries = libraries, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build --incompatible_enable_cc_toolchain_resolution |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
bazel-bin | ||
bazel-out | ||
bazel-test_secondary_lto_cache | ||
bazel-testlogs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary") | ||
|
||
cc_binary( | ||
name = "hello-world", | ||
srcs = ["hello-world.cc"], | ||
linkopts = [ | ||
"-sAUTO_NATIVE_LIBRARIES=0", | ||
"-flto", | ||
], | ||
) | ||
|
||
wasm_cc_binary( | ||
name = "hello-world-wasm", | ||
cc_target = ":hello-world", | ||
outputs = [ | ||
"hello-world.js", | ||
"hello-world.wasm", | ||
], | ||
) | ||
|
||
|
||
allsey87 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bazel_dep(name = "platforms", version = "0.0.9") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
local_repository( | ||
name = "emsdk", | ||
path = "..", | ||
) | ||
|
||
load("@emsdk//:deps.bzl", "deps") | ||
|
||
deps() | ||
|
||
load("@emsdk//:emscripten_deps.bzl", "emscripten_deps") | ||
|
||
emscripten_deps() | ||
|
||
load("@emsdk//:emscripten_cache.bzl", "emscripten_cache") | ||
|
||
emscripten_cache( | ||
flags = ["--lto"], | ||
libraries = [ | ||
"crtbegin", | ||
"libprintf_long_double-debug", | ||
"libstubs-debug", | ||
"libnoexit", | ||
"libc-debug", | ||
"libdlmalloc", | ||
"libcompiler_rt", | ||
"libc++-noexcept", | ||
"libc++abi-debug-noexcept", | ||
"libsockets" | ||
] | ||
) | ||
|
||
load("@emsdk//:toolchains.bzl", "register_emscripten_toolchains") | ||
|
||
register_emscripten_toolchains() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include <iostream> | ||
|
||
int main(int argc, char** argv) { | ||
std::cout << "hello world!" << std::endl; | ||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.