2 # tuklib_physmem.cmake - see tuklib_physmem.m4 for description and comments
4 # NOTE: Compared tuklib_physmem.m4, this lacks support for Tru64, IRIX, and
5 # Linux sysinfo() (usually sysconf() is used on GNU/Linux).
9 # This file has been put into the public domain.
10 # You can do whatever you want with this file.
13 include("${CMAKE_CURRENT_LIST_DIR}/tuklib_common.cmake")
14 include(CheckCSourceCompiles)
15 include(CheckIncludeFile)
17 function(tuklib_physmem_internal_check)
18 # Shortcut on Windows:
20 # Nothing to do, the tuklib_physmem.c handles it.
21 set(TUKLIB_PHYSMEM_DEFINITIONS "" CACHE INTERNAL "")
25 # Full check for special cases:
26 check_c_source_compiles("
27 #if defined(_WIN32) || defined(__CYGWIN__) || defined(__OS2__) \
28 || defined(__DJGPP__) || defined(__VMS) \
29 || defined(AMIGA) || defined(__AROS__) || defined(__QNX__)
30 int main(void) { return 0; }
35 TUKLIB_PHYSMEM_SPECIAL)
36 if(TUKLIB_PHYSMEM_SPECIAL)
37 # Nothing to do, the tuklib_physmem.c handles it.
38 set(TUKLIB_PHYSMEM_DEFINITIONS "" CACHE INTERNAL "")
42 # Look for AIX-specific solution before sysconf(), because the test
43 # for sysconf() will pass on AIX but won't actually work
44 # (sysconf(_SC_PHYS_PAGES) compiles but always returns -1 on AIX).
45 check_c_source_compiles("
46 #include <sys/systemcfg.h>
49 (void)_system_configuration.physmem;
54 if(TUKLIB_PHYSMEM_AIX)
55 set(TUKLIB_PHYSMEM_DEFINITIONS "TUKLIB_PHYSMEM_AIX" CACHE INTERNAL "")
60 check_c_source_compiles("
65 i = sysconf(_SC_PAGESIZE);
66 i = sysconf(_SC_PHYS_PAGES);
70 TUKLIB_PHYSMEM_SYSCONF)
71 if(TUKLIB_PHYSMEM_SYSCONF)
72 set(TUKLIB_PHYSMEM_DEFINITIONS "TUKLIB_PHYSMEM_SYSCONF"
78 check_include_file(sys/param.h HAVE_SYS_PARAM_H)
80 list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_SYS_PARAM_H)
83 check_c_source_compiles("
84 #ifdef HAVE_SYS_PARAM_H
85 # include <sys/param.h>
87 #include <sys/sysctl.h>
90 int name[2] = { CTL_HW, HW_PHYSMEM };
92 size_t mem_ptr_size = sizeof(mem);
93 sysctl(name, 2, &mem, &mem_ptr_size, NULL, 0);
97 TUKLIB_PHYSMEM_SYSCTL)
98 if(TUKLIB_PHYSMEM_SYSCTL)
100 set(TUKLIB_PHYSMEM_DEFINITIONS
101 "HAVE_PARAM_H;TUKLIB_PHYSMEM_SYSCTL"
104 set(TUKLIB_PHYSMEM_DEFINITIONS
105 "TUKLIB_PHYSMEM_SYSCTL"
112 check_c_source_compiles("
113 #include <sys/param.h>
114 #include <sys/pstat.h>
117 struct pst_static pst;
118 pstat_getstatic(&pst, sizeof(pst), 1, 0);
119 (void)pst.physical_memory;
124 TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
125 if(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
126 set(TUKLIB_PHYSMEM_DEFINITIONS "TUKLIB_PHYSMEM_PSTAT_GETSTATIC"
132 function(tuklib_physmem TARGET_OR_ALL)
133 if(NOT DEFINED TUKLIB_PHYSMEM_FOUND)
134 message(STATUS "Checking how to detect the amount of physical memory")
135 tuklib_physmem_internal_check()
137 if(DEFINED TUKLIB_PHYSMEM_DEFINITIONS)
138 set(TUKLIB_PHYSMEM_FOUND 1 CACHE INTERNAL "")
140 set(TUKLIB_PHYSMEM_FOUND 0 CACHE INTERNAL "")
142 "No method to detect the amount of physical memory was found")
146 if(TUKLIB_PHYSMEM_FOUND)
147 tuklib_add_definitions("${TARGET_OR_ALL}"
148 "${TUKLIB_PHYSMEM_DEFINITIONS}")