xref: /aosp_15_r20/external/stg/cmake/FindJemalloc.cmake (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2#
3# Copyright 2023 Google LLC
4#
5# Licensed under the Apache License v2.0 with LLVM Exceptions (the "License");
6# you may not use this file except in compliance with the License.  You may
7# obtain a copy of the License at
8#
9# https://llvm.org/LICENSE.txt
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16#
17# Author: Aleksei Vetrov
18
19#[=======================================================================[.rst:
20FindJemalloc
21---------
22
23Finds the jemalloc library.
24
25Imported Targets
26^^^^^^^^^^^^^^^^
27
28This module provides the following imported targets, if found:
29
30``Jemalloc::Jemalloc``
31  The Jemalloc library
32
33Result Variables
34^^^^^^^^^^^^^^^^
35
36This will define the following variables:
37
38``Jemalloc_FOUND``
39  True if the system has the Jemalloc library.
40``Jemalloc_VERSION``
41  The version of the Jemalloc library which was found.
42``Jemalloc_INCLUDE_DIRS``
43  Include directories needed to use Jemalloc.
44``Jemalloc_LIBRARIES``
45  Libraries needed to link to Jemalloc.
46``Jemalloc_DEFINITIONS``
47  the compiler switches required for using Jemalloc
48
49Cache Variables
50^^^^^^^^^^^^^^^
51
52The following cache variables may also be set:
53
54``Jemalloc_INCLUDE_DIR``
55  The directory containing ``jemalloc.h``.
56``Jemalloc_LIBRARY``
57  The path to the ``libjemalloc.so``.
58
59#]=======================================================================]
60
61find_package(PkgConfig)
62pkg_check_modules(PC_Jemalloc QUIET jemalloc)
63
64find_library(
65  Jemalloc_LIBRARY
66  NAMES jemalloc
67  HINTS ${PC_Jemalloc_LIBDIR} ${PC_Jemalloc_LIBRARY_DIRS})
68# Try the value from user if the library is not found.
69if(DEFINED Jemalloc_LIBRARIES AND NOT DEFINED Jemalloc_LIBRARY)
70  set(Jemalloc_LIBRARY ${Jemalloc_LIBRARIES})
71endif()
72mark_as_advanced(Jemalloc_LIBRARY)
73
74find_path(
75  Jemalloc_INCLUDE_DIR
76  NAMES jemalloc.h
77  PATH_SUFFIXES jemalloc
78  HINTS ${PC_Jemalloc_INCLUDEDIR} ${PC_Jemalloc_INCLUDE_DIRS})
79# Try the value from user if the library is not found.
80if(DEFINED Jemalloc_INCLUDE_DIRS AND NOT DEFINED Jemalloc_INCLUDE_DIR)
81  set(Jemalloc_INCLUDE_DIR ${Jemalloc_INCLUDE_DIRS})
82endif()
83mark_as_advanced(Jemalloc_INCLUDE_DIR)
84
85set(Jemalloc_VERSION ${PC_Jemalloc_VERSION})
86
87include(FindPackageHandleStandardArgs)
88find_package_handle_standard_args(
89  Jemalloc
90  REQUIRED_VARS Jemalloc_LIBRARY Jemalloc_INCLUDE_DIR
91  VERSION_VAR Jemalloc_VERSION)
92
93if(Jemalloc_FOUND)
94  set(Jemalloc_LIBRARIES ${Jemalloc_LIBRARY})
95  set(Jemalloc_INCLUDE_DIRS ${Jemalloc_INCLUDE_DIR})
96  set(Jemalloc_DEFINITIONS ${PC_Jemalloc_CFLAGS_OTHER})
97endif()
98
99if(Jemalloc_FOUND AND NOT TARGET Jemalloc::Jemalloc)
100  add_library(Jemalloc::Jemalloc UNKNOWN IMPORTED)
101  set_target_properties(
102    Jemalloc::Jemalloc
103    PROPERTIES IMPORTED_LOCATION "${Jemalloc_LIBRARY}"
104               INTERFACE_COMPILE_OPTIONS "${PC_Jemalloc_CFLAGS_OTHER}"
105               INTERFACE_INCLUDE_DIRECTORIES "${Jemalloc_INCLUDE_DIR}")
106endif()
107