-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
147 lines (124 loc) · 4.75 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Set minimum required version of CMake
cmake_minimum_required(VERSION 3.28)
# Set project name and language
project(applefetch LANGUAGES CXX)
# Set C++ standard to C++17, disable compiler-specific extensions and shared libraries
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Enable Link Time Optimization (if supported)
include(CheckIPOSupported)
check_ipo_supported(RESULT lto_supported OUTPUT lto_error)
if(lto_supported)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
message(STATUS "[INFO] Link Time Optimization (LTO) is enabled.")
else()
message(WARNING "[WARNING] Link Time Optimization (LTO) not supported: ${lto_error}")
endif()
# Project options
option(BUILD_TESTS "Build tests" OFF)
option(ENABLE_COMPILE_FLAGS "Enable compile flags" ON)
# Enforce out-of-source builds
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(FATAL_ERROR "[ERROR] In-source builds are not allowed. Use a separate build directory.")
endif()
# Set default build type to Release
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "[INFO] Defaulting to 'Release' build type.")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the build type." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
# Include external CMake modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
# Include custom modules
include(Flags)
include(External)
# Optionally enable global ccache
find_program(CCACHE ccache)
if(CCACHE)
message(STATUS "[INFO] Ccache enabled for faster builds.")
set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE})
else()
message(WARNING "[WARNING] Ccache not found. Consider installing it to speed up rebuilds.")
endif()
# Get the project version using Git tags if available, else default to "unknown"
set(PROJECT_VERSION "unknown")
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
find_package(Git REQUIRED)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --always
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(GIT_TAG)
set(PROJECT_VERSION ${GIT_TAG})
message(STATUS "[INFO] Project version set to ${PROJECT_VERSION} from Git.")
else()
message(WARNING "[WARNING] Failed to retrieve Git tag. Using fallback version: ${PROJECT_VERSION}.")
endif()
else()
message(WARNING "[WARNING] Git repository not found. Using fallback version: ${PROJECT_VERSION}.")
endif()
# Generate the version header using the inferred Git tag version
configure_file(${CMAKE_SOURCE_DIR}/src/version.hpp.in ${CMAKE_BINARY_DIR}/generated/version.hpp @ONLY)
include_directories(${CMAKE_BINARY_DIR}/generated)
# Add the main library target
add_library(${PROJECT_NAME}-lib STATIC
# find . -name "*.cpp"
src/app.cpp
src/core/args.cpp
src/core/env.cpp
src/core/shell.cpp
src/main.cpp
src/modules/cpu.cpp
src/modules/display.cpp
src/modules/host.cpp
src/modules/memory.cpp
)
# Include headers relatively to the src directory
target_include_directories(${PROJECT_NAME}-lib PUBLIC src)
# Apply public compile flags to the library target if enabled
if(ENABLE_COMPILE_FLAGS)
apply_compile_flags(${PROJECT_NAME}-lib)
endif()
# Fetch and link external dependencies to the library target
fetch_and_link_external_dependencies(${PROJECT_NAME}-lib)
# Add the main executable and link the library
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}-lib)
# Add install target
include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
# Add tests if enabled
if(BUILD_TESTS)
# Enable testing with CTest
enable_testing()
# Add test executable
add_executable(tests tests/test_all.cpp)
target_link_libraries(tests PRIVATE ${PROJECT_NAME}-lib)
# Define a function to register tests with CTest
function(register_test test_name)
add_test(NAME ${test_name} COMMAND tests ${test_name})
endfunction()
# Register tests using the function
register_test(test_args::none)
register_test(test_args::help)
register_test(test_args::version)
register_test(test_args::invalid)
register_test(test_host::get_version)
register_test(test_host::get_architecture)
register_test(test_host::get_model_identifier)
register_test(test_host::get_uptime)
register_test(test_host::get_packages)
register_test(test_host::get_shell)
register_test(test_display::get_resolution)
register_test(test_display::get_refresh_rate)
register_test(test_cpu::get_cpu_model)
register_test(test_memory::get_memory_usage)
message(STATUS "[INFO] Tests enabled.")
endif()
# Print the build type
message(STATUS "[INFO] Build type: ${CMAKE_BUILD_TYPE}.")