Skip to content

Commit d72ad4d

Browse files
committed
CMakeLists.txt: Add support for pkg-config
Create and install pkg-config (.pc) file for dependency detection.
1 parent 32043f4 commit d72ad4d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,27 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${PROJECT_NAME}Config.cmake
133133
install(EXPORT ${PROJECT_NAME}-targets
134134
FILE ${PROJECT_NAME}Targets.cmake
135135
DESTINATION ${PYBIND11_JSON_CMAKECONFIG_INSTALL_DIR})
136+
137+
# pkg-config support
138+
if(NOT prefix_for_pc_file)
139+
if(IS_ABSOLUTE "${CMAKE_INSTALL_DATAROOTDIR}")
140+
set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}")
141+
else()
142+
set(pc_datarootdir "${CMAKE_INSTALL_DATAROOTDIR}")
143+
if(CMAKE_VERSION VERSION_LESS 3.20)
144+
set(prefix_for_pc_file "\${pcfiledir}/..")
145+
while(pc_datarootdir)
146+
get_filename_component(pc_datarootdir "${pc_datarootdir}" DIRECTORY)
147+
string(APPEND prefix_for_pc_file "/..")
148+
endwhile()
149+
else()
150+
cmake_path(RELATIVE_PATH CMAKE_INSTALL_PREFIX BASE_DIRECTORY CMAKE_INSTALL_DATAROOTDIR
151+
OUTPUT_VARIABLE prefix_for_pc_file)
152+
endif()
153+
endif()
154+
endif()
155+
set(includedir_for_pc_file "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
156+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/pybind11_json.pc.in"
157+
"${CMAKE_CURRENT_BINARY_DIR}/pybind11_json.pc" @ONLY)
158+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pybind11_json.pc"
159+
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig/")

pybind11_json.pc.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
prefix=@prefix_for_pc_file@
2+
includedir=@includedir_for_pc_file@
3+
4+
Name: @PROJECT_NAME@
5+
Description: Using nlohmann::json with pybind11
6+
Version: @PROJECT_VERSION@
7+
Cflags: -I${includedir}

0 commit comments

Comments
 (0)