Skip to content

Commit

Permalink
Improve flags handling for meson
Browse files Browse the repository at this point in the history
This aligns this to what other tools are doing.

This is required to fix --sysroot flag even without cross compiling for example when using in combination with a hermetic toolchain.
  • Loading branch information
mering committed Jan 2, 2025
1 parent 92f81ee commit ff2d6f9
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions foreign_cc/meson.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def _create_meson_script(configureParameters):
inputs = configureParameters.inputs

tools = get_tools_info(ctx)
flags = get_flags_info(ctx)
script = pkgconfig_script(inputs.ext_build_dirs)

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

# set flags same as foreign_cc/private/cc_toolchain_util.bzl
# cannot use get_flags_info() because bazel adds additional flags that
# aren't compatible with compiler or linker above
copts = (ctx.fragments.cpp.copts + ctx.fragments.cpp.conlyopts + getattr(ctx.attr, "copts", [])) or []
cxxopts = (ctx.fragments.cpp.copts + ctx.fragments.cpp.cxxopts + getattr(ctx.attr, "copts", [])) or []

copts = flags.cc
cxxopts = flags.cxx
if copts:
script.append("##export_var## CFLAGS \"{} ${{CFLAGS:-}}\"".format(" ".join(copts).replace("\"", "'")))
script.append("##export_var## CFLAGS \"{} ${{CFLAGS:-}}\"".format(_join_flags_list(ctx.workspace_name, copts).replace("\"", "'")))
if cxxopts:
script.append("##export_var## CXXFLAGS \"{} ${{CXXFLAGS:-}}\"".format(" ".join(cxxopts).replace("\"", "'")))
script.append("##export_var## CXXFLAGS \"{} ${{CXXFLAGS:-}}\"".format(_join_flags_list(ctx.workspace_name, cxxopts).replace("\"", "'")))

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

script.append("##export_var## CMAKE {}".format(attrs.cmake_path))
script.append("##export_var## NINJA {}".format(attrs.ninja_path))
Expand Down Expand Up @@ -243,3 +239,6 @@ def _absolutize(workspace_name, text, force = False):
return "\"{}\"".format(text)

return absolutize_path_in_str(workspace_name, "$EXT_BUILD_ROOT/", text, force)

def _join_flags_list(workspace_name, flags):
return " ".join([_absolutize(workspace_name, flag) for flag in flags])

0 comments on commit ff2d6f9

Please sign in to comment.