-
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
base: master
Are you sure you want to change the base?
Add C++ modules support #2291
Changes from all commits
41eb63f
8bda27b
9e4ca69
6eeb13a
36b1c16
97fa023
6655639
66a0864
e81bc86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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`.' | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,6 +110,7 @@ option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system cer | |
| option(HTTPLIB_USE_NON_BLOCKING_GETADDRINFO "Enables the non-blocking alternatives for getaddrinfo." ON) | ||
| option(HTTPLIB_REQUIRE_ZSTD "Requires ZSTD to be found & linked, or fails build." OFF) | ||
| option(HTTPLIB_USE_ZSTD_IF_AVAILABLE "Uses ZSTD (if available) to enable zstd support." ON) | ||
| option(HTTPLIB_BUILD_MODULES "Build httplib modules (requires HTTPLIB_COMPILE to be ON)." OFF) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would this need to be an option? Does it add any other dependency? Cannot it be always be built, without an option?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to leave it as opt-in because C++ module support is compiler dependent. Most newer compiler versions offer it, but older ones do not. The build could break if we force it on older toolchains. |
||
| # Defaults to static library but respects standard BUILD_SHARED_LIBS if set | ||
| include(CMakeDependentOption) | ||
| cmake_dependent_option(HTTPLIB_SHARED "Build the library as a shared library instead of static. Has no effect if using header-only." | ||
|
|
@@ -367,3 +368,10 @@ if(HTTPLIB_TEST) | |
| include(CTest) | ||
| add_subdirectory(test) | ||
| endif() | ||
|
|
||
| if(HTTPLIB_BUILD_MODULES) | ||
| if(NOT HTTPLIB_COMPILE) | ||
| message(FATAL_ERROR "HTTPLIB_BUILD_MODULES requires HTTPLIB_COMPILE to be ON.") | ||
| endif() | ||
| add_subdirectory(modules) | ||
| endif() | ||
| 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 | ||
| ) | ||
|
Comment on lines
+3
to
+7
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This requires CMake 3.28, but this project only requires CMake 3.0.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C++ modules require a later version of CMake, so activating modules should require the user has the correct version. |
||
|
|
||
| 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 | ||
| ) | ||
| 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; | ||
| } | ||
| } |
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?