1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15if (systemlib_ZLIB)
16  find_package(PkgConfig)
17  pkg_search_module(ZLIB REQUIRED zlib)
18  set(zlib_INCLUDE_DIR ${ZLIB_INCLUDE_DIRS})
19  set(ADD_LINK_DIRECTORY ${ADD_LINK_DIRECTORY} ${ZLIB_LIBRARY_DIRS})
20  set(ADD_CFLAGS ${ADD_CFLAGS} ${ZLIB_CFLAGS_OTHER})
21
22  # To meet DEPENDS zlib from other projects.
23  # If we hit this line, zlib is already built and installed to the system.
24  add_custom_target(zlib)
25  add_custom_target(zlib_copy_headers_to_destination)
26
27else (systemlib_ZLIB)
28  include (ExternalProject)
29
30  set(zlib_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/external/zlib_archive)
31  set(ZLIB_URL https://github.com/madler/zlib)
32  set(ZLIB_BUILD ${CMAKE_CURRENT_BINARY_DIR}/zlib/src/zlib)
33  set(ZLIB_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/zlib/install)
34  # Match zlib version in tensorflow/workspace.bzl
35  set(ZLIB_TAG v1.2.11)
36
37  if(WIN32)
38    if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
39      set(zlib_STATIC_LIBRARIES
40          debug ${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstaticd.lib
41          optimized ${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstatic.lib)
42    else()
43      if(CMAKE_BUILD_TYPE EQUAL Debug)
44        set(zlib_STATIC_LIBRARIES
45            ${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstaticd.lib)
46      else()
47        set(zlib_STATIC_LIBRARIES
48            ${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/zlibstatic.lib)
49      endif()
50    endif()
51  else()
52    set(zlib_STATIC_LIBRARIES
53        ${CMAKE_CURRENT_BINARY_DIR}/zlib/install/lib/libz.a)
54  endif()
55
56  set(ZLIB_HEADERS
57      "${ZLIB_INSTALL}/include/zconf.h"
58      "${ZLIB_INSTALL}/include/zlib.h"
59  )
60
61  ExternalProject_Add(zlib
62      PREFIX zlib
63      GIT_REPOSITORY ${ZLIB_URL}
64      GIT_TAG ${ZLIB_TAG}
65      INSTALL_DIR ${ZLIB_INSTALL}
66      BUILD_IN_SOURCE 1
67      BUILD_BYPRODUCTS ${zlib_STATIC_LIBRARIES}
68      DOWNLOAD_DIR "${DOWNLOAD_LOCATION}"
69      CMAKE_CACHE_ARGS
70          -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=${tensorflow_ENABLE_POSITION_INDEPENDENT_CODE}
71          -DCMAKE_BUILD_TYPE:STRING=Release
72          -DCMAKE_INSTALL_PREFIX:STRING=${ZLIB_INSTALL}
73  )
74
75  # put zlib includes in the directory where they are expected
76  add_custom_target(zlib_create_destination_dir
77      COMMAND ${CMAKE_COMMAND} -E make_directory ${zlib_INCLUDE_DIR}
78      DEPENDS zlib)
79
80  add_custom_target(zlib_copy_headers_to_destination
81      DEPENDS zlib_create_destination_dir)
82
83  foreach(header_file ${ZLIB_HEADERS})
84      add_custom_command(TARGET zlib_copy_headers_to_destination PRE_BUILD
85      COMMAND ${CMAKE_COMMAND} -E copy_if_different ${header_file} ${zlib_INCLUDE_DIR})
86  endforeach()
87endif (systemlib_ZLIB)
88