Commit eaefd922 authored by Kenton Varda's avatar Kenton Varda

Improve heuristics for capnpc -o param on Windows. Fixes #722.

parent 68990bb3
...@@ -382,10 +382,14 @@ public: ...@@ -382,10 +382,14 @@ public:
kj::StringPtr dir = spec.slice(*split + 1); kj::StringPtr dir = spec.slice(*split + 1);
auto plugin = spec.slice(0, *split); auto plugin = spec.slice(0, *split);
KJ_IF_MAYBE(split2, dir.findFirst(':')) { if (*split == 1 && (dir.startsWith("/") || dir.startsWith("\\"))) {
// Grr, there are two colons. Might this be a Windows path? Let's do some heuristics. // The colon is the second character and is immediately followed by a slash or backslash.
if (*split == 1 && (dir.startsWith("/") || dir.startsWith("\\"))) { // So, the user passed something like `-o c:/foo`. Is this a request to run the C plugin
// So, the first ':' was the second char, and was followed by '/' or '\', e.g.: // and output to `/foo`? Or are we on Windows, and is this a request to run the plugin
// `c:/foo`?
KJ_IF_MAYBE(split2, dir.findFirst(':')) {
// There are two colons. The first ':' was the second char, and was followed by '/' or
// '\', e.g.:
// capnp compile -o c:/foo.exe:bar // capnp compile -o c:/foo.exe:bar
// //
// In this case we can conclude that the second colon is actually meant to be the // In this case we can conclude that the second colon is actually meant to be the
...@@ -407,18 +411,24 @@ public: ...@@ -407,18 +411,24 @@ public:
// -> CONTRADICTION // -> CONTRADICTION
// //
// We therefore conclude that the *second* colon is in fact the plugin/location separator. // We therefore conclude that the *second* colon is in fact the plugin/location separator.
//
// Note that there is still an ambiguous case:
// capnp compile -o c:/foo
//
// In this unfortunate case, we have no way to tell if the user meant "use the 'c' plugin
// and output to /foo" or "use the plugin c:/foo and output to the default location". We
// prefer the former interpretation, because the latter is Windows-specific and such
// users can always explicitly specify the output location like:
// capnp compile -o c:/foo:.
dir = dir.slice(*split2 + 1); dir = dir.slice(*split2 + 1);
plugin = spec.slice(0, *split2 + 2); plugin = spec.slice(0, *split2 + 2);
#if _WIN32
} else {
// The user wrote something like:
//
// capnp compile -o c:/foo/bar
//
// What does this mean? It depends on what system we're on. On a Unix system, the above
// clearly is a request to run the `capnpc-c` plugin (perhaps to output C code) and write
// to the directory /foo/bar. But on Windows, absolute paths do not start with '/', and
// the above is actually a request to run the plugin `c:/foo/bar`, outputting to the
// current directory.
outputs.add(OutputDirective { spec.asArray(), nullptr });
return true;
#endif
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment