Skip to content

Commit eb97236

Browse files
committed
cmake: add pkg-config file
pkg-config allows using the library in build systems that are not cmake, by exporting the same information from the cmake -config files in a buildsystem-neutral format. Fixes microsoft#16
1 parent eb29d6b commit eb97236

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,22 @@ endif()
217217
# extra needed libraries
218218
if(WIN32)
219219
list(APPEND mi_libraries psapi shell32 user32 advapi32 bcrypt)
220+
set(pc_libraries "-lpsapi -lshell32 -luser32 -ladvapi32 -lbcrypt")
220221
else()
221222
find_library(MI_LIBPTHREAD pthread)
222223
if (MI_LIBPTHREAD)
223224
list(APPEND mi_libraries ${MI_LIBPTHREAD})
225+
set(pc_libraries "-pthread")
224226
endif()
225227
find_library(MI_LIBRT rt)
226228
if(MI_LIBRT)
227229
list(APPEND mi_libraries ${MI_LIBRT})
228-
endif()
230+
set(pc_libraries "${pc_libraries} -lrt")
231+
endif()
229232
find_library(MI_LIBATOMIC atomic)
230233
if (MI_LIBATOMIC OR MI_USE_LIBATOMIC)
231234
list(APPEND mi_libraries atomic)
235+
set(pc_libraries "${pc_libraries} -latomic")
232236
endif()
233237
endif()
234238

@@ -376,6 +380,15 @@ if (MI_BUILD_OBJECT)
376380
RENAME ${mi_basename}${CMAKE_C_OUTPUT_EXTENSION} )
377381
endif()
378382

383+
# pkg-config file support
384+
include("cmake/JoinPaths.cmake")
385+
join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
386+
join_paths(libdir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_LIBDIR}")
387+
388+
configure_file(mimalloc.pc.in mimalloc.pc @ONLY)
389+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/mimalloc.pc"
390+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig/")
391+
379392
# -----------------------------------------------------------------------------
380393
# API surface testing
381394
# -----------------------------------------------------------------------------

cmake/JoinPaths.cmake

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This module provides function for joining paths
2+
# known from most languages
3+
#
4+
# SPDX-License-Identifier: (MIT OR CC0-1.0)
5+
# Copyright 2020 Jan Tojnar
6+
# https://github.com/jtojnar/cmake-snips
7+
#
8+
# Modelled after Python’s os.path.join
9+
# https://docs.python.org/3.7/library/os.path.html#os.path.join
10+
# Windows not supported
11+
function(join_paths joined_path first_path_segment)
12+
set(temp_path "${first_path_segment}")
13+
foreach(current_segment IN LISTS ARGN)
14+
if(NOT ("${current_segment}" STREQUAL ""))
15+
if(IS_ABSOLUTE "${current_segment}")
16+
set(temp_path "${current_segment}")
17+
else()
18+
set(temp_path "${temp_path}/${current_segment}")
19+
endif()
20+
endif()
21+
endforeach()
22+
set(${joined_path} "${temp_path}" PARENT_SCOPE)
23+
endfunction()

mimalloc.pc.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
prefix=@CMAKE_INSTALL_PREFIX@
2+
libdir=@libdir_for_pc_file@
3+
includedir=@includedir_for_pc_file@
4+
5+
Name: @PROJECT_NAME@
6+
Description: a compact general purpose allocator with excellent performance
7+
Version: @PROJECT_VERSION@
8+
URL: https://github.com/microsoft/mimalloc/
9+
Libs: -L${libdir} -lmimalloc
10+
Libs.private: @pc_libraries@
11+
Cflags: -I${includedir}

0 commit comments

Comments
 (0)