torch-mlir/python/CMakeLists.txt

128 lines
4.6 KiB
CMake
Raw Normal View History

################################################################################
# Resources that must be packaged into the python tree
################################################################################
2020-08-07 14:51:05 +08:00
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/npcomp/compiler/backend/refjit_resources")
add_custom_target(NPCOMPPythonResources)
add_custom_command(
2020-08-07 14:51:05 +08:00
TARGET NPCOMPPythonResources
COMMAND ${CMAKE_COMMAND} -E copy
# TODO: Make the runtime library work for windows.
2020-08-07 14:51:05 +08:00
${CMAKE_BINARY_DIR}/lib/libNPCOMPCompilerRuntimeShlib.so
${CMAKE_CURRENT_BINARY_DIR}/npcomp/compiler/backend/refjit_resources/libNPCOMPCompilerRuntimeShlib.so
)
add_dependencies(NPCOMPPythonResources
NPCOMPCompilerRuntimeShlib
)
2020-04-27 06:50:23 +08:00
################################################################################
# Manage python source files
################################################################################
function (create_symlinks)
# Do nothing if building in-source
if (${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
return()
endif()
foreach (path_file ${ARGN})
get_filename_component(folder ${path_file} PATH)
# Create REAL folder
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${folder}")
# Delete symlink if it exists
file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/${path_file}")
# Get OS dependent path to use in `execute_process`
file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}/${path_file}" link)
file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${path_file}" target)
if (UNIX)
set(command ln -s ${target} ${link})
else()
set(command cmd.exe /c mklink ${link} ${target})
endif()
execute_process(COMMAND ${command}
2020-04-27 06:50:23 +08:00
RESULT_VARIABLE result
ERROR_VARIABLE output)
if (NOT ${result} EQUAL 0)
message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}")
endif()
endforeach(path_file)
endfunction(create_symlinks)
file(GLOB_RECURSE python_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.py)
create_symlinks(${python_files})
################################################################################
# Native extensions
################################################################################
include(NpcompPython)
# Normally on unix-like platforms, extensions are built as "MODULE" libraries
# and do not explicitly link to the python shared object. This allows for
# come greater deployment flexibility since the extension will bind to
# symbols in the python interpreter on load. However, it also keeps the
# linker from erroring on undefined symbols, leaving this to (usually obtuse)
# runtime errors. Building in "SHARED" mode with an explicit link to the
# python libraries allows us to build with the expectation of no undefined
# symbols, which is better for development.
# TODO(laurenzo): Windows requires linking against the PYTHON_LIBRARIES
# TODO(laurenzo): OSX requires allowing undefined (-undefined dynamic_lookup)
if(NPCOMP_PYTHON_BINDINGS_VERSION_LOCKED)
set(NPCOMP_PYEXT_LINK_MODE SHARED)
set(NPCOMP_PYEXT_LIBADD ${PYTHON_LIBRARIES})
else()
set(NPCOMP_PYEXT_LINK_MODE MODULE)
set(NPCOMP_PYEXT_LIBADD)
endif()
if(NPCOMP_ENABLE_IREE)
list(APPEND NPCOMP_PYEXT_LIBADD NPCOMPBackendIREEPythonModule)
endif()
if(NPCOMP_ENABLE_REFJIT)
list(APPEND NPCOMP_PYEXT_LIBADD
NPCOMPBackendRefJITPythonModule
)
endif()
add_library(NPCOMPNativePyExt ${NPCOMP_PYEXT_LINK_MODE}
NpcompModule.cpp
)
set_target_properties(NPCOMPNativePyExt PROPERTIES LIBRARY_OUTPUT_DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}")
set_target_properties(NPCOMPNativePyExt PROPERTIES OUTPUT_NAME _npcomp)
set_target_properties(NPCOMPNativePyExt PROPERTIES PREFIX
"${PYTHON_MODULE_PREFIX}")
set_target_properties(NPCOMPNativePyExt PROPERTIES SUFFIX
"${PYTHON_MODULE_EXTENSION}")
# pybind requires binding code to be compiled with -fvisibility=hidden
# Better code can be generated if the entire project compiles that way, but
# that is not enforced here.
set_target_properties(NPCOMPNativePyExt PROPERTIES CXX_VISIBILITY_PRESET "hidden")
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
target_link_libraries(NPCOMPNativePyExt
PRIVATE
# Transitive dep on the shared library links most things from there.
NPCOMP
NPCOMPInitAll
NPCOMPPythonCommon
${NPCOMP_PYEXT_LIBADD}
)
npcomp_python_target_compile_options(NPCOMPNativePyExt)
mlir_check_all_link_libraries(NPCOMPNativePyExt)