Add zlib as dependency for examples

Similar to what the Bazel build does, this change adds zlib as an additional
dependency when `SAPI_ENABLE_EXAMPLES` is set to `ON`.

PiperOrigin-RevId: 309203959
Change-Id: I201a9e6415789afb1e058bc48cebbc0fc0004fe9
This commit is contained in:
Christian Blichmann 2020-04-30 04:57:09 -07:00 committed by Copybara-Service
parent 79049b09c0
commit aafc597630
7 changed files with 159 additions and 2 deletions

View File

@ -81,7 +81,12 @@ else()
endif()
if(SAPI_ENABLE_EXAMPLES)
find_package(ZLIB REQUIRED)
if(SAPI_DOWNLOAD_ZLIB)
include(cmake/zlib/Download.cmake)
check_target(ZLIB::ZLIB)
else()
find_package(ZLIB REQUIRED)
endif()
endif()
# Find Python 3 and add its location to the cache so that its available in

View File

@ -29,5 +29,8 @@ option(SAPI_DOWNLOAD_LIBUNWIND "Download libunwind at config time" ON)
option(SAPI_DOWNLOAD_LIBCAP "Download libcap at config time" ON)
option(SAPI_DOWNLOAD_LIBFFI "Download libffi at config time" ON)
# Options for building examples
option(SAPI_ENABLE_EXAMPLES "Build example code" ON)
option(SAPI_DOWNLOAD_ZLIB "Download zlib at config time (only if SAPI_ENABLE_EXAMPLES is set)" ON)
option(SAPI_ENABLE_TESTS "Build unit tests" ON)

View File

@ -0,0 +1,30 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.12)
project(zlib-download NONE)
include(ExternalProject)
ExternalProject_Add(zlib
URL https://mirror.bazel.build/zlib.net/zlib-1.2.11.tar.gz
URL_HASH SHA256=c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1 # 2020-04-23
SOURCE_DIR "${CMAKE_BINARY_DIR}/zlib-src"
PATCH_COMMAND patch -p1
< "${CMAKE_SOURCE_DIR}/sandboxed_api/bazel/external/zlib.patch"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
BUILD_IN_SOURCE TRUE
)

77
cmake/zlib/Download.cmake Normal file
View File

@ -0,0 +1,77 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Downloads and unpacks Abseil at configure time
set(workdir "${CMAKE_BINARY_DIR}/zlib-download")
configure_file("${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in"
"${workdir}/CMakeLists.txt")
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE error
WORKING_DIRECTORY "${workdir}")
if(error)
message(FATAL_ERROR "CMake step for ${PROJECT_NAME} failed: ${error}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE error
WORKING_DIRECTORY "${workdir}")
if(error)
message(FATAL_ERROR "Build step for ${PROJECT_NAME} failed: ${error}")
endif()
set(_zlib_src "${CMAKE_BINARY_DIR}/zlib-src")
set(ZLIB_INCLUDE_DIRS ${_zlib_src})
add_library(z STATIC
${_zlib_src}/adler32.c
${_zlib_src}/compress.c
${_zlib_src}/crc32.c
${_zlib_src}/crc32.h
${_zlib_src}/deflate.c
${_zlib_src}/deflate.h
${_zlib_src}/gzclose.c
${_zlib_src}/gzguts.h
${_zlib_src}/gzlib.c
${_zlib_src}/gzread.c
${_zlib_src}/gzwrite.c
${_zlib_src}/infback.c
${_zlib_src}/inffast.c
${_zlib_src}/inffast.h
${_zlib_src}/inffixed.h
${_zlib_src}/inflate.c
${_zlib_src}/inflate.h
${_zlib_src}/inftrees.c
${_zlib_src}/inftrees.h
${_zlib_src}/trees.c
${_zlib_src}/trees.h
${_zlib_src}/uncompr.c
${_zlib_src}/zconf.h
${_zlib_src}/zlib.h
${_zlib_src}/zutil.c
${_zlib_src}/zutil.h
)
add_library(ZLIB::ZLIB ALIAS z)
target_include_directories(z PUBLIC
${_zlib_src}
)
target_compile_options(z PRIVATE
-w
-Dverbose=-1
)
target_link_libraries(z PRIVATE
sapi::base
)

View File

@ -15,4 +15,5 @@
if(SAPI_ENABLE_EXAMPLES)
add_subdirectory(stringop)
add_subdirectory(sum)
add_subdirectory(zlib)
endif()

View File

@ -0,0 +1,42 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# sandboxed_api/examples/zlib:zlib-sapi
add_sapi_library(zlib-sapi
FUNCTIONS deflateInit_
deflate
deflateEnd
INPUTS ${ZLIB_INCLUDE_DIRS}/zlib.h
LIBRARY ZLIB::ZLIB
LIBRARY_NAME Zlib
NAMESPACE "sapi::zlib"
)
add_library(sapi::zlib_sapi ALIAS zlib-sapi)
target_link_libraries(zlib-sapi PRIVATE
ZLIB::ZLIB
)
# sandboxed_api/examples/zlib:main_zlib
add_executable(main_zlib
main_zlib.cc
)
target_link_libraries(main_zlib PRIVATE
sapi::base
glog::glog
sapi::flags
sapi::sapi
sapi::status
sapi::statusor
sapi::zlib_sapi
)

View File

@ -37,5 +37,4 @@ add_executable(zpipe
add_executable(sandbox2::zpipe ALIAS zpipe)
target_link_libraries(zpipe PRIVATE
ZLIB::ZLIB
-static # Fully static link
)