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