Skip to content

Commit 1dba78b

Browse files
jarttensorflower-gardener
authored andcommitted
Automatically build SWIG from source
This change allows Bazel to fetch and build SWIG rather than getting it from the system. This change also improves the i/o performance of the SWIG build, makes it hermetically sealed, and ensures tf_py_wrap_cc() can function correctly across Bazel repositories. CC: tensorflow#4983 Change: 136783531
1 parent 2da2ae7 commit 1dba78b

File tree

12 files changed

+471
-84
lines changed

12 files changed

+471
-84
lines changed

configure

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,6 @@ else
107107
perl -pi -e "s,WITH_HDFS_SUPPORT = (False|True),WITH_HDFS_SUPPORT = False,s" tensorflow/core/platform/default/build_config.bzl
108108
fi
109109

110-
## Find swig path
111-
if [ -z "$SWIG_PATH" ]; then
112-
SWIG_PATH=`type -p swig 2> /dev/null || true`
113-
fi
114-
if [[ ! -e "$SWIG_PATH" ]]; then
115-
echo "Can't find swig. Ensure swig is in \$PATH or set \$SWIG_PATH."
116-
exit 1
117-
fi
118-
# Convert swig path to Windows style before writing into swig_path
119-
if is_windows; then
120-
SWIG_PATH="$(cygpath -m "$SWIG_PATH")"
121-
fi
122-
echo "$SWIG_PATH" > tensorflow/tools/swig/swig_path
123-
124110
# Invoke python_config and set up symlinks to python includes
125111
./util/python/python_config.sh --setup "$PYTHON_BIN_PATH"
126112

tensorflow/BUILD

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ package_group(
6363
packages = ["//tensorflow/..."],
6464
)
6565

66-
sh_binary(
67-
name = "swig",
68-
srcs = ["tools/swig/swig.sh"],
69-
data = glob(["tools/swig/**"]),
70-
)
71-
7266
filegroup(
7367
name = "all_files",
7468
srcs = glob(

tensorflow/g3doc/get_started/os_setup.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,9 @@ binary path.
568568

569569
```bash
570570
# For Python 2.7:
571-
$ sudo apt-get install python-numpy swig python-dev python-wheel
571+
$ sudo apt-get install python-numpy python-dev python-wheel
572572
# For Python 3.x:
573-
$ sudo apt-get install python3-numpy swig python3-dev python3-wheel
573+
$ sudo apt-get install python3-numpy python3-dev python3-wheel
574574
```
575575

576576
#### Optional: Install CUDA (GPUs on Linux)
@@ -617,20 +617,16 @@ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
617617

618618
### Prepare environment for Mac OS X
619619

620-
We recommend using [homebrew](http://brew.sh) to install the bazel and SWIG
621-
dependencies, and installing python dependencies using easy_install or pip.
622-
623-
Of course you can also install Swig from source without using homebrew. In that
624-
case, be sure to install its dependency [PCRE](http://www.pcre.org) and not
625-
PCRE2.
620+
We recommend using [homebrew](http://brew.sh) to install the bazel dependency,
621+
and installing python dependencies using easy_install or pip.
626622

627623
#### Dependencies
628624

629625
Follow instructions [here](http://bazel.io/docs/install.html) to install the
630-
dependencies for bazel. You can then use homebrew to install bazel and SWIG:
626+
dependencies for bazel. You can then use homebrew to install bazel:
631627

632628
```bash
633-
$ brew install bazel swig
629+
$ brew install bazel
634630
```
635631

636632
You can install the python dependencies using easy_install or pip. Using

tensorflow/python/lib/io/file_io.i

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,8 @@ string ReadFromStream(tensorflow::io::BufferedInputStream* stream,
275275
%unignoreall
276276

277277
%include "tensorflow/c/tf_status_helper.h"
278+
279+
%ignore tensorflow::io::internal::JoinPathImpl;
278280
%include "tensorflow/core/lib/io/path.h"
279-
%include "tensorflow/core/platform/file_statistics.h"
281+
282+
%include "tensorflow/core/platform/file_statistics.h"

tensorflow/tensorflow.bzl

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -557,33 +557,30 @@ def _py_wrap_cc_impl(ctx):
557557
if len(srcs) != 1:
558558
fail("Exactly one SWIG source file label must be specified.", "srcs")
559559
module_name = ctx.attr.module_name
560-
cc_out = ctx.outputs.cc_out
561-
py_out = ctx.outputs.py_out
562560
src = ctx.files.srcs[0]
563-
args = ["-c++", "-python"]
564-
args += ["-module", module_name]
565-
args += ["-l" + f.path for f in ctx.files.swig_includes]
566-
cc_include_dirs = set()
567-
cc_includes = set()
561+
inputs = set([src])
562+
inputs += ctx.files.swig_includes
568563
for dep in ctx.attr.deps:
569-
cc_include_dirs += [h.dirname for h in dep.cc.transitive_headers]
570-
cc_includes += dep.cc.transitive_headers
571-
args += ["-I" + x for x in cc_include_dirs]
572-
args += ["-I" + ctx.label.workspace_root]
573-
args += ["-o", cc_out.path]
574-
args += ["-outdir", py_out.dirname]
564+
inputs += dep.cc.transitive_headers
565+
inputs += ctx.files._swiglib
566+
swig_include_dirs = set([f.root.path for f in inputs if f.root.path])
567+
swig_include_dirs += sorted([f.dirname for f in ctx.files._swiglib])
568+
args = ["-c++",
569+
"-python",
570+
"-module", module_name,
571+
"-o", ctx.outputs.cc_out.path,
572+
"-outdir", ctx.outputs.py_out.dirname]
573+
args += ["-l" + f.path for f in ctx.files.swig_includes]
574+
args += ["-I" + i for i in swig_include_dirs]
575575
args += [src.path]
576-
outputs = [cc_out, py_out]
577-
# TODO(pcloudy): Move args to arguments after
578-
# https://github.com/bazelbuild/bazel/issues/1926 is fixed
579-
ctx.action(command=" ".join(["tensorflow/tools/swig/swig.sh"] + args),
580-
arguments=[],
581-
mnemonic="PythonSwig",
582-
inputs=sorted(set([src]) + cc_includes + ctx.files.swig_includes +
583-
ctx.attr.swig_deps.files),
576+
outputs = [ctx.outputs.cc_out,
577+
ctx.outputs.py_out]
578+
ctx.action(executable=ctx.executable._swig,
579+
arguments=args,
580+
inputs=list(inputs),
584581
outputs=outputs,
585-
use_default_shell_env=True,
586-
progress_message="SWIGing {input}".format(input=src.path))
582+
mnemonic="PythonSwig",
583+
progress_message="SWIGing " + src.path)
587584
return struct(files=set(outputs))
588585

589586
_py_wrap_cc = rule(
@@ -600,11 +597,17 @@ _py_wrap_cc = rule(
600597
allow_files = True,
601598
providers = ["cc"],
602599
),
603-
"swig_deps": attr.label(default = Label(
604-
"//tensorflow:swig", # swig_templates
605-
)),
606600
"module_name": attr.string(mandatory = True),
607601
"py_module_name": attr.string(mandatory = True),
602+
"_swig": attr.label(
603+
default = Label("@swig//:swig"),
604+
executable = True,
605+
cfg = "host",
606+
),
607+
"_swiglib": attr.label(
608+
default = Label("@swig//:templates"),
609+
allow_files = True,
610+
),
608611
},
609612
outputs = {
610613
"cc_out": "%{module_name}.cc",

tensorflow/tools/docker/Dockerfile.devel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1414
python-dev \
1515
rsync \
1616
software-properties-common \
17-
swig \
1817
unzip \
1918
zip \
2019
zlib1g-dev \

tensorflow/tools/docker/Dockerfile.devel-gpu

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1515
python-dev \
1616
rsync \
1717
software-properties-common \
18-
swig \
1918
unzip \
2019
zip \
2120
zlib1g-dev \

tensorflow/tools/swig/swig.sh

Lines changed: 0 additions & 25 deletions
This file was deleted.

tensorflow/workspace.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ def tf_workspace(path_prefix = "", tf_repo_name = ""):
126126
actual = str(Label("//util/python:python_headers")),
127127
)
128128

129+
native.new_http_archive(
130+
name = "pcre",
131+
sha256 = "ccdf7e788769838f8285b3ee672ed573358202305ee361cfec7a4a4fb005bbc7",
132+
url = "http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-8.39.tar.gz",
133+
strip_prefix = "pcre-8.39",
134+
build_file = str(Label("//third_party:pcre.BUILD")),
135+
)
136+
137+
native.new_http_archive(
138+
name = "swig",
139+
sha256 = "a2669657cabcedc371f63c0457407a183e0b6b2ef4e7e303c1ec9a3964cc7813",
140+
url = "http://ufpr.dl.sourceforge.net/project/swig/swig/swig-3.0.2/swig-3.0.2.tar.gz",
141+
strip_prefix = "swig-3.0.2",
142+
build_file = str(Label("//third_party:swig.BUILD")),
143+
)
144+
129145
# grpc expects //external:protobuf_clib and //external:protobuf_compiler
130146
# to point to the protobuf's compiler library.
131147
native.bind(

third_party/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
licenses(["notice"]) # Apache 2.0

third_party/pcre.BUILD

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
licenses(["notice"]) # BSD
2+
3+
exports_files(["LICENSE"])
4+
5+
cc_library(
6+
name = "pcre",
7+
srcs = [
8+
"pcre_byte_order.c",
9+
"pcre_chartables.c",
10+
"pcre_compile.c",
11+
"pcre_config.c",
12+
"pcre_dfa_exec.c",
13+
"pcre_exec.c",
14+
"pcre_fullinfo.c",
15+
"pcre_get.c",
16+
"pcre_globals.c",
17+
"pcre_internal.h",
18+
"pcre_jit_compile.c",
19+
"pcre_maketables.c",
20+
"pcre_newline.c",
21+
"pcre_ord2utf8.c",
22+
"pcre_refcount.c",
23+
"pcre_string_utils.c",
24+
"pcre_study.c",
25+
"pcre_tables.c",
26+
"pcre_ucd.c",
27+
"pcre_valid_utf8.c",
28+
"pcre_version.c",
29+
"pcre_xclass.c",
30+
"ucp.h",
31+
],
32+
hdrs = [
33+
"pcre.h",
34+
"pcreposix.h",
35+
],
36+
copts = [
37+
"-DHAVE_BCOPY=1",
38+
"-DHAVE_INTTYPES_H=1",
39+
"-DHAVE_MEMMOVE=1",
40+
"-DHAVE_STDINT_H=1",
41+
"-DHAVE_STRERROR=1",
42+
"-DHAVE_SYS_STAT_H=1",
43+
"-DHAVE_SYS_TYPES_H=1",
44+
"-DHAVE_UNISTD_H=1",
45+
"-DLINK_SIZE=2",
46+
"-DMATCH_LIMIT=10000000",
47+
"-DMATCH_LIMIT_RECURSION=1000",
48+
"-DMAX_NAME_COUNT=10000",
49+
"-DMAX_NAME_SIZE=32",
50+
"-DNEWLINE=10",
51+
"-DNO_RECURSE",
52+
"-DPARENS_NEST_LIMIT=50",
53+
"-DPCRE_STATIC=1",
54+
"-DPOSIX_MALLOC_THRESHOLD=10",
55+
"-DSTDC_HEADERS=1",
56+
"-DSUPPORT_UCP",
57+
"-DSUPPORT_UTF",
58+
],
59+
includes = ["."],
60+
visibility = ["@swig//:__pkg__"], # Please use RE2
61+
alwayslink = 1,
62+
)
63+
64+
genrule(
65+
name = "pcre_h",
66+
srcs = ["pcre.h.in"],
67+
outs = ["pcre.h"],
68+
cmd = "sed -e s/@PCRE_MAJOR@/8/" +
69+
" -e s/@PCRE_MINOR@/39/" +
70+
" -e s/@PCRE_PRERELEASE@//" +
71+
" -e s/@PCRE_DATE@/redacted/" +
72+
" $< >$@",
73+
)
74+
75+
genrule(
76+
name = "pcre_chartables_c",
77+
srcs = ["pcre_chartables.c.dist"],
78+
outs = ["pcre_chartables.c"],
79+
cmd = "cp $< $@",
80+
)

0 commit comments

Comments
 (0)