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