Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake: optionally link to ZLIB as systemlib / shared objects. #15382

Merged
merged 1 commit into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion tensorflow/contrib/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ if (NOT WIN32)
# for targets that link ${CMAKE_THREAD_LIBS_INIT}.
find_package (Threads)

# Options for linking CUDA/CUDNN libraries
option(tensorflow_PATH_STATIC_LIB "Additional library search path for libcudnn_static.a, libnccl_static.a, libculibos.a" /usr/local/cuda/lib64/)
option(tensorflow_CUDNN_INCLUDE "cudnn.h header install path" /usr/include/)
if (NOT tensorflow_CUDNN_INCLUDE)
Expand All @@ -73,6 +74,14 @@ if (NOT WIN32)
# option's default value is OFF. Fill it with real default values
set(tensorflow_CUDA_LIBRARY_PATH /usr/local/cuda/lib64)
endif (NOT tensorflow_CUDA_LIBRARY_PATH)

# Options for linking other libraries
option(systemlib_ZLIB "Use the system installed library as shared objects instead of downloading ZLIB and statically linking to it: ZLIB" OFF)

option(systemlib_ALL "Turn on every possible systemlib_* options" OFF)
if (systemlib_ALL)
set (systmelib_ZLIB ON)
endif (systemlib_ALL)
endif()

if (WIN32)
Expand Down Expand Up @@ -188,8 +197,10 @@ if (tensorflow_BUILD_CC_TESTS)
include(googletest)
endif()

add_definitions(${ADD_CFLAGS})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be protected like:

if len(ADD_CLAGS) > 0
  add_definitions...

I cannot see how add_definitions or link_directories will behave if these are unset.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought add_definitions with null strings or unset strings behaves ok (no effect on runtime); however, I'll recheck this issue.

link_directories(${ADD_LINK_DIRECTORY})

set(tensorflow_EXTERNAL_LIBRARIES
${zlib_STATIC_LIBRARIES}
${gif_STATIC_LIBRARIES}
${png_STATIC_LIBRARIES}
${jpeg_STATIC_LIBRARIES}
Expand All @@ -203,6 +214,15 @@ set(tensorflow_EXTERNAL_LIBRARIES
${re2_STATIC_LIBRARIES}
${sqlite_STATIC_LIBRARIES}
)

if (systemlib_ZLIB)
set(tensorflow_EXTERNAL_LIBRARIES ${tensorflow_EXTERNAL_LIBRARIES}
${ZLIB_LIBRARIES})
else (systemlib_ZLIB)
set(tensorflow_EXTERNAL_LIBRARIES ${tensorflow_EXTERNAL_LIBRARIES}
${zlib_STATIC_LIBRARIES})
endif (systemlib_ZLIB)

set(tensorflow_EXTERNAL_DEPENDENCIES
zlib_copy_headers_to_destination
gif_copy_headers_to_destination
Expand Down
108 changes: 61 additions & 47 deletions tensorflow/contrib/cmake/external/zlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,75 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
include (ExternalProject)
if (systemlib_ZLIB)
find_package(PkgConfig)
pkg_search_module(ZLIB REQUIRED zlib)
set(zlib_INCLUDE_DIR ${ZLIB_INCLUDE_DIRS})
set(ADD_LINK_DIRECTORY ${ADD_LINK_DIRECTORY} ${ZLIB_LIBRARY_DIRS})
set(ADD_CFLAGS ${ADD_CFLAGS} ${ZLIB_CFLAGS_OTHER})

set(zlib_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/external/zlib_archive)
set(ZLIB_URL https://github.com/madler/zlib)
set(ZLIB_BUILD ${CMAKE_CURRENT_BINARY_DIR}/zlib/src/zlib)
set(ZLIB_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/zlib/install)
set(ZLIB_TAG 50893291621658f355bc5b4d450a8d06a563053d)
# To meet DEPENDS zlib from other projects.
# If we hit this line, zlib is already built and installed to the system.
add_custom_target(zlib)
add_custom_target(zlib_copy_headers_to_destination)

if(WIN32)
if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
set(zlib_STATIC_LIBRARIES
debug ${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstaticd.lib
optimized ${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstatic.lib)
else()
if(CMAKE_BUILD_TYPE EQUAL Debug)
else (systemlib_ZLIB)
include (ExternalProject)

set(zlib_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/external/zlib_archive)
set(ZLIB_URL https://github.com/madler/zlib)
set(ZLIB_BUILD ${CMAKE_CURRENT_BINARY_DIR}/zlib/src/zlib)
set(ZLIB_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/zlib/install)
set(ZLIB_TAG 50893291621658f355bc5b4d450a8d06a563053d)

if(WIN32)
if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
set(zlib_STATIC_LIBRARIES
${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstaticd.lib)
debug ${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstaticd.lib
optimized ${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstatic.lib)
else()
set(zlib_STATIC_LIBRARIES
${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstatic.lib)
if(CMAKE_BUILD_TYPE EQUAL Debug)
set(zlib_STATIC_LIBRARIES
${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstaticd.lib)
else()
set(zlib_STATIC_LIBRARIES
${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstatic.lib)
endif()
endif()
else()
set(zlib_STATIC_LIBRARIES
${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/libz.a)
endif()
else()
set(zlib_STATIC_LIBRARIES
${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/libz.a)
endif()

set(ZLIB_HEADERS
"${ZLIB_INSTALL}/include/zconf.h"
"${ZLIB_INSTALL}/include/zlib.h"
)
set(ZLIB_HEADERS
"${ZLIB_INSTALL}/include/zconf.h"
"${ZLIB_INSTALL}/include/zlib.h"
)

ExternalProject_Add(zlib
PREFIX zlib
GIT_REPOSITORY ${ZLIB_URL}
GIT_TAG ${ZLIB_TAG}
INSTALL_DIR ${ZLIB_INSTALL}
BUILD_IN_SOURCE 1
BUILD_BYPRODUCTS ${zlib_STATIC_LIBRARIES}
DOWNLOAD_DIR "${DOWNLOAD_LOCATION}"
CMAKE_CACHE_ARGS
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=${tensorflow_ENABLE_POSITION_INDEPENDENT_CODE}
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_INSTALL_PREFIX:STRING=${ZLIB_INSTALL}
)
ExternalProject_Add(zlib
PREFIX zlib
GIT_REPOSITORY ${ZLIB_URL}
GIT_TAG ${ZLIB_TAG}
INSTALL_DIR ${ZLIB_INSTALL}
BUILD_IN_SOURCE 1
BUILD_BYPRODUCTS ${zlib_STATIC_LIBRARIES}
DOWNLOAD_DIR "${DOWNLOAD_LOCATION}"
CMAKE_CACHE_ARGS
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=${tensorflow_ENABLE_POSITION_INDEPENDENT_CODE}
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_INSTALL_PREFIX:STRING=${ZLIB_INSTALL}
)

# put zlib includes in the directory where they are expected
add_custom_target(zlib_create_destination_dir
COMMAND ${CMAKE_COMMAND} -E make_directory ${zlib_INCLUDE_DIR}
DEPENDS zlib)
# put zlib includes in the directory where they are expected
add_custom_target(zlib_create_destination_dir
COMMAND ${CMAKE_COMMAND} -E make_directory ${zlib_INCLUDE_DIR}
DEPENDS zlib)

add_custom_target(zlib_copy_headers_to_destination
DEPENDS zlib_create_destination_dir)
add_custom_target(zlib_copy_headers_to_destination
DEPENDS zlib_create_destination_dir)

foreach(header_file ${ZLIB_HEADERS})
add_custom_command(TARGET zlib_copy_headers_to_destination PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${header_file} ${zlib_INCLUDE_DIR})
endforeach()
foreach(header_file ${ZLIB_HEADERS})
add_custom_command(TARGET zlib_copy_headers_to_destination PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${header_file} ${zlib_INCLUDE_DIR})
endforeach()
endif (systemlib_ZLIB)