Skip to content

Commit 7ccfbdf

Browse files
Clean up python configuration:
- Remove experimental attributes (local_checks, python_include, numpy_include, remote_config_repo). Use of experimental remote repo can only be enabled now via env variable (preferred to change to workspace file) - Fix hardcoded fallback value for python bin. New fallback is to use 'which python' and fail if python not set in path (solves tensorflow#9436 / works around bazelbuild/bazel#3057). - Fixes minor issues with template: adds spaces to srcs in genrule, remove extra space after genrules. - Some dead code clean up (use of empty configuration) PiperOrigin-RevId: 160594421
1 parent cd15cb8 commit 7ccfbdf

2 files changed

Lines changed: 45 additions & 74 deletions

File tree

third_party/py/BUILD.tpl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,5 @@ config_setting(
3131
)
3232

3333
%{PYTHON_INCLUDE_GENRULE}
34-
3534
%{PYTHON_IMPORT_LIB_GENRULE}
36-
3735
%{NUMPY_INCLUDE_GENRULE}

third_party/py/python_configure.bzl

Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
* `PYTHON_LIB_PATH`: Location of python libraries.
1010
"""
1111

12-
_NUMPY_INCLUDE_PATH = "NUMPY_INCLUDE_PATH"
1312
_PYTHON_BIN_PATH = "PYTHON_BIN_PATH"
14-
_PYTHON_INCLUDE_PATH = "PYTHON_INCLUDE_PATH"
1513
_PYTHON_LIB_PATH = "PYTHON_LIB_PATH"
1614
_TF_PYTHON_CONFIG_REPO = "TF_PYTHON_CONFIG_REPO"
1715

@@ -121,7 +119,7 @@ def _genrule(src_dir, genrule_name, command, outs):
121119
' cmd = """\n' +
122120
command +
123121
'\n """,\n' +
124-
')\n\n'
122+
')\n'
125123
)
126124

127125

@@ -157,12 +155,27 @@ def _symlink_genrule_for_dir(repository_ctx, src_dir, dest_dir, genrule_name,
157155
# On Windows, symlink is not supported, so we just copy all the files.
158156
cmd = 'cp -f' if _is_windows(repository_ctx) else 'ln -s'
159157
command.append(cmd + ' "%s" "%s"' % (src_files[i] , dest))
160-
outs.append(' "' + dest_dir + dest_files[i] + '",')
158+
outs.append(' "' + dest_dir + dest_files[i] + '",')
161159
genrule = _genrule(src_dir, genrule_name, " && ".join(command),
162160
"\n".join(outs))
163161
return genrule
164162

165163

164+
def _get_python_bin(repository_ctx):
165+
"""Gets the python bin path."""
166+
python_bin = _get_env_var(repository_ctx, _PYTHON_BIN_PATH,
167+
None, False)
168+
if python_bin != None:
169+
return python_bin
170+
python_bin_path = repository_ctx.which("python")
171+
if python_bin_path != None:
172+
return str(python_bin_path)
173+
path = _get_env_var(repository_ctx, "PATH")
174+
_python_configure_fail("Cannot find python in PATH, please make sure " +
175+
"python is installed and add its directory in PATH, or set the " +
176+
"environment variable PYTHON_BIN_PATH.\nPATH=%s" % (path))
177+
178+
166179
def _get_python_lib(repository_ctx, python_bin):
167180
"""Gets the python lib path."""
168181
print_lib = ("<<END\n" +
@@ -252,63 +265,32 @@ def _get_numpy_include(repository_ctx, python_bin):
252265

253266
def _create_local_python_repository(repository_ctx):
254267
"""Creates the repository containing files set up to build with Python."""
255-
python_include = None
256-
numpy_include = None
257-
empty_config = False
258-
# If local checks were requested, the python and numpy include will be auto
259-
# detected on the host config (using _PYTHON_BIN_PATH).
260-
python_bin = _get_env_var(repository_ctx, _PYTHON_BIN_PATH,
261-
"/usr/bin/python")
262-
if repository_ctx.attr.local_checks:
263-
# TODO(nlopezgi): The default argument here is a workaround until
264-
# bazelbuild/bazel#3057 is resolved.
265-
_check_python_bin(repository_ctx, python_bin)
266-
python_lib = _get_env_var(repository_ctx, _PYTHON_LIB_PATH,
268+
python_bin = _get_python_bin(repository_ctx)
269+
_check_python_bin(repository_ctx, python_bin)
270+
python_lib = _get_env_var(repository_ctx, _PYTHON_LIB_PATH,
267271
_get_python_lib(repository_ctx, python_bin))
268-
_check_python_lib(repository_ctx, python_lib)
269-
python_include = _get_python_include(repository_ctx, python_bin)
270-
numpy_include = _get_numpy_include(repository_ctx, python_bin) + '/numpy'
271-
else:
272-
# Otherwise, we assume user provides all paths (via ENV or attrs)
273-
python_include = _get_env_var(repository_ctx, _PYTHON_INCLUDE_PATH,
274-
repository_ctx.attr.python_include)
275-
numpy_include = _get_env_var(repository_ctx, _NUMPY_INCLUDE_PATH,
276-
repository_ctx.attr.numpy_include) + '/numpy'
277-
if empty_config:
278-
_tpl(repository_ctx, "BUILD", {
279-
"%{PYTHON_INCLUDE_GENRULE}": ('filegroup(\n' +
280-
' name = "python_include",\n' +
281-
' srcs = [],\n' +
282-
')\n'),
283-
"%{PYTHON_IMPORT_LIB_GENRULE}": ('filegroup(\n' +
284-
' name = "python_import_lib",\n' +
285-
' srcs = [],\n' +
286-
')\n'),
287-
"%{NUMPY_INCLUDE_GENRULE}": ('filegroup(\n' +
288-
' name = "numpy_include",\n' +
289-
' srcs = [],\n' +
290-
')\n'),
291-
})
292-
else:
293-
python_include_rule = _symlink_genrule_for_dir(
294-
repository_ctx, python_include, 'python_include', 'python_include')
295-
python_import_lib_genrule = ""
296-
# To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib
297-
# See https://docs.python.org/3/extending/windows.html
298-
if _is_windows(repository_ctx):
299-
python_include = _norm_path(python_include)
300-
python_import_lib_name = _get_python_import_lib_name(repository_ctx, python_bin)
301-
python_import_lib_src = python_include.rsplit('/', 1)[0] + "/libs/" + python_import_lib_name
302-
python_import_lib_genrule = _symlink_genrule_for_dir(
303-
repository_ctx, None, '', 'python_import_lib',
304-
[python_import_lib_src], [python_import_lib_name])
305-
numpy_include_rule = _symlink_genrule_for_dir(
306-
repository_ctx, numpy_include, 'numpy_include/numpy', 'numpy_include')
307-
_tpl(repository_ctx, "BUILD", {
308-
"%{PYTHON_INCLUDE_GENRULE}": python_include_rule,
309-
"%{PYTHON_IMPORT_LIB_GENRULE}": python_import_lib_genrule,
310-
"%{NUMPY_INCLUDE_GENRULE}": numpy_include_rule,
311-
})
272+
_check_python_lib(repository_ctx, python_lib)
273+
python_include = _get_python_include(repository_ctx, python_bin)
274+
numpy_include = _get_numpy_include(repository_ctx, python_bin) + '/numpy'
275+
python_include_rule = _symlink_genrule_for_dir(
276+
repository_ctx, python_include, 'python_include', 'python_include')
277+
python_import_lib_genrule = ""
278+
# To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib
279+
# See https://docs.python.org/3/extending/windows.html
280+
if _is_windows(repository_ctx):
281+
python_include = _norm_path(python_include)
282+
python_import_lib_name = _get_python_import_lib_name(repository_ctx, python_bin)
283+
python_import_lib_src = python_include.rsplit('/', 1)[0] + "/libs/" + python_import_lib_name
284+
python_import_lib_genrule = _symlink_genrule_for_dir(
285+
repository_ctx, None, '', 'python_import_lib',
286+
[python_import_lib_src], [python_import_lib_name])
287+
numpy_include_rule = _symlink_genrule_for_dir(
288+
repository_ctx, numpy_include, 'numpy_include/numpy', 'numpy_include')
289+
_tpl(repository_ctx, "BUILD", {
290+
"%{PYTHON_INCLUDE_GENRULE}": python_include_rule,
291+
"%{PYTHON_IMPORT_LIB_GENRULE}": python_import_lib_genrule,
292+
"%{NUMPY_INCLUDE_GENRULE}": numpy_include_rule,
293+
})
312294

313295

314296
def _create_remote_python_repository(repository_ctx, remote_config_repo):
@@ -321,27 +303,18 @@ def _create_remote_python_repository(repository_ctx, remote_config_repo):
321303

322304
def _python_autoconf_impl(repository_ctx):
323305
"""Implementation of the python_autoconf repository rule."""
324-
remote_config_repo = _get_env_var(repository_ctx, _TF_PYTHON_CONFIG_REPO,
325-
repository_ctx.attr.remote_config_repo, False)
326-
if remote_config_repo != "":
327-
_create_remote_python_repository(repository_ctx, remote_config_repo)
306+
if _TF_PYTHON_CONFIG_REPO in repository_ctx.os.environ:
307+
_create_remote_python_repository(repository_ctx,
308+
repository_ctx.os.environ[_TF_PYTHON_CONFIG_REPO])
328309
else:
329310
_create_local_python_repository(repository_ctx)
330311

331312

332313
python_configure = repository_rule(
333314
implementation = _python_autoconf_impl,
334-
attrs = {
335-
"local_checks": attr.bool(mandatory = False, default = True),
336-
"python_include": attr.string(mandatory = False),
337-
"numpy_include": attr.string(mandatory = False),
338-
"remote_config_repo": attr.string(mandatory = False, default =""),
339-
},
340315
environ = [
341316
_PYTHON_BIN_PATH,
342-
_PYTHON_INCLUDE_PATH,
343317
_PYTHON_LIB_PATH,
344-
_NUMPY_INCLUDE_PATH,
345318
_TF_PYTHON_CONFIG_REPO,
346319
],
347320
)

0 commit comments

Comments
 (0)