Skip to content

Commit 3267cbb

Browse files
authored
Allow compiling against slightly different tensorflow directory layouts. (#21199)
1 parent 07579ea commit 3267cbb

File tree

7 files changed

+39
-11
lines changed

7 files changed

+39
-11
lines changed

cmake/modules/FindTensorFlow.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
include(FindPackageHandleStandardArgs)
55

66
find_path(TF_INCLUDE_DIR
7-
NAMES tensorflow/c
7+
NAMES third_party/tensorflow/c tensorflow/c
88
HINTS ${SWIFT_TENSORFLOW_TARGET_INCLUDE_DIR} /usr/include /usr/local/include)
9+
if (EXISTS ${TF_INCLUDE_DIR}/third_party/tensorflow/c/c_api.h)
10+
# This is experimental and not covered by CI.
11+
set(TF_PATH_ADJUSTMENT "third_party")
12+
else()
13+
# Note: This is the normal workflow.
14+
set(TF_PATH_ADJUSTMENT "")
15+
endif()
916

1017
find_library(TF_LIBRARY
1118
NAMES tensorflow

lib/AST/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ endif()
55

66
if(SWIFT_ENABLE_TENSORFLOW)
77
find_package(TensorFlow REQUIRED)
8+
if (TF_PATH_ADJUSTMENT)
9+
include_directories(BEFORE "${TF_INCLUDE_DIR}/${TF_PATH_ADJUSTMENT}")
10+
endif()
811
include_directories(BEFORE "${TF_INCLUDE_DIR}")
912
endif()
1013

lib/SIL/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
if(SWIFT_ENABLE_TENSORFLOW)
22
find_package(TensorFlow REQUIRED)
3+
if (TF_PATH_ADJUSTMENT)
4+
include_directories(BEFORE "${TF_INCLUDE_DIR}/${TF_PATH_ADJUSTMENT}" )
5+
endif()
36
include_directories(BEFORE "${TF_INCLUDE_DIR}" )
47
endif()
58

lib/SILOptimizer/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
if(SWIFT_ENABLE_TENSORFLOW)
22
find_package(TensorFlow REQUIRED)
3+
if (TF_PATH_ADJUSTMENT)
4+
include_directories(BEFORE "${TF_INCLUDE_DIR}/${TF_PATH_ADJUSTMENT}")
5+
endif()
36
include_directories(BEFORE "${TF_INCLUDE_DIR}")
47
endif()
58

stdlib/public/CTensorFlow/CMakeLists.txt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ include("../../../cmake/modules/SwiftList.cmake")
2323
find_package(TensorFlow REQUIRED)
2424
message(STATUS "Building CTensorFlow.")
2525

26+
if (TF_PATH_ADJUSTMENT)
27+
include_directories(BEFORE "${TF_INCLUDE_DIR}/${TF_PATH_ADJUSTMENT}")
28+
endif()
2629
include_directories(BEFORE "${TF_INCLUDE_DIR}")
2730

2831
# Get target SDKs.
@@ -53,20 +56,31 @@ foreach(sdk ${TARGET_SDKS})
5356

5457
# Copy headers.
5558
macro(copy_tf_headers src dst)
59+
if (TF_PATH_ADJUSTMENT)
60+
set(copy_tf_headers_path_adjustment "${TF_PATH_ADJUSTMENT}/")
61+
else()
62+
set(copy_tf_headers_path_adjustment "")
63+
endif()
5664
add_custom_command(
5765
OUTPUT "${output_dir}/${dst}"
5866
DEPENDS "${src}"
5967
COMMAND
6068
"${SWIFT_SOURCE_DIR}/utils/fix-tf-headers.sh"
6169
"${src}"
62-
"${output_dir}/${dst}")
70+
"${output_dir}/${dst}"
71+
"${copy_tf_headers_path_adjustment}")
6372

6473
list(APPEND outputs "${output_dir}/${dst}")
6574
endmacro(copy_tf_headers)
6675

67-
copy_tf_headers("${TF_INCLUDE_DIR}/tensorflow/c/c_api.h" c_api.h)
68-
copy_tf_headers("${TF_INCLUDE_DIR}/tensorflow/c/c_api_experimental.h" c_api_experimental.h)
69-
copy_tf_headers("${TF_INCLUDE_DIR}/tensorflow/c/eager/c_api.h" c_api_eager.h)
76+
if (TF_PATH_ADJUSTMENT)
77+
set(adjusted_tf_include_dir "${TF_INCLUDE_DIR}/${TF_PATH_ADJUSTMENT}")
78+
else()
79+
set(adjusted_tf_include_dir "${TF_INCLUDE_DIR}")
80+
endif()
81+
copy_tf_headers("${adjusted_tf_include_dir}/tensorflow/c/c_api.h" c_api.h)
82+
copy_tf_headers("${adjusted_tf_include_dir}/tensorflow/c/c_api_experimental.h" c_api_experimental.h)
83+
copy_tf_headers("${adjusted_tf_include_dir}/tensorflow/c/eager/c_api.h" c_api_eager.h)
7084
copy_tf_headers("${CMAKE_CURRENT_SOURCE_DIR}/ctensorflow_init.h" ctensorflow_init.h)
7185

7286
# Put the output dir itself last so that it isn't considered the primary output.

stdlib/public/TensorFlow/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ endif()
2121
find_package(TensorFlow REQUIRED)
2222
message(STATUS "Building TensorFlow.")
2323

24-
include_directories(BEFORE "${TF_INCLUDE_DIR}")
25-
2624
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
2725
set(swift_stdlib_compile_flags "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}")
2826
list(APPEND swift_stdlib_compile_flags "-Xllvm" "-sil-inline-generics")
2927
list(APPEND swift_stdlib_compile_flags "-Xllvm" "-sil-partial-specialization")
3028
list(APPEND swift_stdlib_compile_flags "-Xfrontend" "-enable-sil-ownership")
3129
list(APPEND swift_stdlib_compile_flags "-swift-version" "4")
3230
list(APPEND swift_stdlib_compile_flags "-force-single-frontend-invocation")
33-
list(APPEND swift_stdlib_compile_flags "-Xcc" "-I${TF_INCLUDE_DIR}")
3431
# FIXME(SR-7972): Some tests fail when TensorFlow is optimized.
3532
list(APPEND swift_stdlib_compile_flags "-Onone")
3633

utils/fix-tf-headers.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ set -x
77

88
src="$1"
99
dst="$2"
10+
path_adjustment="$3"
1011

1112
mkdir -p "$(dirname $2)"
1213
cp "$1" "$2"
13-
sed -i -e 's#include "tensorflow/c/c_api.h"#include "c_api.h"#g' "$2"
14-
sed -i -e 's#include "tensorflow/c/c_api_experimental.h"#include "c_api_experimental.h"#g' "$2"
15-
sed -i -e 's#include "tensorflow/c/eager/c_api.h"#include "c_api_eager.h"#g' "$2"
14+
sed -i -e 's#include "'"$3"'tensorflow/c/c_api.h"#include "c_api.h"#g' "$2"
15+
sed -i -e 's#include "'"$3"'tensorflow/c/c_api_experimental.h"#include "c_api_experimental.h"#g' "$2"
16+
sed -i -e 's#include "'"$3"'tensorflow/c/eager/c_api.h"#include "c_api_eager.h"#g' "$2"

0 commit comments

Comments
 (0)