Skip to content

Commit bffb108

Browse files
authored
Fix cmake build for macOS 12.3 where python no longer exists. (#9477)
1 parent 522c663 commit bffb108

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

Firestore/Protos/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
include(FindPythonInterp)
16+
1517
# Generate output in-place. So long as the build is idempotent this helps
1618
# verify that the protoc-generated output isn't changing.
1719
set(OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
@@ -197,7 +199,7 @@ if(FIREBASE_IOS_PROTOC_GENERATE_SOURCES)
197199
COMMENT "Generating nanopb sources"
198200
OUTPUT ${NANOPB_GENERATED_SOURCES}
199201
COMMAND
200-
python
202+
${PYTHON_EXECUTABLE}
201203
${CMAKE_CURRENT_SOURCE_DIR}/build_protos.py
202204
--nanopb
203205
--protoc=$<TARGET_FILE:protoc>
@@ -229,7 +231,7 @@ if(FIREBASE_IOS_PROTOC_GENERATE_SOURCES)
229231
COMMENT "Generating C++ protobuf sources"
230232
OUTPUT ${PROTOBUF_CPP_GENERATED_SOURCES}
231233
COMMAND
232-
python
234+
${PYTHON_EXECUTABLE}
233235
${CMAKE_CURRENT_SOURCE_DIR}/build_protos.py
234236
--cpp
235237
--protoc=$<TARGET_FILE:protoc>

Firestore/Protos/build_protos.py

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
import sys
2323

2424
import argparse
25+
import contextlib
2526
import datetime
27+
import io
2628
import os
2729
import os.path
2830
import re
31+
import stat
2932
import subprocess
33+
import tempfile
3034

3135

3236
CPP_GENERATOR = 'nanopb_cpp_generator.py'
@@ -104,6 +108,46 @@ def main():
104108
ObjcProtobufGenerator(args, proto_files).run()
105109

106110

111+
@contextlib.contextmanager
112+
def CppGeneratorScriptTweaked(path):
113+
"""
114+
Set the shebang line of the CPP_GENERATOR script to use the same Python
115+
interpreter as this process.
116+
117+
This is a workaround for the fact that `python` is hardcoded as the python
118+
interpreter, which does not always exist in the new world where Python2
119+
support has largely disappeared (e.g. macOS 12.3). Changing it to `python3`
120+
would possibly break some builds too.
121+
"""
122+
# Read the script into memory.
123+
with io.open(path, 'rt', encoding='utf8') as f:
124+
lines = [line for line in f]
125+
126+
# Verify that the read file looks like the right one.
127+
if lines[0] != u'#!/usr/bin/env python\n':
128+
raise RuntimeError('unexpected first line of ' + path + ': ' + lines[0])
129+
130+
# Replace the shebang line with a custom one.
131+
lines[0] = u'#!' + sys.executable + u'\n'
132+
133+
# Create a temporary file to which to write the tweaked script.
134+
(handle, temp_path) = tempfile.mkstemp('.py', dir=os.path.dirname(path))
135+
os.close(handle)
136+
137+
try:
138+
# Write the lines of the tweaked script to the temporary file.
139+
with io.open(temp_path, 'wt', encoding='utf8') as f:
140+
f.writelines(lines)
141+
142+
# Make sure that the temporary file is executable.
143+
st = os.stat(temp_path)
144+
os.chmod(temp_path, st.st_mode | stat.S_IEXEC)
145+
146+
yield temp_path
147+
finally:
148+
os.unlink(temp_path)
149+
150+
107151
class NanopbGenerator(object):
108152
"""Builds and runs the nanopb plugin to protoc."""
109153

@@ -130,9 +174,6 @@ def __run_generator(self, out_dir):
130174
"""Invokes protoc using the nanopb plugin."""
131175
cmd = protoc_command(self.args)
132176

133-
gen = os.path.join(os.path.dirname(__file__), CPP_GENERATOR)
134-
cmd.append('--plugin=protoc-gen-nanopb=%s' % gen)
135-
136177
nanopb_flags = ' '.join([
137178
'--extension=.nanopb',
138179
'--source-extension=.cc',
@@ -146,8 +187,11 @@ def __run_generator(self, out_dir):
146187
])
147188
cmd.append('--nanopb_out=%s:%s' % (nanopb_flags, out_dir))
148189

149-
cmd.extend(self.proto_files)
150-
run_protoc(self.args, cmd)
190+
gen = os.path.join(os.path.dirname(__file__), CPP_GENERATOR)
191+
with CppGeneratorScriptTweaked(gen) as gen_tweaked:
192+
cmd.append('--plugin=protoc-gen-nanopb=%s' % gen_tweaked)
193+
cmd.extend(self.proto_files)
194+
run_protoc(self.args, cmd)
151195

152196

153197
class ObjcProtobufGenerator(object):

Firestore/core/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
include(CheckSymbolExists)
1616
include(CheckIncludeFiles)
17+
include(FindPythonInterp)
1718

1819

1920
## firestore_util
@@ -285,7 +286,7 @@ add_custom_command(
285286
OUTPUT
286287
${GRPC_ROOT_CERTIFICATE_SOURCES}
287288
COMMAND
288-
python ${FIREBASE_SOURCE_DIR}/scripts/binary_to_array.py
289+
${PYTHON_EXECUTABLE} ${FIREBASE_SOURCE_DIR}/scripts/binary_to_array.py
289290
--output_header=${OUTPUT_DIR}/grpc_root_certificates_generated.h
290291
--output_source=${OUTPUT_DIR}/grpc_root_certificates_generated.cc
291292
--cpp_namespace=firebase::firestore::remote

0 commit comments

Comments
 (0)