|
1 | 1 | load("@bazel_skylib//rules:build_test.bzl", "build_test") |
| 2 | +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") |
| 3 | +load("@rules_cc//cc:cc_binary.bzl", "cc_binary") |
2 | 4 | load("@rules_cc//cc:cc_library.bzl", "cc_library") |
| 5 | +load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix") |
| 6 | +load("@rules_pkg//pkg:zip.bzl", "pkg_zip") |
3 | 7 | load("@rules_python//python:pip.bzl", "compile_pip_requirements") |
4 | 8 | load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary") |
| 9 | +load("//bazel:extract_zip.bzl", "extract_zip") |
5 | 10 |
|
6 | 11 | package(features = ["layering_check"]) |
7 | 12 |
|
@@ -36,17 +41,99 @@ compile_pip_requirements( |
36 | 41 | requirements_txt = "requirements_lock_3_13.txt", |
37 | 42 | ) |
38 | 43 |
|
39 | | -cc_library( |
40 | | - name = "runfiles", |
| 44 | +# Various tcl libraries that we want to embed into the binary. |
| 45 | +pkg_files( |
| 46 | + name = "tcl_core_files", |
| 47 | + srcs = ["@tcl_lang//:tcl_core"], |
| 48 | + strip_prefix = strip_prefix.from_root(), |
| 49 | +) |
| 50 | + |
| 51 | +pkg_files( |
| 52 | + name = "tclreadline_files", |
| 53 | + srcs = ["@tclreadline//:tclreadline_scripts"], |
| 54 | + prefix = "tclreadline", |
| 55 | + strip_prefix = strip_prefix.from_root(), |
| 56 | +) |
| 57 | + |
| 58 | +# Packaging up all tcl resources to one complete zip file. It can be |
| 59 | +# directly embedded in Tcl9 in a //zipfs:/ vfs. |
| 60 | +pkg_zip( |
| 61 | + name = "tcl_resources_zip", |
41 | 62 | srcs = [ |
42 | | - "InitRunFiles.cpp", |
43 | | - ], |
44 | | - data = [ |
45 | | - "@tcl_lang//:tcl_core", |
| 63 | + ":tcl_core_files", |
| 64 | + ":tclreadline_files", |
46 | 65 | ], |
| 66 | +) |
| 67 | + |
| 68 | +# ... however, Tcl8 still needs access to a directory, so unpack in that case. |
| 69 | +extract_zip( |
| 70 | + name = "tcl_resources_dir", |
| 71 | + src = ":tcl_resources_zip", |
| 72 | +) |
| 73 | + |
| 74 | +genrule( |
| 75 | + name = "tcl_resources_zip_data_h", |
| 76 | + srcs = [":tcl_resources_zip"], |
| 77 | + outs = ["tcl_resources_zip_data.h"], |
| 78 | + cmd = "$(location :embed) kTclResourceZip $< $@", |
| 79 | + tools = [":embed"], |
| 80 | +) |
| 81 | + |
| 82 | +cc_binary( |
| 83 | + name = "embed", |
| 84 | + srcs = ["embed.cc"], |
| 85 | +) |
| 86 | + |
| 87 | +# As long as we support tcl8 (using runfiles) we need this flag. |
| 88 | +bool_flag( |
| 89 | + name = "use_zipfs", |
| 90 | + build_setting_default = False, # False until we can use tcl9 |
| 91 | +) |
| 92 | + |
| 93 | +config_setting( |
| 94 | + name = "zipfs_config", |
| 95 | + flag_values = {":use_zipfs": "true"}, |
| 96 | +) |
| 97 | + |
| 98 | +# Library initialization is a bit different for tcl8 and tcl9. |
| 99 | +# In tcl9, we can use a zipped version of the needed include files, but |
| 100 | +# for tcl8, we have to use the unpacked zip file and point our runfiles |
| 101 | +# to it. |
| 102 | +cc_library( |
| 103 | + name = "tcl_library_init", |
| 104 | + srcs = ["tcl_library_init.cc"] + |
| 105 | + select({ |
| 106 | + ":zipfs_config": ["tcl_resources_zip_data.h"], |
| 107 | + "//conditions:default": [], |
| 108 | + }), |
| 109 | + hdrs = ["tcl_library_init.h"], |
| 110 | + copts = select({ |
| 111 | + ":zipfs_config": [], |
| 112 | + "//conditions:default": ["-DUSE_TCL_RUNFILE_INIT"], |
| 113 | + }), |
| 114 | + data = select({ |
| 115 | + ":zipfs_config": [], |
| 116 | + "//conditions:default": [":tcl_resources_dir"], |
| 117 | + }), |
| 118 | + visibility = ["//visibility:public"], |
| 119 | + deps = [ |
| 120 | + "@tcl_lang//:tcl", |
| 121 | + ] + select({ |
| 122 | + ":zipfs_config": [], |
| 123 | + "//conditions:default": ["@rules_cc//cc/runfiles"], |
| 124 | + }), |
| 125 | +) |
| 126 | + |
| 127 | +# shim old library as it is still referenced in src/sta, implementing the |
| 128 | +# old behavior. |
| 129 | +cc_library( |
| 130 | + name = "runfiles", |
| 131 | + srcs = ["InitRunFiles.cpp"], |
| 132 | + data = [":tcl_resources_dir"], |
47 | 133 | visibility = ["//visibility:public"], |
48 | 134 | deps = [ |
49 | 135 | "@rules_cc//cc/runfiles", |
| 136 | + "@tcl_lang//:tcl", |
50 | 137 | ], |
51 | 138 | alwayslink = True, |
52 | 139 | ) |
|
0 commit comments