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

Refactor includes #924

Closed
wants to merge 17 commits into from
Closed

Refactor includes #924

wants to merge 17 commits into from

Conversation

whb07
Copy link
Contributor

@whb07 whb07 commented Apr 24, 2021

Two primary changes based on the way include statements are handled for the project.

  1. Headers defined by the secp256k1 library are now imported via #include "secp256k1.h".
  2. Changes to the files that got included are based on the "include what you use" tool.

For the first point to happen, the Makefile.am got tweaked and declared the AM_CPPFLAGS variable and included as a dependency for the different targets on the file. I do not have extensive autoconf/automake experience, so I just went with this variable and while it "works", I am not clear if it is the best way to do it.

At any rate, within say secp256k1.c, one can include the header in the cleaner syntax without the "include/" prefix. This also allows other external libraries and build configs to work by defining the lib's include path as such.

The latter point makes use of the aggregate tooling built on Clang's different tools called "include what you use". Essentially it shows what needs to be declared/removed to include what you use.

@sipa
Copy link
Contributor

sipa commented Apr 24, 2021

At least the implementation choice-specific includes are incorrect (e.g. you should include "field.h" instead of "field_5x52.h", and "scalar.h" instead of "scalar_4x64.h").

@whb07
Copy link
Contributor Author

whb07 commented Apr 24, 2021

At least the implementation choice-specific includes are incorrect (e.g. you should include "field.h" instead of "field_5x52.h", and "scalar.h" instead of "scalar_4x64.h").

Yes I am starting to see that! Well partially it appears, a tricky devil this project uses some indirection based on implementations but that then get included via that indirection. I am going to be manually editing those for proper declarations.

Outside of that @sipa what do you make of the Makefile.am change? Like i mentioned before I am no expert in that tooling so I have no strong opinions and need proper guidance. One thing to note is that the "SECP_INCLUDES" variable appears always empty, is that on purpose, or some dead code? Or does that ever have a value in other build systems?

@real-or-random
Copy link
Contributor

@whb07

Is the purpose of this PR simply to make the code cleaner or is this solving any particular issue that you have when using the library?

@whb07
Copy link
Contributor Author

whb07 commented Apr 25, 2021

@whb07

Is the purpose of this PR simply to make the code cleaner or is this solving any particular issue that you have when using the library?

Both! Which is leading me to believe that it might be best to split the two main changes into individual PRs. Building separately from source using a different build system has the issues of the different lib's inclusion.

The source files in contrib refer to the header here as :

#include <string.h>
#include <secp256k1.h>

#include "lax_der_parsing.h"

Which is fine and dandy but in the files within src/ they are referred as:

#include "include/secp256k1.h"
...

Then when building out of source, you end up with the error of "include/secp256k1.h" not found. So this cleans up the syntax and styling, whilst at the same time clearing up the differing build issues.

What do you think?

real-or-random added a commit that referenced this pull request May 5, 2021
3c90bdd change local lib headers to be relative for those pointing at "include/" dir (William Bright)

Pull request description:

  Referencing #924 , this PR splits the two issues brought on to a smaller to digest change. What this does is removes the prefix "include/" when referencing the local library header files.

  e.g:
  from:
  ```cpp
  #include "include/secp256k1.h"
  ```
  to:
  ```cpp
  #include "secp256k1.h"
  ```

  Rationale besides styling and consistency across other files in the repo, it makes it easier for outside builds to properly locate the headers.

  A live example seen here when attempting to build this library within bitcoin repo:
  ```sh
  [ 14%] Building CXX object leveldb/CMakeFiles/leveldb.dir/util/bloom.cc.o
  /tmp/bitcoin/src/secp256k1/src/secp256k1.c:7:10: fatal error: include/secp256k1.h: No such file or directory
      7 | #include "include/secp256k1.h"
        |          ^~~~~~~~~~~~~~~~~~~~~
  compilation terminated.
  make[2]: *** [secp256k1/CMakeFiles/Secp256k1.dir/build.make:76: secp256k1/CMakeFiles/Secp256k1.dir/src/secp256k1.c.o] Error 1
  make[1]: *** [CMakeFiles/Makefile2:537: secp256k1/CMakeFiles/Secp256k1.dir/all] Error 2
  make[1]: *** Waiting for unfinished jobs....

  ```

ACKs for top commit:
  gmaxwell:
    ACK 3c90bdd
  real-or-random:
    ACK 3c90bdd code looks good and even the tests compile fine now without `-I` args

Tree-SHA512: 94d212718c6f4901f1c310aff504b7afedda91268143ffe1b45e9883cd517c0599e40ac798a51b54d66cd31646fe8cb1a489f1776612cfb5963654f4a1cee757
@real-or-random
Copy link
Contributor

So now that #925 has been merged, the point of this PR would be to tidy up the remaining includes.

That's not strictly necessary but it's a good idea from time to time. @whb07 Do you want to tidy and squash the changes here?

Unfortunately we cannot fully automate the include stuff at this point, due to our special implementation-specific includes. Otherwise we could add a check like to the CI. For example,

make -k CC='include-what-you-use -Xiwyu --keep=src/assumptions.h -Xiwyu --no_comments

comes pretty close to what we need. Maybe it's possible to make it work with the --mapping_file argument, not sure.

the Makefile.am got tweaked and declared the AM_CPPFLAGS variable and included as a dependency for the different targets on the file. I do not have extensive autoconf/automake experience, so I just went with this variable and while it "works", I am not clear if it is the best way to do it.

I think this change should be removed from the PR then, since the point of #925 was exactly the opposite, namely to avoid more -I arguments.

real-or-random added a commit that referenced this pull request Apr 20, 2023
…de/secp256k1.h`

8e142ca Move `SECP256K1_INLINE` macro definition out from `include/secp256k1.h` (Hennadii Stepanov)
7744589 Remove `SECP256K1_INLINE` usage from examples (Hennadii Stepanov)

Pull request description:

  From [IRC](https://gnusha.org/secp256k1/2023-01-31.log):
  > 06:29 \< hebasto\> What are reasons to define the `SECP256K1_INLINE` macro in user's `include/secp256k1.h` header, while it is used internally only?
  > 06:32 \< hebasto\> I mean, any other (or a new dedicated) header in `src` looks more appropriate, no?
  > 06:35 \< sipa\> I think it may just predate any "utility" internal headers.
  > 06:42 \< sipa\> I think it makes sense to move it to util.h

  Pros:
  - it is a step in direction to better organized headers (in context of #924, #1039)

  Cons:
  - code duplication for `SECP256K1_GNUC_PREREQ` macro

ACKs for top commit:
  sipa:
    utACK 8e142ca
  real-or-random:
    utACK 8e142ca

Tree-SHA512: 180e0ba7c2ef242b765f20698b67d06c492b7b70866c21db27c18d8b2e85c3e11f86c6cb99ffa88bbd23891ce3ee8a24bc528f2c91167ec2fddc167463f78eac
@real-or-random
Copy link
Contributor

Closing this PR for now due to lack of activity, it can always be reopenend. (This is somewhat tracked in #1039 in a sense that any cleaning of includes should ideally solve #1039 or make progress towards it.)

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.

3 participants