Skip to content

Commit

Permalink
Options prefix add. Executables build option add: #794
Browse files Browse the repository at this point in the history
  • Loading branch information
Artiom N. committed Jul 28, 2024
1 parent bf83738 commit 94ce21c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 62 deletions.
28 changes: 23 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022, Intel Corporation

cmake_minimum_required(VERSION 3.5)

project(PCM)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# set(CMAKE_POSITION_INDEPENDENT_CODE ON)


option(PCM_NO_ASAN "Disable address sanitizer" OFF)
option(PCM_FUZZ "Enable fuzzing" OFF)
option(PCM_BUILD_EXECUTABLES "Build PCM utilities" ON)

if(MSVC)
option(PCM_NO_STATIC_MSVC_RUNTIME_LIBRARY "Disable using static runtime under MSVC" OFF)
endif()

foreach(opt NO_STATIC_MSVC_RUNTIME_LIBRARY;FUZZ;NO_ASAN)
if(${opt})
message(DEPRECATION "Option \"${opt}\" is deprecated and will be removed soon. Please use \"PCM_${opt}\"")
set(PCM_${opt} ${opt})
endif()
endforeach()

include(GNUInstallDirs)

Expand All @@ -19,6 +36,7 @@ foreach(file ${PCM_X_ARTIFACTS})
file(REMOVE ${file})
message(STATUS "Removing old artifact from current source directory : ${file}")
endforeach()

if(PCM_X_ARTIFACTS)
message(WARNING
" Old pcm utilities (.x) were indicated in build folder.\n"
Expand Down Expand Up @@ -82,12 +100,12 @@ if(UNIX) # APPLE, LINUX, FREE_BSD
elseif()
set (PCM_DYNAMIC "")
endif()
if(NO_ASAN)
if(PCM_NO_ASAN)
message(STATUS "AddressSanitizer is disabled")
set(PCM_ASAN "")
else()
message(STATUS "AddressSanitizer is enabled")
message(STATUS "To disable AddressSanitizer, use -DNO_ASAN=1 option")
message(STATUS "To disable AddressSanitizer, use -DPCM_NO_ASAN=1 option")
set(PCM_ASAN "-fsanitize=address")
endif()
set(PCM_HARDENING_FLAGS "-fPIE -fstack-protector -D_FORTIFY_SOURCE=2 -ftrapv ${PCM_ASAN} -fwrapv -fno-delete-null-pointer-checks -fno-strict-overflow -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer")
Expand Down Expand Up @@ -117,12 +135,12 @@ if(UNIX) # APPLE, LINUX, FREE_BSD

endif(UNIX)

if(FUZZ)
if(PCM_FUZZ)
set(FUZZER_OPTIONS "-fsanitize=fuzzer,address -fprofile-instr-generate -fcoverage-mapping")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FUZZER_OPTIONS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FUZZER_OPTIONS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FUZZER_OPTIONS}")
endif(FUZZ)
endif(PCM_FUZZ)

#######################
# Install
Expand Down
116 changes: 59 additions & 57 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ if(UNIX) # LINUX, FREE_BSD, APPLE
$<$<CONFIG:RelWithDebInfo>:PCM_SILENT>
)

if(NO_ASAN)
if(PCM_NO_ASAN)
set(PCM_DYNAMIC_ASAN "")
set(PCM_STATIC_ASAN "")
else()
Expand Down Expand Up @@ -102,7 +102,7 @@ if(MSVC)
file(GLOB WINDOWS_SOURCES winpmem/winpmem.cpp windows/stdafx.cpp freegetopt/getopt.cpp)
add_library(PCM_STATIC STATIC ${COMMON_SOURCES} ${WINDOWS_SOURCES})
target_compile_definitions(PCM_STATIC PRIVATE UNICODE _UNICODE _CONSOLE)
if (NO_STATIC_MSVC_RUNTIME_LIBRARY)
if(PCM_NO_STATIC_MSVC_RUNTIME_LIBRARY)
set(PCM_MSVC_RUNTIME_LIBRARY_OPTIONS "")
else()
set(PCM_MSVC_RUNTIME_LIBRARY_OPTIONS "/MT$<$<CONFIG:Debug>:d>")
Expand Down Expand Up @@ -175,66 +175,68 @@ endif(SIMDJSON_IS_APPLICABLE)
# End of SIMDJSON dependency section
#######################

foreach(PROJECT_NAME ${PROJECT_NAMES})
file(GLOB PROJECT_FILE ${PROJECT_NAME}.cpp)
set(LIBS PCM_STATIC)
if(PCM_BUILD_EXECUTABLES)
foreach(PROJECT_NAME ${PROJECT_NAMES})
file(GLOB PROJECT_FILE ${PROJECT_NAME}.cpp)
set(LIBS PCM_STATIC)

add_executable(${PROJECT_NAME} ${PROJECT_FILE})
add_executable(${PROJECT_NAME} ${PROJECT_FILE})

if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE "${PCM_MSVC_RUNTIME_LIBRARY_OPTIONS}")
endif(MSVC)
if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE "${PCM_MSVC_RUNTIME_LIBRARY_OPTIONS}")
endif(MSVC)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "${PCM_STATIC_ASAN}")
endif()

# specific file for pcm-raw project
if(${PROJECT_NAME} STREQUAL pcm-raw)
set(LIBS ${LIBS} PCM_SIMDJSON)
endif(${PROJECT_NAME} STREQUAL pcm-raw)

if(${PROJECT_NAME} STREQUAL pcm-sensor-server)
if(NO_SSL)
message(STATUS "SSL is disabled")
else()
message(STATUS "Compiling with SSL support, requires libssl-dev or openssl-devel or libopenssl-devel or libopenssl-dev package installed")
message(STATUS "To disable SSL support, use -DNO_SSL=1 option")
find_package(OpenSSL ${MINIMUM_OPENSSL_VERSION} QUIET)
if(OPENSSL_FOUND)
message(STATUS "OpenSSL version ${OPENSSL_VERSION} >= ${MINIMUM_OPENSSL_VERSION}, OpenSSL support enabled")
target_compile_options(${PROJECT_NAME} PRIVATE "-DUSE_SSL")
set(LIBS ${LIBS} OpenSSL::SSL OpenSSL::Crypto)
else()
message(STATUS "OpenSSL support has been disabled, the version is less than ${MINIMUM_OPENSSL_VERSION}")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "${PCM_STATIC_ASAN}")
endif()
endif()
file(READ pcm-sensor-server.service.in SENSOR_SERVICE_IN)
string(REPLACE "@@CMAKE_INSTALL_SBINDIR@@" "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SBINDIR}" SENSOR_SERVICE "${SENSOR_SERVICE_IN}")
file(WRITE "${CMAKE_BINARY_DIR}/pcm-sensor-server.service" "${SENSOR_SERVICE}")
file(GLOB PROJECT_FILE ${PROJECT_NAME}.cpp pcm-accel-common.h pcm-accel-common.cpp)
target_include_directories(pcm-sensor-server PUBLIC ${CMAKE_SOURCE_DIR})
if(LINUX_SYSTEMD)
install(FILES "${CMAKE_BINARY_DIR}/pcm-sensor-server.service" DESTINATION "${LINUX_SYSTEMD_UNITDIR}")
endif(LINUX_SYSTEMD)
endif(${PROJECT_NAME} STREQUAL pcm-sensor-server)

if(LINUX OR FREE_BSD)
set(LIBS ${LIBS} Threads::Threads)
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_SBINDIR})
endif(LINUX OR FREE_BSD)

if(APPLE)
set(LIBS ${LIBS} Threads::Threads PcmMsr)
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_SBINDIR})
endif(APPLE)

if(MSVC)
target_compile_definitions(${PROJECT_NAME} PRIVATE _UNICODE UNICODE _CONSOLE) # for all, except pcm-lib and pcm-service
endif(MSVC)

target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBS})
endforeach(PROJECT_NAME ${PROJECT_NAMES})
# specific file for pcm-raw project
if(${PROJECT_NAME} STREQUAL pcm-raw)
set(LIBS ${LIBS} PCM_SIMDJSON)
endif(${PROJECT_NAME} STREQUAL pcm-raw)

if(${PROJECT_NAME} STREQUAL pcm-sensor-server)
if(NO_SSL)
message(STATUS "SSL is disabled")
else()
message(STATUS "Compiling with SSL support, requires libssl-dev or openssl-devel or libopenssl-devel or libopenssl-dev package installed")
message(STATUS "To disable SSL support, use -DNO_SSL=1 option")
find_package(OpenSSL ${MINIMUM_OPENSSL_VERSION} QUIET)
if(OPENSSL_FOUND)
message(STATUS "OpenSSL version ${OPENSSL_VERSION} >= ${MINIMUM_OPENSSL_VERSION}, OpenSSL support enabled")
target_compile_options(${PROJECT_NAME} PRIVATE "-DUSE_SSL")
set(LIBS ${LIBS} OpenSSL::SSL OpenSSL::Crypto)
else()
message(STATUS "OpenSSL support has been disabled, the version is less than ${MINIMUM_OPENSSL_VERSION}")
endif()
endif()
file(READ pcm-sensor-server.service.in SENSOR_SERVICE_IN)
string(REPLACE "@@CMAKE_INSTALL_SBINDIR@@" "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SBINDIR}" SENSOR_SERVICE "${SENSOR_SERVICE_IN}")
file(WRITE "${CMAKE_BINARY_DIR}/pcm-sensor-server.service" "${SENSOR_SERVICE}")
file(GLOB PROJECT_FILE ${PROJECT_NAME}.cpp pcm-accel-common.h pcm-accel-common.cpp)
target_include_directories(pcm-sensor-server PUBLIC ${CMAKE_SOURCE_DIR})
if(LINUX_SYSTEMD)
install(FILES "${CMAKE_BINARY_DIR}/pcm-sensor-server.service" DESTINATION "${LINUX_SYSTEMD_UNITDIR}")
endif(LINUX_SYSTEMD)
endif(${PROJECT_NAME} STREQUAL pcm-sensor-server)

if(LINUX OR FREE_BSD)
set(LIBS ${LIBS} Threads::Threads)
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_SBINDIR})
endif(LINUX OR FREE_BSD)

if(APPLE)
set(LIBS ${LIBS} Threads::Threads PcmMsr)
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_SBINDIR})
endif(APPLE)

if(MSVC)
target_compile_definitions(${PROJECT_NAME} PRIVATE _UNICODE UNICODE _CONSOLE) # for all, except pcm-lib and pcm-service
endif(MSVC)

target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBS})
endforeach(PROJECT_NAME ${PROJECT_NAMES})
endif(PCM_BUILD_EXECUTABLES)

#######################
# Install
Expand Down

0 comments on commit 94ce21c

Please sign in to comment.