Skip to content

Commit e9de3cc

Browse files
committed
initial commit
0 parents  commit e9de3cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+4971
-0
lines changed

Diff for: LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright Brian White. All rights reserved.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to
5+
deal in the Software without restriction, including without limitation the
6+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+
sell copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19+
IN THE SOFTWARE.

Diff for: README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
Description
3+
===========
4+
5+
A simple [node.js](https://nodejs.org) binding to [cpu_features](https://github.com/google/cpu_features) for obtaining information about installed CPU(s).
6+
7+
8+
Requirements
9+
============
10+
11+
* [node.js](http://nodejs.org/) -- v8.0.0 or newer
12+
* An appropriate build environment -- see [node-gyp's documentation](https://github.com/nodejs/node-gyp/blob/master/README.md)
13+
* CMake -- any modern version (v3.14+ required for Windows and must be available in %PATH%)
14+
15+
16+
Install
17+
=======
18+
19+
npm install cpu-features
20+
21+
22+
Example
23+
=======
24+
25+
```js
26+
// Generally it's a good idea to just call this once and
27+
// reuse the result since `cpu-features` does not cache
28+
// the result itself.
29+
const features = require('cpu-features')();
30+
31+
console.log(features);
32+
// example output:
33+
// { arch: 'x86',
34+
// brand: 'Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz',
35+
// family: 6,
36+
// model: 58,
37+
// stepping: 9,
38+
// uarch: 'INTEL_IVB',
39+
// flags:
40+
// { aes: true,
41+
// erms: true,
42+
// f16c: true,
43+
// ssse3: true,
44+
// sse4_1: true,
45+
// sse4_2: true,
46+
// avx: true,
47+
// cx16: true,
48+
// popcnt: true,
49+
// rdrnd: true } }
50+
```

Diff for: binding.gyp

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'cpufeatures',
5+
'dependencies': [ 'build_deps' ],
6+
'include_dirs': [
7+
'deps/cpu_features/include',
8+
'src',
9+
"<!(node -e \"require('nan')\")",
10+
],
11+
'sources': [
12+
'src/binding.cc'
13+
],
14+
'cflags': [ '-O2' ],
15+
'conditions': [
16+
[ 'OS=="win"', {
17+
'link_settings': {
18+
'libraries': [ '<(module_root_dir)/deps/cpu_features/build/Release/cpu_features.lib' ],
19+
},
20+
}, { # POSIX
21+
'link_settings': {
22+
'libraries': [ '<(module_root_dir)/deps/cpu_features/build/libcpu_features.a' ]
23+
},
24+
}],
25+
],
26+
},
27+
28+
{
29+
'target_name': 'config_deps',
30+
'type': 'none',
31+
'actions': [
32+
{
33+
'action_name': 'config_deps',
34+
'message': 'Configuring dependencies',
35+
'inputs': [ '<(module_root_dir)/deps/cpu_features/CMakeLists.txt' ],
36+
'conditions': [
37+
[ 'OS=="win"', {
38+
'outputs': [ '<(module_root_dir)/deps/cpu_features/build/CpuFeatures.sln' ],
39+
'conditions': [
40+
['target_arch=="ia32"', {
41+
'variables': { 'cmake_arch': 'Win32' },
42+
}, {
43+
'variables': { 'cmake_arch': 'x64' },
44+
}],
45+
],
46+
'action': [ 'cd <(module_root_dir)/deps/cpu_features/build && cmake -DCMAKE_BUILD_TYPE=Release -A <(cmake_arch) ..' ],
47+
}, { # POSIX
48+
'outputs': [ '<(module_root_dir)/deps/cpu_features/build/Makefile' ],
49+
'action': [
50+
'cmake',
51+
'-DCMAKE_BUILD_TYPE=Release',
52+
'-B<(module_root_dir)/deps/cpu_features/build',
53+
'-H<(module_root_dir)/deps/cpu_features',
54+
],
55+
}],
56+
],
57+
}
58+
],
59+
},
60+
61+
{
62+
'target_name': 'build_deps',
63+
'dependencies': [ 'config_deps' ],
64+
'type': 'none',
65+
'actions': [
66+
{
67+
'action_name': 'build_deps',
68+
'message': 'Building dependencies',
69+
'inputs': [ '<(module_root_dir)/deps/cpu_features/CMakeLists.txt' ],
70+
'conditions': [
71+
[ 'OS=="win"', {
72+
'outputs': [ '<(module_root_dir)/deps/cpu_features/build/Release/cpu_features.lib' ],
73+
'action': [ 'cd <(module_root_dir)/deps/cpu_features/build && cmake --build . --config Release' ],
74+
}, { # POSIX
75+
'outputs': [ '<(module_root_dir)/deps/cpu_features/build/libcpu_features.a' ],
76+
'action': [
77+
'cmake',
78+
'--build',
79+
'<(module_root_dir)/deps/cpu_features/build',
80+
'--config',
81+
'Release',
82+
],
83+
}],
84+
],
85+
},
86+
],
87+
},
88+
89+
],
90+
}

Diff for: deps/cpu_features/.clang-format

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: Google
4+
...

Diff for: deps/cpu_features/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cmake_build/

Diff for: deps/cpu_features/CMakeLists.txt

+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
3+
project(CpuFeatures VERSION 0.1.0)
4+
5+
# Default Build Type to be Release
6+
if(NOT CMAKE_BUILD_TYPE)
7+
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
8+
"Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
9+
FORCE)
10+
endif(NOT CMAKE_BUILD_TYPE)
11+
12+
# BUILD_TESTING is a standard CMake variable, but we declare it here to make it
13+
# prominent in the GUI.
14+
option(BUILD_TESTING "Enable test (depends on googletest)." OFF)
15+
# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to make
16+
# it prominent in the GUI.
17+
# cpu_features uses bit-fields which are - to some extends - implementation-defined (see https://en.cppreference.com/w/c/language/bit_field).
18+
# As a consequence it is discouraged to use cpu_features as a shared library because different compilers may interpret the code in different ways.
19+
# Prefer static linking from source whenever possible.
20+
option(BUILD_SHARED_LIBS "Build library as shared." OFF)
21+
# PIC
22+
option(BUILD_PIC "Build with Position Independant Code." OFF) # Default is off at least for GCC
23+
24+
include(CheckIncludeFile)
25+
include(CheckSymbolExists)
26+
include(GNUInstallDirs)
27+
28+
macro(setup_include_and_definitions TARGET_NAME)
29+
target_include_directories(${TARGET_NAME}
30+
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
31+
PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/internal>
32+
)
33+
target_compile_definitions(${TARGET_NAME}
34+
PUBLIC STACK_LINE_READER_BUFFER_SIZE=1024
35+
)
36+
endmacro()
37+
38+
set(PROCESSOR_IS_MIPS FALSE)
39+
set(PROCESSOR_IS_ARM FALSE)
40+
set(PROCESSOR_IS_AARCH64 FALSE)
41+
set(PROCESSOR_IS_X86 FALSE)
42+
set(PROCESSOR_IS_POWER FALSE)
43+
44+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^mips")
45+
set(PROCESSOR_IS_MIPS TRUE)
46+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
47+
set(PROCESSOR_IS_ARM TRUE)
48+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64")
49+
set(PROCESSOR_IS_AARCH64 TRUE)
50+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64)|(^i.86$)")
51+
set(PROCESSOR_IS_X86 TRUE)
52+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)")
53+
set(PROCESSOR_IS_POWER TRUE)
54+
endif()
55+
56+
macro(add_cpu_features_headers_and_sources HDRS_LIST_NAME SRCS_LIST_NAME)
57+
list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpu_features_macros.h)
58+
if(PROCESSOR_IS_MIPS)
59+
list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_mips.h)
60+
list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_mips.c)
61+
elseif(PROCESSOR_IS_ARM)
62+
list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_arm.h)
63+
list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_arm.c)
64+
elseif(PROCESSOR_IS_AARCH64)
65+
list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_aarch64.h)
66+
list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_aarch64.c)
67+
elseif(PROCESSOR_IS_X86)
68+
list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_x86.h)
69+
list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/internal/cpuid_x86.h)
70+
list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_x86.c)
71+
elseif(PROCESSOR_IS_POWER)
72+
list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_ppc.h)
73+
list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_ppc.c)
74+
else()
75+
message(FATAL_ERROR "Unsupported architectures ${CMAKE_SYSTEM_PROCESSOR}")
76+
endif()
77+
endmacro()
78+
79+
#
80+
# library : utils
81+
#
82+
83+
add_library(utils OBJECT
84+
${PROJECT_SOURCE_DIR}/include/internal/bit_utils.h
85+
${PROJECT_SOURCE_DIR}/include/internal/filesystem.h
86+
${PROJECT_SOURCE_DIR}/include/internal/stack_line_reader.h
87+
${PROJECT_SOURCE_DIR}/include/internal/string_view.h
88+
${PROJECT_SOURCE_DIR}/src/filesystem.c
89+
${PROJECT_SOURCE_DIR}/src/stack_line_reader.c
90+
${PROJECT_SOURCE_DIR}/src/string_view.c
91+
)
92+
setup_include_and_definitions(utils)
93+
94+
#
95+
# library : unix_based_hardware_detection
96+
#
97+
98+
if(UNIX)
99+
add_library(unix_based_hardware_detection OBJECT
100+
${PROJECT_SOURCE_DIR}/include/internal/hwcaps.h
101+
${PROJECT_SOURCE_DIR}/include/internal/unix_features_aggregator.h
102+
${PROJECT_SOURCE_DIR}/src/hwcaps.c
103+
${PROJECT_SOURCE_DIR}/src/unix_features_aggregator.c
104+
)
105+
setup_include_and_definitions(unix_based_hardware_detection)
106+
check_include_file(dlfcn.h HAVE_DLFCN_H)
107+
if(HAVE_DLFCN_H)
108+
target_compile_definitions(unix_based_hardware_detection PRIVATE HAVE_DLFCN_H)
109+
endif()
110+
check_symbol_exists(getauxval "sys/auxv.h" HAVE_STRONG_GETAUXVAL)
111+
if(HAVE_STRONG_GETAUXVAL)
112+
target_compile_definitions(unix_based_hardware_detection PRIVATE HAVE_STRONG_GETAUXVAL)
113+
endif()
114+
set_property(TARGET unix_based_hardware_detection PROPERTY POSITION_INDEPENDENT_CODE ${BUILD_PIC})
115+
endif()
116+
117+
#
118+
# library : cpu_features
119+
#
120+
set (CPU_FEATURES_HDRS)
121+
set (CPU_FEATURES_SRCS)
122+
add_cpu_features_headers_and_sources(CPU_FEATURES_HDRS CPU_FEATURES_SRCS)
123+
list(APPEND CPU_FEATURES_SRCS $<TARGET_OBJECTS:utils>)
124+
if(NOT PROCESSOR_IS_X86 AND UNIX)
125+
list(APPEND CPU_FEATURES_SRCS $<TARGET_OBJECTS:unix_based_hardware_detection>)
126+
endif()
127+
add_library(cpu_features ${CPU_FEATURES_HDRS} ${CPU_FEATURES_SRCS})
128+
set_target_properties(cpu_features PROPERTIES PUBLIC_HEADER "${CPU_FEATURES_HDRS}")
129+
setup_include_and_definitions(cpu_features)
130+
target_link_libraries(cpu_features PUBLIC ${CMAKE_DL_LIBS})
131+
set_property(TARGET cpu_features PROPERTY POSITION_INDEPENDENT_CODE ${BUILD_PIC})
132+
target_include_directories(cpu_features
133+
PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cpu_features>
134+
)
135+
136+
#
137+
# ndk_compat
138+
#
139+
140+
if(ANDROID)
141+
add_subdirectory(ndk_compat)
142+
endif()
143+
144+
#
145+
# tests
146+
#
147+
148+
include(CTest)
149+
if(BUILD_TESTING)
150+
# Automatically incorporate googletest into the CMake Project if target not
151+
# found.
152+
if(NOT TARGET gtest OR NOT TARGET gmock_main)
153+
# Download and unpack googletest at configure time.
154+
configure_file(
155+
cmake/googletest.CMakeLists.txt.in
156+
googletest-download/CMakeLists.txt
157+
)
158+
159+
execute_process(
160+
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
161+
RESULT_VARIABLE result
162+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
163+
164+
if(result)
165+
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
166+
endif()
167+
168+
execute_process(
169+
COMMAND ${CMAKE_COMMAND} --build .
170+
RESULT_VARIABLE result
171+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
172+
173+
if(result)
174+
message(FATAL_ERROR "Build step for googletest failed: ${result}")
175+
endif()
176+
177+
# Prevent overriding the parent project's compiler/linker settings on
178+
# Windows.
179+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
180+
181+
# Add googletest directly to our build. This defines the gtest and
182+
# gtest_main targets.
183+
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
184+
${CMAKE_BINARY_DIR}/googletest-build
185+
EXCLUDE_FROM_ALL)
186+
endif()
187+
188+
add_subdirectory(test)
189+
endif()
190+
191+
#
192+
# Install cpu_features
193+
#
194+
195+
include(GNUInstallDirs)
196+
install(TARGETS cpu_features
197+
EXPORT CpuFeaturesTargets
198+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cpu_features
199+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
200+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
201+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
202+
)
203+
install(EXPORT CpuFeaturesTargets
204+
NAMESPACE CpuFeatures::
205+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures
206+
COMPONENT Devel
207+
)
208+
include(CMakePackageConfigHelpers)
209+
configure_package_config_file(cmake/CpuFeaturesConfig.cmake.in
210+
"${PROJECT_BINARY_DIR}/CpuFeaturesConfig.cmake"
211+
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures"
212+
NO_SET_AND_CHECK_MACRO
213+
NO_CHECK_REQUIRED_COMPONENTS_MACRO
214+
)
215+
write_basic_package_version_file(
216+
"${PROJECT_BINARY_DIR}/CpuFeaturesConfigVersion.cmake"
217+
COMPATIBILITY SameMajorVersion
218+
)
219+
install(
220+
FILES
221+
"${PROJECT_BINARY_DIR}/CpuFeaturesConfig.cmake"
222+
"${PROJECT_BINARY_DIR}/CpuFeaturesConfigVersion.cmake"
223+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures"
224+
COMPONENT Devel
225+
)

0 commit comments

Comments
 (0)