xref: /honee/pytorch_pkgconfig.py (revision 8a8cb6e06ce4728cc6d80ca92f8de31da49852e5)
1*ae2b091fSJames Wright# SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2*ae2b091fSJames Wright# SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3*ae2b091fSJames Wright
44c07ec22SJames Wrightfrom pathlib import Path
54c07ec22SJames Wrightimport torch
64c07ec22SJames Wrightimport torch.utils.cpp_extension as C
74c07ec22SJames Wrightimport torch.utils as tutils
84c07ec22SJames Wrightimport re
94c07ec22SJames Wright
104c07ec22SJames Wrightbuild_dir = Path('./build')
110b4e9c1bSJames Wrightif not build_dir.is_dir():
120b4e9c1bSJames Wright    build_dir.mkdir()
134c07ec22SJames Wrightpkgconfig_path = build_dir / 'libtorch.pc'
144c07ec22SJames Wright
154c07ec22SJames Wrightvariables = {}
164c07ec22SJames Wrightkeywords = {}
174c07ec22SJames Wright
184c07ec22SJames Wright
194c07ec22SJames Wrightdef add_variable(file, variable, value):
204c07ec22SJames Wright    file.write(f"{variable}={value}\n")
214c07ec22SJames Wright
224c07ec22SJames Wright
234c07ec22SJames Wrightdef add_keyword(file, key, value):
244c07ec22SJames Wright    file.write(f"{key}: {value}\n")
254c07ec22SJames Wright
264c07ec22SJames Wright
274c07ec22SJames Wrightvariables['prefix'] = Path(C.library_paths()[0]).parent.as_posix()
284c07ec22SJames Wright
294c07ec22SJames Wrightkeywords['Name'] = 'libTorch'
304c07ec22SJames Wrightkeywords['Description'] = 'Custom made PC for PyTorch'
314c07ec22SJames Wrightkeywords['Version'] = torch.__version__
324c07ec22SJames Wright
334c07ec22SJames Wrightkeywords['Cflags'] = ''
344c07ec22SJames Wrightfor include_path in C.include_paths():
354c07ec22SJames Wright    keywords['Cflags'] += f'-I{include_path} '
364c07ec22SJames Wright
374c07ec22SJames Wright# Need to search the CMake file to see whether the library was compiled with the CXX11 ABI standard
384c07ec22SJames Wrightregex_ABI = re.compile(r'"(\S*GLIBCXX_USE_CXX11_ABI\S*)"')
394c07ec22SJames WrighttorchCMakePath = Path(tutils.cmake_prefix_path) / 'Torch/TorchConfig.cmake'
404c07ec22SJames Wrightabi_flag = ''
414c07ec22SJames Wrightwith torchCMakePath.open('r') as f:
424c07ec22SJames Wright    for line in f:
434c07ec22SJames Wright        regex_result = regex_ABI.search(line)
444c07ec22SJames Wright        if regex_result:
454c07ec22SJames Wright            abi_flag = regex_result[1]
464c07ec22SJames Wright
474c07ec22SJames Wrightkeywords['Cflags'] += abi_flag
484c07ec22SJames Wright
494c07ec22SJames Wrightkeywords['Libs'] = ''
504c07ec22SJames Wrightfor lib_path in C.library_paths():
514c07ec22SJames Wright    keywords['Libs'] += f'-L{lib_path} '
524c07ec22SJames Wrightkeywords['Libs'] += '-lc10 -ltorch_cpu '
534c07ec22SJames Wrightif torch.cuda.is_available():
544c07ec22SJames Wright    keywords['Libs'] += '-lc10_cuda -ltorch_cuda '
554c07ec22SJames Wright    # Need to force linking with libtorch_cuda.so, so find path and specify linking flag to force it
564c07ec22SJames Wright    # This flag might be of limited portability
574c07ec22SJames Wright    for lib_path in C.library_paths():
584c07ec22SJames Wright        torch_cuda_path = Path(lib_path) / 'libtorch_cuda.so'
594c07ec22SJames Wright        if torch_cuda_path.exists():
604c07ec22SJames Wright            variables['torch_cuda_path'] = torch_cuda_path.as_posix()
614c07ec22SJames Wright            keywords['Libs'] += f'-Wl,--no-as-needed,"{torch_cuda_path.as_posix()}" '
624c07ec22SJames Wrightkeywords['Libs'] += '-ltorch '
634c07ec22SJames Wrightkeywords['Libs.private'] = ''
644c07ec22SJames Wright
654c07ec22SJames Wrightwith pkgconfig_path.open('w') as file:
664c07ec22SJames Wright    for variable, value in variables.items():
674c07ec22SJames Wright        add_variable(file, variable, value)
684c07ec22SJames Wright
694c07ec22SJames Wright    file.write('\n')
704c07ec22SJames Wright
714c07ec22SJames Wright    for keyword, value in keywords.items():
724c07ec22SJames Wright        add_keyword(file, keyword, value)
734c07ec22SJames Wright
744c07ec22SJames Wrightprint(pkgconfig_path.absolute())
75