Skip to content

Commit a652cb7

Browse files
committed
Merge branch 'macos-python-update' into bump_CI_images
2 parents 9f50f58 + 2c5a8e5 commit a652cb7

File tree

4 files changed

+88
-30
lines changed

4 files changed

+88
-30
lines changed

config/python/python_config.py

+60-19
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,79 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import argparse
1718
import json
1819
import os
20+
import pathlib
1921
import shutil
2022
import subprocess
23+
from typing import Optional
2124

2225

2326
class Config:
24-
def __init__(self):
25-
cipd_install_dir = os.getenv("PW_PYTHON_CIPD_INSTALL_DIR")
26-
self.python_exe = os.path.join(cipd_install_dir, "bin", "python3")
27-
if not os.path.exists(self.python_exe):
28-
self.python_exe = shutil.which("python3")
29-
if not self.python_exe:
30-
raise FileNotFoundError("Python3 not found")
31-
32-
def get_version(self):
33-
out = subprocess.check_output(
34-
[self.python_exe, "-c", "import sys; print(sys.version_info.major, sys.version_info.minor)"], text=True
35-
)
36-
return out.strip().split(" ")
27+
def __init__(self, python_exe: Optional[pathlib.Path] = None):
28+
self._paths: Optional[dict] = None
29+
self._version: Optional[list] = None
3730

31+
if python_exe:
32+
self.python_exe = python_exe
33+
else:
34+
cipd_install_dir = os.getenv("PW_PYTHON_CIPD_INSTALL_DIR")
35+
if not cipd_install_dir:
36+
raise RuntimeError("PW_PYTHON_CIPD_INSTALL_DIR not set")
37+
self.python_exe = os.path.join(cipd_install_dir, "bin", "python3")
38+
if not os.path.exists(self.python_exe):
39+
self.python_exe = shutil.which("python3")
40+
if not self.python_exe:
41+
raise FileNotFoundError("Python3 not found")
3842

39-
if __name__ == "__main__":
40-
config = Config()
41-
version = config.get_version()
43+
@property
44+
def paths(self) -> dict:
45+
if not self._paths:
46+
out = subprocess.check_output(
47+
[self.python_exe, "-c", "from sysconfig import get_paths; import json; print(json.dumps(get_paths()))"], text=True
48+
).strip()
49+
self._paths = json.loads(out)
50+
return self._paths
51+
52+
@property
53+
def version(self) -> list[str]:
54+
if not self._version:
55+
self._version = (
56+
subprocess.check_output(
57+
[self.python_exe, "-c", "import sys; print(sys.version_info.major, sys.version_info.minor)"], text=True
58+
)
59+
.strip()
60+
.split(" ")
61+
)
62+
return self._version
63+
64+
@property
65+
def include(self) -> str:
66+
return self.paths["include"]
67+
68+
69+
def parse_args():
70+
parser = argparse.ArgumentParser(description="Generate Python config")
71+
parser.add_argument("--python", type=pathlib.Path, help="Python executable to use", required=False)
72+
return parser.parse_args()
73+
74+
75+
def main():
76+
args = parse_args()
77+
config = Config(args.python)
4278
print(
4379
json.dumps(
4480
{
4581
"version": {
46-
"major": version[0],
47-
"minor": version[1],
48-
}
82+
"major": config.version[0],
83+
"minor": config.version[1],
84+
},
85+
"include": config.include,
4986
}
5087
)
5188
)
89+
90+
91+
if __name__ == "__main__":
92+
main()

docs/guides/BUILDING.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ that generates inputs to [ninja](https://ninja-build.org/).
88

99
The build system has been tested on the following Operating Systems:
1010

11-
- macOS 10.15
11+
- macOS 13
1212
- Debian 11 (64 bit required)
1313
- Ubuntu 22.04 LTS
1414

@@ -107,7 +107,14 @@ sudo apt-get install libsdl2-dev
107107

108108
### Installing prerequisites on macOS
109109

110-
On macOS, install Xcode from the Mac App Store.
110+
On macOS, install Xcode from the Mac App Store. After installing Xcode, install
111+
python3.11 using brew:
112+
113+
```sh
114+
brew install python@3.11
115+
python3.11 -m pip install --upgrade setuptools
116+
python3.11 -m pip install --upgrade pip
117+
```
111118

112119
#### UI builds
113120

scripts/setup/bootstrap.sh

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
1414
# limitations under the License.
1515
#
1616

17+
# Installation of python using brew without specifying the version will add the python
18+
# and python3 executables to the PATH. However, installation of specific version
19+
# of python on macOS using brew will not add the
20+
# python/python3 executable to the PATH. To make pigweed use brew installed python
21+
# on macOS, we need to add `/usr/local/opt/python@$version/libexec/bin/` to the PATH.
22+
if [ "$(uname)" = "Darwin" ]; then
23+
PYTHON_VERSIONS=("3.12" "3.11")
24+
for version in "${PYTHON_VERSIONS[@]}"; do
25+
_PYTHON_PATH="/usr/local/opt/python@$version/libexec/bin/"
26+
if [ -d "$_PYTHON_PATH" ]; then
27+
PATH="$_PYTHON_PATH:$PATH"
28+
break
29+
fi
30+
done
31+
fi
32+
1733
_install_additional_pip_requirements() {
1834
_SETUP_PLATFORM=$1
1935
shift

src/pybindings/pycontroller/BUILD.gn

+3-9
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,10 @@ shared_library("CHIPController") {
4646

4747
# TODO: Update to use GN tools to get actual paths
4848
include_dirs = [ "${chip_root}/third_party/pybind11/repo/include" ]
49-
version = python_config.version
50-
if (current_os == "mac") {
51-
include_dirs +=
52-
[ "${chip_root}/.environment/cipd/packages/python/include/python" +
53-
version.major + "." + version.minor ]
54-
} else if (current_os == "linux") {
55-
include_dirs +=
56-
[ "/usr/include/python" + version.major + "." + version.minor ]
57-
} else {
49+
if (current_os != "mac" && current_os != "linux") {
5850
assert(false, "OS not supported.")
5951
}
52+
include_dirs += [ python_config.include ]
6053

6154
sources = [
6255
"ControllerBindings/PyChip_ChipError.cpp",
@@ -92,6 +85,7 @@ shared_library("CHIPController") {
9285
]
9386
}
9487
if (current_os == "linux") {
88+
version = python_config.version
9589
libs = [ "python" + version.major + "." + version.minor ]
9690
}
9791
}

0 commit comments

Comments
 (0)