Skip to content

Commit 8cc819d

Browse files
authored
Add options to conditionally include Python (rusty1s#196)
* Add `WITH_PYTHON` to conditionally link to Python. * Only include `Python.h` when WITH_PYTHON is set. * Avoid including extensions.h as it includes Python.h. * Better way to include `getpid()`. * Define `WITH_PYTHON` when building with setup.py. * Only include Pyinit when building with Python. * Only include Pyinit when building with Python.
1 parent fe8c3ce commit 8cc819d

36 files changed

+91
-23
lines changed

CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(CMAKE_CXX_STANDARD 14)
44
set(TORCHSPARSE_VERSION 0.6.12)
55

66
option(WITH_CUDA "Enable CUDA support" OFF)
7+
option(WITH_PYTHON "Link to Python when building" ON)
78

89
if(WITH_CUDA)
910
enable_language(CUDA)
@@ -12,7 +13,10 @@ if(WITH_CUDA)
1213
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-relaxed-constexpr")
1314
endif()
1415

15-
find_package(Python3 COMPONENTS Development)
16+
if (WITH_PYTHON)
17+
add_definitions(-DWITH_PYTHON)
18+
find_package(Python3 COMPONENTS Development)
19+
endif()
1620
find_package(Torch REQUIRED)
1721

1822
file(GLOB HEADERS csrc/sparse.h)
@@ -22,7 +26,10 @@ if(WITH_CUDA)
2226
endif()
2327

2428
add_library(${PROJECT_NAME} SHARED ${OPERATOR_SOURCES})
25-
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES} Python3::Python)
29+
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES})
30+
if (WITH_PYTHON)
31+
target_link_libraries(${PROJECT_NAME} PRIVATE Python3::Python)
32+
endif()
2633
set_target_properties(${PROJECT_NAME} PROPERTIES EXPORT_NAME TorchSparse)
2734

2835
target_include_directories(${PROJECT_NAME} INTERFACE

csrc/convert.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#ifdef WITH_PYTHON
12
#include <Python.h>
3+
#endif
24
#include <torch/script.h>
35

46
#include "cpu/convert_cpu.h"
@@ -8,12 +10,14 @@
810
#endif
911

1012
#ifdef _WIN32
13+
#ifdef WITH_PYTHON
1114
#ifdef WITH_CUDA
1215
PyMODINIT_FUNC PyInit__convert_cuda(void) { return NULL; }
1316
#else
1417
PyMODINIT_FUNC PyInit__convert_cpu(void) { return NULL; }
1518
#endif
1619
#endif
20+
#endif
1721

1822
torch::Tensor ind2ptr(torch::Tensor ind, int64_t M) {
1923
if (ind.device().is_cuda()) {

csrc/cpu/convert_cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <torch/extension.h>
3+
#include "../extensions.h"
44

55
torch::Tensor ind2ptr_cpu(torch::Tensor ind, int64_t M);
66
torch::Tensor ptr2ind_cpu(torch::Tensor ptr, int64_t E);

csrc/cpu/diag_cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <torch/extension.h>
3+
#include "../extensions.h"
44

55
torch::Tensor non_diag_mask_cpu(torch::Tensor row, torch::Tensor col, int64_t M,
66
int64_t N, int64_t k);

csrc/cpu/ego_sample_cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <torch/extension.h>
3+
#include "../extensions.h"
44

55
std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor,
66
torch::Tensor, torch::Tensor>

csrc/cpu/hgt_sample_cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <torch/extension.h>
3+
#include "../extensions.h"
44

55
typedef std::string node_t;
66
typedef std::string rel_t;

csrc/cpu/metis_cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <torch/extension.h>
3+
#include "../extensions.h"
44

55
torch::Tensor partition_cpu(torch::Tensor rowptr, torch::Tensor col,
66
torch::optional<torch::Tensor> optional_value,

csrc/cpu/neighbor_sample_cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <torch/extension.h>
3+
#include "../extensions.h"
44

55
typedef std::string node_t;
66
typedef std::tuple<std::string, std::string, std::string> edge_t;

csrc/cpu/relabel_cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <torch/extension.h>
3+
#include "../extensions.h"
44

55
std::tuple<torch::Tensor, torch::Tensor> relabel_cpu(torch::Tensor col,
66
torch::Tensor idx);

csrc/cpu/rw_cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <torch/extension.h>
3+
#include "../extensions.h"
44

55
torch::Tensor random_walk_cpu(torch::Tensor rowptr, torch::Tensor col,
66
torch::Tensor start, int64_t walk_length);

0 commit comments

Comments
 (0)