Skip to content

Commit 66d595e

Browse files
committed
fix: Improve quoting for mkdirs and rmrf
This is necessary to fix handling of directories with spaces on Windows platform.
1 parent 3c3f5f9 commit 66d595e

File tree

10 files changed

+199
-42
lines changed

10 files changed

+199
-42
lines changed

MODULE.bazel.lock

Lines changed: 158 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

foreign_cc/built_tools/private/built_tools_framework.bzl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ def built_tool_rule_impl(ctx, script_lines, out_dir, mnemonic, additional_tools
119119
script = [
120120
"##script_prelude##",
121121
] + env_prelude + [
122-
"##rm_rf## $$INSTALLDIR$$",
123-
"##rm_rf## $$BUILD_TMPDIR$$",
124-
"##mkdirs## $$INSTALLDIR$$",
125-
"##mkdirs## $$BUILD_TMPDIR$$",
126-
"##copy_dir_contents_to_dir## ./{} $$BUILD_TMPDIR$$".format(root),
127-
"cd $$BUILD_TMPDIR$$",
122+
"##rm_rf## \"$$INSTALLDIR$$\"",
123+
"##rm_rf## \"$$BUILD_TMPDIR$$\"",
124+
"##mkdirs## \"$$INSTALLDIR$$\"",
125+
"##mkdirs## \"$$BUILD_TMPDIR$$\"",
126+
"##copy_dir_contents_to_dir## ./{} \"$$BUILD_TMPDIR$$\"".format(root),
127+
"cd \"$$BUILD_TMPDIR$$\"",
128128
]
129129

130130
script.append("##enable_tracing##")

foreign_cc/cmake.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ def _cmake_impl(ctx):
171171
if "Unix Makefiles" == generator:
172172
make_data = get_make_data(ctx)
173173
tools_data.append(make_data)
174-
generate_args.append("-DCMAKE_MAKE_PROGRAM={}".format(make_data.path))
174+
generate_args.append("-DCMAKE_MAKE_PROGRAM=\"{}\"".format(make_data.path))
175175
elif "Ninja" in generator:
176176
ninja_data = get_ninja_data(ctx)
177177
tools_data.append(ninja_data)
178-
generate_args.append("-DCMAKE_MAKE_PROGRAM={}".format(ninja_data.path))
178+
generate_args.append("-DCMAKE_MAKE_PROGRAM=\"{}\"".format(ninja_data.path))
179179

180180
attrs = create_attrs(
181181
ctx.attr,
@@ -230,7 +230,7 @@ def _create_configure_script(configureParameters):
230230

231231
# Note that even though directory is always passed, the
232232
# following arguments can take precedence.
233-
cmake_commands.append("{cmake} --build {dir} --config {config} {target} {args}".format(
233+
cmake_commands.append("\"{cmake}\" --build \"{dir}\" --config {config} {target} {args}".format(
234234
cmake = attrs.cmake_path,
235235
dir = ".",
236236
args = build_args,
@@ -245,7 +245,7 @@ def _create_configure_script(configureParameters):
245245
for arg in ctx.attr.install_args
246246
])
247247

248-
cmake_commands.append("{cmake} --install {dir} --config {config} {args}".format(
248+
cmake_commands.append("\"{cmake}\" --install \"{dir}\" --config {config} {args}".format(
249249
cmake = attrs.cmake_path,
250250
dir = ".",
251251
args = install_args,

foreign_cc/private/cmake_script.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ def create_cmake_script(
161161

162162
script += pkgconfig_script(ext_build_dirs)
163163

164-
directory = "$$EXT_BUILD_ROOT$$/" + root
164+
directory = "\"$$EXT_BUILD_ROOT$$/" + root + "\""
165165

166166
script.append("##enable_tracing##")
167167

168168
# Configure the CMake generate command
169169
cmake_prefixes = [cmake_prefix] if cmake_prefix else []
170170
script.append(" ".join(cmake_prefixes + [
171-
cmake_path,
171+
"\"{}\"".format(cmake_path),
172172
str_cmake_cache_entries,
173173
" ".join(options),
174174
# Generator is always set last and will override anything specified by the user

foreign_cc/private/configure_script.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def create_configure_script(
7373
options = " ".join(autoreconf_options),
7474
).lstrip())
7575

76-
script.append("##mkdirs## $$BUILD_TMPDIR$$/$$INSTALL_PREFIX$$")
76+
script.append("##mkdirs## \"$$BUILD_TMPDIR$$/$$INSTALL_PREFIX$$\"")
7777

7878
make_commands = []
7979
script.append("{env_vars} {prefix}\"{configure}\" {prefix_flag}$$BUILD_TMPDIR$$/$$INSTALL_PREFIX$$ {user_options}".format(

foreign_cc/private/framework.bzl

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,9 @@ def get_env_prelude(ctx, installdir, data_dependencies):
320320
"""
321321
env_snippet = [
322322
"export EXT_BUILD_ROOT=##pwd##",
323-
"export INSTALLDIR=$$EXT_BUILD_ROOT$$/" + installdir,
324-
"export BUILD_TMPDIR=$$INSTALLDIR$$.build_tmpdir",
325-
"export EXT_BUILD_DEPS=$$INSTALLDIR$$.ext_build_deps",
323+
"export INSTALLDIR=\"$$EXT_BUILD_ROOT$$/" + installdir + "\"",
324+
"export BUILD_TMPDIR=\"$$INSTALLDIR$$.build_tmpdir" + "\"",
325+
"export EXT_BUILD_DEPS=\"$$INSTALLDIR$$.ext_build_deps" + "\"",
326326
]
327327

328328
env = dict()
@@ -472,14 +472,14 @@ def cc_external_rule_impl(ctx, attrs):
472472
"##echo## \"\"",
473473
"##script_prelude##",
474474
] + env_prelude + [
475-
"##path## $$EXT_BUILD_ROOT$$",
476-
"##rm_rf## $$BUILD_TMPDIR$$",
477-
"##rm_rf## $$EXT_BUILD_DEPS$$",
478-
"##mkdirs## $$INSTALLDIR$$",
479-
"##mkdirs## $$BUILD_TMPDIR$$",
480-
"##mkdirs## $$EXT_BUILD_DEPS$$",
475+
"##path## \"$$EXT_BUILD_ROOT$$\"",
476+
"##rm_rf## \"$$BUILD_TMPDIR$$\"",
477+
"##rm_rf## \"$$EXT_BUILD_DEPS$$\"",
478+
"##mkdirs## \"$$INSTALLDIR$$\"",
479+
"##mkdirs## \"$$BUILD_TMPDIR$$\"",
480+
"##mkdirs## \"$$EXT_BUILD_DEPS$$\"",
481481
] + _print_env() + _copy_deps_and_tools(inputs) + [
482-
"cd $$BUILD_TMPDIR$$",
482+
"cd \"$$BUILD_TMPDIR$$\"",
483483
] + attrs.create_configure_script(ConfigureParameters(ctx = ctx, attrs = attrs, inputs = inputs)) + postfix_script + [
484484
# replace references to the root directory when building ($BUILD_TMPDIR)
485485
# and the root where the dependencies were installed ($EXT_BUILD_DEPS)
@@ -488,7 +488,7 @@ def cc_external_rule_impl(ctx, attrs):
488488
"##replace_absolute_paths## $$INSTALLDIR$$ $$EXT_BUILD_DEPS$$",
489489
"##replace_sandbox_paths## $$INSTALLDIR$$ $$EXT_BUILD_ROOT$$",
490490
installdir_copy.script,
491-
"cd $$EXT_BUILD_ROOT$$",
491+
"cd \"$$EXT_BUILD_ROOT$$\"",
492492
] + [
493493
"##replace_symlink## {}".format(file.path)
494494
for file in (
@@ -704,9 +704,9 @@ def _get_transitive_artifacts(deps):
704704

705705
def _print_env():
706706
return [
707-
"##echo## \"Environment:______________\"",
707+
"##echo## Environment:______________",
708708
"##env##",
709-
"##echo## \"__________________________\"",
709+
"##echo## __________________________",
710710
]
711711

712712
def _normalize_path(path):
@@ -767,14 +767,14 @@ def _copy_deps_and_tools(files):
767767
lines += _symlink_contents_to_dir("include", files.headers + files.include_dirs)
768768

769769
if files.tools_files:
770-
lines.append("##mkdirs## $$EXT_BUILD_DEPS$$/bin")
770+
lines.append("##mkdirs## \"$$EXT_BUILD_DEPS$$/bin\"")
771771
for tool in files.tools_files:
772772
tool_prefix = "$EXT_BUILD_ROOT/"
773773
tool = tool[len(tool_prefix):] if tool.startswith(tool_prefix) else tool
774-
lines.append("##symlink_to_dir## $$EXT_BUILD_ROOT$$/{} $$EXT_BUILD_DEPS$$/bin/ False".format(tool))
774+
lines.append("##symlink_to_dir## \"$$EXT_BUILD_ROOT$$/{}\" \"$$EXT_BUILD_DEPS$$/bin/\" False".format(tool))
775775

776776
for ext_dir in files.ext_build_dirs:
777-
lines.append("##symlink_to_dir## $$EXT_BUILD_ROOT$$/{} $$EXT_BUILD_DEPS$$ True".format(_file_path(ext_dir)))
777+
lines.append("##symlink_to_dir## \"$$EXT_BUILD_ROOT$$/{}\" \"$$EXT_BUILD_DEPS$$\" True".format(_file_path(ext_dir)))
778778

779779
lines.append("##path## $$EXT_BUILD_DEPS$$/bin")
780780

@@ -786,13 +786,13 @@ def _symlink_contents_to_dir(dir_name, files_list):
786786
files_list = collections.uniq(files_list)
787787
if len(files_list) == 0:
788788
return []
789-
lines = ["##mkdirs## $$EXT_BUILD_DEPS$$/" + dir_name]
789+
lines = ["##mkdirs## \"$$EXT_BUILD_DEPS$$/" + dir_name + "\""]
790790

791791
for file in files_list:
792792
path = _file_path(file).strip()
793793
if path:
794794
lines.append("##symlink_contents_to_dir## \
795-
$$EXT_BUILD_ROOT$$/{} $$EXT_BUILD_DEPS$$/{} True".format(path, dir_name))
795+
\"$$EXT_BUILD_ROOT$$/{}\" \"$$EXT_BUILD_DEPS$$/{}\" True".format(path, dir_name))
796796

797797
return lines
798798

@@ -1121,4 +1121,4 @@ def _expand_locations_in_string(ctx, expandable, data):
11211121
if "EXT_BUILD_ROOT" in expandable:
11221122
return ctx.expand_location(expandable, data)
11231123
else:
1124-
return ctx.expand_location(expandable.replace("$(execpath ", "$$EXT_BUILD_ROOT$$/$(execpath "), data)
1124+
return ctx.expand_location(expandable.replace("$(execpath ", "\"$$EXT_BUILD_ROOT$$\"/$(execpath "), data)

foreign_cc/private/framework/toolchains/windows_commands.bzl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def script_extension():
99
return ".sh"
1010

1111
def pwd():
12-
return "$(type -t cygpath > /dev/null && cygpath $(pwd) -m || pwd -W)"
12+
return "\"$(type -t cygpath > /dev/null && cygpath \"$(pwd)\" -m || pwd -W)\""
1313

1414
def echo(text):
1515
return "echo \"{text}\"".format(text = text)
@@ -108,7 +108,7 @@ fi
108108
)
109109

110110
def copy_dir_contents_to_dir(source, target):
111-
return """cp -L -r --no-target-directory "{source}" "{target}" && $REAL_FIND "{target}" -type f -exec touch -r "{source}" "{{}}" \\;""".format(
111+
return """cp -L -r --no-target-directory {source} {target} && $REAL_FIND {target} -type f -exec touch -r {source} "{{}}" \\;""".format(
112112
source = source,
113113
target = target,
114114
)
@@ -253,28 +253,28 @@ fi""".format(dir_ = dir_)
253253
return FunctionAndCallInfo(text = text)
254254

255255
def define_absolute_paths(dir_, abs_path):
256-
return "##replace_in_files## {dir_} {REPLACE_VALUE} {abs_path}".format(
256+
return "##replace_in_files## \"{dir_}\" \"{REPLACE_VALUE}\" \"{abs_path}\"".format(
257257
dir_ = dir_,
258258
REPLACE_VALUE = "\\${EXT_BUILD_DEPS}",
259259
abs_path = abs_path,
260260
)
261261

262262
def replace_absolute_paths(dir_, abs_path):
263-
return "##replace_in_files## {dir_} {abs_path} {REPLACE_VALUE}".format(
263+
return "##replace_in_files## \"{dir_}\" \"{abs_path}\" \"{REPLACE_VALUE}\"".format(
264264
dir_ = dir_,
265265
REPLACE_VALUE = "\\${EXT_BUILD_DEPS}",
266266
abs_path = abs_path,
267267
)
268268

269269
def define_sandbox_paths(dir_, abs_path):
270-
return "##replace_in_files## {dir_} {REPLACE_VALUE} {abs_path}".format(
270+
return "##replace_in_files## \"{dir_}\" \"{REPLACE_VALUE}\" \"{abs_path}\"".format(
271271
dir_ = dir_,
272272
REPLACE_VALUE = "\\${EXT_BUILD_ROOT}",
273273
abs_path = abs_path,
274274
)
275275

276276
def replace_sandbox_paths(dir_, abs_path):
277-
return "##replace_in_files## {dir_} {abs_path} {REPLACE_VALUE}".format(
277+
return "##replace_in_files## \"{dir_}\" \"{abs_path}\" \"{REPLACE_VALUE}\"".format(
278278
dir_ = dir_,
279279
REPLACE_VALUE = "\\${EXT_BUILD_ROOT}",
280280
abs_path = abs_path,

foreign_cc/private/run_shell_file_utils.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def copy_directory(actions, orig_path, copy_path):
4646
return _created_by_script(
4747
file = dir_copy,
4848
script = "\n".join([
49-
"##mkdirs## $$EXT_BUILD_ROOT$$/" + dir_copy.path,
50-
"##copy_dir_contents_to_dir## {} $$EXT_BUILD_ROOT$$/{}".format(
49+
"##mkdirs## \"$$EXT_BUILD_ROOT$$/" + dir_copy.path + "\"",
50+
"##copy_dir_contents_to_dir## \"{}\" \"$$EXT_BUILD_ROOT$$/{}\"".format(
5151
orig_path,
5252
dir_copy.path,
5353
),

test/symlink_contents_to_dir_test_rule.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def _symlink_contents_to_dir_test_rule_impl(ctx):
1111
dir2 = detect_root(ctx.attr.dir2)
1212
script_lines = [
1313
"##mkdirs## aaa",
14-
"##symlink_contents_to_dir## %s aaa False" % dir1,
14+
"##symlink_contents_to_dir## \"%s\" aaa False" % dir1,
1515
"##symlink_contents_to_dir## %s aaa False" % dir2,
1616
"ls -R aaa > %s" % out.path,
1717
]

toolchains/prebuilt_toolchains.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,5 @@ def main():
505505
)
506506
)
507507

508-
509508
if __name__ == "__main__":
510509
main()

0 commit comments

Comments
 (0)