Fix typos
[xz/debian.git] / cmake / tuklib_physmem.cmake
blobe4888ebeb760b892f82b8818395b1ab9ab03bb57
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:
20     if(WIN32 OR CYGWIN)
21         # Nothing to do, the tuklib_physmem.c handles it.
22         set(TUKLIB_PHYSMEM_DEFINITIONS "" CACHE INTERNAL "")
23         return()
24     endif()
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; }
32             #else
33             compile error
34             #endif
35         "
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 "")
40         return()
41     endif()
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>
48             int main(void)
49             {
50                 (void)_system_configuration.physmem;
51                 return 0;
52             }
53         "
54         TUKLIB_PHYSMEM_AIX)
55     if(TUKLIB_PHYSMEM_AIX)
56         set(TUKLIB_PHYSMEM_DEFINITIONS "TUKLIB_PHYSMEM_AIX" CACHE INTERNAL "")
57         return()
58     endif()
60     # sysconf()
61     check_c_source_compiles("
62             #include <unistd.h>
63             int main(void)
64             {
65                 long i;
66                 i = sysconf(_SC_PAGESIZE);
67                 i = sysconf(_SC_PHYS_PAGES);
68                 return 0;
69             }
70         "
71         TUKLIB_PHYSMEM_SYSCONF)
72     if(TUKLIB_PHYSMEM_SYSCONF)
73         set(TUKLIB_PHYSMEM_DEFINITIONS "TUKLIB_PHYSMEM_SYSCONF"
74             CACHE INTERNAL "")
75         return()
76     endif()
78     # sysctl()
79     check_include_file(sys/param.h HAVE_SYS_PARAM_H)
80     if(HAVE_SYS_PARAM_H)
81         list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_SYS_PARAM_H)
82     endif()
84     check_c_source_compiles("
85             #ifdef HAVE_SYS_PARAM_H
86             #   include <sys/param.h>
87             #endif
88             #include <sys/sysctl.h>
89             int main(void)
90             {
91                 int name[2] = { CTL_HW, HW_PHYSMEM };
92                 unsigned long mem;
93                 size_t mem_ptr_size = sizeof(mem);
94                 sysctl(name, 2, &mem, &mem_ptr_size, NULL, 0);
95                 return 0;
96             }
97         "
98         TUKLIB_PHYSMEM_SYSCTL)
99     if(TUKLIB_PHYSMEM_SYSCTL)
100         if(HAVE_SYS_PARAM_H)
101             set(TUKLIB_PHYSMEM_DEFINITIONS
102                 "HAVE_PARAM_H;TUKLIB_PHYSMEM_SYSCTL"
103                 CACHE INTERNAL "")
104         else()
105             set(TUKLIB_PHYSMEM_DEFINITIONS
106                 "TUKLIB_PHYSMEM_SYSCTL"
107                 CACHE INTERNAL "")
108         endif()
109         return()
110     endif()
112     # HP-UX
113     check_c_source_compiles("
114             #include <sys/param.h>
115             #include <sys/pstat.h>
116             int main(void)
117             {
118                 struct pst_static pst;
119                 pstat_getstatic(&pst, sizeof(pst), 1, 0);
120                 (void)pst.physical_memory;
121                 (void)pst.page_size;
122                 return 0;
123             }
124         "
125         TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
126     if(TUKLIB_PHYSMEM_PSTAT_GETSTATIC)
127         set(TUKLIB_PHYSMEM_DEFINITIONS "TUKLIB_PHYSMEM_PSTAT_GETSTATIC"
128             CACHE INTERNAL "")
129         return()
130     endif()
131 endfunction()
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 "")
140         else()
141             set(TUKLIB_PHYSMEM_FOUND 0 CACHE INTERNAL "")
142             message(WARNING
143                 "No method to detect the amount of physical memory was found")
144         endif()
145     endif()
147     if(TUKLIB_PHYSMEM_FOUND)
148         tuklib_add_definitions("${TARGET_OR_ALL}"
149                                "${TUKLIB_PHYSMEM_DEFINITIONS}")
150     endif()
151 endfunction()