Skip to content

Commit 0002a03

Browse files
committed
update CMakeLists.txt
1 parent 9bc7626 commit 0002a03

1 file changed

Lines changed: 77 additions & 162 deletions

File tree

CMakeLists.txt

Lines changed: 77 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ set(CMAKE_C_STANDARD 99)
2424
set(CMAKE_CXX_STANDARD 17)
2525
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2626
set(CMAKE_CXX_EXTENSIONS OFF)
27-
# Set default build type to Release if not specified (Optimized builds)
27+
28+
# Set default build type to Release if not specified
2829
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
2930
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
3031
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
@@ -34,27 +35,25 @@ if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
3435
set(CMAKE_CUDA_ARCHITECTURES 60 70 75 80 86 89 90)
3536
endif()
3637

37-
# Global Compile flags (corresponding to CFLAGS/NVCCFLAGS)
38+
# -----------------------------------------------------------------------------
39+
# [ELEGANT DESIGN] Target-based Compile Flags
40+
# -----------------------------------------------------------------------------
41+
add_library(cupdlpx_compile_flags INTERFACE)
42+
3843
if(MSVC)
39-
add_compile_options(
40-
$<$<COMPILE_LANGUAGE:C,CXX>:/O2>
41-
$<$<COMPILE_LANGUAGE:C,CXX>:/W4>
42-
$<$<COMPILE_LANGUAGE:C,CXX>:/Zi>
43-
)
44+
target_compile_options(cupdlpx_compile_flags INTERFACE /O2 /W4 /Zi)
45+
target_compile_definitions(cupdlpx_compile_flags INTERFACE
46+
_CRT_SECURE_NO_WARNINGS
47+
strtok_r=strtok_s
48+
)
4449
else()
45-
add_compile_options(-O3 -Wall -Wextra -g)
46-
endif()
47-
48-
if (NOT WIN32)
49-
add_compile_options(-fPIC)
50-
endif()
51-
52-
# Windows compatibility
53-
if (WIN32)
54-
add_definitions(-Dstrtok_r=strtok_s)
50+
target_compile_options(cupdlpx_compile_flags INTERFACE -O3 -Wall -Wextra -g)
51+
if(NOT WIN32)
52+
target_compile_options(cupdlpx_compile_flags INTERFACE -fPIC)
53+
endif()
5554
endif()
5655

57-
# CUDA standards and RDC (Relocatable Device Code)
56+
# CUDA standards and RDC
5857
set(CMAKE_CUDA_STANDARD 17)
5958
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
6059

@@ -66,130 +65,107 @@ include(CMakeDependentOption)
6665
option(CUPDLPX_BUILD_STATIC_LIB "Build the cuPDLPx static library" ON)
6766
option(CUPDLPX_BUILD_SHARED_LIB "Build the cuPDLPx shared library" ON)
6867

69-
# format: cmake_dependent_option(OPTION "docstring" DEFAULT_VALUE "DEPENDENCY_VARIABLE" FORCE_OFF_VALUE)
70-
cmake_dependent_option(CUPDLPX_BUILD_PYTHON "Build the cuPDLPx Python bindings" OFF
71-
"CUPDLPX_BUILD_STATIC_LIB" OFF)
72-
73-
cmake_dependent_option(CUPDLPX_BUILD_CLI "Build the cuPDLPx command-line executable" ON
74-
"CUPDLPX_BUILD_STATIC_LIB" OFF)
75-
76-
cmake_dependent_option(CUPDLPX_BUILD_TESTS "Build the cuPDLPx test suite" OFF
77-
"CUPDLPX_BUILD_STATIC_LIB" OFF)
68+
cmake_dependent_option(CUPDLPX_BUILD_PYTHON "Build the cuPDLPx Python bindings" OFF "CUPDLPX_BUILD_STATIC_LIB" OFF)
69+
cmake_dependent_option(CUPDLPX_BUILD_CLI "Build the cuPDLPx command-line executable" ON "CUPDLPX_BUILD_STATIC_LIB" OFF)
70+
cmake_dependent_option(CUPDLPX_BUILD_TESTS "Build the cuPDLPx test suite" OFF "CUPDLPX_BUILD_STATIC_LIB" OFF)
7871

7972
# -----------------------------------------------------------------------------
8073
# FIND DEPENDENCIES
8174
# -----------------------------------------------------------------------------
82-
# Core dependencies (required for Julia/Yggdrasil and Python)
8375
find_package(CUDAToolkit REQUIRED)
84-
8576
include(FetchContent)
86-
# 1. Try to find ZLIB in the system first (Standard for Linux/Mac)
87-
find_package(ZLIB QUIET)
88-
89-
if(ZLIB_FOUND)
90-
message(STATUS "Found System ZLIB: ${ZLIB_LIBRARIES}")
91-
else()
92-
# 2. If not found (Common on Windows), fetch from source
93-
message(STATUS "ZLIB not found in system. Fetching from source...")
9477

78+
# 1. ZLIB Configuration
79+
find_package(ZLIB QUIET)
80+
if(NOT ZLIB_FOUND)
81+
message(STATUS "ZLIB not found. Fetching from source...")
9582
FetchContent_Declare(
9683
zlib
9784
GIT_REPOSITORY https://github.com/madler/zlib.git
9885
GIT_TAG v1.3
9986
)
10087
FetchContent_MakeAvailable(zlib)
101-
102-
# 3. Create alias ZLIB::ZLIB to match your CORE_LINK_LIBS usage
10388
if(NOT TARGET ZLIB::ZLIB)
10489
if(TARGET zlibstatic)
105-
# Prefer zlibstatic to ensure static linking (avoids DLL issues on Windows)
10690
add_library(ZLIB::ZLIB ALIAS zlibstatic)
107-
# Expose include directories so zlib.h can be found
10891
target_include_directories(zlibstatic INTERFACE ${zlib_SOURCE_DIR} ${zlib_BINARY_DIR})
10992
else()
110-
# Fallback if the target is simply named 'zlib'
11193
add_library(ZLIB::ZLIB ALIAS zlib)
11294
target_include_directories(zlib INTERFACE ${zlib_SOURCE_DIR} ${zlib_BINARY_DIR})
11395
endif()
11496
endif()
11597
endif()
11698

117-
if (CUPDLPX_BUILD_PYTHON)
118-
# Dependencies required only for Python bindings
119-
find_package(pybind11 CONFIG REQUIRED)
120-
find_package(Python3 COMPONENTS Interpreter REQUIRED) # For versioning script and pybind11
121-
endif()
122-
99+
# 2. PSLP Configuration
123100
set(PSLP_VERSION_TAG "v0.0.6")
124-
125101
FetchContent_Declare(
126102
pslp
127103
GIT_REPOSITORY https://github.com/dance858/PSLP.git
128104
GIT_TAG ${PSLP_VERSION_TAG}
129105
)
130-
131106
FetchContent_MakeAvailable(pslp)
132-
include_directories(${pslp_SOURCE_DIR}/PSLP)
133-
add_compile_definitions(PSLP_VERSION=\"${PSLP_VERSION_TAG}\")
107+
108+
# --- PSLP FIX BLOCK (Cross-Platform & Header fix) ---
109+
if(TARGET PSLP)
110+
# Fix Linux absolute path error
111+
set_target_properties(PSLP PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "")
112+
113+
# Expose PSLP's internal headers that cuPDLPx needs
114+
target_include_directories(PSLP INTERFACE
115+
$<BUILD_INTERFACE:${pslp_SOURCE_DIR}/include>
116+
$<BUILD_INTERFACE:${pslp_SOURCE_DIR}/include/PSLP>
117+
)
118+
119+
# Defensive flag filtering for MSVC
120+
if(MSVC)
121+
get_target_property(PSLP_OPTS PSLP COMPILE_OPTIONS)
122+
if(PSLP_OPTS)
123+
list(REMOVE_ITEM PSLP_OPTS "-Wall" "-Wextra" "-Wpedantic" "-Werror")
124+
set_target_properties(PSLP PROPERTIES COMPILE_OPTIONS "${PSLP_OPTS}")
125+
endif()
126+
endif()
127+
endif()
128+
129+
target_compile_definitions(cupdlpx_compile_flags INTERFACE PSLP_VERSION="${PSLP_VERSION_TAG}")
134130

135131
# -----------------------------------------------------------------------------
136-
# SOURCE DISCOVERY & TARGET DEFINITION
132+
# TARGET DEFINITIONS
137133
# -----------------------------------------------------------------------------
138-
# Using file(GLOB) for convenience, but explicit lists are recommended for robust builds
139-
file(GLOB C_SOURCES
140-
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.c"
141-
)
142-
file(GLOB CU_SOURCES
143-
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cu"
144-
)
145-
# Exclude cli.c from library builds
134+
file(GLOB C_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c")
135+
file(GLOB CU_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cu")
146136
list(REMOVE_ITEM C_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/cli.c")
147137

148-
# Set common include directories for the core libraries
149138
set(CORE_INCLUDE_DIRS
150-
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include # Public API headers
151-
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/internal # Internal implementation headers
139+
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
140+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/internal
152141
)
153142

154-
# Set common link libraries
155-
set(CORE_LINK_LIBS PUBLIC
156-
CUDA::cudart
157-
CUDA::cublas
158-
CUDA::cusparse
159-
ZLIB::ZLIB
160-
PSLP
143+
set(CORE_LINK_LIBS
144+
PUBLIC cupdlpx_compile_flags
145+
PUBLIC CUDA::cudart
146+
PUBLIC CUDA::cublas
147+
PUBLIC CUDA::cusparse
148+
PUBLIC ZLIB::ZLIB
149+
PUBLIC PSLP
161150
)
162151

163-
# -----------------------------------------------------------------------------
164-
# 1. Core STATIC Library (cupdlpx_core)
165-
# -----------------------------------------------------------------------------
152+
# 1. Core STATIC Library
166153
if(CUPDLPX_BUILD_STATIC_LIB)
167-
add_library(cupdlpx_core STATIC
168-
${C_SOURCES}
169-
${CU_SOURCES}
170-
)
154+
add_library(cupdlpx_core STATIC ${C_SOURCES} ${CU_SOURCES})
171155
target_include_directories(cupdlpx_core ${CORE_INCLUDE_DIRS})
172156
target_link_libraries(cupdlpx_core ${CORE_LINK_LIBS})
173-
174157
set_target_properties(cupdlpx_core PROPERTIES
175158
POSITION_INDEPENDENT_CODE ON
176159
CUDA_SEPARABLE_COMPILATION ON
177160
CUDA_RESOLVE_DEVICE_SYMBOLS ON
178161
)
179162
endif()
180163

181-
# -----------------------------------------------------------------------------
182-
# 2. Shared Library (libcupdlpx.so)
183-
# -----------------------------------------------------------------------------
164+
# 2. Shared Library
184165
if(CUPDLPX_BUILD_SHARED_LIB)
185-
add_library(cupdlpx_shared SHARED
186-
${C_SOURCES}
187-
${CU_SOURCES}
188-
)
166+
add_library(cupdlpx_shared SHARED ${C_SOURCES} ${CU_SOURCES})
189167
target_include_directories(cupdlpx_shared ${CORE_INCLUDE_DIRS})
190168
target_link_libraries(cupdlpx_shared ${CORE_LINK_LIBS})
191-
192-
# Shared library must resolve device symbols as it is a final link point
193169
set_target_properties(cupdlpx_shared PROPERTIES
194170
OUTPUT_NAME "cupdlpx"
195171
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
@@ -198,115 +174,54 @@ if(CUPDLPX_BUILD_SHARED_LIB)
198174
)
199175
endif()
200176

201-
# -----------------------------------------------------------------------------
202-
# 3. CLI Executable (cupdlpx)
203-
# -----------------------------------------------------------------------------
177+
# 3. CLI Executable
204178
if(CUPDLPX_BUILD_CLI)
205-
if(NOT TARGET cupdlpx_core)
206-
message(FATAL_ERROR "CUPDLPX_BUILD_CLI=ON requires CUPDLPX_BUILD_STATIC_LIB=ON.")
207-
endif()
208-
209179
add_executable(cupdlpx_cli src/cli.c)
210-
211-
target_include_directories(cupdlpx_cli PRIVATE
212-
${CMAKE_CURRENT_SOURCE_DIR}/include
213-
${CMAKE_CURRENT_SOURCE_DIR}/internal
214-
)
215-
216-
# Link CLI to the static core library
180+
target_include_directories(cupdlpx_cli PRIVATE include internal)
217181
target_link_libraries(cupdlpx_cli PRIVATE cupdlpx_core)
218-
219-
# CLI is a final executable, it must resolve device symbols
220182
set_target_properties(cupdlpx_cli PROPERTIES
221183
OUTPUT_NAME "cupdlpx"
222184
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
223185
CUDA_RESOLVE_DEVICE_SYMBOLS ON
224186
)
225187
endif()
226188

227-
# -----------------------------------------------------------------------------
228-
# 4. Tests (CTest Integration)
229-
# -----------------------------------------------------------------------------
189+
# 4. Tests
230190
if(CUPDLPX_BUILD_TESTS)
231-
if(NOT TARGET cupdlpx_core)
232-
message(FATAL_ERROR "CUPDLPX_BUILD_TESTS=ON requires CUPDLPX_BUILD_STATIC_LIB=ON.")
233-
endif()
234-
235191
enable_testing()
236-
file(GLOB TEST_SOURCES
237-
"${CMAKE_CURRENT_SOURCE_DIR}/test/*.c"
238-
"${CMAKE_CURRENT_SOURCE_DIR}/test/*.cu"
239-
)
240-
192+
file(GLOB TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/test/*.c" "${CMAKE_CURRENT_SOURCE_DIR}/test/*.cu")
241193
foreach(TEST_SRC ${TEST_SOURCES})
242194
get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE)
243-
244195
add_executable(${TEST_NAME} ${TEST_SRC})
245-
246-
# Link tests to the core static library
247196
target_link_libraries(${TEST_NAME} PRIVATE cupdlpx_core)
248-
249-
# Set up test includes
250-
target_include_directories(${TEST_NAME}
251-
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
252-
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/internal
253-
)
254-
255-
# Tests are final executables, they must resolve device symbols
197+
target_include_directories(${TEST_NAME} PRIVATE include internal)
256198
set_target_properties(${TEST_NAME} PROPERTIES
257199
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests"
258200
CUDA_RESOLVE_DEVICE_SYMBOLS ON
259201
)
260-
261-
# Register with CTest
262202
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
263-
264203
endforeach()
265204
endif()
266205

267-
# -----------------------------------------------------------------------------
268-
# 5. Python Bindings (Conditional)
269-
# -----------------------------------------------------------------------------
206+
# 5. Python Bindings
270207
if (CUPDLPX_BUILD_PYTHON)
271-
if(NOT TARGET cupdlpx_core)
272-
message(FATAL_ERROR "CUPDLPX_BUILD_PYTHON=ON requires CUPDLPX_BUILD_STATIC_LIB=ON.")
273-
endif()
274-
208+
find_package(pybind11 CONFIG REQUIRED)
209+
find_package(Python3 COMPONENTS Interpreter REQUIRED)
275210
add_subdirectory(python_bindings)
276211
endif()
277212

278-
# -----------------------------------------------------------------------------
279213
# 6. Install Targets
280-
# -----------------------------------------------------------------------------
281-
282214
if (CUPDLPX_BUILD_PYTHON)
283-
install(DIRECTORY include/
284-
DESTINATION include/
285-
FILES_MATCHING PATTERN "*.h"
286-
)
287-
215+
install(DIRECTORY include/ DESTINATION include/ FILES_MATCHING PATTERN "*.h")
288216
else()
289217
if(TARGET cupdlpx_core)
290-
install(TARGETS cupdlpx_core
291-
ARCHIVE DESTINATION lib
292-
)
218+
install(TARGETS cupdlpx_core ARCHIVE DESTINATION lib)
293219
endif()
294-
295220
if(TARGET cupdlpx_shared)
296-
install(TARGETS cupdlpx_shared
297-
LIBRARY DESTINATION lib
298-
RUNTIME DESTINATION bin # 'bin' for DLLs on Windows, 'lib' for .so on Linux
299-
)
221+
install(TARGETS cupdlpx_shared LIBRARY DESTINATION lib RUNTIME DESTINATION bin)
300222
endif()
301-
302223
if(TARGET cupdlpx_cli)
303-
install(TARGETS cupdlpx_cli
304-
RUNTIME DESTINATION bin
305-
)
224+
install(TARGETS cupdlpx_cli RUNTIME DESTINATION bin)
306225
endif()
307-
308-
install(DIRECTORY include/
309-
DESTINATION include/
310-
FILES_MATCHING PATTERN "*.h"
311-
)
226+
install(DIRECTORY include/ DESTINATION include/ FILES_MATCHING PATTERN "*.h")
312227
endif()

0 commit comments

Comments
 (0)