Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

add optional target specification to CpuFeatures#390

Merged
pchickey merged 1 commit intobytecodealliance:masterfrom
froydnj:target-for-cpu-features
Jan 21, 2020
Merged

add optional target specification to CpuFeatures#390
pchickey merged 1 commit intobytecodealliance:masterfrom
froydnj:target-for-cpu-features

Conversation

@froydnj
Copy link
Copy Markdown
Contributor

@froydnj froydnj commented Jan 14, 2020

This change (or something like it) is a prerequisite to being able to
pass --target [TARGET] to lucetc for cross-compilation purposes.

@froydnj
Copy link
Copy Markdown
Contributor Author

froydnj commented Jan 14, 2020

...it doesn't actually add the --target command-line option, because I'm not entirely sure that it's the right design to have the target separate from the specification of CpuFeatures itself. Of course, there could be a check added in CpuFeatures::isa_builder to make sure we're not specifying some non-x86-64 target and things could be rearranged later, but as this is my first patch, I'm not sure what the conventions around the design here are.

@jedisct1
Copy link
Copy Markdown
Contributor

Thanks!

This is a good start, and having the target distinct from CpuFeatures seems appropriate.

Shared libraries are currently created using the ld tool, that differs a lot between platforms. How are you planning to build Mach-O libraries from Linux?

@froydnj
Copy link
Copy Markdown
Contributor Author

froydnj commented Jan 14, 2020

Shared libraries are currently created using the ld tool, that differs a lot between platforms. How are you planning to build Mach-O libraries from Linux?

It looks like there's support for overriding what linker + flags to use already via environment variables:

lucet/lucetc/src/lib.rs

Lines 379 to 384 in 4b59161

let mut cmd_ld = Command::new(env::var("LD").unwrap_or(LD_DEFAULT.into()));
cmd_ld.arg(objpath.as_ref());
let env_ldflags = env::var("LDFLAGS").unwrap_or(LDFLAGS_DEFAULT.into());
for flag in env_ldflags.split_whitespace() {
cmd_ld.arg(flag);
}

so users who want to cross compile can just set the appropriate environment variables. It's not very nice, but it would work in the short term.

An alternative approach is select things based on the dynamically-determined target, not the statically-determined build platform. That's probably the right thing to do in the long term. This might wind up selecting more canonical names (ld64 for OS X targets, for instance), but I think that's OK; anybody who wants to do cross compilation probably already has the tools set up in PATH and whatnot.

As far as the tools themselves, Apple open-sources their binutils equivalents (ld64 and cctools), people have worked to make them easily compilable, and Firefox already uses those tools to cross-compile for Mac. So a lot of the hard work has already been done. :)

@jedisct1
Copy link
Copy Markdown
Contributor

If a target is given, maybe we should ignore the defaults for the linker name and arguments, and print a message explaining that they have to be defined.

Otherwise, we may get a bunch of "it doesn't work!" support tickets from people setting the target without overriding the linker opts.

@froydnj
Copy link
Copy Markdown
Contributor Author

froydnj commented Jan 14, 2020

If a target is given, maybe we should ignore the defaults for the linker name and arguments, and print a message explaining that they have to be defined.

After thinking about it a little more, I think a better design is to have Compiler::new and company take Triples directly, rather than Option<String>, and pass the Triple around everywhere, including to link_so. The parsing into triples can be done early during command-line processing, and nobody will have to remember that None means "for the host".

@froydnj froydnj force-pushed the target-for-cpu-features branch 2 times, most recently from 334881c to 4225f74 Compare January 15, 2020 19:55
@froydnj
Copy link
Copy Markdown
Contributor Author

froydnj commented Jan 15, 2020

New version up (not sure what the conventions are here about pushing new patches vs. force pushing); I did not require that providing --target also meant that you had to provide the environment variable, since we can infer the defaults from the targets in at least the simple cases so far.

Currently, `lucetc` assumes that the machine that it's compiling
for (the "target") is the same as the machine on which `lucetc` is
running ("the host").  While this is a reasonable assumption to make,
many future uses of `lucetc` may require these machines to be distinct.
Towards that end, let's introduce a `--target` option for specifying the
machine `lucetc` is supposed to be generating code for, and thread that
information through to all the places that require it.
@froydnj froydnj force-pushed the target-for-cpu-features branch from 4225f74 to b60dc3c Compare January 16, 2020 20:51
@froydnj
Copy link
Copy Markdown
Contributor Author

froydnj commented Jan 21, 2020

Review ping; patch updated.

Copy link
Copy Markdown
Contributor

@pchickey pchickey left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

@pchickey pchickey merged commit 8796798 into bytecodealliance:master Jan 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants