From 7179a7b103495b9a46d5926d42aef86585ebcc7a Mon Sep 17 00:00:00 2001 From: Xorg Date: Thu, 21 May 2020 18:43:34 +0200 Subject: [PATCH] CMake: fix Unix install and format --- CMakeLists.txt | 24 ++++++- cpuid_tool/CMakeLists.txt | 21 ++++-- libcpuid/CMakeLists.txt | 130 +++++++++++++++++++++----------------- tests/CMakeLists.txt | 28 ++++++++ 4 files changed, 135 insertions(+), 68 deletions(-) create mode 100644 tests/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 96df5ea..727523e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,30 @@ cmake_minimum_required(VERSION 3.14) set(VERSION "0.4.1") -project(cpuid LANGUAGES C CXX ASM_MASM VERSION ${VERSION}) +set(SOVERSION 14) +project( + cpuid + LANGUAGES C CXX ASM_MASM + VERSION ${VERSION}) set(CMAKE_CXX_STANDARD 11) set(CMAKE_C_STANDARD 99) +# Global variables +set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") + +if(UNIX) + include(GNUInstallDirs) + set(prefix "${CMAKE_INSTALL_PREFIX}") + set(exec_prefix "\${prefix}") + set(libdir "\${exec_prefix}/lib") + set(includedir "\${prefix}/include") + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/libcpuid.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libcpuid.pc" ESCAPE_QUOTES + @ONLY) + install(FILES "${PROJECT_BINARY_DIR}/libcpuid.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") +endif(UNIX) + +# Include subdirectories add_subdirectory(libcpuid) -add_subdirectory(cpuid_tool) \ No newline at end of file +add_subdirectory(cpuid_tool) +add_subdirectory(tests) diff --git a/cpuid_tool/CMakeLists.txt b/cpuid_tool/CMakeLists.txt index b7f4430..57d6eca 100644 --- a/cpuid_tool/CMakeLists.txt +++ b/cpuid_tool/CMakeLists.txt @@ -1,9 +1,16 @@ add_executable(cpuid_tool cpuid_tool.c) -target_link_libraries(cpuid_tool PRIVATE cpuid) -install(TARGETS cpuid_tool - CONFIGURATIONS Debug - RUNTIME DESTINATION bin/Debug) -install(TARGETS cpuid_tool - CONFIGURATIONS Release - RUNTIME DESTINATION bin/Release) \ No newline at end of file +target_link_libraries(cpuid_tool cpuid) + +if(WIN32) + install( + TARGETS cpuid_tool + CONFIGURATIONS Debug + RUNTIME DESTINATION bin/Debug) + install( + TARGETS cpuid_tool + CONFIGURATIONS Release + RUNTIME DESTINATION bin/Release) +else() + install(TARGETS cpuid_tool RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +endif() diff --git a/libcpuid/CMakeLists.txt b/libcpuid/CMakeLists.txt index 96533ac..66a13d6 100644 --- a/libcpuid/CMakeLists.txt +++ b/libcpuid/CMakeLists.txt @@ -1,4 +1,5 @@ -set(cpuid_sources cpuid_main.c +set(cpuid_sources + cpuid_main.c recog_intel.c recog_amd.c rdtsc.c @@ -7,43 +8,48 @@ set(cpuid_sources cpuid_main.c msrdriver.c asm-bits.c) -if ("${MSVC_CXX_ARCHITECTURE_ID}" MATCHES "x64") - list(APPEND cpuid_sources masm-x64.asm) -endif () +if(WIN32 AND "${MSVC_CXX_ARCHITECTURE_ID}" MATCHES "x64") + list(APPEND cpuid_sources masm-x64.asm) +endif() -add_library(cpuid - ${cpuid_sources} - ) +add_library(cpuid SHARED ${cpuid_sources}) target_include_directories(cpuid SYSTEM PUBLIC $) target_compile_definitions(cpuid PRIVATE VERSION="${PROJECT_VERSION}") -set(CPUID_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/libcpuid.h ${CMAKE_CURRENT_SOURCE_DIR}/libcpuid_constants.h ${CMAKE_CURRENT_SOURCE_DIR}/libcpuid_types.h) -set_target_properties(cpuid PROPERTIES - PUBLIC_HEADER "${CPUID_HEADERS}" - ) +set_target_properties(cpuid PROPERTIES VERSION ${PROJECT_VERSION}) +set_target_properties(cpuid PROPERTIES SOVERSION ${SOVERSION}) +set_target_properties(cpuid PROPERTIES PUBLIC_HEADER "libcpuid.h;libcpuid_types.h;libcpuid_constants.h") -#Documentation +# Documentation find_package(Doxygen) option(ENABLE_DOCS "Enable building documentation." ON) -if (DOXYGEN_FOUND AND ENABLE_DOCS) - set(top_srcdir ${PROJECT_SOURCE_DIR}) - configure_file(Doxyfile.in - Doxyfile - ESCAPE_QUOTES - ) - add_custom_target(docs - ALL - ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/ - COMMENT "Generating API documentation with Doxygen" VERBATIM - ) -else () - add_custom_target(docs - echo "Doxygen was not installed when CMake was run or ENABLE_DOCS was OFF. Check that Doxygen is installed and rerun `cmake .`" VERBATIM - ) -endif () +if(DOXYGEN_FOUND AND ENABLE_DOCS) + set(top_srcdir ${PROJECT_SOURCE_DIR}) + configure_file(Doxyfile.in Doxyfile ESCAPE_QUOTES) + add_custom_target( + docs ALL + ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/ + COMMENT "Generating API documentation with Doxygen" + VERBATIM) + if(UNIX) + install( + DIRECTORY "${CMAKE_SOURCE_DIR}/docs/man/" + DESTINATION "${CMAKE_INSTALL_MANDIR}" + FILES_MATCHING + PATTERN "cpu_*_t.3" + PATTERN "libcpuid.3" + PATTERN "cpuid_tool.3") + endif(UNIX) +else() + add_custom_target( + docs + echo + "Doxygen was not installed when CMake was run or ENABLE_DOCS was OFF. Check that Doxygen is installed and rerun `cmake .`" + VERBATIM) +endif() -#Configuration +# Configuration set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated") set(config_install_dir "lib/cmake/${PROJECT_NAME}") set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake") @@ -53,34 +59,40 @@ set(namespace "${PROJECT_NAME}::") include(CMakePackageConfigHelpers) -# Configure 'ConfigVersion.cmake' -# Use: -# * PROJECT_VERSION -write_basic_package_version_file( - "${version_config}" COMPATIBILITY SameMajorVersion -) +# Configure 'ConfigVersion.cmake' Use: * PROJECT_VERSION +write_basic_package_version_file("${version_config}" COMPATIBILITY SameMajorVersion) -# Configure 'Config.cmake' -# Use variables: -# * TARGETS_EXPORT_NAME -# * PROJECT_NAME -configure_package_config_file( - "${CMAKE_SOURCE_DIR}/cmake/Config.cmake.in" - "${project_config}" - INSTALL_DESTINATION "${config_install_dir}" -) -#Installation -install(TARGETS cpuid - EXPORT "${TARGETS_EXPORT_NAME}" - LIBRARY DESTINATION "lib" - ARCHIVE DESTINATION "lib" - RUNTIME DESTINATION "bin" - INCLUDES DESTINATION "include") +# Configure 'Config.cmake' Use variables: * TARGETS_EXPORT_NAME * PROJECT_NAME +configure_package_config_file("${CMAKE_MODULE_PATH}/Config.cmake.in" "${project_config}" + INSTALL_DESTINATION "${config_install_dir}") -install(FILES "${project_config}" "${version_config}" - DESTINATION "${config_install_dir}" - ) -install(EXPORT "${TARGETS_EXPORT_NAME}" - NAMESPACE "${namespace}" - DESTINATION "${config_install_dir}" - ) \ No newline at end of file +# Installation +if(WIN32) + install( + TARGETS cpuid + EXPORT "${TARGETS_EXPORT_NAME}" + LIBRARY DESTINATION "lib" + ARCHIVE DESTINATION "lib" + RUNTIME DESTINATION "bin" + INCLUDES + DESTINATION "include") + install( + EXPORT "${TARGETS_EXPORT_NAME}" + NAMESPACE "${namespace}" + DESTINATION "${config_install_dir}") + +else() + install( + TARGETS cpuid + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +endif() + +install(FILES "${project_config}" "${version_config}" DESTINATION "${config_install_dir}") + +add_custom_target( + consistency + COMMAND "./check-consistency.py" "./" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ + COMMENT "Run tests (fast mode)" + VERBATIM) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..51c1809 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,28 @@ +add_executable(convert_instlatx64 convert_instlatx64.c) +target_link_libraries(convert_instlatx64 PUBLIC cpuid) + +add_custom_target(test DEPENDS test-fast) + +add_custom_target( + test-fast + COMMAND ${CMAKE_COMMAND} -E env "LD_LD_PRELOAD=${CMAKE_BINARY_DIR}/libcpuid" ./run_tests.py + "${CMAKE_BINARY_DIR}/cpuid_tool/cpuid_tool" "." --show-test-fast-warning + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ + COMMENT "Run tests (fast mode)" + VERBATIM) + +add_custom_target( + test-old + COMMAND ${CMAKE_COMMAND} -E env "LD_LD_PRELOAD=${CMAKE_BINARY_DIR}/libcpuid" ./run_tests.py + "${CMAKE_BINARY_DIR}/cpuid_tool/cpuid_tool" "." + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ + COMMENT "Run tests (old mode)" + VERBATIM) + +add_custom_target( + fix-tests + COMMAND ${CMAKE_COMMAND} -E env "LD_LD_PRELOAD=${CMAKE_BINARY_DIR}/libcpuid" ./run_tests.py + "${CMAKE_BINARY_DIR}/cpuid_tool/cpuid_tool" "." --fix + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ + COMMENT "Fix tests" + VERBATIM)