diff options
author | Ashkan Aliabadi <ashkan.aliabadi@gmail.com> | 2021-07-14 14:52:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-14 14:52:20 -0700 |
commit | 6c89db05524d1d52a88251ef23f29c46ec873a99 (patch) | |
tree | 086e053f5a56c6f5819c2e02b14d96ce5f97754b | |
parent | 5916273f79a21551890fd3d56fc5375a78d1598d (diff) | |
parent | 41f97cfd347b27f041deb486e1df51b0049371ce (diff) | |
download | cpuinfo-6c89db05524d1d52a88251ef23f29c46ec873a99.tar.gz |
Merge pull request #61 from kiplingw/kip-pkg-config
Add support for pkg-config (#60)
-rw-r--r-- | CMakeLists.txt | 39 | ||||
-rw-r--r-- | README.md | 79 | ||||
-rw-r--r-- | libcpuinfo.pc.in | 12 |
3 files changed, 128 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e2d7d53..845ec28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,10 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.5 FATAL_ERROR) -# ---[ Project and semantic versioning. -PROJECT(cpuinfo C CXX) +# ---[ Setup project +PROJECT( + cpuinfo + LANGUAGES C CXX + HOMEPAGE_URL https://github.com/pytorch/cpuinfo) # ---[ Options. SET(CPUINFO_LIBRARY_TYPE "default" CACHE STRING "Type of cpuinfo library (shared, static, or default) to build") @@ -14,6 +17,7 @@ OPTION(CPUINFO_BUILD_TOOLS "Build command-line tools" ON) OPTION(CPUINFO_BUILD_UNIT_TESTS "Build cpuinfo unit tests" ON) OPTION(CPUINFO_BUILD_MOCK_TESTS "Build cpuinfo mock tests" ON) OPTION(CPUINFO_BUILD_BENCHMARKS "Build cpuinfo micro-benchmarks" ON) +OPTION(CPUINFO_BUILD_PKG_CONFIG "Build pkg-config manifest" ON) # ---[ CMake options INCLUDE(GNUInstallDirs) @@ -821,3 +825,34 @@ IF(CPUINFO_SUPPORTED_PLATFORM AND CPUINFO_BUILD_TOOLS) INSTALL(TARGETS cpuid-dump RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) ENDIF() ENDIF() + +# ---[ pkg-config manifest. This is mostly from JsonCpp... +IF(CPUINFO_BUILD_PKG_CONFIG) + + FUNCTION(JOIN_PATHS joined_path first_path_segment) + SET(temp_path "${first_path_segment}") + FOREACH(current_segment IN LISTS ARGN) + IF(NOT ("${current_segment}" STREQUAL "")) + IF(IS_ABSOLUTE "${current_segment}") + SET(temp_path "${current_segment}") + ELSE() + SET(temp_path "${temp_path}/${current_segment}") + ENDIF() + ENDIF() + ENDFOREACH() + SET(${joined_path} "${temp_path}" PARENT_SCOPE) + ENDFUNCTION() + + JOIN_PATHS(libdir_for_pc_file "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}") + JOIN_PATHS(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") + + CONFIGURE_FILE( + "libcpuinfo.pc.in" + "libcpuinfo.pc" + @ONLY) + + INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/libcpuinfo.pc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") + +ENDIF() + @@ -100,6 +100,84 @@ for (uint32_t i = 0; i < current_l2->processor_count; i++) { pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set); ``` +## Use via pkg-config + +If you would like to provide your project's build environment with the necessary compiler and linker flags in a portable manner, the library by default when built enables `CPUINFO_BUILD_PKG_CONFIG` and will generate a [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/) manifest (_libcpuinfo.pc_). Here are several examples of how to use it: + +### Command Line + +If you used your distro's package manager to install the library, you can verify that it is available to your build environment like so: + +```console +$ pkg-config --cflags --libs libcpuinfo +-I/usr/include/x86_64-linux-gnu/ -L/lib/x86_64-linux-gnu/ -lcpuinfo +``` + +If you have installed the library from source into a non-standard prefix, pkg-config may need help finding it: + +```console +$ PKG_CONFIG_PATH="/home/me/projects/cpuinfo/prefix/lib/pkgconfig/:$PKG_CONFIG_PATH" pkg-config --cflags --libs libcpuinfo +-I/home/me/projects/cpuinfo/prefix/include -L/home/me/projects/cpuinfo/prefix/lib -lcpuinfo +``` + +### GNU Autotools + +To [use](https://autotools.io/pkgconfig/pkg_check_modules.html) with the GNU Autotools include the following snippet in your project's `configure.ac`: + +```makefile +# CPU INFOrmation library... +PKG_CHECK_MODULES( + [libcpuinfo], [libcpuinfo], [], + [AC_MSG_ERROR([libcpuinfo missing...])]) +YOURPROJECT_CXXFLAGS="$YOURPROJECT_CXXFLAGS $libcpuinfo_CFLAGS" +YOURPROJECT_LIBS="$YOURPROJECT_LIBS $libcpuinfo_LIBS" +``` + +### Meson + +To use with Meson you just need to add `dependency('libcpuinfo')` as a dependency for your executable. + +```meson +project( + 'MyCpuInfoProject', + 'cpp', + meson_version: '>=0.55.0' +) + +executable( + 'MyCpuInfoExecutable', + sources: 'main.cpp', + dependencies: dependency('libcpuinfo') +) +``` + +### CMake + +To use with CMake use the [FindPkgConfig](https://cmake.org/cmake/help/latest/module/FindPkgConfig.html) module. Here is an example: + +```cmake +cmake_minimum_required(VERSION 3.6) +project("MyCpuInfoProject") + +find_package(PkgConfig) +pkg_check_modules(CpuInfo REQUIRED IMPORTED_TARGET libcpuinfo) + +add_executable(${PROJECT_NAME} main.cpp) +target_link_libraries(${PROJECT_NAME} PkgConfig::CpuInfo) +``` + +### Makefile + +To use within a vanilla makefile, you can call pkg-config directly to supply compiler and linker flags using shell substitution. + +```makefile +CFLAGS=-g3 -Wall -Wextra -Werror ... +LDFLAGS=-lfoo ... +... +CFLAGS+= $(pkg-config --cflags libcpuinfo) +LDFLAGS+= $(pkg-config --libs libcpuinfo) +``` + ## Exposed information - [x] Processor (SoC) name - [x] Microarchitecture @@ -202,3 +280,4 @@ pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set); - [x] Using `GetLogicalProcessorInformationEx` (Windows) - [x] Using sysfs (Linux) - [x] Using chipset name (ARM/Linux) + diff --git a/libcpuinfo.pc.in b/libcpuinfo.pc.in new file mode 100644 index 0000000..3027da3 --- /dev/null +++ b/libcpuinfo.pc.in @@ -0,0 +1,12 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=@CMAKE_INSTALL_PREFIX@ +libdir=@libdir_for_pc_file@ +includedir=@includedir_for_pc_file@ + +Name: lib@CMAKE_PROJECT_NAME@ +Description: Library to detect essential performance optimization information about host CPU. +Version: +URL: @PROJECT_HOMEPAGE_URL@ +Libs: -L${libdir} -lcpuinfo +Cflags: -I${includedir} + |