osxcross/tools/cpucount.c
Thomas Pöchtrager 2a4842f3cc add libc++ build script
update README with libc++ build instructions and samples
fix mavericks SDK download link (pointed to 10.8)
build xar only for <=10.5
add -g0 to the clang invocation command to avoid dsymutil from being run (debugging is not supported, but I guess you don't want to debug the resulting binaries anyway if you build them on a non OS X system)
attempt to make the toolchain less path dependent (gcc still breaks though, because of hardcoded paths), but clang and cctools can be moved now
update cctools to 845
add DISABLE_LTO_SUPPORT option (DISABLE_LTO_SUPPORT=1 ./build.sh) to disable linking against libLTO.so
add support for 32 bit systems
add FreeBSD support
update PACKAGE script
2014-03-26 20:32:55 +01:00

100 lines
2.1 KiB
C

/*
* Copyright (C) 2012 by Thomas Poechtrager
* t.poechtrager@gmail.com
*
* OSXCross is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* OSXCross is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef __CYGWIN__
#define WIN32
#endif //__CYGWIN__
#ifdef WIN32
#include <windows.h>
#endif //WIN32
#ifdef __linux__
#define __USE_GNU
#include <sched.h>
#undef __USE_GNU
#endif //__linux__
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
#include <unistd.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#ifndef HW_AVAILCPU
#define HW_AVAILCPU 25
#endif //HW_AVAILCPU
#endif //BSD
int getcpucount()
{
#ifdef WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#else
#ifdef __linux__
cpu_set_t cs;
int i, cpucount = 0;
CPU_ZERO(&cs);
sched_getaffinity(0, sizeof(cs), &cs);
for(i = 0; i < 128; i++)
{
if(CPU_ISSET(i, &cs))
cpucount++;
}
return cpucount ? cpucount : 1;
#else
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
int cpucount = 0;
int mib[4];
size_t len = sizeof(cpucount);
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU;
sysctl(mib, 2, &cpucount, &len, NULL, 0);
if(cpucount < 1)
{
mib[1] = HW_NCPU;
sysctl(mib, 2, &cpucount, &len, NULL, 0);
}
return cpucount ? cpucount : 1;
#else
#warning unknown platform
return 1;
#endif //BSD
#endif //__linux__
#endif //WIN32
}
int main()
{
printf("%d\n", getcpucount());
return 0;
}