Skip to content

Commit 3a70fdf

Browse files
authored
Improve flags handling for meson (#1303)
1 parent 2bbab4f commit 3a70fdf

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

examples/third_party/glib/BUILD.glib.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ DEPS = [
2222

2323
meson(
2424
name = "glib",
25+
copts = select({
26+
":msvc_compiler": [
27+
# This string is used as enum in gio/glib-compile-resources.c:718
28+
"-UCOMPILER_MSVC",
29+
],
30+
"//conditions:default": [],
31+
}),
2532
env = select({
2633
":msvc_compiler": {
2734
"INCLUDE": "$$EXT_BUILD_DEPS/include",

foreign_cc/meson.bzl

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def _create_meson_script(configureParameters):
6969
inputs = configureParameters.inputs
7070

7171
tools = get_tools_info(ctx)
72+
flags = get_flags_info(ctx)
7273
script = pkgconfig_script(inputs.ext_build_dirs)
7374

7475
# CFLAGS and CXXFLAGS are also set in foreign_cc/private/cmake_script.bzl, so that meson
@@ -81,20 +82,15 @@ def _create_meson_script(configureParameters):
8182
if " " not in tools.cxx:
8283
script.append("##export_var## CXX {}".format(_absolutize(ctx.workspace_name, tools.cxx)))
8384

84-
# set flags same as foreign_cc/private/cc_toolchain_util.bzl
85-
# cannot use get_flags_info() because bazel adds additional flags that
86-
# aren't compatible with compiler or linker above
87-
copts = (ctx.fragments.cpp.copts + ctx.fragments.cpp.conlyopts + getattr(ctx.attr, "copts", [])) or []
88-
cxxopts = (ctx.fragments.cpp.copts + ctx.fragments.cpp.cxxopts + getattr(ctx.attr, "copts", [])) or []
89-
85+
copts = flags.cc
86+
cxxopts = flags.cxx
9087
if copts:
91-
script.append("##export_var## CFLAGS \"{} ${{CFLAGS:-}}\"".format(" ".join(copts).replace("\"", "'")))
88+
script.append("##export_var## CFLAGS \"{} ${{CFLAGS:-}}\"".format(_join_flags_list(ctx.workspace_name, copts).replace("\"", "'")))
9289
if cxxopts:
93-
script.append("##export_var## CXXFLAGS \"{} ${{CXXFLAGS:-}}\"".format(" ".join(cxxopts).replace("\"", "'")))
90+
script.append("##export_var## CXXFLAGS \"{} ${{CXXFLAGS:-}}\"".format(_join_flags_list(ctx.workspace_name, cxxopts).replace("\"", "'")))
9491

95-
flags = get_flags_info(ctx)
9692
if flags.cxx_linker_executable:
97-
script.append("##export_var## LDFLAGS \"{} ${{LDFLAGS:-}}\"".format(" ".join(flags.cxx_linker_executable).replace("\"", "'")))
93+
script.append("##export_var## LDFLAGS \"{} ${{LDFLAGS:-}}\"".format(_join_flags_list(ctx.workspace_name, flags.cxx_linker_executable).replace("\"", "'")))
9894

9995
script.append("##export_var## CMAKE {}".format(attrs.cmake_path))
10096
script.append("##export_var## NINJA {}".format(attrs.ninja_path))
@@ -240,7 +236,7 @@ def meson_with_requirements(name, requirements, **kwargs):
240236
)
241237

242238
def _absolutize(workspace_name, text, force = False):
243-
if text.strip(" ").startswith("C:") or text.strip(" ").startswith("c:"):
244-
return "\"{}\"".format(text)
245-
246239
return absolutize_path_in_str(workspace_name, "$EXT_BUILD_ROOT/", text, force)
240+
241+
def _join_flags_list(workspace_name, flags):
242+
return " ".join([_absolutize(workspace_name, flag) for flag in flags])

foreign_cc/private/cc_toolchain_util.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def absolutize_path_in_str(workspace_name, root_str, text, force = False):
383383

384384
def _prefix(text, from_str, prefix):
385385
(before, middle, after) = text.partition(from_str)
386-
if not middle or before.endswith("/"):
386+
if not middle or before.endswith("/") or before.endswith("\\"):
387387
return text
388388
return before + prefix + middle + after
389389

0 commit comments

Comments
 (0)