Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit 4847bc8

Browse files
authored
CMake tests (#737)
* build: add some compatibility for CMake<3.17 Ensure that we can properly set `DT_RUNPATH` for libraries and executables on older CMake releases. This has been fixed upstream, but rather than requiring an extremely new CMake, provide a compatibility shim. * Experimental: add Experimental module This adds the experimental module and improves the install rules further to support renamed modules and do less work in the case of an executable target. * build: add testing support Enhance the CMake setup to be able to run the test suite. This will make it easier to enable tests for X10.
1 parent 63ae748 commit 4847bc8

File tree

10 files changed

+130
-7
lines changed

10 files changed

+130
-7
lines changed

CMakeLists.txt

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ cmake_minimum_required(VERSION 3.15.1)
22
project(TensorFlow
33
LANGUAGES CXX Swift)
44

5+
if(CMAKE_VERSION VERSION_LESS 3.17)
6+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
7+
set(CMAKE_EXECUTABLE_RUNTIME_Swift_FLAG "-Xlinker -rpath -Xlinker ")
8+
set(CMAKE_SHARED_LIBRARY_RUNTIME_Swift_FLAG "-Xlinker -rpath -Xlinker ")
9+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
10+
set(CMAKE_EXECUTABLE_RUNTIME_Swift_FLAG_SEP "")
11+
set(CMAKE_SHARED_LIBRARY_RUNTIME_Swift_FLAG_SEP "")
12+
else()
13+
set(CMAKE_EXECUTABLE_RUNTIME_Swift_FLAG_SEP ":")
14+
set(CMAKE_SHARED_LIBRARY_RUNTIME_Swift_FLAG_SEP ":")
15+
endif()
16+
endif()
17+
endif()
18+
519
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
620

721
set(CMAKE_CXX_STANDARD 14)
@@ -13,9 +27,13 @@ option(USE_BUNDLED_CTENSORFLOW
1327

1428
find_package(TensorFlow REQUIRED)
1529

30+
include(CTest)
1631
include(SwiftSupport)
1732

1833
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xllvm -sil-inline-generics>")
1934
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xllvm -sil-partial-specialization>")
2035

2136
add_subdirectory(Sources)
37+
if(BUILD_TESTING)
38+
add_subdirectory(Tests)
39+
endif()

Sources/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_subdirectory(CTensorFlow)
2+
add_subdirectory(third_party/Experimental)
23
add_subdirectory(Tensor)
34
add_subdirectory(TensorFlow)
45
if(X10_INCLUDE_DIR)

Sources/Tensor/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ add_library(Tensor SHARED
33
TensorUtilities.swift)
44
set_target_properties(Tensor PROPERTIES
55
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
6+
target_compile_options(Tensor PRIVATE
7+
$<$<BOOL:${BUILD_TESTING}>:-enable-testing>)
68

79
_install_target(Tensor)

Sources/TensorFlow/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ add_library(TensorFlow SHARED
6363
Optimizers/Optimizer.swift
6464
Optimizers/SGD.swift)
6565
set_target_properties(TensorFlow PROPERTIES
66+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}
6667
OUTPUT_NAME swiftTensorFlow)
68+
target_compile_options(TensorFlow PRIVATE
69+
$<$<BOOL:${BUILD_TESTING}>:-enable-testing>)
6770
target_link_libraries(TensorFlow PRIVATE
6871
CTensorFlow
6972
Tensor)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# NOTE: this uses an underscored name here and changes that because
2+
# `Experimental` is a utility library used to indicate that this is an
3+
# experimental build. This renaming is a quick workaround for that conflict.
4+
add_library(_Experimental
5+
Complex.swift)
6+
set_target_properties(_Experimental PROPERTIES
7+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}
8+
OUTPUT_NAME Experimental
9+
Swift_MODULE_NAME Experimental)
10+
target_compile_options(_Experimental PRIVATE
11+
$<$<BOOL:${BUILD_TESTING}>:-enable-testing>)
12+
13+
_install_target(_Experimental)

Tests/CMakeLists.txt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
find_package(XCTest CONFIG QUIET)
2+
3+
add_subdirectory(ExperimentalTests)
4+
add_subdirectory(TensorFlowTests)
5+
add_subdirectory(TensorTests)
6+
7+
add_executable(SwiftAPIsTests
8+
LinuxMain.swift)
9+
set_target_properties(SwiftAPIsTests PROPERTIES
10+
BUILD_RPATH "$<TARGET_FILE_DIR:Tensor>;$<TARGET_FILE_DIR:TensorFlow>")
11+
target_link_libraries(SwiftAPIsTests PRIVATE
12+
ExperimentalTests
13+
TensorFlowTests
14+
TensorTests)
15+
16+
add_test(NAME SwiftAPIsTests
17+
COMMAND SwiftAPIsTests)
18+
# NOTE: this workaround brought to you from the toolchain. The toolchain
19+
# pre-packages the tensorflow swift-apis, which is given preferential treatment.
20+
# Furthermore, `-no-toolchain-rpath` is not honoured on Linux. This requires
21+
# that we resort to `LD_LIBRARY_PATH` to resolve the library path even though
22+
# the `DT_RUNPATH` on the binary is set properly.
23+
set_tests_properties(SwiftAPIsTests PROPERTIES
24+
ENVIRONMENT LD_LIBRARY_PATH=$<TARGET_FILE_DIR:Tensor>:$<TARGET_FILE_DIR:TensorFlow>)
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_library(ExperimentalTests
2+
ComplexTests.swift
3+
XCTestManifests.swift)
4+
target_link_libraries(ExperimentalTests PUBLIC
5+
_Experimental
6+
XCTest)

Tests/TensorFlowTests/CMakeLists.txt

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
add_library(TensorFlowTests
2+
ContextTests.swift
3+
FreezableTests.swift
4+
Helpers.swift
5+
InitializerTests.swift
6+
LayerTests.swift
7+
LazyTensorEvaluationTests.swift
8+
LazyTensorExplicitTraceTests.swift
9+
LazyTensorHandleTests.swift
10+
LazyTensorOperationTests.swift
11+
LazyTensorShapeInferenceTests.swift
12+
LazyTensorTestHelper.swift
13+
LazyTensorTFFunctionBuilderTests.swift
14+
LazyTensorTraceCacheTests.swift
15+
LazyTensorTraceTests.swift
16+
LossTests.swift
17+
OptimizerTests.swift
18+
RuntimeTests.swift
19+
SequencedTests.swift
20+
SequencedTests.swift.gyb
21+
SequentialTests.swift
22+
SequentialTests.swift.gyb
23+
TensorAutoDiffTests.swift
24+
TensorGroupTests.swift
25+
TensorTests.swift
26+
TrivialModelTests.swift
27+
XCTestManifests.swift
28+
29+
OperatorTests/BasicTests.swift
30+
OperatorTests/ComparisonTests.swift
31+
OperatorTests/DatasetTests.swift
32+
OperatorTests/ImageTests.swift
33+
OperatorTests/LinearAlgebraTests.swift
34+
OperatorTests/MathTests.swift
35+
OperatorTests/MatrixTests.swift)
36+
target_link_libraries(TensorFlowTests PUBLIC
37+
TensorFlow
38+
XCTest)

Tests/TensorTests/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
add_library(TensorTests
2+
PRNGTests.swift
3+
UtilitiesTests.swift
4+
XCTestManifests.swift)
5+
target_link_libraries(TensorTests PUBLIC
6+
Tensor
7+
XCTest)

cmake/modules/SwiftSupport.cmake

+18-7
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,34 @@ function(get_swift_host_os result_var_name)
5353
endfunction()
5454

5555
function(_install_target module)
56-
get_swift_host_arch(swift_arch)
5756
get_swift_host_os(swift_os)
57+
get_target_property(type ${module} TYPE)
58+
5859
install(TARGETS ${module}
5960
ARCHIVE DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}
6061
LIBRARY DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}
6162
RUNTIME DESTINATION bin)
63+
if(type STREQUAL EXECUTABLE)
64+
return()
65+
endif()
66+
67+
get_swift_host_arch(swift_arch)
68+
get_target_property(module_name ${module} Swift_MODULE_NAME)
69+
if(NOT module_name)
70+
set(module_name ${module})
71+
endif()
72+
6273
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
63-
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module}.swiftdoc
64-
DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}/${mmodule}.swiftmodule
74+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
75+
DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}/${module_name}.swiftmodule
6576
RENAME ${swift_arch}.swiftdoc)
66-
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module}.swiftmodule
67-
DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}/${mmodule}.swiftmodule
77+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
78+
DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}/${module_name}.swiftmodule
6879
RENAME ${swift_arch}.swiftmodule)
6980
else()
7081
install(FILES
71-
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module}.swiftdoc
72-
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module}.swiftmodule
82+
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
83+
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
7384
DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${swift_os}/${swift_arch})
7485
endif()
7586
endfunction()

0 commit comments

Comments
 (0)