Skip to content
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

Support reflecting CXXFLAGS and LDFLAGS environment variables #1030

Merged
merged 5 commits into from
Nov 6, 2024

Conversation

onion108
Copy link
Contributor

Added support for environment variables CXXFLAGS and LDFLAGS while generating makefile.

See #923.

Added support for environment variables `CXXFLAGS` and `LDFLAGS` while
generating makefile.
@ken-matsui
Copy link
Member

Why would you prefer to do this instead of my idea?

@onion108
Copy link
Contributor Author

Firstly, the most important reason is, as far as I know, many build systems that are widely used made use of those environment variables (autoconf, Makefile, CMake etc.). I think it will be less confusing to make the behavior consistent as those build system. (Even cargo respects RUSTFLAGS)

Also sometimes I would like to temporarily change the flags frequently to quickly see the result and adjust my flags in the config after I'm confident about the result. I personally prefer setting the environment variables to edit the config file over and over again because it's convenient to just add an environment variable in the command line, and I don't need to repeatly edit and save a file just for something experimental (in my personal opinion).

Another reason (may be too personal) is that, if I downloaded a project that I don't own and I want to build it with my preferred toolchain, for instance I want to build with Homebrew ruby instead of the default ruby provided by macOS (but the project's config doesn't contain flags for Homebrew ruby), it's just convenient to just set an environment variable instead of editing config file, because if I changed the project config file and next time I want to upgrade the repository, I still need to revert the change, pull the repository and change it again. It may fail the compilation or even have different behavior after the compilation but since those environment variables were set by me, it means I know the risks and I'll take them. (And actually I set it globally, in real life)

By the way, (not very seriously though) we supported CXX environment variable so why not support others as well?

To conclude I think the most important reason is to keep the behavior consistent as build systems that are widely used, and it will be also super convenient for some users to quickly edit the flags just for experimental uses (especially command-line nerds like me, yeah). About the potential risks it may bring, like compilation error or different behavior etc., since the system won't set those environment variables automatically (and it means they are set only by user) I think user would and should take the risks, and once user set the environment variable (even globally) they usually knows what they're doing (and hoping the build system uses those variables).

@onion108
Copy link
Contributor Author

P.S.: I do think your idea is great and should be used on most projects, but I think the support of environment flags still should be implemented as well for the consistency with other widely-used build systems and some advanced user may require it to work

@ken-matsui
Copy link
Member

Thanks for the clarification, I'll review your PR when I get a chance. Please also make the PR title clearer.

Copy link
Member

@ken-matsui ken-matsui left a comment

Choose a reason for hiding this comment

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

Thank you for working on this. This is a good start; I left some comments. Also, please address the clang-tidy error.

src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
Cleaned up code and did some change according to the reviewer's comment,
also fix some bugs in edge cases
@onion108 onion108 changed the title Add support for CXXFLAGS and LDFLAGS Add support for CXXFLAGS and LDFLAGS for consistency with other build system Nov 2, 2024
Copy link
Member

@ken-matsui ken-matsui left a comment

Choose a reason for hiding this comment

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

Overall, this looks great. Thanks for your work. Let's tweak more details.

src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
Renamed `ignoreXXX` variables to more meaningful and self-explanatory one.

Changed code structure of `parseEnvFlags` to make it more compact.

Made test cases more general.
@onion108
Copy link
Contributor Author

onion108 commented Nov 4, 2024

All review comments are resolved in the latest commit.

By the way I enabled workflow on the fork repo so I could run poac tidy and see the results to prevent me from making dumb mistakes. For some strange reasons (that I don't know) poac tidy's output in the actions is quite different from my machine (clang-tidy 19). My tidy seems telling me about almost nothing about my mistakes (like making const when possible), but always complains about circular header file dependency in Git2 library downloaded via Homebrew (which doesn't exists in the workflow output).

Another thing about tidy is after I committed my latest change, poac tidy (on the Github Workflow, hence the correct one) told me that in my test the code

assertEq(argsEscapeMixed.size(), 5);
                                ~^~

is a magic constant and tell me to consider replacing it with a named constant (but tbh we really need a named constant like ARGS_ESCAPE_MIXED_SIZE_EXPECTED just to check if the size is correct ...? I'm not quite sure about that but definitely it will not pass the clang-tidy action if I don't do so).

After digging deeper in the project I found a file Algos.cc that I think may be better for parseEnvFlags and its tests (and perhaps getEnvFlags as well?) to be placed in. I may consider doing in the next commit (after checking the code itself is fine and no dumb mistakes or naming mistakes or such), but I'm not quite sure if I'm going to move both parseEnvFlags and getEnvFlags, making parseEnvFlags internal to Algos.cc or just move parseEnvFlags to that file (and declare it in the header file as well).

@ken-matsui
Copy link
Member

All review comments are resolved in the latest commit.

Thanks!

By the way I enabled workflow on the fork repo so I could run poac tidy and see the results to prevent me from making dumb mistakes. For some strange reasons (that I don't know) poac tidy's output in the actions is quite different from my machine (clang-tidy 19). My tidy seems telling me about almost nothing about my mistakes (like making const when possible), but always complains about circular header file dependency in Git2 library downloaded via Homebrew (which doesn't exists in the workflow output).

This problem was due to the previous poac tidy implementation, and I fixed it. Currently, the fix is available only on the main. GitHub Actions one is always using the source code from the commit that triggered the action, which always contains my fix anyway, so that's why we see differences.

Another thing about tidy is after I committed my latest change, poac tidy (on the Github Workflow, hence the correct one) told me that in my test the code

assertEq(argsEscapeMixed.size(), 5);
                                ~^~

is a magic constant and tell me to consider replacing it with a named constant (but tbh we really need a named constant like ARGS_ESCAPE_MIXED_SIZE_EXPECTED just to check if the size is correct ...? I'm not quite sure about that but definitely it will not pass the clang-tidy action if I don't do so).

Using a named constant here makes no sense, so let's use NOLINT (or NOLINTNEXTLINE depending on the code elegance). See here for more info.

After digging deeper in the project I found a file Algos.cc that I think may be better for parseEnvFlags and its tests (and perhaps getEnvFlags as well?) to be placed in. I may consider doing in the next commit (after checking the code itself is fine and no dumb mistakes or naming mistakes or such), but I'm not quite sure if I'm going to move both parseEnvFlags and getEnvFlags, making parseEnvFlags internal to Algos.cc or just move parseEnvFlags to that file (and declare it in the header file as well).

Currently, they are used only by BuildConfig.cc so it makes sense to put them here. There's no compelling reason to make them project public APIs. Thanks for the suggestion anyway :)

Suppress `clang-tidy` errors (`cppcoreguidelines-avoid-magic-numbers`,`readability-magic-numbers`) in `testParseEnvFlags` on the statements that checks for the size, because these errors don't make sense in this situation.
@onion108
Copy link
Contributor Author

onion108 commented Nov 4, 2024

Thanks for your reply. Added NOLINTNEXTLINE for *-magic-numbers before size checks and checked Github Actions. All the tests on the Github Actions passed now.
スクリーンショット 2024-11-04 10 51 30

@ken-matsui
Copy link
Member

Thanks, will have a look when I get the time.

Copy link
Member

@ken-matsui ken-matsui 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. After tweaking small things, ok for main. Thanks.

src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
src/BuildConfig.cc Outdated Show resolved Hide resolved
@ken-matsui ken-matsui changed the title Add support for CXXFLAGS and LDFLAGS for consistency with other build system Support reflecting CXXFLAGS and LDFLAGS environment variables Nov 6, 2024
Fixed typo in comments and added test cases for double backslashes.
Copy link
Member

@ken-matsui ken-matsui left a comment

Choose a reason for hiding this comment

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

Thanks for your time to work on this!

@ken-matsui ken-matsui merged commit bc3bbbc into poac-dev:main Nov 6, 2024
16 checks passed
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.

2 participants