Skip to content

Commit cf83964

Browse files
GregoryComerfacebook-github-bot
authored andcommitted
CMake Use python instead of python3 when in a (non-base) conda environment (#2687)
Summary: When building inside a conda environment (the recommended configuration), we should use 'python' instead of 'python3'. In some environments (mac), 'python3' may resolve to the incorrect python binary. This change maintains backwards compatibility for non-conda users by still defaulting to python3. Note that pytorch core appears to default to 'python', instead of 'python3', so we may want to look at doing so in the future. Bypassing unrelated lint (fix already in progress). bypass-github-export-checks bypass-github-executorch-ci-checks Pull Request resolved: #2687 Test Plan: On both Linux and Mac, I performed the following: Built in non-base conda environment, Checked build log to ensure it uses 'python'. Built in base conda environment. Checked build log to ensure it uses 'python3'. Built in non-conda environment. Checked build log to ensure it uses 'python3'. Reviewed By: mcr229 Differential Revision: D55370314 Pulled By: GregoryComer fbshipit-source-id: 281bafa78f6523d7c03b8a736134f18bd6cea45b
1 parent 456dab5 commit cf83964

File tree

21 files changed

+112
-77
lines changed

21 files changed

+112
-77
lines changed

CMakeLists.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313
# cloning or pulling the upstream repo. Once this is done, you don't need to do
1414
# it again until you pull from the upstream repo again.
1515
#
16-
# NOTE: If your `buck2` binary is not on the PATH, you can change this line to
17-
# say something like `-DBUCK2=/tmp/buck2` to point directly to the tool.
18-
#[[
19-
(rm -rf cmake-out \
20-
&& mkdir cmake-out \
21-
&& cd cmake-out \
22-
&& cmake -DBUCK2=buck2 ..)
23-
]]
24-
#
2516
# ### Build ###
2617
#
2718
# NOTE: The `-j` argument specifies how many jobs/processes to use when
@@ -169,8 +160,9 @@ option(EXECUTORCH_BUILD_XNNPACK "Build the XNNPACK backend" OFF)
169160
option(EXECUTORCH_BUILD_VULKAN "Build the Vulkan backend" OFF)
170161

171162
if(NOT PYTHON_EXECUTABLE)
172-
set(PYTHON_EXECUTABLE python3)
163+
resolve_python_executable()
173164
endif()
165+
message(STATUS "Using python executable '${PYTHON_EXECUTABLE}'")
174166

175167
# TODO(dbort): Fix these warnings and remove this flag.
176168
set(_common_compile_options -Wno-deprecated-declarations -fPIC)

backends/apple/mps/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ if(NOT CMAKE_CXX_STANDARD)
1111
set(CMAKE_CXX_STANDARD 17)
1212
endif()
1313

14-
if(NOT PYTHON_EXECUTABLE)
15-
set(PYTHON_EXECUTABLE python3)
16-
endif()
17-
1814
# Source root directory for executorch.
1915
if(NOT EXECUTORCH_ROOT)
2016
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
2117
endif()
2218

19+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
20+
21+
if(NOT PYTHON_EXECUTABLE)
22+
resolve_python_executable()
23+
endif()
24+
2325
if(NOT FLATC_EXECUTABLE)
2426
set(FLATC_EXECUTABLE flatc)
2527
endif()
Submodule serialization_lib updated from bd8c529 to 187af0d

backends/vulkan/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ if(NOT EXECUTORCH_ROOT)
2020
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
2121
endif()
2222

23+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
24+
2325
if(NOT RUNTIME_PATH)
2426
set(RUNTIME_PATH ${CMAKE_CURRENT_SOURCE_DIR}/runtime)
2527
endif()
2628

2729
if(NOT PYTHON_EXECUTABLE)
28-
set(PYTHON_EXECUTABLE python3)
30+
resolve_python_executable()
2931
endif()
3032

3133
if(NOT FLATC_EXECUTABLE)

backends/xnnpack/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ if(NOT CMAKE_CXX_STANDARD)
1717
set(CMAKE_CXX_STANDARD 17)
1818
endif()
1919

20-
if(NOT PYTHON_EXECUTABLE)
21-
set(PYTHON_EXECUTABLE python3)
22-
endif()
23-
2420
if(NOT FLATC_EXECUTABLE)
2521
set(FLATC_EXECUTABLE flatc)
2622
endif()
@@ -32,6 +28,10 @@ endif()
3228

3329
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
3430

31+
if(NOT PYTHON_EXECUTABLE)
32+
resolve_python_executable()
33+
endif()
34+
3535
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
3636
set(_common_compile_options -Wno-deprecated-declarations)
3737

build/Utils.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,18 @@ function(resolve_buck2)
203203
endif()
204204
endif()
205205
endfunction()
206+
207+
# Sets the value of the PYTHON_EXECUTABLE variable to 'python' if in
208+
# an active (non-base) conda environment, and 'python3' otherwise. This
209+
# maintains backwards compatibility for non-conda users and avoids conda
210+
# users needing to explicitly set PYTHON_EXECUTABLE=python.
211+
function(resolve_python_executable)
212+
# Counter-intuitively, CONDA_DEFAULT_ENV contains the name of the
213+
# active environment.
214+
if(DEFINED ENV{CONDA_DEFAULT_ENV} AND
215+
NOT $ENV{CONDA_DEFAULT_ENV} STREQUAL "base")
216+
set(PYTHON_EXECUTABLE python PARENT_SCOPE)
217+
else()
218+
set(PYTHON_EXECUTABLE python3 PARENT_SCOPE)
219+
endif()
220+
endfunction()

configurations/CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ if(NOT CMAKE_CXX_STANDARD)
1111
set(CMAKE_CXX_STANDARD 17)
1212
endif()
1313

14-
if(NOT PYTHON_EXECUTABLE)
15-
set(PYTHON_EXECUTABLE python3)
16-
endif()
1714
# Source root directory for executorch.
1815
if(NOT EXECUTORCH_ROOT)
1916
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
@@ -23,6 +20,12 @@ if(NOT TORCH_ROOT)
2320
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
2421
endif()
2522

23+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
24+
25+
if(NOT PYTHON_EXECUTABLE)
26+
resolve_python_executable()
27+
endif()
28+
2629
set(_common_compile_options -Wno-deprecated-declarations)
2730

2831
include(${EXECUTORCH_ROOT}/build/Utils.cmake)

examples/apple/mps/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ if(NOT CMAKE_CXX_STANDARD)
1818
set(CMAKE_CXX_STANDARD 17)
1919
endif()
2020

21-
if(NOT PYTHON_EXECUTABLE)
22-
set(PYTHON_EXECUTABLE python3)
23-
endif()
24-
2521
if(NOT FLATC_EXECUTABLE)
2622
set(FLATC_EXECUTABLE flatc)
2723
endif()
@@ -31,6 +27,12 @@ if(NOT EXECUTORCH_ROOT)
3127
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
3228
endif()
3329

30+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
31+
32+
if(NOT PYTHON_EXECUTABLE)
33+
resolve_python_executable()
34+
endif()
35+
3436
# Source root directory for pytorch.
3537
if(NOT TORCH_ROOT)
3638
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)

examples/arm/CMakeLists.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ project(arm_example)
1616
option(EXECUTORCH_SELECT_OPS_LIST "Register the following list of ops" OFF)
1717

1818
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
19-
20-
if(NOT PYTHON_EXECUTABLE)
21-
set(PYTHON_EXECUTABLE python3)
22-
endif()
2319
# Source root directory for executorch.
2420
if(NOT EXECUTORCH_ROOT)
2521
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
2622
endif()
23+
24+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
25+
26+
if(NOT PYTHON_EXECUTABLE)
27+
resolve_python_executable()
28+
endif()
29+
2730
# Source root directory for pytorch.
2831
if(NOT TORCH_ROOT)
2932
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)

examples/models/llama2/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ project(llama_runner)
2020

2121
option(EXECUTORCH_BUILD_OPTIMIZED "Build the optimized kernels" OFF)
2222

23-
if(NOT PYTHON_EXECUTABLE)
24-
set(PYTHON_EXECUTABLE python3)
25-
endif()
26-
2723
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
2824
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
2925

26+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
27+
28+
if(NOT PYTHON_EXECUTABLE)
29+
resolve_python_executable()
30+
endif()
31+
3032
if(NOT CMAKE_CXX_STANDARD)
3133
set(CMAKE_CXX_STANDARD 17)
3234
# Can't set to 11 due to executor_runner.cpp make_unique

examples/portable/custom_ops/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ if(NOT CMAKE_CXX_STANDARD)
2222
set(CMAKE_CXX_STANDARD 17)
2323
endif()
2424

25-
if(NOT PYTHON_EXECUTABLE)
26-
set(PYTHON_EXECUTABLE python3)
27-
endif()
2825
# Source root directory for executorch.
2926
if(NOT EXECUTORCH_ROOT)
3027
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
@@ -37,6 +34,10 @@ endif()
3734
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
3835
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
3936

37+
if(NOT PYTHON_EXECUTABLE)
38+
resolve_python_executable()
39+
endif()
40+
4041
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
4142

4243
# Let files say "include <executorch/path/to/header.h>".

examples/qualcomm/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ endif()
1111
cmake_minimum_required(VERSION 3.19)
1212
project(qualcomm_runner_example)
1313

14-
if(NOT PYTHON_EXECUTABLE)
15-
set(PYTHON_EXECUTABLE python3)
16-
endif()
1714
# Source root directory for executorch.
1815
if(NOT EXECUTORCH_ROOT)
1916
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
@@ -23,6 +20,13 @@ if(NOT TORCH_ROOT)
2320
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
2421
endif()
2522

23+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
24+
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
25+
26+
if(NOT PYTHON_EXECUTABLE)
27+
resolve_python_executable()
28+
endif()
29+
2630
if(NOT CMAKE_BUILD_TYPE)
2731
set(CMAKE_BUILD_TYPE Debug)
2832
endif()
@@ -41,7 +45,6 @@ set(_common_include_directories ${EXECUTORCH_ROOT}/..)
4145
#
4246
# The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}.
4347
#
44-
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
4548
set(EXECUTORCH_SRCS_FILE
4649
"${CMAKE_CURRENT_BINARY_DIR}/../../executorch_srcs.cmake"
4750
)
@@ -55,7 +58,6 @@ get_filename_component(EXECUTORCH_SOURCE_DIR
5558
set(_qnn_executor_runner__srcs ${_executor_runner__srcs})
5659

5760
# portable_ops_lib
58-
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
5961
gen_selected_ops("" "" "ON")
6062
generate_bindings_for_kernels(
6163
FUNCTIONS_YAML ${EXECUTORCH_ROOT}/kernels/portable/functions.yaml

examples/sdk/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ if(NOT CMAKE_CXX_STANDARD)
1414
set(CMAKE_CXX_STANDARD 17)
1515
endif()
1616

17-
if(NOT PYTHON_EXECUTABLE)
18-
set(PYTHON_EXECUTABLE python3)
19-
endif()
2017
# Source root directory for executorch.
2118
if(NOT EXECUTORCH_ROOT)
2219
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
@@ -29,6 +26,10 @@ endif()
2926
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
3027
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
3128

29+
if(NOT PYTHON_EXECUTABLE)
30+
resolve_python_executable()
31+
endif()
32+
3233
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
3334

3435
# Let files say "include <executorch/path/to/header.h>".

examples/selective_build/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
cmake_minimum_required(VERSION 3.19)
1919
project(selective_build_example)
2020

21-
if(NOT PYTHON_EXECUTABLE)
22-
set(PYTHON_EXECUTABLE python3)
23-
endif()
24-
2521
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
2622
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
23+
2724
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
2825
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
2926

27+
if(NOT PYTHON_EXECUTABLE)
28+
resolve_python_executable()
29+
endif()
30+
3031
if(NOT CMAKE_CXX_STANDARD)
3132
set(CMAKE_CXX_STANDARD 17)
3233
# Can't set to 11 due to executor_runner.cpp make_unique

examples/xtensa/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ if(NOT CMAKE_CXX_STANDARD)
1111
set(CMAKE_CXX_STANDARD 17)
1212
endif()
1313

14-
if(NOT PYTHON_EXECUTABLE)
15-
set(PYTHON_EXECUTABLE python3)
16-
endif()
17-
1814
# Set the project name.
1915
project(xtensa_executorch_example)
2016

@@ -23,6 +19,12 @@ if(NOT EXECUTORCH_ROOT)
2319
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
2420
endif()
2521

22+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
23+
24+
if(NOT PYTHON_EXECUTABLE)
25+
resolve_python_executable()
26+
endif()
27+
2628
# Let files say "include <executorch/path/to/header.h>".
2729
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
2830

examples/xtensa/ops/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ if(NOT CMAKE_CXX_STANDARD)
1111
set(CMAKE_CXX_STANDARD 17)
1212
endif()
1313

14-
if(NOT PYTHON_EXECUTABLE)
15-
set(PYTHON_EXECUTABLE python3)
16-
endif()
17-
1814
# Source root directory for pytorch.
1915
if(NOT TORCH_ROOT)
2016
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
@@ -23,6 +19,10 @@ endif()
2319
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
2420
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
2521

22+
if(NOT PYTHON_EXECUTABLE)
23+
resolve_python_executable()
24+
endif()
25+
2626
# ATen compliant ops that are needed to run this model.
2727
set(_aten_ops__srcs
2828
"${CMAKE_CURRENT_SOURCE_DIR}/op_add.cpp"

install_requirements.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
# Install required python dependencies for developing
99
# Dependencies are defined in .pyproject.toml
10-
if [[ -z $BUCK ]];
11-
then
12-
BUCK=buck2
13-
fi
14-
1510
if [[ -z $PYTHON_EXECUTABLE ]];
1611
then
17-
PYTHON_EXECUTABLE=python3
12+
if [[ -z $CONDA_DEFAULT_ENV ]] || [[ $CONDA_DEFAULT_ENV == "base" ]];
13+
then
14+
PYTHON_EXECUTABLE=python3
15+
else
16+
PYTHON_EXECUTABLE=python
17+
fi
1818
fi
1919

2020
# Parse options.

kernels/optimized/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ if(NOT CMAKE_CXX_STANDARD)
1616
set(CMAKE_CXX_STANDARD 17)
1717
endif()
1818

19-
if(NOT PYTHON_EXECUTABLE)
20-
set(PYTHON_EXECUTABLE python3)
21-
endif()
2219
# Source root directory for executorch.
2320
if(NOT EXECUTORCH_ROOT)
2421
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
@@ -33,6 +30,10 @@ set(_common_compile_options -Wno-deprecated-declarations)
3330
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
3431
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
3532

33+
if(NOT PYTHON_EXECUTABLE)
34+
resolve_python_executable()
35+
endif()
36+
3637
# Generate C++ bindings to register kernels into both PyTorch (for AOT) and
3738
# Executorch (for runtime). Here select all ops in optimized.yaml
3839
set(_yaml "${CMAKE_CURRENT_LIST_DIR}/optimized-oss.yaml")

0 commit comments

Comments
 (0)