diff options
author | Kip Warner <kip@thevertigo.com> | 2021-07-04 04:04:30 +0300 |
---|---|---|
committer | Kip Warner <kip@thevertigo.com> | 2021-07-04 04:04:30 +0300 |
commit | ba6692c830a7d9cbd96df4fc6862d539d0e54447 (patch) | |
tree | 5896afb5748761170e98ac7d8d2f9370fbff8f47 | |
parent | 5916273f79a21551890fd3d56fc5375a78d1598d (diff) |
Add support for pkg-config (#60)
-rw-r--r-- | CMakeLists.txt | 36 | ||||
-rw-r--r-- | README.md | 63 | ||||
-rw-r--r-- | libcpuinfo.pc.in | 12 |
3 files changed, 110 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e2d7d53..a492b10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,11 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.5 FATAL_ERROR) # ---[ Project and semantic versioning. -PROJECT(cpuinfo C CXX) +PROJECT( + cpuinfo + LANGUAGES C CXX + HOMEPAGE_URL https://github.com/pytorch/cpuinfo + VERSION 0.1.0) # ---[ Options. SET(CPUINFO_LIBRARY_TYPE "default" CACHE STRING "Type of cpuinfo library (shared, static, or default) to build") @@ -14,6 +18,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 +826,32 @@ 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 AND (CPUINFO_LIBRARY_TYPE STREQUAL "default" OR CPUINFO_LIBRARY_TYPE STREQUAL "shared")) + + 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,68 @@ 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 automatically have your project's build environment use the appropriate compiler and linker build flags, [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/) can greatly simplify things. It is the portable international _de facto_ standard for determining build flags. Here is an example: + +### GNU Autotools + +To [use](https://autotools.io/pkgconfig/pkg_check_modules.html) with the GNU Autotools, as an example, include the following snippet in your project's `configure.ac`: + +```makefile +# CPU INFOrmation library... +PKG_CHECK_MODULES( + [libcpuinfo], [libcpuinfo >= 0.1], [], + [AC_MSG_ERROR([libcpuinfo >= 0.1 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 a CMake build environment, 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 +264,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..a8fe607 --- /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: libcpuinfo +Description: Library to detect essential performance optimization information about host CPU. +Version: @PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@ +URL: @PROJECT_HOMEPAGE_URL@ +Libs: -L${libdir} -lcpuinfo +Cflags: -I${includedir} + |