-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add C++ modules support #2291
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
Open
mikomikotaishi
wants to merge
8
commits into
yhirose:master
Choose a base branch
from
mikomikotaishi:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+540
−0
Open
Add C++ modules support #2291
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
41eb63f
Add C++ modules support
mikomikotaishi 8bda27b
Add module examples
mikomikotaishi 9e4ca69
Missing semicolon
mikomikotaishi 6eeb13a
Update GitHub Actions script and create a modules updating script
mikomikotaishi 36b1c16
Name the unused param
mikomikotaishi 97fa023
Merge branch 'yhirose:master' into master
mikomikotaishi 6655639
Merge branch 'master' into master
mikomikotaishi 66a0864
Merge branch 'yhirose:master' into master
mikomikotaishi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| name: Update Module Exports | ||
|
|
||
| on: | ||
| pull_request: | ||
| paths: | ||
| - 'httplib.h' | ||
| push: | ||
| branches: | ||
| - master | ||
| - main | ||
| paths: | ||
| - 'httplib.h' | ||
|
|
||
| jobs: | ||
| update-exports: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 # Fetch all history for proper diff | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.11' | ||
|
|
||
| - name: Check for changes in httplib.h | ||
| id: check_changes | ||
| run: | | ||
| if git diff --name-only HEAD~1 HEAD | grep -q "httplib.h"; then | ||
| echo "changes=true" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "changes=false" >> $GITHUB_OUTPUT | ||
| fi | ||
|
|
||
| - name: Update module exports | ||
| if: steps.check_changes.outputs.changes == 'true' | ||
| run: | | ||
| python3 update_modules.py | ||
|
|
||
| - name: Check if module file was modified | ||
| if: steps.check_changes.outputs.changes == 'true' | ||
| id: check_module_changes | ||
| run: | | ||
| if git diff --quiet modules/httplib.cppm; then | ||
| echo "modified=false" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "modified=true" >> $GITHUB_OUTPUT | ||
| fi | ||
|
|
||
| - name: Commit changes | ||
| if: steps.check_module_changes.outputs.modified == 'true' | ||
| run: | | ||
| git config user.name "github-actions[bot]" | ||
| git config user.email "github-actions[bot]@users.noreply.github.com" | ||
| git add modules/httplib.cppm | ||
| git commit -m "chore: update module exports for httplib.h changes" | ||
|
|
||
| - name: Push changes (for push events) | ||
| if: steps.check_module_changes.outputs.modified == 'true' && github.event_name == 'push' | ||
| run: | | ||
| git push | ||
|
|
||
| - name: Push changes (for pull requests) | ||
| if: steps.check_module_changes.outputs.modified == 'true' && github.event_name == 'pull_request' | ||
| run: | | ||
| git push origin HEAD:${{ github.head_ref }} | ||
|
|
||
| - name: Add comment to PR | ||
| if: steps.check_module_changes.outputs.modified == 'true' && github.event_name == 'pull_request' | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| github.rest.issues.createComment({ | ||
| issue_number: context.issue.number, | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| body: '✅ Module exports have been automatically updated based on changes to `httplib.h`.' | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| add_library(httplib_module) | ||
|
|
||
| target_sources(httplib_module | ||
| PUBLIC | ||
| FILE_SET CXX_MODULES FILES | ||
| httplib.cppm | ||
| ) | ||
|
|
||
| target_compile_features(httplib_module PUBLIC cxx_std_20) | ||
|
|
||
| target_include_directories(httplib_module PUBLIC | ||
| $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> | ||
| $<INSTALL_INTERFACE:include> | ||
| ) | ||
|
|
||
| add_library(httplib::module ALIAS httplib_module) | ||
|
|
||
| # Installation | ||
| install(TARGETS httplib_module | ||
| EXPORT ${PROJECT_NAME}Targets | ||
| LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
| ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
| RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} | ||
| FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/httplib/modules | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // | ||
| // httplib.cppm | ||
| // | ||
| // Copyright (c) 2025 Yuji Hirose. All rights reserved. | ||
| // MIT License | ||
| // | ||
|
|
||
| module; | ||
|
|
||
| #include "../httplib.h" | ||
|
|
||
| export module httplib; | ||
|
|
||
| export namespace httplib { | ||
| using httplib::SSLVerifierResponse; | ||
| using httplib::StatusCode; | ||
| using httplib::Headers; | ||
| using httplib::Params; | ||
| using httplib::Match; | ||
| using httplib::DownloadProgress; | ||
| using httplib::UploadProgress; | ||
| using httplib::Response; | ||
| using httplib::ResponseHandler; | ||
| using httplib::FormData; | ||
| using httplib::FormField; | ||
| using httplib::FormFields; | ||
| using httplib::FormFiles; | ||
| using httplib::MultipartFormData; | ||
| using httplib::UploadFormData; | ||
| using httplib::UploadFormDataItems; | ||
| using httplib::DataSink; | ||
| using httplib::ContentProvider; | ||
| using httplib::ContentProviderWithoutLength; | ||
| using httplib::ContentProviderResourceReleaser; | ||
| using httplib::FormDataProvider; | ||
| using httplib::FormDataProviderItems; | ||
| using httplib::ContentReceiverWithProgress; | ||
| using httplib::ContentReceiver; | ||
| using httplib::FormDataHeader; | ||
| using httplib::ContentReader; | ||
| using httplib::Range; | ||
| using httplib::Ranges; | ||
| using httplib::Request; | ||
| using httplib::Response; | ||
| using httplib::Error; | ||
| using httplib::to_string; | ||
| using httplib::operator<<; | ||
| using httplib::Stream; | ||
| using httplib::TaskQueue; | ||
| using httplib::ThreadPool; | ||
| using httplib::Logger; | ||
| using httplib::ErrorLogger; | ||
| using httplib::SocketOptions; | ||
| using httplib::default_socket_options; | ||
| using httplib::status_message; | ||
| using httplib::get_bearer_token_auth; | ||
| using httplib::Server; | ||
| using httplib::Result; | ||
| using httplib::ClientConnection; | ||
| using httplib::ClientImpl; | ||
| using httplib::Client; | ||
|
|
||
| #ifdef CPPHTTPLIB_OPENSSL_SUPPORT | ||
| using httplib::SSLServer; | ||
| using httplib::SSLClient; | ||
| #endif | ||
|
|
||
| using httplib::hosted_at; | ||
| using httplib::encode_uri_component; | ||
| using httplib::encode_uri; | ||
| using httplib::decode_uri_component; | ||
| using httplib::decode_uri; | ||
| using httplib::encode_path_component; | ||
| using httplib::decode_path_component; | ||
| using httplib::encode_query_component; | ||
| using httplib::decode_query_component; | ||
| using httplib::append_query_params; | ||
| using httplib::make_range_header; | ||
| using httplib::make_basic_authentication_header; | ||
|
|
||
| using httplib::get_client_ip; | ||
|
|
||
| namespace stream { | ||
| using httplib::stream::Result; | ||
| using httplib::stream::Get; | ||
| using httplib::stream::Post; | ||
| using httplib::stream::Put; | ||
| using httplib::stream::Patch; | ||
| using httplib::stream::Delete; | ||
| using httplib::stream::Head; | ||
| using httplib::stream::Options; | ||
| } | ||
| } |
yhirose marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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 adding this GitHub Action. But after further consideration, I now think this process should be done in CMakeLists.txt. You could generate
modules/httplib.cppmunder this linecpp-httplib/CMakeLists.txt
Line 216 in 87c2b4e
We actually do the similar to generate
httplib.handhttplib.ccwithsplit.pyand put them in a distribution package. By doing this, we no longer need to keepmodules/httplib.cppmin this repository. It will be created on the fly only when necessary.@sum01 @jimmy-park Is my comment above correct?
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.
I think it would be difficult to parse an especially large file without syntax analysis or parsing library. I don't have a lot of experience with this sort of thing, and because the header contains things like method definitions outside of the class it seems to be a very complex task. Then including a C++ parser would require a dependency even for a source generation script which would be additional bloat.
I guess this could be done by attaching to the header file that
split.pygenerates, but I haven't actually seen what that looks like.As for dynamically generating the module, this was raised before on the Dear ImGui library which currently does something like this. I think this is a bad choice for module API (having a static file is obviously best for consumers to be able to just read it from a distance without additional interaction, i.e. a "what-you-see-is-what-you-get" sort of API), but ultimately as you are in charge I'll look into this if you prefer it.
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.
I'm looking more into this and I think if we want to allow CMake to compile the module if it's generated into an "out" directory, we have to force the out directory to be named "out" so that it can be written into the CMakeLists.txt script
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.
Anyway @yhirose do you have any opinion on the output directory having a hard-coded name?
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.
@sum01 @jimmy-park @Tachi107 could you please answer this question?