set(ZLIB_PROJECT_NAME zlib_${ZLIB_VERSION})
set(ZLIB_BUILD_ROOT ${EXTERNAL_BUILD_ROOT}/builds/${ZLIB_PROJECT_NAME})
set(ZLIB_CMAKE_ARGS
  -DCMAKE_BUILD_TYPE=${EXTERNAL_BUILD_TYPE}
  -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
  -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
  -DCMAKE_GENERATOR=${CMAKE_GENERATOR}
  -DCMAKE_INSTALL_PREFIX=${EXTERNAL_BUILD_ROOT}
  -DCMAKE_POSITION_INDEPENDENT_CODE=${CMAKE_POSITION_INDEPENDENT_CODE}
  -DCMAKE_SHARED_LINKER_FLAGS=${CMAKE_SHARED_LINKER_FLAGS}
)

if (CMAKE_TOOLCHAIN_FILE)
  set(ZLIB_CMAKE_ARGS
    ${ZLIB_CMAKE_ARGS}
    -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} 
  )
endif()

ExternalProject_Add(zlib_project
  DOWNLOAD_NO_PROGRESS 1
  URL https://github.com/madler/zlib/archive/${ZLIB_VERSION}.tar.gz
  PREFIX ${ZLIB_BUILD_ROOT}
  CMAKE_ARGS ${ZLIB_CMAKE_ARGS}
)

if(MINGW)
  set(ZLIB_LIBRARIES ${EXTERNAL_BUILD_ROOT}/lib/libzlibstatic${CMAKE_STATIC_LIBRARY_SUFFIX})
else()
  set(ZLIB_LIBRARIES ${EXTERNAL_BUILD_ROOT}/lib/libz${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()
