-
Notifications
You must be signed in to change notification settings - Fork 249
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
feat: Add command -cwd
(change working directory)
#1024
Conversation
e6500f4
to
0dfbc25
Compare
Thanks for your pull request! It looks like this may be your first contribution to cppfront. I've emailed you the Contributor License Agreement (CLA), and once it's signed I can look at your pull request. Thanks again for your contribution. |
0dfbc25
to
ab0f9ec
Compare
Sure thing! Do you mind re-sending it though? My email got a bit mixed up so you sent it to the wrong one. |
Thanks! This is new to me as I'm only familiar with changing the working directory in shell scripts before invoking the compiler, so I have some questions... Can you point me to a similar existing flag on another compiler I could take a look at to understand the prior art better? Then I could look at examples and what other compilers do for this feature. A question about this example:
I'm slightly confused... Is that intended to be Today I can write
I tried variations of this but wasn't able to get this to work in bash? Thanks for your patience with all the questions! It's just because it's unfamiliar to me so I want to be sure I understand. Thanks again for the suggestion! |
Agree (and I do the same), the crux here though is that But (as evident from my many edits to the description for this PR), as I was adding it I got a feeling this middle-ground implementation of touching as little as possible but cover my specific use-case probably is redundant and I'll look into changing it to be, as you said, just
Long story short, let's keep this as "Work In Progress" and I'll ping when I have cooked something up. :) |
Actually, I think you've identified a real issue that should be easy to fix: Compilers generally put their output in the current directory. Cppfront should, too. When I run: $ cppfront ../demo.cpp2
$ g++ ../demo.cpp the first line puts the output file in the parent directory, the second in the current directory. So I think cppfront should just put files in the current directly by default, like all compilers do. @JohelEGP / @MaxSagebaum / everyone: Would making that change destabilize any of you, such as this Godbolt environment ? |
Yep, that was my plan as well. Especially useful when you want |
I usually prefer the -o option. But, I see the merit to align it to the standard behavior. The cwd option seems a good idea. |
Picking this up again after the April madness: I think we ended up agreeing that ideally cppfront would put files in the current directory by default. Is that right? My main question before doing that is whether it would destabilize the Godbolt environment linked above, or similar uses? Thanks again for pointing out this area needs improvement, however we end up improving it! |
My suggestion was to output to the current working directory, though mirror the input tree for the output(s). If relative includes are used that approach wouldn't interfere (maybe this was what you meant?). |
For what it's worth I would suggest to avoid this kind of situation and go for what compilers already do as much as possible. |
a22af16
to
7c20da7
Compare
I agree, though just to highlight the difference here: I imagine that a useful and easy to grasp approach could be that:
This way the behavior should remain the same (all tests are unaffected). // hello.h2
#include <subdir/world.h>
// .. $ tree
.
├── hello.cpp2
├── hello.h2
└── subdir
└── world.h2
$ cppfront -preserve-hierarchy -output-dir /tmp/test ./hello.cpp2 ./hello.h2 ./subdir/world.h2
$ tree /tmp/test
/tmp/test/
├── hello.cpp
├── hello.h
└── subdir
└── world.h
# compile ... Basically: The include written in I'll experiment a bit but please chip in with feedback. @hsutter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for waiting. I think with the suggested change we get both things -- -cwd
plus changing the default to put the Cpp1 output in the current directory. Final sanity check: Does that plan seem sensible?
source/to_cpp1.h
Outdated
@@ -164,6 +164,15 @@ static cmdline_processor::register_flag cmd_cpp1_filename( | |||
[](std::string const& name) { flag_cpp1_filename = name; } | |||
); | |||
|
|||
static auto flag_cwd = std::filesystem::path{};; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double ;;
source/to_cpp1.h
Outdated
if (!flag_cpp1_filename.empty()) { // use override if present | ||
cpp1_filename = flag_cpp1_filename; | ||
} | ||
else if (!flag_cwd.empty()) { // strip leading path if cwd was explicitly set |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we make this like be just else {
, then I think the semantics would be that you get your desired behavior if -cwd
is specified, and we get the desired behavior change of putting the output in the current directory, correct?
…ath is stripped In addition to enabling `-cwd` behavior, this will change the default to put files in the current directory even if a pathname is used to refer to a file in another directory (this default behavior will be overridden by `-o` or `-cwd` if specified)
Thanks! @helmesjo when you get a chance would you please check that the commit I pushed onto this PR before merging still keeps the behavior that you wanted? |
@hsutter Will do, and sorry for the delay! |
No worries, you've been waiting on me for a while! So have others, gradually working my way through the backlog... |
* cli: Add command '-cwd' (change working directory). * Tweak to strip leading path always, so that the `.cpp2` source file path is stripped In addition to enabling `-cwd` behavior, this will change the default to put files in the current directory even if a pathname is used to refer to a file in another directory (this default behavior will be overridden by `-o` or `-cwd` if specified) * Remove stray `;;` for clean builds --------- Co-authored-by: Herb Sutter <herb.sutter@gmail.com>
Makes it possible to have
cppfront
output generated files into a separate directory (unless-output
is specified).Before:
After (when using
-cwd
):$ ls ./hello.cpp2 subdir/ $ cppfront -cwd ./subdir ./hello.cpp $ tree . ├── hello.cpp2 └── subdir └── hello.cpp
Side note:
Arguably the output path should always be the current working directory with option to retain relative sub-directory hierarchy, eg. when passing multiple files (this is how most tools operate), and not be derived from input files. Though I assumed that would break tests (and require more special handling/options).
So running
cd build && cppfront [options] $(find . -name "*.h2" -o -name "*.cpp2")
with this structure:could yield this result: