Allowing CMake projects to include this library as dependency #73

Closed
adi-g15 wants to merge 2 commits from master into master
adi-g15 commented 2021-08-28 04:35:32 -05:00 (Migrated from github.com)

Currently, if we add it as submodule, or with CPM (which I did), we can't include the <cppcodec/...> headers.

It fails with this error, even after including cppcodec as a library dependency:

fatal error: cppcodec/base64_rfc4648.hpp: No such file or directory

2021-08-28_15-00

@quiga has already fixed it (https://github.com/quiga/cppcodec).

github.com/quiga/cppcodec@849ccb04a6/CMakeLists.txt (L70-L73)

I used his fork as an upstream and it worked, there are changes mainly in CMakeLists you can see the diff. So, I created this PR so that the fix is merged with upstream :D

Currently, if we add it as submodule, or with [CPM](https://github.com/cpm-cmake/CPM.cmake) (which I did), we can't include the `<cppcodec/...>` headers. It fails with this error, even after including cppcodec as a library dependency: **fatal error: cppcodec/base64_rfc4648.hpp: No such file or directory** ![2021-08-28_15-00](https://user-images.githubusercontent.com/37269665/131213478-f644a221-fd1a-42fc-9dbc-1798548efff9.png) @quiga has already fixed it (https://github.com/quiga/cppcodec). https://github.com/quiga/cppcodec/blob/849ccb04a6f75aa300f6d6d6c8d163aca5e1e936/CMakeLists.txt#L70-L73 I used his fork as an upstream and it worked, there are changes mainly in CMakeLists you can see the diff. So, I created this PR so that the fix is merged with upstream :D
adi-g15 commented 2021-08-28 04:56:56 -05:00 (Migrated from github.com)

I have created a test case for this: https://github.com/adi-g15/tmp-pr-fix

main : Using current repo
quiga-fix: Using the modified one

Sorry if I have got some error in the CMakeLists

github.com/adi-g15/tmp-pr-fix@ad7a33e0aa/CMakeLists.txt (L1-L6)

I have created a test case for this: https://github.com/adi-g15/tmp-pr-fix [`main`](https://github.com/adi-g15/tmp-pr-fix/tree/main) : Using current repo [`quiga-fix`](https://github.com/adi-g15/tmp-pr-fix/tree/quiga-fix): Using the modified one Sorry if I have got some error in the CMakeLists https://github.com/adi-g15/tmp-pr-fix/blob/ad7a33e0aac96372ee05acfe85fd721daa5c5ab6/CMakeLists.txt#L1-L6
jpetso commented 2021-08-28 11:28:51 -05:00 (Migrated from github.com)

Thanks! I haven't looked at this in a long time and recently got some "more modern" CMake exposure at work, so overall I'd like to start with saying thanks and I know that this stuff needs a lot of work. This PR is a good start, line comments to follow.

Thanks! I haven't looked at this in a long time and recently got some "more modern" CMake exposure at work, so overall I'd like to start with saying thanks and I know that this stuff needs a lot of work. This PR is a good start, line comments to follow.
jpetso (Migrated from github.com) requested changes 2021-08-28 11:40:46 -05:00
@ -36,0 +24,4 @@
# fine for anything except ancient MSVC versions.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
jpetso (Migrated from github.com) commented 2021-08-28 11:30:27 -05:00

These should probably be CACHE variables, right? Then there's a way for the user to set it in the configuration, including via ccmake.

These should probably be `CACHE` variables, right? Then there's a way for the user to set it in the configuration, including via `ccmake`.
@ -99,3 +84,4 @@
if (BUILD_EXAMPLES)
add_subdirectory(example)
endif()
jpetso (Migrated from github.com) commented 2021-08-28 11:40:17 -05:00

Would this also work if declared as INTERFACE library? I think that would be ideal given that this is what cppcodec actually is.

Would this also work if declared as `INTERFACE` library? I think that would be ideal given that this is what cppcodec actually is.
@ -118,3 +95,1 @@
FILES ${CMAKE_CURRENT_BINARY_DIR}/cppcodec-1.pc
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig
)
if (NOT WIN32)
jpetso (Migrated from github.com) commented 2021-08-28 11:32:54 -05:00

Style nitpick: a single target_include_directories() can contain both PUBLIC and PRIVATE sections, so I think it's cleaner to move the PUBLIC into the next line (so a hypothetical PRIVATE wouldn't look out of place, not that it matters much here).

Style nitpick: a single `target_include_directories()` can contain both `PUBLIC` and `PRIVATE` sections, so I think it's cleaner to move the `PUBLIC` into the next line (so a hypothetical `PRIVATE` wouldn't look out of place, not that it matters much here).
adi-g15 (Migrated from github.com) reviewed 2021-08-28 13:22:44 -05:00
@ -36,0 +24,4 @@
# fine for anything except ancient MSVC versions.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
adi-g15 (Migrated from github.com) commented 2021-08-28 13:22:44 -05:00

Yes, that should be a CACHE variable, didn't notice earlier... maybe with option(BUILD_TESTS "Build bundled tests" OFF) (https://stackoverflow.com/q/36358217/12339402)

Yes, that should be a CACHE variable, didn't notice earlier... maybe with `option(BUILD_TESTS "Build bundled tests" OFF)` (https://stackoverflow.com/q/36358217/12339402)
adi-g15 (Migrated from github.com) reviewed 2021-08-28 13:22:46 -05:00
@ -118,3 +95,1 @@
FILES ${CMAKE_CURRENT_BINARY_DIR}/cppcodec-1.pc
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig
)
if (NOT WIN32)
adi-g15 (Migrated from github.com) commented 2021-08-28 13:22:46 -05:00

Yes (I don't know much, I simply separate out each target_include_directories in my projects, I checked the docs to make sure what are the optional args 😅)

Yes (I don't know much, I simply separate out each target_include_directories in my projects, I checked the docs to make sure what are the optional args 😅)
adi-g15 (Migrated from github.com) reviewed 2021-08-28 13:25:41 -05:00
@ -99,3 +84,4 @@
if (BUILD_EXAMPLES)
add_subdirectory(example)
endif()
adi-g15 (Migrated from github.com) commented 2021-08-28 13:25:41 -05:00

Sorry, I don't know this one too, it didn't work if the OBJECT is just replaced with INTERFACE as it doesn't take source arguments. I use INTERFACE only for header only libs of mine, this one requires .cpp files too, so i don't know how that works for INTERFACE.

Sorry, I don't know this one too, it didn't work if the OBJECT is just replaced with INTERFACE as it doesn't take source arguments. I use INTERFACE only for header only libs of mine, this one requires .cpp files too, so i don't know how that works for INTERFACE.
adi-g15 (Migrated from github.com) reviewed 2021-08-28 13:27:21 -05:00
@ -36,0 +24,4 @@
# fine for anything except ancient MSVC versions.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
adi-g15 (Migrated from github.com) commented 2021-08-28 13:27:21 -05:00

And, btw, these changes were made by @quiga, so the credit goes to him. I just found it made it easier to include so created this PR upstream.

And, btw, these changes were made by @quiga, so the credit goes to him. I just found it made it easier to include so created this PR upstream.
jpetso (Migrated from github.com) reviewed 2021-08-28 14:07:53 -05:00
@ -36,0 +24,4 @@
# fine for anything except ancient MSVC versions.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
jpetso (Migrated from github.com) commented 2021-08-28 14:07:53 -05:00

Sorry yeah, I meant option() (for ON/OFF variables).

Sorry yeah, I meant `option()` (for ON/OFF variables).
adi-g15 (Migrated from github.com) reviewed 2021-08-29 14:24:15 -05:00
@ -36,0 +24,4 @@
# fine for anything except ancient MSVC versions.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
adi-g15 (Migrated from github.com) commented 2021-08-29 14:24:15 -05:00

Also, just to say, I can't edit the code, I created the PR from quiga's branch (so can't edit that code), maybe you being a maintainer can make changes in the PR's changes :D

Also, just to say, I can't edit the code, I created the PR from quiga's branch (so can't edit that code), maybe you being a maintainer can make changes in the PR's changes :D
kingsamchen (Migrated from github.com) reviewed 2022-08-21 09:37:42 -05:00
@ -99,3 +84,4 @@
if (BUILD_EXAMPLES)
add_subdirectory(example)
endif()
kingsamchen (Migrated from github.com) commented 2022-08-21 09:37:42 -05:00

If we can restrict CMake versions >= 3.12, we can leave OBJECT here as-is, because since version 3.12, object libraries can be linked to with target_link_libraries(); and with added target_include_directories(), this library can be consumed simply via FetchContent.

Otherwise, you may need to specify INTERFACE in add_library() and use target_sources to add source files.
and target_sources requires CMake 3.1, you also must use INTERFACE in target_include_directories()

If we can restrict CMake versions >= 3.12, we can leave `OBJECT` here as-is, because [since version 3.12, object libraries can be linked to with `target_link_libraries()`](https://cmake.org/cmake/help/latest/command/add_library.html#object-libraries); and with added `target_include_directories()`, this library can be consumed simply via FetchContent. Otherwise, you may need to specify `INTERFACE` in `add_library()` and use [target_sources](https://cmake.org/cmake/help/latest/command/target_sources.html#command:target_sources) to add source files. and `target_sources` requires CMake 3.1, you also must use `INTERFACE` in `target_include_directories()`
kingsamchen (Migrated from github.com) reviewed 2022-08-21 09:42:57 -05:00
@ -99,3 +84,4 @@
if (BUILD_EXAMPLES)
add_subdirectory(example)
endif()
kingsamchen (Migrated from github.com) commented 2022-08-21 09:42:57 -05:00

Fyi, maybe we can use nlohmann/json's CMakeLists.txt as a reference impl

Fyi, maybe we can use nlohmann/json's [CMakeLists.txt](https://github.com/nlohmann/json/blob/develop/CMakeLists.txt) as a reference impl
jpetso commented 2022-08-21 12:24:49 -05:00 (Migrated from github.com)

Hi everyone, I took everyone's contributions to heart and finally uploaded PR #76, hopefully containing the best version of what was started on this one here. If you have time, please have a look and let me know if that works for you (or any other suggestions). Closing this PR in favor of the other one.

Hi everyone, I took everyone's contributions to heart and finally uploaded PR #76, hopefully containing the best version of what was started on this one here. If you have time, please have a look and let me know if that works for you (or any other suggestions). Closing this PR in favor of the other one.

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-tplgy/cppcodec!73
No description provided.