1+ # find python binary
2+ find_program (PYTHON_EXECUTABLE NAMES python3)
3+
4+ if (NOT EXISTS ${PYTHON_EXECUTABLE} )
5+ message (FATAL_ERROR "python3 is not found, install python firstly" )
6+ endif ()
7+
8+ # get torch path, torch npu path, pybind11 path via python script
9+ execute_process (
10+ COMMAND ${PYTHON_EXECUTABLE} "-c"
11+ "import torch; import torch_npu; import os; import pybind11;
12+ torch_dir = os.path.realpath(os.path.dirname(torch.__file__));
13+ torch_npu_dir = os.path.realpath(os.path.dirname(torch_npu.__file__));
14+ pybind11_dir = os.path.realpath(os.path.dirname(pybind11.__file__));
15+ abi_enabled=torch.compiled_with_cxx11_abi();
16+ print(torch_dir, torch_npu_dir, pybind11_dir, abi_enabled, end='');
17+ quit(0)
18+ "
19+ RESULT_VARIABLE EXEC_RESULT
20+ OUTPUT_VARIABLE OUTPUT_ENV_DEFINES)
21+
22+ # if failed to run the python script
23+ if (NOT ${EXEC_RESULT} EQUAL 0)
24+ message (FATAL_ERROR "failed to get run python script to get ENVS like TORCH_DIR etc" )
25+ else ()
26+ message (STATUS "run python script successfully, output string is [${OUTPUT_ENV_DEFINES} ]" )
27+ endif ()
28+
29+ # extract TORCH_DIR and set it
30+ execute_process (
31+ COMMAND sh -c "echo \" ${OUTPUT_ENV_DEFINES} \" | awk '{print $1}'"
32+ OUTPUT_VARIABLE TORCH_DIR
33+ RESULT_VARIABLE EXEC_RESULT
34+ OUTPUT_STRIP_TRAILING_WHITESPACE
35+ )
36+
37+ # extract TORCH_NPU_DIR and set it
38+ execute_process (
39+ COMMAND sh -c "echo \" ${OUTPUT_ENV_DEFINES} \" | awk '{print $2}'"
40+ OUTPUT_VARIABLE TORCH_NPU_DIR
41+ RESULT_VARIABLE EXEC_RESULT
42+ OUTPUT_STRIP_TRAILING_WHITESPACE
43+ )
44+
45+ # extract PYBIND11_DIR and set it
46+ execute_process (
47+ COMMAND sh -c "echo \" ${OUTPUT_ENV_DEFINES} \" | awk '{print $3}'"
48+ OUTPUT_VARIABLE PYBIND11_DIR
49+ RESULT_VARIABLE EXEC_RESULT
50+ OUTPUT_STRIP_TRAILING_WHITESPACE
51+ )
52+
53+ # extract PYTROCH_ABI and set it
54+ execute_process (
55+ COMMAND sh -c "echo \" ${OUTPUT_ENV_DEFINES} \" | awk '{print $4}'"
56+ OUTPUT_VARIABLE TORCH_API_ENABLED
57+ RESULT_VARIABLE EXEC_RESULT
58+ OUTPUT_STRIP_TRAILING_WHITESPACE
59+ )
60+
61+ message (STATUS "SOC_VERSION=${SOC_VERSION} " )
62+ message (STATUS "TORCH_DIR=${TORCH_DIR} " )
63+ message (STATUS "TORCH_NPU_DIR=${TORCH_NPU_DIR} " )
64+ message (STATUS "PYBIND11_DIR=${PYBIND11_DIR} " )
65+
66+ # set _GLIBCXX_USE_CXX11_ABI
67+ if (${TORCH_API_ENABLED} STREQUAL "True" )
68+ add_compile_options (-D_GLIBCXX_USE_CXX11_ABI=1)
69+ message (STATUS "_GLIBCXX_USE_CXX11_ABI=1" )
70+ else ()
71+ add_compile_options (-D_GLIBCXX_USE_CXX11_ABI=0)
72+ message (STATUS "_GLIBCXX_USE_CXX11_ABI=0" )
73+ endif ()
0 commit comments