Skip to content

Commit fd9b9f6

Browse files
Update cmake for the use of local libtorch (#36)
* Update cmake for the use of local libtorch
1 parent 701cd4e commit fd9b9f6

File tree

1 file changed

+81
-38
lines changed

1 file changed

+81
-38
lines changed

CMakeLists.txt

Lines changed: 81 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ cmake_minimum_required(VERSION 3.14)
1616
project(
1717
cddp
1818
VERSION 0.1.0
19-
DESCRIPTION "CDDP: A C++ library for MPC"
19+
DESCRIPTION "CDDP: A C++ library for Trajectory Optimization and MPC"
2020
HOMEPAGE_URL "https://github.com/astomodynamics/cddp-cpp"
2121
)
2222

@@ -30,8 +30,11 @@ option(CDDP-CPP_BUILD_TESTS "Whether to build tests." ON)
3030
option(CDDP-CPP_GUROBI "Whether to use Gurobi solver." OFF)
3131
option(GUROBI_ROOT "Path to Gurobi installation" "")
3232
set(GUROBI_ROOT /home/tom/.local/lib/gurobi1103/linux64)
33-
set(CDDP-CPP_TORCH "Whether to use LibTorch." ON) # cannot be turned off
34-
option(CDDP-CPP_TORCH_GPU "Whether to use GPU." ON)
33+
34+
# LibTorch Configuration
35+
option(CDDP-CPP_TORCH "Whether to use LibTorch" ON)
36+
option(CDDP-CPP_TORCH_GPU "Whether to use GPU support in LibTorch" ON)
37+
set(LIBTORCH_DIR /home/tom/.local/lib/libtorch CACHE PATH "Path to local LibTorch installation") # FIXME: Change this to your local LibTorch installation directory
3538

3639
# Find packages
3740
find_package(Eigen3 REQUIRED)
@@ -68,50 +71,85 @@ endif()
6871

6972
# LibTorch
7073
if (CDDP-CPP_TORCH)
71-
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build/libtorch/share/cmake/Torch/TorchConfig.cmake)
72-
message(STATUS "Found LibTorch at ${CMAKE_CURRENT_SOURCE_DIR}/build/libtorch")
73-
else()
74-
message(STATUS "Downloading LibTorch...")
75-
# Download and extract LibTorch
76-
if (CDDP-CPP_TORCH_GPU)
74+
# Function to download and extract LibTorch
75+
function(download_libtorch cuda_support download_dir)
76+
if(cuda_support) # FIXME: Chenge the version of LibTorch to your desired version
7777
set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu124/libtorch-cxx11-abi-shared-with-deps-2.5.1%2Bcu124.zip")
78-
else()
78+
else()
7979
set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.5.1%2Bcpu.zip")
80-
endif()
81-
82-
# Set the download directory
83-
set(DOWLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build)
80+
endif()
8481

85-
# Create the download directory
86-
file(MAKE_DIRECTORY ${DOWLOAD_DIR})
82+
set(DOWNLOAD_PATH "${download_dir}/libtorch-shared-with-deps-latest.zip")
8783

88-
# Download the file
89-
file(DOWNLOAD ${LIBTORCH_URL} ${DOWLOAD_DIR}/libtorch-shared-with-deps-latest.zip
90-
STATUS DOWNLOAD_STATUS
84+
message(STATUS "Downloading LibTorch from ${LIBTORCH_URL}")
85+
file(DOWNLOAD "${LIBTORCH_URL}" "${DOWNLOAD_PATH}"
9186
SHOW_PROGRESS
92-
)
87+
STATUS DOWNLOAD_STATUS
88+
)
9389

94-
# Check if the download was successful
95-
list(GET DOWNLOAD_STATUS 0 DOWNLOAD_STATUS_CODE)
96-
if(NOT DOWNLOAD_STATUS_CODE EQUAL 0)
97-
message(FATAL_ERROR "Failed to download LibTorch.")
98-
endif()
99-
100-
# Extract the file
101-
execute_process(
102-
COMMAND ${CMAKE_COMMAND} -E tar xvf ${DOWLOAD_DIR}/libtorch-shared-with-deps-latest.zip
103-
WORKING_DIRECTORY ${DOWLOAD_DIR}
104-
)
105-
106-
# Remove the zip file
107-
file(REMOVE ${DOWLOAD_DIR}/libtorch-shared-with-deps-latest.zip)
90+
list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
91+
list(GET DOWNLOAD_STATUS 1 ERROR_MESSAGE)
92+
93+
if(NOT STATUS_CODE EQUAL 0)
94+
message(FATAL_ERROR "Failed to download LibTorch: ${ERROR_MESSAGE}")
95+
endif()
96+
97+
message(STATUS "Extracting LibTorch...")
98+
execute_process(
99+
COMMAND ${CMAKE_COMMAND} -E tar xf "${DOWNLOAD_PATH}"
100+
WORKING_DIRECTORY "${download_dir}"
101+
RESULT_VARIABLE EXTRACT_RESULT
102+
)
103+
104+
if(NOT EXTRACT_RESULT EQUAL 0)
105+
message(FATAL_ERROR "Failed to extract LibTorch")
108106
endif()
109107

110-
# Set the path to the LibTorch installation
111-
set(LIBTORCH_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build/libtorch)
108+
file(REMOVE "${DOWNLOAD_PATH}")
109+
endfunction()
112110

113-
find_package(Torch REQUIRED PATHS ${LIBTORCH_DIR} NO_DEFAULT_PATH)
114-
message(STATUS "Found LibTorch: ${TORCH_LIBRARIES}")
111+
# Try to find LibTorch in the following priority order:
112+
# 1. Local LibTorch directory (if specified)
113+
# 2. Previously installed LibTorch under build directory
114+
# 3. Download and install new copy
115+
set(TORCH_FOUND FALSE)
116+
117+
# Priority 1: Check local LibTorch directory
118+
if(LIBTORCH_DIR)
119+
if(EXISTS "${LIBTORCH_DIR}/share/cmake/Torch/TorchConfig.cmake")
120+
find_package(Torch REQUIRED PATHS "${LIBTORCH_DIR}" NO_DEFAULT_PATH)
121+
set(TORCH_FOUND TRUE)
122+
message(STATUS "Found LibTorch in local directory: ${LIBTORCH_DIR}")
123+
else()
124+
message(WARNING "Specified LIBTORCH_DIR does not contain a valid LibTorch installation")
125+
endif()
126+
endif()
127+
128+
# Priority 2: Check previously installed LibTorch under build directory
129+
if(NOT TORCH_FOUND)
130+
set(BUILD_LIBTORCH_DIR "${CMAKE_BINARY_DIR}/libtorch")
131+
if(EXISTS "${BUILD_LIBTORCH_DIR}/share/cmake/Torch/TorchConfig.cmake")
132+
find_package(Torch REQUIRED PATHS "${BUILD_LIBTORCH_DIR}" NO_DEFAULT_PATH)
133+
set(TORCH_FOUND TRUE)
134+
message(STATUS "Found LibTorch in build directory: ${BUILD_LIBTORCH_DIR}")
135+
endif()
136+
endif()
137+
138+
# Priority 3: Download and install new copy
139+
if(NOT TORCH_FOUND)
140+
message(STATUS "LibTorch not found in preferred locations. Downloading fresh copy...")
141+
download_libtorch(${CDDP-CPP_TORCH_GPU} "${CMAKE_BINARY_DIR}")
142+
find_package(Torch REQUIRED PATHS "${CMAKE_BINARY_DIR}/libtorch" NO_DEFAULT_PATH)
143+
message(STATUS "Successfully downloaded and installed LibTorch to: ${CMAKE_BINARY_DIR}/libtorch")
144+
endif()
145+
146+
# Set compilation flags for CUDA if GPU support is enabled
147+
if(CDDP-CPP_TORCH_GPU)
148+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
149+
endif()
150+
151+
# Export LibTorch variables for other parts of the build
152+
set(TORCH_INSTALL_PREFIX ${Torch_DIR}/../../../ CACHE PATH "LibTorch installation directory")
115153
endif()
116154

117155

@@ -161,6 +199,11 @@ target_include_directories(${PROJECT_NAME} PUBLIC
161199
${TORCH_INCLUDE_DIRS}
162200
)
163201

202+
# Ensure proper CUDA support if enabled
203+
if(CDDP-CPP_TORCH_GPU)
204+
set_property(TARGET ${PROJECT_NAME} PROPERTY CUDA_ARCHITECTURES native)
205+
endif()
206+
164207
# Gurobi
165208
if (CDDP-CPP_GUROBI)
166209
if (NOT GUROBI_ROOT)

0 commit comments

Comments
 (0)