Commit c02faf04 authored by moto's avatar moto Committed by Facebook GitHub Bot
Browse files

Introduce helper function to define extension (#2077)

Summary:
Similar to https://github.com/pytorch/audio/issues/2040 this commit refactor the part of the CMakeLists.txt
which defines extension module so that second extension can be added
easily.

Pull Request resolved: https://github.com/pytorch/audio/pull/2077

Reviewed By: carolineechen

Differential Revision: D33189998

Pulled By: mthrok

fbshipit-source-id: dc562ce5360332479a7493c21a2930c6fcc6be84
parent dba00177
...@@ -136,10 +136,46 @@ endif() ...@@ -136,10 +136,46 @@ endif()
# _torchaudio.so # _torchaudio.so
################################################################################ ################################################################################
if (BUILD_TORCHAUDIO_PYTHON_EXTENSION) if (BUILD_TORCHAUDIO_PYTHON_EXTENSION)
# See https://github.com/pytorch/pytorch/issues/38122
find_library(TORCH_PYTHON_LIBRARY torch_python PATHS "${TORCH_INSTALL_PREFIX}/lib")
if (WIN32)
find_package(Python3 ${PYTHON_VERSION} EXACT COMPONENTS Development)
set(ADDITIONAL_ITEMS Python3::Python)
endif()
function(define_extension name sources libraries definitions)
add_library(${name} SHARED ${sources})
target_compile_definitions(${name} PRIVATE "${definitions}")
target_include_directories(
${name} PRIVATE ${PROJECT_SOURCE_DIR} ${Python_INCLUDE_DIR})
target_link_libraries(
${name}
${libraries}
${TORCH_PYTHON_LIBRARY}
${ADDITIONAL_ITEMS}
)
set_target_properties(${name} PROPERTIES PREFIX "")
if (MSVC)
set_target_properties(${name} PROPERTIES SUFFIX ".pyd")
endif(MSVC)
if (APPLE)
# https://github.com/facebookarchive/caffe2/issues/854#issuecomment-364538485
# https://github.com/pytorch/pytorch/commit/73f6715f4725a0723d8171d3131e09ac7abf0666
set_target_properties(${name} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
endif()
install(
TARGETS ${name}
LIBRARY DESTINATION .
RUNTIME DESTINATION . # For Windows
)
endfunction()
set( set(
EXTENSION_SOURCES EXTENSION_SOURCES
pybind/pybind.cpp pybind/pybind.cpp
) )
#----------------------------------------------------------------------------#
# START OF CUSTOMIZATION LOGICS
#----------------------------------------------------------------------------#
if(BUILD_SOX) if(BUILD_SOX)
list( list(
APPEND APPEND
...@@ -150,53 +186,13 @@ if (BUILD_TORCHAUDIO_PYTHON_EXTENSION) ...@@ -150,53 +186,13 @@ if (BUILD_TORCHAUDIO_PYTHON_EXTENSION)
pybind/sox/utils.cpp pybind/sox/utils.cpp
) )
endif() endif()
add_library( #----------------------------------------------------------------------------#
_torchaudio # END OF CUSTOMIZATION LOGICS
SHARED #----------------------------------------------------------------------------#
${EXTENSION_SOURCES} define_extension(
)
target_compile_definitions(
_torchaudio
PRIVATE ${LIBTORCHAUDIO_COMPILE_DEFINITIONS}
)
set_target_properties(_torchaudio PROPERTIES PREFIX "")
if (MSVC)
set_target_properties(_torchaudio PROPERTIES SUFFIX ".pyd")
endif(MSVC)
if (APPLE)
# https://github.com/facebookarchive/caffe2/issues/854#issuecomment-364538485
# https://github.com/pytorch/pytorch/commit/73f6715f4725a0723d8171d3131e09ac7abf0666
set_target_properties(_torchaudio PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
endif()
target_include_directories(
_torchaudio
PRIVATE
${PROJECT_SOURCE_DIR}
${Python_INCLUDE_DIR}
)
# See https://github.com/pytorch/pytorch/issues/38122
find_library(TORCH_PYTHON_LIBRARY torch_python PATHS "${TORCH_INSTALL_PREFIX}/lib")
if (WIN32)
find_package(Python3 ${PYTHON_VERSION} EXACT COMPONENTS Development)
set(ADDITIONAL_ITEMS Python3::Python)
endif()
target_link_libraries(
_torchaudio _torchaudio
"${EXTENSION_SOURCES}"
libtorchaudio libtorchaudio
${TORCH_PYTHON_LIBRARY} "${LIBTORCHAUDIO_COMPILE_DEFINITIONS}"
${ADDITIONAL_ITEMS}
)
install(
TARGETS _torchaudio
LIBRARY DESTINATION .
RUNTIME DESTINATION . # For Windows
) )
endif() endif()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment