[v0.10] Use CMake instead of Make, which will take care of pciutils automatically if it is not installed

This commit is contained in:
Dr-Noob
2021-09-04 14:05:16 +02:00
parent 039e7c350d
commit bdf9eb0079
4 changed files with 127 additions and 54 deletions

81
CMakeLists.txt Normal file
View File

@@ -0,0 +1,81 @@
cmake_minimum_required(VERSION 3.10)
include(CheckLanguage)
include(ExternalProject)
project(gpufetch CXX)
set(SRC_DIR "src")
set(COMMON_DIR "${SRC_DIR}/common")
set(CUDA_DIR "${SRC_DIR}/cuda")
if(NOT WIN32)
string(ASCII 27 Esc)
set(ColorReset "${Esc}[m")
set(ColorBold "${Esc}[1m")
set(Red "${Esc}[31m")
set(Green "${Esc}[32m")
set(BoldRed "${Esc}[1;31m")
set(BoldGreen "${Esc}[1;32m")
set(BoldYellow "${Esc}[1;33m")
endif()
check_language(CUDA)
if(CMAKE_CUDA_COMPILER)
enable_language(CUDA)
else()
message(FATAL_ERROR "${BoldRed}[ERROR]${ColorReset} Unable to find CUDA compiler. You may use -DCMAKE_CUDA_COMPILER and -DCMAKE_CUDA_COMPILER_TOOLKIT_ROOT if CUDA is installed but not detected by CMake")
endif()
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
find_package(PCIUTILS)
if(NOT ${PCIUTILS_FOUND})
message(STATUS "${BoldYellow}pciutils not found, downloading and building a local copy...${ColorReset}")
# Download and build pciutils
set(PCIUTILS_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/pciutils-install)
ExternalProject_Add(pciutils
GIT_REPOSITORY https://github.com/pciutils/pciutils
CONFIGURE_COMMAND ""
BUILD_COMMAND make SHARED=no
BUILD_IN_SOURCE true
INSTALL_COMMAND make PREFIX=${PCIUTILS_INSTALL_LOCATION} install-lib
)
include_directories(${PCIUTILS_INSTALL_LOCATION}/include)
link_directories(${PCIUTILS_INSTALL_LOCATION}/lib)
else()
include_directories(${PCIUTILS_INCLUDE_DIR})
link_libraries(${PCIUTILS_LIBRARIES})
endif()
set(SANITY_FLAGS "-Wfloat-equal -Wshadow -Wpointer-arith")
set(CMAKE_CXX_FLAGS "${SANITY_FLAGS} -Wall -Wextra -pedantic -fstack-protector-all -pedantic")
# https://en.wikipedia.org/w/index.php?title=CUDA&section=5#GPUs_supported
# https://raw.githubusercontent.com/PointCloudLibrary/pcl/master/cmake/pcl_find_cuda.cmake
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL "11.0")
set(CMAKE_CUDA_ARCHITECTURES 35 37 50 52 53 60 61 62 70 72 75 80 86)
elseif(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL "10.0")
set(CMAKE_CUDA_ARCHITECTURES 30 32 35 37 50 52 53 60 61 62 70 72 75)
elseif(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL "9.0")
set(CMAKE_CUDA_ARCHITECTURES 30 32 35 37 50 52 53 60 61 62 70 72)
elseif(${CMAKE_CUDA_COMPILER_VERSION} VERSION_EQUAL "8.0")
set(CMAKE_CUDA_ARCHITECTURES 20 21 30 32 35 37 50 52 53 60 61 62)
endif()
link_directories(${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/x86_64-linux/lib)
add_library(cuda_backend STATIC ${CUDA_DIR}/cuda.cpp ${CUDA_DIR}/uarch.cpp ${CUDA_DIR}/pci.cpp)
add_executable(gpufetch ${COMMON_DIR}/main.cpp ${COMMON_DIR}/args.cpp ${COMMON_DIR}/gpu.cpp ${COMMON_DIR}/pci.cpp ${COMMON_DIR}/global.cpp ${COMMON_DIR}/printer.cpp)
if(NOT ${PCIUTILS_FOUND})
add_dependencies(cuda_backend pciutils)
add_dependencies(gpufetch pciutils)
endif()
target_include_directories(cuda_backend PUBLIC ${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/samples/common/inc ${CMAKE_CUDA_COMPILER_TOOLKIT_ROOT}/targets/x86_64-linux/include)
target_link_libraries(cuda_backend cudart)
target_link_libraries(gpufetch cuda_backend pci z)
install(TARGETS gpufetch DESTINATION bin)

View File

@@ -1,54 +0,0 @@
CXX ?= g++
CUDA_PATH ?= /usr/local/cuda/
PREFIX ?= /usr
CXXFLAGS+=-Wall -Wextra -pedantic -fstack-protector-all -pedantic
SANITY_FLAGS=-Wfloat-equal -Wshadow -Wpointer-arith
SRC_COMMON=src/common/
SRC_CUDA=src/cuda/
COMMON_SRC = $(SRC_COMMON)main.cpp $(SRC_COMMON)gpu.cpp $(SRC_COMMON)args.cpp $(SRC_COMMON)global.cpp $(SRC_COMMON)printer.cpp $(SRC_COMMON)pci.cpp
COMMON_HDR = $(SRC_COMMON)ascii.hpp $(SRC_COMMON)gpu.hpp $(SRC_COMMON)args.hpp $(SRC_COMMON)global.hpp $(SRC_COMMON)printer.hpp $(SRC_COMMON)pci.hpp
CUDA_SRC = $(SRC_CUDA)cuda.cpp $(SRC_CUDA)uarch.cpp $(SRC_CUDA)pci.cpp
CUDA_HDR = $(SRC_CUDA)cuda.hpp $(SRC_CUDA)uarch.hpp $(SRC_CUDA)pci.hpp $(SRC_CUDA)chips.hpp
SOURCE += $(COMMON_SRC) $(CUDA_SRC)
HEADERS += $(COMMON_HDR) $(CUDA_HDR)
OUTPUT=gpufetch
CXXFLAGS+= -I pciutils/install/include -I $(CUDA_PATH)/samples/common/inc -I $(CUDA_PATH)/targets/x86_64-linux/include -L $(CUDA_PATH)/targets/x86_64-linux/lib -L pciutils/install/lib
LDFLAGS+=-lcudart -lpci
all: CXXFLAGS += -O3
all: $(OUTPUT)
debug: CXXFLAGS += -g -O0
debug: $(OUTPUT)
static: CXXFLAGS += -static -O3
static: $(OUTPUT)
strict: CXXFLAGS += -O3 -Werror -fsanitize=undefined -D_FORTIFY_SOURCE=2
strict: $(OUTPUT)
$(OUTPUT): Makefile $(SOURCE) $(HEADERS)
$(CXX) $(CXXFLAGS) $(SANITY_FLAGS) $(SOURCE) $(LDFLAGS) -o $(OUTPUT)
run: $(OUTPUT)
./$(OUTPUT)
clean:
@rm -f $(OUTPUT)
install: $(OUTPUT)
install -Dm755 "gpufetch" "$(DESTDIR)$(PREFIX)/bin/gpufetch"
install -Dm644 "LICENSE" "$(DESTDIR)$(PREFIX)/share/licenses/gpufetch-git/LICENSE"
install -Dm644 "gpufetch.1" "$(DESTDIR)$(PREFIX)/share/man/man1/gpufetch.1.gz"
uninstall:
rm -f "$(DESTDIR)$(PREFIX)/bin/gpufetch"
rm -f "$(DESTDIR)$(PREFIX)/share/licenses/gpufetch-git/LICENSE"
rm -f "$(DESTDIR)$(PREFIX)/share/man/man1/gpufetch.1.gz"

17
build.sh Executable file
View File

@@ -0,0 +1,17 @@
#!/bin/bash
# gpufetch build script
set -e
rm -rf build/ gpufetch
mkdir build/
cd build/
# In case you have CUDA installed but it is not detected,
# - set CMAKE_CUDA_COMPILER to your nvcc binary:
# - set CMAKE_CUDA_COMPILER_TOOLKIT_ROOT to the CUDA root dir
cmake -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DCMAKE_CUDA_COMPILER_TOOLKIT_ROOT=/usr/local/cuda/ ..
#cmake ..
make -j$(nproc)
cd -
ln -s build/gpufetch .

29
cmake/FindPCIUTILS.cmake Normal file
View File

@@ -0,0 +1,29 @@
# - Try to find the pciutils directory library
# Once done this will define
#
# PCIUTILS_FOUND - system has PCIUtils
# PCIUTILS_INCLUDE_DIR - the PCIUTILS include directory
# PCIUTILS_LIBRARIES - The libraries needed to use PCIUtils
if(PCIUTILS_INCLUDE_DIR AND PCIUTILS_LIBRARIES)
set(PCIUTILS_FIND_QUIETLY TRUE)
endif(PCIUTILS_INCLUDE_DIR AND PCIUTILS_LIBRARIES)
FIND_PATH(PCIUTILS_INCLUDE_DIR pci/pci.h)
FIND_LIBRARY(PCIUTILS_LIBRARY NAMES pci)
if(PCIUTILS_LIBRARY)
FIND_LIBRARY(RESOLV_LIBRARY NAMES resolv)
if(RESOLV_LIBRARY)
set(PCIUTILS_LIBRARIES ${PCIUTILS_LIBRARY} ${RESOLV_LIBRARY})
else(RESOLV_LIBRARY)
set(PCIUTILS_LIBRARIES ${PCIUTILS_LIBRARY})
endif(RESOLV_LIBRARY)
endif(PCIUTILS_LIBRARY)
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(PCIUTILS DEFAULT_MSG PCIUTILS_LIBRARIES PCIUTILS_INCLUDE_DIR)
MARK_AS_ADVANCED(PCIUTILS_INCLUDE_DIR PCIUTILS_LIBRARIES)