Skip to content

build: enable handling of alpine-linux-musl triple #61376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 18, 2023

Conversation

MaxDesiatov
Copy link
Contributor

Currently, when building LLVM/clang on Alpine Linux, CMake toolchain file specifies incorrect <cpu_arch>-unknown-linux-musl target, which makes the build immediately fail. Correct target that allows building on Alpine should be specified as <cpu_arch>-alpine-linux-musl, with <cpu_arch> replaced with respective CPU platform, e.g. aarch64.

@MaxDesiatov
Copy link
Contributor Author

@swift-ci please smoke test

@MaxDesiatov MaxDesiatov force-pushed the maxd/build-alpine-product branch 2 times, most recently from 7b51100 to 32dac24 Compare September 30, 2022 12:54
@MaxDesiatov
Copy link
Contributor Author

@swift-ci please smoke test

@tomerd
Copy link
Contributor

tomerd commented Sep 30, 2022

the general fix makes sense. is a more elegant way to detect it than shelling out to clang? how does clang do it?

@MaxDesiatov
Copy link
Contributor Author

MaxDesiatov commented Sep 30, 2022

AFAIU, clang implements distro-specific parsing of /proc/version and /etc/*release files when running on Linux. Here we delegate to that logic, an alternative would be to reimplement it in our Python build scripts.

Copy link
Member

@etcwilde etcwilde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nits, lets use vendor instead of distro so that it keeps with existing triple terminology. Otherwise, LGTM.

How great it would be if build-script could just take a triple instead of trying to magically munge something together.

sysroot_arch, abi = self.get_linux_abi(arch)
return '{}-unknown-linux-{}'.format(sysroot_arch, abi)
sysroot_arch, distro, abi = self.get_linux_abi(arch)
return '{}-{}-linux-{}'.format(sysroot_arch, distro, abi)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field being substituted is the "vendor". I agree with @etcwilde that we should stick to standard terminology here.

SuSe also depends on the vendor being proper IIRC. Keep in mind that this will make cross-compilation more complicated as the target triple is embedded into the serialised swiftmodule. Do we want to consider filtering this in the driver? That would allow us to use the same swiftmodule on different distributions but get the correct triple for the link phase.

It may be interesting to consider actually adding a new field into the swiftmodule to actually embed a different clang triple to the swift triple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field being substituted is the "vendor". I agree with @etcwilde that we should stick to standard terminology here.

Thanks, that's updated now.

SuSe also depends on the vendor being proper IIRC. Keep in mind that this will make cross-compilation more complicated as the target triple is embedded into the serialised swiftmodule. Do we want to consider filtering this in the driver? That would allow us to use the same swiftmodule on different distributions but get the correct triple for the link phase.

It may be interesting to consider actually adding a new field into the swiftmodule to actually embed a different clang triple to the swift triple.

I don't have an informed opinion on this. Should I tag someone else to review this aspect of the change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could have sworn that the name of the swift module helped in this regard. e.g. Blarpy.swiftmodule/x86_64-apple-macos.swiftmodule vs Blarpy.swiftmodule/arm64-apple-macos.swiftmodule are applied to their corresponding architectures on macOS. I haven't looked closely so take it with a grain of salt, but having the swiftmodule key'd on its full triple should work on Linux too. The build system probably needs to learn to do this on things that aren't just Apple platforms though.

@xymus, can you confirm that? Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a different target triple should help to avoid loading incompatible swiftmodules in the case of different distributions. I wouldn't trust loading a swiftmodule built on a different distro as they could refer to things from the C world that are organized differently. That being said, I don't work much in the Linux side of things so I'd be interested to know if it's an accepted practice there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default Linux toolchain distribution does not use the directory layout like Windows and macOS, so you lose that ability to differentiate. How much C code actually gets serialised into the object layout? The only case that I would actually be concerned about is if we are serialising opaque C structures (e.g. pthread_t) somehow. The remainder of the differences I believe should be handled implicitly as the macros are contained within the C module, which is re-constructed through the modulemap. The current loading behavior I feel may be a bit confusing for users. We should definitely include a test case for that along with a reasonable error message if we intend to ship this as is.

@MaxDesiatov
Copy link
Contributor Author

@swift-ci please smoke test

vendor = 'unknown'

try:
output = shell.capture(["clang", "--version"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This concerns me. This is dependent on the value of -D LLVM_DEFALT_TARGET_TRIPLE passed when building clang. Worse yet, it simply invokes clang without any guarantees on which clang is being invoked. So, if I were to build a clang and add it to my path even on alpine, this could give me unknown as the vendor or any other literal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be a more reliable alternative to this invocation then, in your opinion?

Copy link
Member

@compnerd compnerd Oct 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that you should consider exposing this as a flag that can be explicitly specified, defaulting to processing /etc/*-release, /etc/os-release, and lsb_release -b. The default would only work under a non-cross-compiling scenario of course, but that applies to most cases for the default. We could expose this as a completely generic --default-target flag perhaps.

Copy link
Contributor Author

@MaxDesiatov MaxDesiatov Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to use -print-target-triple, which reliably gives us the actual triple and that's the only output it produces, so we don't need to do any additional parsing.

@MaxDesiatov MaxDesiatov force-pushed the maxd/build-alpine-product branch from 52593c5 to d3768b3 Compare November 21, 2022 12:15
@MaxDesiatov MaxDesiatov force-pushed the maxd/build-alpine-product branch 2 times, most recently from 26c62b6 to 1e37999 Compare January 11, 2023 16:26
@MaxDesiatov MaxDesiatov force-pushed the maxd/build-alpine-product branch 2 times, most recently from 5e1b11c to 5db8cee Compare January 18, 2023 14:47
@MaxDesiatov MaxDesiatov force-pushed the maxd/build-alpine-product branch 3 times, most recently from 37c40ec to 4181a4c Compare June 12, 2023 13:52
@MaxDesiatov MaxDesiatov force-pushed the maxd/build-alpine-product branch from 4181a4c to b62eb2a Compare June 19, 2023 19:45
@MaxDesiatov MaxDesiatov force-pushed the maxd/build-alpine-product branch from b62eb2a to 4ce56d3 Compare July 5, 2023 10:15
Currently, when building LLVM/clang on Alpine Linux, CMake toolchain file specifies incorrect `<cpu_arch>-unknown-linux-musl` target, which makes the build immediately fail. Correct target that allows building on Alpine should be specified as `<cpu_arch>-alpine-linux-musl`, with `<cpu_arch>` replaced with respective CPU platform, e.g. `aarch64`.
@MaxDesiatov MaxDesiatov force-pushed the maxd/build-alpine-product branch from 4ce56d3 to 501a5da Compare July 5, 2023 21:14
@MaxDesiatov MaxDesiatov requested a review from compnerd July 13, 2023 14:36
@MaxDesiatov MaxDesiatov requested a review from al45tair July 13, 2023 14:36
@MaxDesiatov
Copy link
Contributor Author

@swift-ci test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci build toolchain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants