1 # SPDX-License-Identifier: 0BSD
3 #############################################################################
5 # tuklib_physmem.cmake - see tuklib_physmem.m4 for description and comments
7 # NOTE: Compared tuklib_physmem.m4, this lacks support for Tru64, IRIX, and
8 # Linux sysinfo() (usually sysconf() is used on GNU/Linux).
10 # Author: Lasse Collin
12 #############################################################################
14 include("${CMAKE_CURRENT_LIST_DIR}/tuklib_common.cmake")
15 include(CheckCSourceCompiles)
16 include(CheckIncludeFile)
18 function(tuklib_physmem_internal_check)
19 # Shortcut on Windows:
21 # Nothing to do, the tuklib_physmem.c handles it.
22 set(TUKLIB_PHYSMEM_DEFINITIONS "" CACHE INTERNAL "")
26 # Full check for special cases:
27 check_c_source_compiles("
28 #if defined(_WIN32) || defined(__CYGWIN__) || defined(__OS2__) \
29 || defined(__DJGPP__) || defined(__VMS) \
30 || defined(AMIGA) || defined(__AROS__) || defined(__QNX__)
31 int main(void) { return 0; }
36 TUKLIB_PHYSMEM_SPECIAL)
37 if(TUKLIB_PHYSMEM_SPECIAL)
38 # Nothing to do, the tuklib_physmem.c handles it.
39 set(TUKLIB_PHYSMEM_DEFINITIONS "" CACHE INTERNAL "")
43 # Look for AIX-specific solution before sysconf(), because the test
44 # for sysconf() will pass on AIX but won't actually work
45 # (sysconf(_SC_PHYS_PAGES) compiles but always returns -1 on AIX).
46 check_c_source_compiles("
47 #include <sys/systemcfg.h>
50 (void)_system_configuration.physmem;
55 if(TUKLIB_PHYSMEM_AIX)
56 set(TUKLIB_PHYSMEM_DEFINITIONS "TUKLIB_PHYSMEM_AIX" CACHE INTERNAL "")
61 check_c_source_compiles("
66 i = sysconf(_SC_PAGESIZE);
67 i = sysconf(_SC_PHYS_PAGES);
71 TUKLIB_PHYSMEM_SYSCONF)
72 if(TUKLIB_PHYSMEM_SYSCONF)
73 set(TUKLIB_PHYSMEM_DEFINITIONS "TUKLIB_PHYSMEM_SYSCONF"
79 check_include_file(sys/param.h HAVE_SYS_PARAM_H)
81 list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_SYS_PARAM_H)
84 check_c_source_compiles("
85 #ifdef HAVE_SYS_PARAM_H
86 # include <sys/param.h>
88 #include <sys/sysctl.h>
91 int name[2] = { CTL_HW, HW_PHYSMEM };
93 size_t mem_ptr_size = sizeof(mem);
94 sysctl(name, 2, &mem, &mem_ptr_size, NULL, 0);
98 TUKLIB_PHYSMEM_SYSCTL)
99 if(TUKLIB_PHYSMEM_SYSCTL)
101 set(TUKLIB_PHYSMEM_DEFINITIONS
102 "HAVE_PARAM_H;TUKLIB_PHYSMEM_SYSCTL"
105 set(TUKLIB_PHYSMEM_DEFINITIONS
106 "TUKLIB_PHYSMEM_SYSCTL"
113 check_c_source_compiles("
114 #include <sys/param.h>
115 #include <sys/pstat.h>
118 struct pst_static pst;
119 pstat_getstatic(&pst, sizeof(pst), 1, 0);
120 (void)pst.physical_memory;
125 TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
126 if(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
127 set(TUKLIB_PHYSMEM_DEFINITIONS "TUKLIB_PHYSMEM_PSTAT_GETSTATIC"
133 function(tuklib_physmem TARGET_OR_ALL)
134 if(NOT DEFINED TUKLIB_PHYSMEM_FOUND)
135 message(STATUS "Checking how to detect the amount of physical memory")
136 tuklib_physmem_internal_check()
138 if(DEFINED TUKLIB_PHYSMEM_DEFINITIONS)
139 set(TUKLIB_PHYSMEM_FOUND 1 CACHE INTERNAL "")
141 set(TUKLIB_PHYSMEM_FOUND 0 CACHE INTERNAL "")
143 "No method to detect the amount of physical memory was found")
147 if(TUKLIB_PHYSMEM_FOUND)
148 tuklib_add_definitions("${TARGET_OR_ALL}"
149 "${TUKLIB_PHYSMEM_DEFINITIONS}")