Skip to content

Commit 435d304

Browse files
authored
Merge pull request #74 from cpp-best-practices/interface
2 parents edbe771 + 63121ed commit 435d304

3 files changed

Lines changed: 22 additions & 16 deletions

File tree

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ An executable:
6161

6262
```cmake
6363
add_executable(myprogram main.cpp)
64-
target_link_libraries(myprogram PRIVATE project_options project_warnings) # connect project_options to myprogram
64+
target_link_libraries(myprogram PRIVATE project_options project_warnings) # link project_options/warnings
6565
6666
# Find dependencies:
6767
set(DEPENDENCIES_CONFIGURED fmt Eigen3)
@@ -86,7 +86,7 @@ A header-only library:
8686

8787
```cmake
8888
add_library(my_header_only_lib INTERFACE)
89-
target_link_libraries(my_header_only_lib INTERFACE project_options project_warnings) # connect project_options to my_header_only_lib
89+
target_link_libraries(my_header_only_lib INTERFACE project_options project_warnings) # link project_options/warnings
9090
9191
# Includes
9292
set(INCLUDE_DIR "include") # must be relative paths
@@ -110,17 +110,17 @@ target_link_system_libraries(
110110
111111
# Package the project
112112
package_project(
113-
TARGETS my_header_only_lib
114-
PUBLIC_DEPENDENCIES_CONFIGURED ${DEPENDENCIES_CONFIGURED}
115-
PUBLIC_INCLUDES ${INCLUDE_DIR}
113+
TARGETS my_header_only_lib project_options project_warnings
114+
INTERFACE_DEPENDENCIES_CONFIGURED ${DEPENDENCIES_CONFIGURED}
115+
INTERFACE_INCLUDES ${INCLUDE_DIR}
116116
)
117117
```
118118

119119
A library with separate header and source files
120120

121121
```cmake
122122
add_library(my_lib "./src/my_lib/lib.cpp")
123-
target_link_libraries(my_lib INTERFACE project_options project_warnings) # connect project_options to my_lib
123+
target_link_libraries(my_lib PRIVATE project_options project_warnings) # link project_options/warnings
124124
125125
# Includes
126126
set(INCLUDE_DIR "include") # must be relative paths
@@ -145,7 +145,7 @@ target_link_system_libraries(
145145
# Package the project
146146
package_project(
147147
TARGETS my_lib
148-
PUBLIC_INCLUDES ${INCLUDE_DIR}
148+
INTERFACE_INCLUDES ${INCLUDE_DIR}
149149
)
150150
```
151151

@@ -226,13 +226,13 @@ The following arguments specify the package:
226226

227227
- `TARGETS`: the targets you want to package. It is recursively found for the current folder if not specified
228228

229-
- `PUBLIC_INCLUDES`: a list of public/interface include directories or files.
229+
- `INTERFACE_INCLUDES` or `PUBLIC_INCLUDES`: a list of interface/public include directories or files.
230230

231231
<sub>NOTE: The given include directories are directly installed to the install destination. To have an `include` folder in the install destination with the content of your include directory, name your directory `include`.</sub>
232232

233-
- `PUBLIC_DEPENDENCIES_CONFIGURED`: the names of the INTERFACE/PUBLIC dependencies that are found using `CONFIG`.
233+
- `INTERFACE_DEPENDENCIES_CONFIGURED` or `PUBLIC_DEPENDENCIES_CONFIGURED`: the names of the interface/public dependencies that are found using `CONFIG`.
234234

235-
- `PUBLIC_DEPENDENCIES`: the INTERFACE/PUBLIC dependencies that are found by any means using `find_dependency`. The arguments must be specified within quotes (e.g. `"<dependency> 1.0.0 EXACT"` or `"<dependency> CONFIG"`).
235+
- `INTERFACE_DEPENDENCIES` or `PUBLIC_DEPENDENCIES`: the interface/public dependencies that will be found by any means using `find_dependency`. The arguments must be specified within quotes (e.g.`"<dependency> 1.0.0 EXACT"` or `"<dependency> CONFIG"`).
236236

237237
- `PRIVATE_DEPENDENCIES_CONFIGURED`: the names of the PRIVATE dependencies found using `CONFIG`. Only included when `BUILD_SHARED_LIBS` is `OFF`.
238238

src/PackageProject.cmake

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ function(package_project)
2121
# recursively found for the current folder if not specified
2222
TARGETS
2323
# a list of public/interface include directories or files
24+
INTERFACE_INCLUDES
2425
PUBLIC_INCLUDES
2526
# the names of the INTERFACE/PUBLIC dependencies that are found using `CONFIG`
27+
INTERFACE_DEPENDENCIES_CONFIGURED
2628
PUBLIC_DEPENDENCIES_CONFIGURED
2729
# the INTERFACE/PUBLIC dependencies that are found by any means using `find_dependency`.
2830
# the arguments must be specified within double quotes (e.g. "<dependency> 1.0.0 EXACT" or "<dependency> CONFIG").
31+
INTERFACE_DEPENDENCIES
2932
PUBLIC_DEPENDENCIES
3033
# the names of the PRIVATE dependencies that are found using `CONFIG`. Only included when BUILD_SHARED_LIBS is OFF.
3134
PRIVATE_DEPENDENCIES_CONFIGURED
@@ -81,6 +84,7 @@ function(package_project)
8184
set(_PackageProject_INSTALL_DESTINATION "${_PackageProject_CONFIG_INSTALL_DESTINATION}")
8285

8386
# Installation of the public/interface includes
87+
set(_PackageProject_PUBLIC_INCLUDES "${_PackageProject_PUBLIC_INCLUDES}" "${_PackageProject_INTERFACE_INCLUDES}")
8488
if(NOT
8589
"${_PackageProject_PUBLIC_INCLUDES}"
8690
STREQUAL
@@ -101,6 +105,8 @@ function(package_project)
101105
endif()
102106

103107
# Append the configured public dependencies
108+
set(_PackageProject_PUBLIC_DEPENDENCIES_CONFIGURED "${_PackageProject_PUBLIC_DEPENDENCIES_CONFIGURED}"
109+
"${_PackageProject_INTERFACE_DEPENDENCIES_CONFIGURED}")
104110
if(NOT
105111
"${_PackageProject_PUBLIC_DEPENDENCIES_CONFIGURED}"
106112
STREQUAL
@@ -112,7 +118,7 @@ function(package_project)
112118
endif()
113119
list(APPEND _PackageProject_PUBLIC_DEPENDENCIES ${_PUBLIC_DEPENDENCIES_CONFIG})
114120
# ycm arg
115-
set(_PackageProject_DEPENDENCIES ${_PackageProject_PUBLIC_DEPENDENCIES})
121+
set(_PackageProject_DEPENDENCIES ${_PackageProject_PUBLIC_DEPENDENCIES} ${_PackageProject_INTERFACE_DEPENDENCIES})
116122

117123
# Append the configured private dependencies
118124
if(NOT

test/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ package_project(
9898
# lib
9999
# project_warnings
100100
# project_options
101-
PUBLIC_DEPENDENCIES_CONFIGURED
101+
INTERFACE_DEPENDENCIES_CONFIGURED
102102
${DEPENDENCIES_CONFIGURED}
103-
PUBLIC_INCLUDES
103+
INTERFACE_INCLUDES
104104
${INCLUDE_DIR})
105105

106106
# package separately (for testing)
@@ -109,17 +109,17 @@ package_project(
109109
myproj_header_only_lib
110110
TARGETS
111111
lib
112-
PUBLIC_DEPENDENCIES_CONFIGURED
112+
INTERFACE_DEPENDENCIES_CONFIGURED
113113
${DEPENDENCIES_CONFIGURED}
114-
PUBLIC_INCLUDES
114+
INTERFACE_INCLUDES
115115
${INCLUDE_DIR})
116116

117117
package_project(
118118
NAME
119119
myproj_lib
120120
TARGETS
121121
lib2
122-
PUBLIC_INCLUDES
122+
INTERFACE_INCLUDES
123123
${INCLUDE_DIR2})
124124

125125
package_project(

0 commit comments

Comments
 (0)