Skip to content

Commit

Permalink
[CMake] Simplify CMake handling for zlib
Browse files Browse the repository at this point in the history
Rather than handling zlib handling manually, use find_package from CMake
to find zlib properly. Use this to normalize the LLVM_ENABLE_ZLIB,
HAVE_ZLIB, HAVE_ZLIB_H. Furthermore, require zlib if LLVM_ENABLE_ZLIB is
set to YES, which requires the distributor to explicitly select whether
zlib is enabled or not. This simplifies the CMake handling and usage in
the rest of the tooling.

This is a reland of abb0075 with all followup changes and fixes that
should address issues that were reported in PR44780.

Differential Revision: https://reviews.llvm.org/D79219
  • Loading branch information
petrhosek committed Jul 23, 2020
1 parent c6f2142 commit 1d09ecf
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 68 deletions.
4 changes: 4 additions & 0 deletions clang/CMakeLists.txt
Expand Up @@ -114,6 +114,10 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)

if(LLVM_ENABLE_ZLIB)
find_package(ZLIB)
endif()

include(AddLLVM)
include(TableGen)
include(HandleLLVMOptions)
Expand Down
11 changes: 1 addition & 10 deletions clang/test/CMakeLists.txt
Expand Up @@ -9,23 +9,14 @@ endif ()

string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} CLANG_TOOLS_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})

if(CLANG_BUILT_STANDALONE)
# Set HAVE_LIBZ according to recorded LLVM_ENABLE_ZLIB value. This
# value is forced to 0 if zlib was not found, so it is fine to use it
# instead of HAVE_LIBZ (not recorded).
if(LLVM_ENABLE_ZLIB)
set(HAVE_LIBZ 1)
endif()
endif()

llvm_canonicalize_cmake_booleans(
CLANG_BUILD_EXAMPLES
CLANG_ENABLE_ARCMT
CLANG_ENABLE_STATIC_ANALYZER
CLANG_SPAWN_CC1
ENABLE_BACKTRACES
ENABLE_EXPERIMENTAL_NEW_PASS_MANAGER
HAVE_LIBZ
LLVM_ENABLE_ZLIB
LLVM_ENABLE_PER_TARGET_RUNTIME_DIR
LLVM_ENABLE_PLUGINS
LLVM_ENABLE_THREADS)
Expand Down
2 changes: 1 addition & 1 deletion clang/test/lit.site.cfg.py.in
Expand Up @@ -16,7 +16,7 @@ config.host_triple = "@LLVM_HOST_TRIPLE@"
config.target_triple = "@TARGET_TRIPLE@"
config.host_cxx = "@CMAKE_CXX_COMPILER@"
config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@"
config.have_zlib = @HAVE_LIBZ@
config.have_zlib = @LLVM_ENABLE_ZLIB@
config.clang_arcmt = @CLANG_ENABLE_ARCMT@
config.clang_default_cxx_stdlib = "@CLANG_DEFAULT_CXX_STDLIB@"
config.clang_staticanalyzer = @CLANG_ENABLE_STATIC_ANALYZER@
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/test/lit.common.configured.in
Expand Up @@ -57,7 +57,7 @@ elif config.android:
else:
set_default("target_suffix", "-%s" % config.target_arch)

set_default("have_zlib", "@HAVE_LIBZ@")
set_default("have_zlib", "@LLVM_ENABLE_ZLIB@")
set_default("libcxx_used", "@LLVM_LIBCXX_USED@")

# LLVM tools dir can be passed in lit parameters, so try to
Expand Down
4 changes: 4 additions & 0 deletions lld/CMakeLists.txt
Expand Up @@ -51,6 +51,10 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)

if(LLVM_ENABLE_ZLIB)
find_package(ZLIB)
endif()

include(AddLLVM)
include(TableGen)
include(HandleLLVMOptions)
Expand Down
11 changes: 1 addition & 10 deletions lld/test/CMakeLists.txt
Expand Up @@ -4,17 +4,8 @@ set(LLVM_BUILD_MODE "%(build_mode)s")
set(LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}/%(build_config)s")
set(LLVM_LIBS_DIR "${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/%(build_config)s")

if(LLD_BUILT_STANDALONE)
# Set HAVE_LIBZ according to recorded LLVM_ENABLE_ZLIB value. This
# value is forced to 0 if zlib was not found, so it is fine to use it
# instead of HAVE_LIBZ (not recorded).
if(LLVM_ENABLE_ZLIB)
set(HAVE_LIBZ 1)
endif()
endif()

llvm_canonicalize_cmake_booleans(
HAVE_LIBZ
LLVM_ENABLE_ZLIB
LLVM_LIBXML2_ENABLED
)

Expand Down
2 changes: 1 addition & 1 deletion lld/test/lit.site.cfg.py.in
Expand Up @@ -14,7 +14,7 @@ config.lld_libs_dir = "@LLVM_LIBRARY_OUTPUT_INTDIR@"
config.lld_tools_dir = "@LLVM_RUNTIME_OUTPUT_INTDIR@"
config.target_triple = "@TARGET_TRIPLE@"
config.python_executable = "@Python3_EXECUTABLE@"
config.have_zlib = @HAVE_LIBZ@
config.have_zlib = @LLVM_ENABLE_ZLIB@
config.sizeof_void_p = @CMAKE_SIZEOF_VOID_P@

# Support substitution of the tools and libs dirs with user parameters. This is
Expand Down
Expand Up @@ -50,7 +50,7 @@
#include <compression.h>
#endif

#if defined(HAVE_LIBZ)
#if LLVM_ENABLE_ZLIB
#include <zlib.h>
#endif

Expand Down Expand Up @@ -582,7 +582,7 @@ bool GDBRemoteCommunication::DecompressPacket() {
}
#endif

#if defined(HAVE_LIBZ)
#if LLVM_ENABLE_ZLIB
if (decompressed_bytes == 0 && decompressed_bufsize != ULONG_MAX &&
decompressed_buffer != nullptr &&
m_compression_type == CompressionType::ZlibDeflate) {
Expand Down
Expand Up @@ -1053,7 +1053,7 @@ void GDBRemoteCommunicationClient::MaybeEnableCompression(
}
#endif

#if defined(HAVE_LIBZ)
#if LLVM_ENABLE_ZLIB
if (avail_type == CompressionType::None) {
for (auto compression : supported_compressions) {
if (compression == "zlib-deflate") {
Expand Down
36 changes: 11 additions & 25 deletions llvm/cmake/config-ix.cmake
Expand Up @@ -56,7 +56,6 @@ check_include_file(sys/types.h HAVE_SYS_TYPES_H)
check_include_file(termios.h HAVE_TERMIOS_H)
check_include_file(unistd.h HAVE_UNISTD_H)
check_include_file(valgrind/valgrind.h HAVE_VALGRIND_VALGRIND_H)
check_include_file(zlib.h HAVE_ZLIB_H)
check_include_file(fenv.h HAVE_FENV_H)
check_symbol_exists(FE_ALL_EXCEPT "fenv.h" HAVE_DECL_FE_ALL_EXCEPT)
check_symbol_exists(FE_INEXACT "fenv.h" HAVE_DECL_FE_INEXACT)
Expand Down Expand Up @@ -118,19 +117,6 @@ endif()
# Don't look for these libraries if we're using MSan, since uninstrumented third
# party code may call MSan interceptors like strlen, leading to false positives.
if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
set(HAVE_LIBZ 0)
if(LLVM_ENABLE_ZLIB)
foreach(library z zlib_static zlib)
string(TOUPPER ${library} library_suffix)
check_library_exists(${library} compress2 "" HAVE_LIBZ_${library_suffix})
if(HAVE_LIBZ_${library_suffix})
set(HAVE_LIBZ 1)
set(ZLIB_LIBRARIES "${library}")
break()
endif()
endforeach()
endif()

# Don't look for these libraries on Windows.
if (NOT PURE_WINDOWS)
# Skip libedit if using ASan as it contains memory leaks.
Expand All @@ -154,6 +140,15 @@ if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
set(HAVE_TERMINFO 0)
endif()

if(LLVM_ENABLE_ZLIB)
if(LLVM_ENABLE_ZLIB STREQUAL FORCE_ON)
find_package(ZLIB REQUIRED)
else()
find_package(ZLIB)
endif()
set(LLVM_ENABLE_ZLIB "${ZLIB_FOUND}")
endif()

find_library(ICONV_LIBRARY_PATH NAMES iconv libiconv libiconv-2 c)
set(LLVM_LIBXML2_ENABLED 0)
set(LIBXML2_FOUND 0)
Expand All @@ -168,17 +163,15 @@ if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
endif()
endif()
endif()
else()
set(LLVM_ENABLE_ZLIB FALSE)
endif()
endif()

if (LLVM_ENABLE_LIBXML2 STREQUAL "FORCE_ON" AND NOT LLVM_LIBXML2_ENABLED)
message(FATAL_ERROR "Failed to congifure libxml2")
endif()

if (LLVM_ENABLE_ZLIB STREQUAL "FORCE_ON" AND NOT HAVE_LIBZ)
message(FATAL_ERROR "Failed to configure zlib")
endif()

check_library_exists(xar xar_open "" HAVE_LIBXAR)
if(HAVE_LIBXAR)
set(XAR_LIB xar)
Expand Down Expand Up @@ -517,13 +510,6 @@ else( LLVM_ENABLE_THREADS )
message(STATUS "Threads disabled.")
endif()

if (LLVM_ENABLE_ZLIB )
# Check if zlib is available in the system.
if ( NOT HAVE_ZLIB_H OR NOT HAVE_LIBZ )
set(LLVM_ENABLE_ZLIB 0)
endif()
endif()

if (LLVM_ENABLE_DOXYGEN)
message(STATUS "Doxygen enabled.")
find_package(Doxygen REQUIRED)
Expand Down
6 changes: 0 additions & 6 deletions llvm/include/llvm/Config/config.h.cmake
Expand Up @@ -109,9 +109,6 @@
/* Define to 1 if you have the `pthread_setname_np' function. */
#cmakedefine HAVE_PTHREAD_SETNAME_NP ${HAVE_PTHREAD_SETNAME_NP}

/* Define to 1 if you have the `z' library (-lz). */
#cmakedefine HAVE_LIBZ ${HAVE_LIBZ}

/* Define to 1 if you have the <link.h> header file. */
#cmakedefine HAVE_LINK_H ${HAVE_LINK_H}

Expand Down Expand Up @@ -220,9 +217,6 @@
/* Define to 1 if you have the <valgrind/valgrind.h> header file. */
#cmakedefine HAVE_VALGRIND_VALGRIND_H ${HAVE_VALGRIND_VALGRIND_H}

/* Define to 1 if you have the <zlib.h> header file. */
#cmakedefine HAVE_ZLIB_H ${HAVE_ZLIB_H}

/* Have host's _alloca */
#cmakedefine HAVE__ALLOCA ${HAVE__ALLOCA}

Expand Down
34 changes: 29 additions & 5 deletions llvm/lib/Support/CMakeLists.txt
@@ -1,7 +1,7 @@
set(system_libs)
if ( LLVM_ENABLE_ZLIB AND HAVE_LIBZ )
set(system_libs ${system_libs} ${ZLIB_LIBRARIES})
if(LLVM_ENABLE_ZLIB)
set(imported_libs ZLIB::ZLIB)
endif()

if( MSVC OR MINGW )
# libuuid required for FOLDERID_Profile usage in lib/Support/Windows/Path.inc.
# advapi32 required for CryptAcquireContextW in lib/Support/Windows/Path.inc.
Expand Down Expand Up @@ -194,10 +194,34 @@ add_llvm_component_library(LLVMSupport
${LLVM_MAIN_INCLUDE_DIR}/llvm/ADT
${LLVM_MAIN_INCLUDE_DIR}/llvm/Support
${Backtrace_INCLUDE_DIRS}
LINK_LIBS ${system_libs} ${delayload_flags} ${Z3_LINK_FILES}
LINK_LIBS ${system_libs} ${imported_libs} ${delayload_flags} ${Z3_LINK_FILES}
)

set_property(TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS "${system_libs}")
set(llvm_system_libs ${system_libs})

if(LLVM_ENABLE_ZLIB)
string(TOUPPER ${CMAKE_BUILD_TYPE} build_type)
get_property(zlib_library TARGET ZLIB::ZLIB PROPERTY LOCATION_${build_type})
if(NOT zlib_library)
get_property(zlib_library TARGET ZLIB::ZLIB PROPERTY LOCATION)
endif()
get_filename_component(zlib_library ${zlib_library} NAME)
if(CMAKE_STATIC_LIBRARY_PREFIX AND zlib_library MATCHES "^${CMAKE_STATIC_LIBRARY_PREFIX}.*")
STRING(REGEX REPLACE "^${CMAKE_STATIC_LIBRARY_PREFIX}" "" zlib_library ${zlib_library})
endif()
if(CMAKE_STATIC_LIBRARY_SUFFIX AND zlib_library MATCHES ".*${CMAKE_STATIC_LIBRARY_SUFFIX}$")
STRING(REGEX REPLACE "${CMAKE_STATIC_LIBRARY_SUFFIX}$" "" zlib_library ${zlib_library})
endif()
if(CMAKE_SHARED_LIBRARY_PREFIX AND zlib_library MATCHES "^${CMAKE_SHARED_LIBRARY_PREFIX}.*")
STRING(REGEX REPLACE "^${CMAKE_SHARED_LIBRARY_PREFIX}" "" zlib_library ${zlib_library})
endif()
if(CMAKE_SHARED_LIBRARY_SUFFIX AND zlib_library MATCHES ".*${CMAKE_SHARED_LIBRARY_SUFFIX}$")
STRING(REGEX REPLACE "${CMAKE_SHARED_LIBRARY_SUFFIX}$" "" zlib_library ${zlib_library})
endif()
set(llvm_system_libs ${llvm_system_libs} "${zlib_library}")
endif()

set_property(TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS "${llvm_system_libs}")

if(LLVM_WITH_Z3)
target_include_directories(LLVMSupport SYSTEM
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Support/CRC.cpp
Expand Up @@ -25,7 +25,7 @@

using namespace llvm;

#if LLVM_ENABLE_ZLIB == 0 || !HAVE_ZLIB_H
#if !LLVM_ENABLE_ZLIB

static const uint32_t CRCTable[256] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Support/Compression.cpp
Expand Up @@ -17,13 +17,13 @@
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
#if LLVM_ENABLE_ZLIB
#include <zlib.h>
#endif

using namespace llvm;

#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
#if LLVM_ENABLE_ZLIB
static Error createError(StringRef Err) {
return make_error<StringError>(Err, inconvertibleErrorCode());
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CMakeLists.txt
@@ -1,12 +1,12 @@
llvm_canonicalize_cmake_booleans(
BUILD_SHARED_LIBS
HAVE_LIBXAR
HAVE_LIBZ
HAVE_OCAMLOPT
HAVE_OCAML_OUNIT
LLVM_ENABLE_DIA_SDK
LLVM_ENABLE_FFI
LLVM_ENABLE_THREADS
LLVM_ENABLE_ZLIB
LLVM_INCLUDE_GO_TESTS
LLVM_LIBXML2_ENABLED
LLVM_LINK_LLVM_DYLIB
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/lit.site.cfg.py.in
Expand Up @@ -33,7 +33,7 @@ config.host_cxx = "@HOST_CXX@"
config.host_ldflags = '@HOST_LDFLAGS@'
config.llvm_use_intel_jitevents = @LLVM_USE_INTEL_JITEVENTS@
config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@"
config.have_zlib = @HAVE_LIBZ@
config.have_zlib = @LLVM_ENABLE_ZLIB@
config.have_libxar = @HAVE_LIBXAR@
config.have_dia_sdk = @LLVM_ENABLE_DIA_SDK@
config.enable_ffi = @LLVM_ENABLE_FFI@
Expand Down
2 changes: 1 addition & 1 deletion llvm/unittests/Support/CompressionTest.cpp
Expand Up @@ -21,7 +21,7 @@ using namespace llvm;

namespace {

#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
#if LLVM_ENABLE_ZLIB

void TestZlibCompression(StringRef Input, int Level) {
SmallString<32> Compressed;
Expand Down

0 comments on commit 1d09ecf

Please sign in to comment.