|
| 1 | +cmake_minimum_required(VERSION 3.31 FATAL_ERROR) |
| 2 | +# project(MyProject LANGUAGES CXX) |
| 3 | + |
| 4 | +include(CMakePrintHelpers) |
| 5 | + |
| 6 | +# set(CMAKE_VERBOSE_MAKEFILE ON) |
| 7 | + |
| 8 | +if(UNIX AND NOT APPLE) |
| 9 | + set(LINUX TRUE) |
| 10 | +endif() |
| 11 | + |
| 12 | +set(CMAKE_C_COMPILER "clang") |
| 13 | +set(CMAKE_CXX_COMPILER "clang++") |
| 14 | +set(CMAKE_CXX_STANDARD 17) |
| 15 | + |
| 16 | + |
| 17 | +set(PROJECT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}") |
| 18 | +set(PROJECT_BINARY_DIR "${CMAKE_BINARY_DIR}") |
| 19 | + |
| 20 | +find_package(chpl REQUIRED HINTS ${PROJECT_ROOT_DIR}/cmake/chapel) |
| 21 | +list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake") |
| 22 | +list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake/chapel") |
| 23 | + |
| 24 | +project(MyProject LANGUAGES CXX C CHPL) |
| 25 | + |
| 26 | + |
| 27 | +# For ExternalProject_Add |
| 28 | +include(FetchContent) |
| 29 | +include(ExternalProject) |
| 30 | + |
| 31 | +# ------------------------------------------------------------------------------ |
| 32 | +# 1) External project: build PyTorch (libtorch) from source as STATIC |
| 33 | +# ------------------------------------------------------------------------------ |
| 34 | + |
| 35 | +# Where to place PyTorch after installation |
| 36 | +set(PYTORCH_INSTALL_DIR "${CMAKE_BINARY_DIR}/pytorch-install") |
| 37 | + |
| 38 | +# ExternalProject_Add can fetch from Git, a local path, or a release tarball. |
| 39 | +# Here, for simplicity, we'll fetch from Git. In practice, you might want |
| 40 | +# a fixed commit or a release tarball for reproducible builds. |
| 41 | + |
| 42 | +ExternalProject_Add( |
| 43 | + pytorch |
| 44 | + GIT_REPOSITORY https://github.com/pytorch/pytorch.git |
| 45 | + GIT_TAG v2.6.0 # Example: specify a particular release |
| 46 | + UPDATE_COMMAND "" # Don’t auto-run 'git pull' |
| 47 | + PATCH_COMMAND "" # No custom patch step |
| 48 | + |
| 49 | + # We need all PyTorch submodules. By default, ExternalProject won't do submodule init. |
| 50 | + # So we can do that in a separate step if we want a full build. For a minimal CPU build, |
| 51 | + # you might not need them all, but let's be safe: |
| 52 | + STEP_TARGETS clone |
| 53 | + # After 'clone', run "git submodule update --init --recursive" |
| 54 | + # to fetch all submodules. |
| 55 | + # We can use a little trick with COMMAND. |
| 56 | +# PATCH_COMMAND "git submodule update --init --recursive" |
| 57 | + |
| 58 | + CMAKE_ARGS |
| 59 | + -DBUILD_SHARED_LIBS=OFF # Build static libraries |
| 60 | + -DBUILD_PYTHON=OFF # Don’t build Python bindings |
| 61 | + -DBUILD_TEST=OFF # Don’t build tests |
| 62 | + -DUSE_CUDA=OFF # Disable CUDA |
| 63 | + -DUSE_CUDNN=OFF # Disable cuDNN |
| 64 | + -DUSE_MKLDNN=OFF # Disable MKLDNN for simplicity |
| 65 | + -DCMAKE_BUILD_TYPE=Release |
| 66 | + -DCMAKE_INSTALL_PREFIX=${PYTORCH_INSTALL_DIR} |
| 67 | + |
| 68 | + INSTALL_DIR ${PYTORCH_INSTALL_DIR} # Where to install |
| 69 | +) |
| 70 | + |
| 71 | +# ------------------------------------------------------------------------------ |
| 72 | +# 2) Create an INTERFACE library to wrap the installed static libs |
| 73 | +# ------------------------------------------------------------------------------ |
| 74 | + |
| 75 | +# We'll create a dummy target that depends on 'pytorch' so that |
| 76 | +# building your own code will first build/install PyTorch. |
| 77 | + |
| 78 | +add_library(torch_interface INTERFACE) |
| 79 | + |
| 80 | +# Ensure that our 'torch_interface' target isn't used until PyTorch is built |
| 81 | +add_dependencies(torch_interface pytorch) |
| 82 | + |
| 83 | +# Include directories for PyTorch |
| 84 | +target_include_directories(torch_interface INTERFACE |
| 85 | + "${PYTORCH_INSTALL_DIR}/include" |
| 86 | + "${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include" |
| 87 | +) |
| 88 | + |
| 89 | +# Link the relevant static libraries. For a minimal CPU-only build, |
| 90 | +# you'll likely need at least these (names can vary by version). |
| 91 | +# The exact set can differ depending on which components got built. |
| 92 | + |
| 93 | +target_link_libraries(torch_interface INTERFACE |
| 94 | + "${PYTORCH_INSTALL_DIR}/lib/libtorch.a" |
| 95 | + "${PYTORCH_INSTALL_DIR}/lib/libtorch_cpu.a" |
| 96 | + "${PYTORCH_INSTALL_DIR}/lib/libc10.a" |
| 97 | + |
| 98 | + # System libraries often needed: |
| 99 | + pthread |
| 100 | + dl |
| 101 | + rt |
| 102 | +) |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +add_executable(CHPLTest lib/CHPLTest.chpl) |
| 107 | + |
| 108 | + |
| 109 | +add_library(torchbridge STATIC "${PROJECT_ROOT_DIR}/lib/bridge.cpp" "${PROJECT_ROOT_DIR}/include/bridge.h") |
| 110 | +add_dependencies(torchbridge torch_interface) |
| 111 | + |
| 112 | +target_include_directories(torchbridge PUBLIC |
| 113 | + "${PYTORCH_INSTALL_DIR}/include" |
| 114 | + "${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include" |
| 115 | + "${PROJECT_ROOT_DIR}/include" |
| 116 | +) |
| 117 | +target_link_directories(torchbridge PUBLIC |
| 118 | + "${PYTORCH_INSTALL_DIR}/lib" |
| 119 | +) |
| 120 | +target_link_libraries(torchbridge torch_interface) |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +# add_executable(TorchBridge lib/Bridge.chpl include/bridge.h) |
| 127 | + |
| 128 | +# target_link_options(TorchBridge |
| 129 | +# PRIVATE |
| 130 | +# "${PROJECT_ROOT_DIR}/include/bridge.h" |
| 131 | +# "-L${PROJECT_BINARY_DIR}" |
| 132 | +# -L. |
| 133 | +# "-ltorchbridge" |
| 134 | +# "-I${PROJECT_BINARY_DIR}" |
| 135 | +# "-L${PYTORCH_INSTALL_DIR}/lib" |
| 136 | +# "-I${PYTORCH_INSTALL_DIR}/include" |
| 137 | +# "-I${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include" |
| 138 | +# "-ltorch" |
| 139 | +# "-ltorch_cpu" |
| 140 | +# "-lcpuinfo" |
| 141 | +# "-lc10" |
| 142 | +# "-lpthread" |
| 143 | +# "-ldl" |
| 144 | + |
| 145 | +# ) |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +# ------------------------------------------------------------------------------ |
| 150 | +# 3) Build your own executable that uses torch_interface |
| 151 | +# ------------------------------------------------------------------------------ |
| 152 | + |
| 153 | +# add_executable(my_app src/main.cpp) |
| 154 | + |
| 155 | +# # Link your app against our interface library |
| 156 | +# target_link_libraries(my_app PRIVATE torch_interface) |
| 157 | + |
0 commit comments