Skip to content

Commit fcf1577

Browse files
committed
新增 CMake 配置文件以支持多个依赖项,重构模块扫描功能,更新安装路径设置,删除过时的 CMake 模块
1 parent 4d09888 commit fcf1577

26 files changed

+589
-175
lines changed

CMakeLists.txt

+14-174
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,10 @@ cmake_minimum_required(VERSION 3.20)
1010
project(Lithium VERSION 1.0.0 LANGUAGES C CXX)
1111

1212
# Set project options
13-
option(ENABLE_ASYNC "Enable Async Server Mode" ON)
14-
option(ENABLE_NATIVE_SERVER "Enable to use INDI native server" OFF)
15-
option(ENABLE_DEBUG "Enable Debug Mode" OFF)
16-
option(ENABLE_FASHHASH "Enable Using emhash8 as fast hash map" OFF)
17-
option(ENABLE_WEB_SERVER "Enable Web Server" ON)
18-
option(ENABLE_WEB_CLIENT "Enable Web Client" ON)
19-
20-
# Set compile definitions based on options
21-
if(ENABLE_ASYNC)
22-
add_compile_definitions(ENABLE_ASYNC_FLAG=1)
23-
endif()
24-
if(ENABLE_DEBUG)
25-
add_compile_definitions(ENABLE_DEBUG_FLAG=1)
26-
endif()
27-
if(ENABLE_NATIVE_SERVER)
28-
add_compile_definitions(ENABLE_NATIVE_SERVER_FLAG=1)
29-
endif()
30-
if(ENABLE_FASHHASH)
31-
add_compile_definitions(ENABLE_FASHHASH_FLAG=1)
32-
endif()
33-
if(ENABLE_WEB_SERVER)
34-
add_compile_definitions(ENABLE_WEB_SERVER_FLAG=1)
35-
endif()
36-
if(ENABLE_WEB_CLIENT)
37-
add_compile_definitions(ENABLE_WEB_CLIENT_FLAG=1)
38-
endif()
13+
include(cmake/options.cmake)
3914

4015
# Set policies
41-
if(POLICY CMP0003)
42-
cmake_policy(SET CMP0003 NEW)
43-
endif()
44-
if(POLICY CMP0043)
45-
cmake_policy(SET CMP0043 NEW)
46-
endif()
16+
include(cmake/policies.cmake)
4717

4818
# Set project directories
4919
set(Lithium_PROJECT_ROOT_DIR ${CMAKE_SOURCE_DIR})
@@ -55,15 +25,15 @@ set(lithium_task_dir ${lithium_src_dir}/task)
5525

5626
add_custom_target(CmakeAdditionalFiles
5727
SOURCES
58-
${lithium_src_dir}/../cmake_modules/compiler_options.cmake)
59-
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/")
60-
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake_modules/")
61-
include(cmake_modules/compiler_options.cmake)
28+
${lithium_src_dir}/../cmake/compiler_options.cmake)
29+
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
30+
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/")
31+
include(cmake/compiler_options.cmake)
6232

6333
# ------------------ CPM Begin ------------------
6434

6535
set(CPM_DOWNLOAD_VERSION 0.35.6)
66-
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake_modules/CPM.cmake")
36+
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM.cmake")
6737

6838
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
6939
message(STATUS "Downloading CPM.cmake")
@@ -137,133 +107,10 @@ include_directories(${CMAKE_SOURCE_DIR}/libs/oatpp-websocket/oatpp-websocket)
137107
include_directories(${CMAKE_SOURCE_DIR}/libs/oatpp-openssl/oatpp-openssl)
138108

139109
# Find packages
140-
find_package(OpenSSL REQUIRED)
141-
find_package(ZLIB REQUIRED)
142-
find_package(SQLite3 REQUIRED)
143-
find_package(fmt REQUIRED)
144-
find_package(Readline REQUIRED)
145-
find_package(pybind11 CONFIG REQUIRED)
110+
include(cmake/find_packages.cmake)
146111

147-
find_package(Python COMPONENTS Interpreter REQUIRED)
148-
include_directories(${pybind11_INCLUDE_DIRS} ${Python_INCLUDE_DIRS})
149-
150-
# Specify the path to requirements.txt
151-
set(REQUIREMENTS_FILE "${CMAKE_CURRENT_SOURCE_DIR}/requirements.txt")
152-
153-
# Define a function to check if a Python package is installed
154-
function(check_python_package package version)
155-
# Replace hyphens with underscores for the import statement
156-
string(REPLACE "-" "_" import_name ${package})
157-
158-
# Check if the package can be imported
159-
execute_process(
160-
COMMAND ${Python_EXECUTABLE} -c "import ${import_name}"
161-
RESULT_VARIABLE result
162-
)
163-
164-
if(NOT result EQUAL 0)
165-
set(result FALSE PARENT_SCOPE)
166-
return()
167-
endif()
168-
169-
# Get the installed package version
170-
execute_process(
171-
COMMAND ${Python_EXECUTABLE} -m pip show ${package}
172-
OUTPUT_VARIABLE package_info
173-
)
174-
175-
# Extract version information from the output
176-
string(FIND "${package_info}" "Version:" version_pos)
177-
178-
if(version_pos EQUAL -1)
179-
set(result FALSE PARENT_SCOPE)
180-
return() # Return false if version not found
181-
endif()
182-
183-
# Extract the version string
184-
string(SUBSTRING "${package_info}" ${version_pos} 1000 version_string)
185-
string(REGEX REPLACE "Version: ([^ ]+).*" "\\1" installed_version "${version_string}")
186-
187-
# Compare versions
188-
if("${installed_version}" VERSION_LESS "${version}")
189-
set(result FALSE PARENT_SCOPE) # Return false if installed version is less than required
190-
return()
191-
endif()
192-
193-
set(result TRUE PARENT_SCOPE)
194-
endfunction()
195-
196-
if (EXISTS "${CMAKE_BINARY_DIR}/check_marker.txt")
197-
message(STATUS "Check marker file found, skipping the checks.")
198-
else()
199-
# Create a virtual environment
200-
set(VENV_DIR "${CMAKE_BINARY_DIR}/venv")
201-
execute_process(
202-
COMMAND ${Python_EXECUTABLE} -m venv ${VENV_DIR}
203-
)
204-
205-
set(PYTHON_EXECUTABLE "${VENV_DIR}/bin/python")
206-
set(PIP_EXECUTABLE "${VENV_DIR}/bin/pip")
207-
208-
# Upgrade pip in the virtual environment
209-
execute_process(
210-
COMMAND ${PIP_EXECUTABLE} install --upgrade pip
211-
)
212-
213-
# Read the requirements.txt file and install missing packages
214-
file(READ ${REQUIREMENTS_FILE} requirements_content)
215-
216-
# Split the requirements file content into lines
217-
string(REPLACE "\n" ";" requirements_list "${requirements_content}")
218-
219-
# Check and install each package
220-
foreach(requirement ${requirements_list})
221-
# Skip empty lines
222-
string(STRIP ${requirement} trimmed_requirement)
223-
if(trimmed_requirement STREQUAL "")
224-
continue()
225-
endif()
226-
227-
# Get the package name and version (without the version number)
228-
if(${trimmed_requirement} MATCHES "==")
229-
string(REPLACE "==" ";" parts ${trimmed_requirement})
230-
elseif(${trimmed_requirement} MATCHES ">=")
231-
string(REPLACE ">=" ";" parts ${trimmed_requirement})
232-
else()
233-
message(WARNING "Could not parse requirement '${trimmed_requirement}'. Skipping...")
234-
continue()
235-
endif()
236-
237-
list(GET parts 0 package_name)
238-
list(GET parts 1 package_version)
239-
240-
# If the package name or version could not be parsed, output a warning and skip
241-
if(NOT package_name OR NOT package_version)
242-
message(WARNING "Could not parse requirement '${trimmed_requirement}'. Skipping...")
243-
continue()
244-
endif()
245-
246-
# Check if the package is installed
247-
message(STATUS "Checking if Python package '${package_name}' is installed...")
248-
check_python_package(${package_name} ${package_version})
249-
if(NOT result)
250-
message(STATUS "Package '${package_name}' is not installed or needs an upgrade. Installing...")
251-
execute_process(
252-
COMMAND ${PIP_EXECUTABLE} install ${trimmed_requirement}
253-
RESULT_VARIABLE install_result
254-
)
255-
if(NOT install_result EQUAL 0)
256-
message(FATAL_ERROR "Failed to install Python package '${package_name}'.")
257-
endif()
258-
else()
259-
message(STATUS "Package '${package_name}' is already installed with a suitable version.")
260-
endif()
261-
endforeach()
262-
execute_process(
263-
COMMAND ${CMAKE_COMMAND} -E touch "${CMAKE_BINARY_DIR}/check_marker.txt"
264-
RESULT_VARIABLE result
265-
)
266-
endif()
112+
# Configure Python environment
113+
include(cmake/python_environment.cmake)
267114

268115
# Configure config.h
269116
configure_file(${lithium_src_dir}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
@@ -273,13 +120,16 @@ set(BUILD_SHARED_LIBS ON)
273120
# Add subdirectories
274121
add_subdirectory(libs)
275122
add_subdirectory(modules)
123+
276124
add_subdirectory(${lithium_module_dir})
125+
277126
add_subdirectory(${lithium_src_dir}/config)
278127
add_subdirectory(${lithium_src_dir}/task)
279128
add_subdirectory(${lithium_src_dir}/server)
280129
add_subdirectory(${lithium_src_dir}/utils)
281130
add_subdirectory(${lithium_src_dir}/addon)
282131
add_subdirectory(${lithium_src_dir}/client)
132+
add_subdirectory(${lithium_src_dir}/target)
283133
add_subdirectory(${lithium_src_dir}/device)
284134
add_subdirectory(tests)
285135

@@ -399,17 +249,7 @@ target_compile_definitions(lithium_server PRIVATE LOGURU_DEBUG_LOGGING)
399249
set_target_properties(lithium_server PROPERTIES OUTPUT_NAME lithium_server)
400250

401251
# Set install paths
402-
if(UNIX AND NOT APPLE)
403-
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
404-
set(CMAKE_INSTALL_PREFIX /usr CACHE PATH "Lithium install path" FORCE)
405-
endif()
406-
endif()
407-
408-
if(WIN32)
409-
set(CMAKE_INSTALL_PREFIX "C:/Program Files/LithiumServer")
410-
elseif(LINUX)
411-
set(CMAKE_INSTALL_PREFIX "/usr/lithium")
412-
endif()
252+
include(cmake/install_paths.cmake)
413253

414254
# Enable folder grouping in IDEs
415255
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

cmake/find_packages.cmake

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
find_package(OpenSSL REQUIRED)
2+
find_package(ZLIB REQUIRED)
3+
find_package(SQLite3 REQUIRED)
4+
find_package(fmt REQUIRED)
5+
find_package(Readline REQUIRED)
6+
find_package(pybind11 CONFIG REQUIRED)
7+
find_package(Python COMPONENTS Interpreter REQUIRED)
8+
include_directories(${pybind11_INCLUDE_DIRS} ${Python_INCLUDE_DIRS})

cmake/install_paths.cmake

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if(UNIX AND NOT APPLE)
2+
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
3+
set(CMAKE_INSTALL_PREFIX /usr CACHE PATH "Lithium install path" FORCE)
4+
endif()
5+
endif()
6+
7+
if(WIN32)
8+
set(CMAKE_INSTALL_PREFIX "C:/Program Files/LithiumServer")
9+
elseif(LINUX)
10+
set(CMAKE_INSTALL_PREFIX "/usr/lithium")
11+
endif()

cmake/options.cmake

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
option(ENABLE_ASYNC "Enable Async Server Mode" ON)
2+
option(ENABLE_NATIVE_SERVER "Enable to use INDI native server" OFF)
3+
option(ENABLE_DEBUG "Enable Debug Mode" OFF)
4+
option(ENABLE_FASHHASH "Enable Using emhash8 as fast hash map" OFF)
5+
option(ENABLE_WEB_SERVER "Enable Web Server" ON)
6+
option(ENABLE_WEB_CLIENT "Enable Web Client" ON)
7+
8+
if(ENABLE_ASYNC)
9+
add_compile_definitions(ENABLE_ASYNC_FLAG=1)
10+
endif()
11+
if(ENABLE_DEBUG)
12+
add_compile_definitions(ENABLE_DEBUG_FLAG=1)
13+
endif()
14+
if(ENABLE_NATIVE_SERVER)
15+
add_compile_definitions(ENABLE_NATIVE_SERVER_FLAG=1)
16+
endif()
17+
if(ENABLE_FASHHASH)
18+
add_compile_definitions(ENABLE_FASHHASH_FLAG=1)
19+
endif()
20+
if(ENABLE_WEB_SERVER)
21+
add_compile_definitions(ENABLE_WEB_SERVER_FLAG=1)
22+
endif()
23+
if(ENABLE_WEB_CLIENT)
24+
add_compile_definitions(ENABLE_WEB_CLIENT_FLAG=1)
25+
endif()

cmake/policies.cmake

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if(POLICY CMP0003)
2+
cmake_policy(SET CMP0003 NEW)
3+
endif()
4+
if(POLICY CMP0043)
5+
cmake_policy(SET CMP0043 NEW)
6+
endif()

0 commit comments

Comments
 (0)