Skip to content

Commit

Permalink
Bug 1817372 - Allow -windows-msvc and -windows-gnu triplets as host a…
Browse files Browse the repository at this point in the history
…nd target. r=firefox-build-system-reviewers,andi

This is the first step before entirely deprecating the use of -mingw32
targets to mean msvc.

Differential Revision: https://phabricator.services.mozilla.com/D170169
  • Loading branch information
glandium committed Feb 17, 2023
1 parent 072a4bc commit 4c26d26
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 50 deletions.
2 changes: 1 addition & 1 deletion build/moz.configure/bindgen.configure
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def bindgen_clang_compiler(
return
# Hack before bug 1617793: if the compiler is clang-cl, hack the target
if cxx_compiler.type == "clang-cl":
target = split_triplet("%s-pc-windows-msvc" % target.raw_cpu, allow_msvc=True)
target = split_triplet("%s-pc-windows-msvc" % target.raw_cpu)
flags = []
if sysroot_path:
flags.extend(("--sysroot", sysroot_path))
Expand Down
52 changes: 38 additions & 14 deletions build/moz.configure/init.configure
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,14 @@ option(
)


@imports(_from="mozbuild.configure.constants", _import="Abi")
@imports(_from="mozbuild.configure.constants", _import="CPU")
@imports(_from="mozbuild.configure.constants", _import="CPU_bitness")
@imports(_from="mozbuild.configure.constants", _import="Endianness")
@imports(_from="mozbuild.configure.constants", _import="Kernel")
@imports(_from="mozbuild.configure.constants", _import="OS")
@imports(_from="__builtin__", _import="ValueError")
def split_triplet(triplet, allow_msvc=False, allow_wasi=False):
def split_triplet(triplet, allow_wasi=False):
# The standard triplet is defined as
# CPU_TYPE-VENDOR-OPERATING_SYSTEM
# There is also a quartet form:
Expand All @@ -464,7 +465,9 @@ def split_triplet(triplet, allow_msvc=False, allow_wasi=False):
# which can probably be improved/cleaned up because they are based on a
# mix of uname and config.guess output, while we now only use the latter,
# which presumably has a cleaner and leaner output. Let's refine later.
os = os.replace("/", "_")
raw_os = os = os.replace("/", "_")
abi = None
sub_configure_alias = triplet
if "android" in os:
canonical_os = "Android"
canonical_kernel = "Linux"
Expand All @@ -476,10 +479,18 @@ def split_triplet(triplet, allow_msvc=False, allow_wasi=False):
canonical_kernel = "kFreeBSD"
elif os.startswith("gnu"):
canonical_os = canonical_kernel = "GNU"
elif os.startswith("mingw") or (allow_msvc and os == "windows-msvc"):
# windows-msvc is only opt-in for the caller of this function until
# full support in bug 1617793.
elif os.startswith("mingw") or os in ("windows-msvc", "windows-gnu"):
canonical_os = canonical_kernel = "WINNT"
if not os.startswith("mingw"):
if os == "windows-msvc":
abi = "msvc"
elif os == "windows-gnu":
abi = "mingw"
# Many things down the line are looking for the string "mingw32"
# until they are all fixed, we pretend that's the raw os we had
# in the first place, even when we didn't.
sub_configure_alias = sub_configure_alias[: -len(os)] + "mingw32"
raw_os = "mingw32"
elif os.startswith("darwin"):
canonical_kernel = "Darwin"
canonical_os = "OSX"
Expand Down Expand Up @@ -563,8 +574,6 @@ def split_triplet(triplet, allow_msvc=False, allow_wasi=False):

if os.startswith("darwin"):
toolchain = "%s-apple-%s" % (cpu, os)
elif canonical_cpu == "aarch64" and canonical_os == "WINNT":
toolchain = "aarch64-windows-msvc"
else:
toolchain = "%s-%s" % (cpu, os)

Expand All @@ -575,10 +584,13 @@ def split_triplet(triplet, allow_msvc=False, allow_wasi=False):
kernel=Kernel(canonical_kernel),
os=OS(canonical_os),
endianness=Endianness(endianness),
# For now, only report the Windows ABI.
abi=abi and Abi(abi),
raw_cpu=cpu,
raw_os=os,
raw_os=raw_os,
toolchain=toolchain,
vendor=vendor,
sub_configure_alias=sub_configure_alias,
)


Expand All @@ -595,6 +607,7 @@ def help_host_target(help, host, target):
kernel="unknown",
os="unknown",
endianness="unknown",
abi="unknown",
raw_cpu="unknown",
raw_os="unknown",
toolchain="unknown-unknown",
Expand All @@ -603,7 +616,19 @@ def help_host_target(help, host, target):

def config_sub(shell, triplet):
config_sub = os.path.join(os.path.dirname(__file__), "..", "autoconf", "config.sub")
return check_cmd_output(shell, config_sub, triplet).strip()
# Config.sub doesn't like the *-windows-msvc/*-windows-gnu triplets, so
# munge those before and after calling config.sub.
suffix = None
mingw_suffix = "-mingw32"
for check_suffix in ("-windows-msvc", "-windows-gnu"):
if triplet.endswith(check_suffix):
suffix = check_suffix
triplet = triplet[: -len(suffix)] + mingw_suffix
result = check_cmd_output(shell, config_sub, triplet).strip()
if suffix:
assert result.endswith(mingw_suffix)
result = result[: -len(mingw_suffix)] + suffix
return result


@depends("--host", shell)
Expand All @@ -617,9 +642,9 @@ def real_host(value, shell):
"PROCESSOR_ARCHITECTURE"
)
if arch == "AMD64":
return split_triplet("x86_64-pc-mingw32")
return split_triplet("x86_64-pc-windows-msvc")
elif arch == "x86":
return split_triplet("i686-pc-mingw32")
return split_triplet("i686-pc-windows-msvc")

if not value:
config_guess = os.path.join(
Expand Down Expand Up @@ -729,13 +754,12 @@ add_old_configure_assignment("HAVE_64BIT_BUILD", have_64_bit)

@depends(host)
def host_for_sub_configure(host):
return "--host=%s" % host.alias
return "--host=%s" % host.sub_configure_alias


@depends(target)
def target_for_sub_configure(target):
target_alias = target.alias
return "--target=%s" % target_alias
return "--target=%s" % target.sub_configure_alias


# These variables are for compatibility with the current moz.builds and
Expand Down
20 changes: 13 additions & 7 deletions build/moz.configure/rust.configure
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,6 @@ def rust_supported_targets(rustc):
if t.startswith("thumb"):
cpu, rest = t.split("-", 1)
retry = "-".join(("arm", rest))
elif t.endswith("-windows-msvc"):
retry = t[: -len("windows-msvc")] + "mingw32"
elif t.endswith("-windows-gnu"):
retry = t[: -len("windows-gnu")] + "mingw32"
else:
continue
try:
Expand Down Expand Up @@ -310,7 +306,12 @@ def detect_rustc_target(
# narrow further down using extra information from the build system.
# - For windows targets, correlate with the C compiler type
if host_or_target.kernel == "WINNT":
if compiler_info.type in ("gcc", "clang"):
if host_or_target.abi:
if host_or_target.abi == "msvc":
suffix = "windows-msvc"
elif host_or_target.abi == "mingw":
suffix = "windows-gnu"
elif compiler_info.type in ("gcc", "clang"):
suffix = "windows-gnu"
else:
suffix = "windows-msvc"
Expand Down Expand Up @@ -374,8 +375,13 @@ def detect_rustc_target(
) and c.rust_target.endswith(suffix):
return c.rust_target

# See if we can narrow down on the exact alias
narrowed = [c for c in candidates if c.target.alias == host_or_target.alias]
# See if we can narrow down on the exact alias.
# We use the sub_configure_alias to keep support mingw32 triplets as input.
narrowed = [
c
for c in candidates
if c.target.sub_configure_alias == host_or_target.sub_configure_alias
]
if len(narrowed) == 1:
return narrowed[0].rust_target
elif narrowed:
Expand Down
29 changes: 25 additions & 4 deletions build/moz.configure/toolchain.configure
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,13 @@ def default_c_compilers(host_or_target, other_c_compiler=None):
host_or_target, target, toolchain_prefix, *other_c_compiler
):
if host_or_target.kernel == "WINNT":
supported = types = ("clang-cl", "clang")
if host_or_target.abi:
if host_or_target.abi == "msvc":
supported = types = ("clang-cl",)
elif host_or_target.abi == "mingw":
supported = types = ("clang",)
else:
supported = types = ("clang-cl", "clang")
elif host_or_target.kernel == "Darwin":
types = ("clang",)
supported = ("clang", "gcc")
Expand Down Expand Up @@ -1339,6 +1345,12 @@ def compiler(
):
flags.extend(overlay_flags)

if (info.type, host_or_target.abi) in (
("clang", "msvc"),
("clang-cl", "mingw"),
):
raise FatalCheckError("Unknown compiler or compiler not supported.")

# If you want to bump the version check here ensure the version
# is known for Xcode in get_compiler_info.
if info.type == "clang" and info.version < "5.0":
Expand Down Expand Up @@ -1512,14 +1524,23 @@ host_cxx_compiler = compiler(

@template
def windows_abi(host_or_target, c_compiler):
@depends(host_or_target, c_compiler)
def windows_abi(host_or_target, c_compiler):
@depends(host_or_target)
def windows_abi(host_or_target):
if host_or_target.os == "WINNT":
return host_or_target.abi

@depends(host_or_target, windows_abi)
def need_windows_abi_from_compiler(host_or_target, windows_abi):
return host_or_target.os == "WINNT" and windows_abi is None

@depends(host_or_target, c_compiler, when=need_windows_abi_from_compiler)
def windows_abi_from_compiler(host_or_target, c_compiler):
if host_or_target.os == "WINNT":
if c_compiler.type == "clang-cl":
return "msvc"
return "mingw"

return windows_abi
return windows_abi | windows_abi_from_compiler


target_windows_abi = windows_abi(target, c_compiler)
Expand Down
2 changes: 1 addition & 1 deletion moz.configure
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def library_name_info_template(host_or_target):
target: target_windows_abi,
}[host_or_target]

@depends(host_or_target, windows_abi, so_version)
@depends(host_or_target, host_or_target.abi | windows_abi, so_version)
def library_name_info_impl(host_or_target, windows_abi, so_version):
if host_or_target.kernel == "WINNT":
# There aren't artifacts for mingw builds, so it's OK that the
Expand Down
5 changes: 5 additions & 0 deletions python/mozbuild/mozbuild/configure/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
"win64",
)

Abi = EnumString.subclass(
"msvc",
"mingw",
)

# The order of those checks matter
CPU_preprocessor_checks = OrderedDict(
(
Expand Down
41 changes: 26 additions & 15 deletions python/mozbuild/mozbuild/test/configure/test_moz_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TargetTest(BaseConfigureTest):
def get_target(self, args, env={}):
if "linux" in self.HOST:
platform = "linux2"
elif "mingw" in self.HOST:
elif "mingw" in self.HOST or "windows" in self.HOST:
platform = "win32"
elif "openbsd6" in self.HOST:
platform = "openbsd6"
Expand All @@ -43,20 +43,21 @@ def test_target(self):
"i686-unknown-linux-gnu",
)
self.assertEqual(
self.get_target(["--target=i686-pc-mingw32"]), "i686-pc-mingw32"
self.get_target(["--target=i686-pc-windows-msvc"]), "i686-pc-windows-msvc"
)


class TestTargetWindows(TargetTest):
# BaseConfigureTest uses this as the return value for config.guess
HOST = "i686-pc-mingw32"
HOST = "i686-pc-windows-msvc"

def test_target(self):
self.assertEqual(self.get_target([]), self.HOST)
self.assertEqual(
self.get_target(["--target=x86_64-pc-mingw32"]), "x86_64-pc-mingw32"
self.get_target(["--target=x86_64-pc-windows-msvc"]),
"x86_64-pc-windows-msvc",
)
self.assertEqual(self.get_target(["--target=x86_64"]), "x86_64-pc-mingw32")
self.assertEqual(self.get_target(["--target=x86_64"]), "x86_64-pc-windows-msvc")

# The tests above are actually not realistic, because most Windows
# machines will have a few environment variables that make us not
Expand All @@ -67,31 +68,41 @@ def test_target(self):
"PROCESSOR_ARCHITECTURE": "x86",
"PROCESSOR_ARCHITEW6432": "AMD64",
}
self.assertEqual(self.get_target([], env), "x86_64-pc-mingw32")
self.assertEqual(self.get_target([], env), "x86_64-pc-windows-msvc")
self.assertEqual(
self.get_target(["--target=i686-pc-mingw32"]), "i686-pc-mingw32"
self.get_target(["--target=i686-pc-windows-msvc"]), "i686-pc-windows-msvc"
)
self.assertEqual(self.get_target(["--target=i686"]), "i686-pc-mingw32")
self.assertEqual(self.get_target(["--target=i686"]), "i686-pc-windows-msvc")

# 64-bits process on x86_64 host.
env = {
"PROCESSOR_ARCHITECTURE": "AMD64",
}
self.assertEqual(self.get_target([], env), "x86_64-pc-mingw32")
self.assertEqual(self.get_target([], env), "x86_64-pc-windows-msvc")
self.assertEqual(
self.get_target(["--target=i686-pc-mingw32"]), "i686-pc-mingw32"
self.get_target(["--target=i686-pc-windows-msvc"]), "i686-pc-windows-msvc"
)
self.assertEqual(self.get_target(["--target=i686"]), "i686-pc-mingw32")
self.assertEqual(self.get_target(["--target=i686"]), "i686-pc-windows-msvc")

# 32-bits process on x86 host.
env = {
"PROCESSOR_ARCHITECTURE": "x86",
}
self.assertEqual(self.get_target([], env), "i686-pc-mingw32")
self.assertEqual(self.get_target([], env), "i686-pc-windows-msvc")
self.assertEqual(
self.get_target(["--target=x86_64-pc-mingw32"]), "x86_64-pc-mingw32"
self.get_target(["--target=x86_64-pc-windows-msvc"]),
"x86_64-pc-windows-msvc",
)
self.assertEqual(self.get_target(["--target=x86_64"]), "x86_64-pc-windows-msvc")

# While host autodection will give us a -windows-msvc triplet, setting host
# is expecting to implicitly set the target.
self.assertEqual(
self.get_target(["--host=x86_64-pc-windows-gnu"]), "x86_64-pc-windows-gnu"
)
self.assertEqual(
self.get_target(["--host=x86_64-pc-mingw32"]), "x86_64-pc-mingw32"
)
self.assertEqual(self.get_target(["--target=x86_64"]), "x86_64-pc-mingw32")


class TestTargetAndroid(TargetTest):
Expand Down Expand Up @@ -150,7 +161,7 @@ def check_nsis_version(version):
sandbox = self.get_sandbox(
{"/usr/bin/makensis": FakeNSIS(version)},
{},
["--target=x86_64-pc-mingw32", "--disable-bootstrap"],
["--target=x86_64-pc-windows-msvc", "--disable-bootstrap"],
{"PATH": "/usr/bin", "MAKENSISU": "/usr/bin/makensis"},
)
return sandbox._value_for(sandbox["nsis_version"])
Expand Down
Loading

0 comments on commit 4c26d26

Please sign in to comment.