@@ -149,6 +149,7 @@ function(gpu_cpp_library)
149
149
set (singleValueArgs
150
150
PREFIX # Desired name for the library target (and by extension, the prefix for naming intermediate targets)
151
151
TYPE # Target type, e.g., MODULE, OBJECT. See https://cmake.org/cmake/help/latest/command/add_library.html
152
+ DESTINATION # The install destination directory to place the build target into
152
153
)
153
154
set (multiValueArgs
154
155
CPU_SRCS # Sources for CPU-only build
@@ -182,30 +183,26 @@ function(gpu_cpp_library)
182
183
INCLUDE_DIRS ${args_INCLUDE_DIRS} )
183
184
set (lib_sources ${${args_PREFIX}_sources} )
184
185
185
- ############################################################################
186
- # Prepare Target Deps
187
- ############################################################################
188
-
189
- # Convert target dependency references into CMake target-dependent expressions
190
- # See https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#id34
191
- set (target_deps )
192
- foreach (dep ${args_DEPS} )
193
- list (APPEND target_deps "$<TARGET_OBJECTS:${dep} >" )
194
- endforeach ()
195
-
196
186
############################################################################
197
187
# Build the Library
198
188
############################################################################
199
189
190
+ # Set the build target name
200
191
set (lib_name ${args_PREFIX} )
201
- if (USE_ROCM )
202
- # Fetch the equivalent HIPified sources if available.
203
- # This presumes that `hipify()` has already been run.
204
- get_hipified_list ("${lib_sources} " lib_sources_hipified )
205
192
206
- # Set properties for the HIPified sources
207
- set_source_files_properties (${lib_sources_hipified}
208
- PROPERTIES HIP_SOURCE_PROPERTY_FORMAT 1 )
193
+ if (USE_ROCM )
194
+ if (lib_sources )
195
+ # Fetch the equivalent HIPified sources if available.
196
+ # This presumes that `hipify()` has already been run.
197
+ #
198
+ # This code is placed under an if-guard so that it won't fail for
199
+ # targets that have nothing to do with HIP, e.g. asmjit
200
+ get_hipified_list ("${lib_sources} " lib_sources_hipified )
201
+
202
+ # Set properties for the HIPified sources
203
+ set_source_files_properties (${lib_sources_hipified}
204
+ PROPERTIES HIP_SOURCE_PROPERTY_FORMAT 1 )
205
+ endif ()
209
206
210
207
# Set the include directories for HIP
211
208
hip_include_directories ("${args_INCLUDE_DIRS} " )
@@ -214,7 +211,6 @@ function(gpu_cpp_library)
214
211
hip_add_library (${lib_name} ${args_TYPE}
215
212
${lib_sources_hipified}
216
213
${args_OTHER_SRCS}
217
- ${target_deps}
218
214
${FBGEMM_HIP_HCC_LIBRARIES}
219
215
HIPCC_OPTIONS
220
216
${HIP_HCC_FLAGS} )
@@ -230,39 +226,61 @@ function(gpu_cpp_library)
230
226
# Create the CPU-only / CUDA library
231
227
add_library (${lib_name} ${args_TYPE}
232
228
${lib_sources}
233
- ${args_OTHER_SRCS}
234
- ${target_deps} )
229
+ ${args_OTHER_SRCS} )
235
230
endif ()
236
231
237
232
############################################################################
238
233
# Library Includes and Linking
239
234
############################################################################
240
235
241
- # Add PyTorch include/
236
+ # Add external include directories
242
237
target_include_directories (${lib_name} PRIVATE
243
238
${TORCH_INCLUDE_DIRS}
244
239
${NCCL_INCLUDE_DIRS} )
245
240
246
241
# Set additional target properties
247
242
set_target_properties (${lib_name} PROPERTIES
248
- # Remove `lib` prefix from the output artifact name, e.g. `libfoo.so` -> `foo.so`
243
+ # Remove `lib` prefix from the output artifact name, e.g.
244
+ # `libfoo.so` -> `foo.so`
249
245
PREFIX ""
250
246
# Enforce -fPIC for STATIC library option, since they are to be
251
247
# integrated into other libraries down the line
252
248
# https://stackoverflow.com/questions/3961446/why-does-gcc-not-implicitly-supply-the-fpic-flag-when-compiling-static-librarie
253
249
POSITION_INDEPENDENT_CODE ON )
254
250
255
- # Link to PyTorch
256
- target_link_libraries (${lib_name}
251
+ if (args_DEPS )
252
+ # Only set this if the library has dependencies that we also build,
253
+ # otherwise we will hit the following error:
254
+ # `No valid ELF RPATH or RUNPATH entry exists in the file`
255
+ set_target_properties (${lib_name} PROPERTIES
256
+ BUILD_WITH_INSTALL_RPATH ON
257
+ # Set the RPATH for the library to include $ORIGIN, so it can look
258
+ # into the same directory for dependency .SO files to load, e.g.
259
+ # fbgemm_gpu.so -> fbgemm.so, asmjit.so
260
+ #
261
+ # More info on RPATHS:
262
+ # https://amir.rachum.com/shared-libraries/#debugging-cheat-sheet
263
+ # https://stackoverflow.com/questions/43330165/how-to-link-a-shared-library-with-cmake-with-relative-path
264
+ # https://stackoverflow.com/questions/57915564/cmake-how-to-set-rpath-to-origin-with-cmake
265
+ # https://stackoverflow.com/questions/58360502/how-to-set-rpath-origin-in-cmake
266
+ INSTALL_RPATH "\$ ORIGIN" )
267
+ endif ()
268
+
269
+ # Collect external libraries for linking
270
+ set (library_dependencies
257
271
${TORCH_LIBRARIES}
258
272
${NCCL_LIBRARIES}
259
- ${CUDA_DRIVER_LIBRARIES} )
273
+ ${CUDA_DRIVER_LIBRARIES}
274
+ ${args_DEPS} )
260
275
261
- # Link to NVML if available
276
+ # Add NVML if available
262
277
if (NVML_LIB_PATH )
263
- target_link_libraries ( ${lib_name} ${NVML_LIB_PATH} )
278
+ list ( APPEND library_dependencies ${NVML_LIB_PATH} )
264
279
endif ()
265
280
281
+ # Link against the external libraries as needed
282
+ target_link_libraries (${lib_name} PRIVATE ${library_dependencies} )
283
+
266
284
# Silence compiler warnings (in asmjit)
267
285
target_compile_options (${lib_name} PRIVATE
268
286
-Wno-deprecated-anon-enum-enum-conversion
@@ -272,21 +290,36 @@ function(gpu_cpp_library)
272
290
# Post-Build Steps
273
291
############################################################################
274
292
293
+ # NOTE: Fix this in future PR
294
+
275
295
# Add a post-build step to remove errant RPATHs from the .SO
276
- add_custom_target (${lib_name} _postbuild ALL
277
- DEPENDS
278
- WORKING_DIRECTORY ${OUTPUT_DIR}
279
- COMMAND bash ${FBGEMM} /.github/scripts/fbgemm_gpu_postbuild.bash )
296
+ # add_custom_target(${lib_name}_postbuild ALL
297
+ # DEPENDS
298
+ # WORKING_DIRECTORY ${OUTPUT_DIR}
299
+ # COMMAND bash ${FBGEMM}/.github/scripts/fbgemm_gpu_postbuild.bash)
280
300
281
- # Set the post-build steps to run AFTER the build completes
282
- add_dependencies (${lib_name} _postbuild ${lib_name} )
301
+ # # Set the post-build steps to run AFTER the build completes
302
+ # add_dependencies(${lib_name}_postbuild ${lib_name})
283
303
284
304
############################################################################
285
305
# Set the Output Variable(s)
286
306
############################################################################
287
307
288
308
set (${args_PREFIX} ${lib_name} PARENT_SCOPE )
289
309
310
+ ############################################################################
311
+ # Add to Install Package
312
+ ############################################################################
313
+
314
+ if (args_DESTINATION )
315
+ install (TARGETS ${args_PREFIX}
316
+ DESTINATION ${args_DESTINATION} )
317
+ endif ()
318
+
319
+ ############################################################################
320
+ # Debug Summary
321
+ ############################################################################
322
+
290
323
BLOCK_PRINT (
291
324
"GPU CPP Library Target: ${args_PREFIX} (${args_TYPE} )"
292
325
" "
@@ -317,10 +350,13 @@ function(gpu_cpp_library)
317
350
"HIPified Source Files:"
318
351
"${lib_sources_hipified} "
319
352
" "
320
- "Target Dependencies:"
321
- "${target_deps } "
353
+ "Library Dependencies:"
354
+ "${library_dependencies } "
322
355
" "
323
356
"Output Library:"
324
357
"${lib_name} "
358
+ " "
359
+ "Destination Directory:"
360
+ "${args_DESTINATION} "
325
361
)
326
362
endfunction ()
0 commit comments