diff --git a/cmake/FindPCRE.cmake b/cmake/FindPCRE.cmake index 87bd51b79..0f5bbfef5 100644 --- a/cmake/FindPCRE.cmake +++ b/cmake/FindPCRE.cmake @@ -1,37 +1,158 @@ -# Copyright (C) 2007-2009 LuaDist. -# Created by Peter Kapec -# Redistribution and use of this file is allowed according to the terms of the MIT license. -# For details see the COPYRIGHT file distributed with LuaDist. -# Note: -# Searching headers and libraries is very simple and is NOT as powerful as scripts -# distributed with CMake, because LuaDist defines directories to search for. -# Everyone is encouraged to contact the author with improvements. Maybe this file -# becomes part of CMake distribution sometimes. - -# - Find pcre -# Find the native PCRE headers and libraries. -# -# PCRE_INCLUDE_DIRS - where to find pcre.h, etc. -# PCRE_LIBRARIES - List of libraries when using pcre. -# PCRE_FOUND - True if pcre found. - -# Look for the header file. -FIND_PATH(PCRE_INCLUDE_DIR NAMES pcre.h) - -# Look for the library. -FIND_LIBRARY(PCRE_LIBRARY NAMES pcre) - -# Handle the QUIETLY and REQUIRED arguments and set PCRE_FOUND to TRUE if all listed variables are TRUE. -INCLUDE(FindPackageHandleStandardArgs) -FIND_PACKAGE_HANDLE_STANDARD_ARGS(PCRE DEFAULT_MSG PCRE_LIBRARY PCRE_INCLUDE_DIR) - -# Copy the results to the output variables. -IF(PCRE_FOUND) - SET(PCRE_LIBRARIES ${PCRE_LIBRARY}) - SET(PCRE_INCLUDE_DIRS ${PCRE_INCLUDE_DIR}) -ELSE(PCRE_FOUND) - SET(PCRE_LIBRARIES) - SET(PCRE_INCLUDE_DIRS) -ENDIF(PCRE_FOUND) - -MARK_AS_ADVANCED(PCRE_INCLUDE_DIRS PCRE_LIBRARIES) +#.rst: +# FindPCRE +# -------- +# +# Find the native PCRE includes and library. +# +# IMPORTED Targets +# ^^^^^^^^^^^^^^^^ +# +# This module defines :prop_tgt:`IMPORTED` target ``PCRE::PCRE``, if +# PCRE has been found. +# +# Result Variables +# ^^^^^^^^^^^^^^^^ +# +# This module defines the following variables: +# +# :: +# +# PCRE_INCLUDE_DIRS - where to find pcre.h, etc. +# PCRE_LIBRARIES - List of libraries when using pcre. +# PCRE_FOUND - True if pcre found. +# +# :: +# +# PCRE_VERSION_STRING - The version of pcre found (x.y.z) +# PCRE_VERSION_MAJOR - The major version of zlib +# PCRE_VERSION_MINOR - The minor version of zlib +# PCRE_VERSION_PATCH - The patch version of zlib +# PCRE_VERSION_TWEAK - The tweak version of zlib +# +# Backward Compatibility +# ^^^^^^^^^^^^^^^^^^^^^^ +# +# The following variable are provided for backward compatibility +# +# :: +# +# PCRE_MAJOR_VERSION - The major version of zlib +# PCRE_MINOR_VERSION - The minor version of zlib +# PCRE_PATCH_VERSION - The patch version of zlib +# +# Hints +# ^^^^^ +# +# A user may set ``PCRE_ROOT`` to a zlib installation root to tell this +# module where to look. + +#============================================================================= +# Copyright 2001-2011 Kitware, Inc. +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +set(_PCRE_SEARCHES) + +# Search PCRE_ROOT first if it is set. +if(PCRE_ROOT) + set(_PCRE_SEARCH_ROOT PATHS ${PCRE_ROOT} NO_DEFAULT_PATH) + list(APPEND _PCRE_SEARCHES _PCRE_SEARCH_ROOT) +endif() + +# Normal search. +set(_PCRE_SEARCH_NORMAL + PATHS "[HKEY_LOCAL_MACHINE\\SOFTWARE\\GnuWin32\\Pcre;InstallPath]" + "$ENV{PROGRAMFILES}/pcre" + ) +list(APPEND _PCRE_SEARCHES _PCRE_SEARCH_NORMAL) + +set(PCRE_NAMES pcre pcredll) +set(PCRE_NAMES_DEBUG pcred) + +# Try each search configuration. +foreach(search ${_PCRE_SEARCHES}) + find_path(PCRE_INCLUDE_DIR NAMES pcre.h ${${search}} PATH_SUFFIXES include) +endforeach() + +# Allow PCRE_LIBRARY to be set manually, as the location of the pcre library +if(NOT PCRE_LIBRARY) + foreach(search ${_PCRE_SEARCHES}) + find_library(PCRE_LIBRARY_RELEASE NAMES ${PCRE_NAMES} ${${search}} PATH_SUFFIXES lib) + find_library(PCRE_LIBRARY_DEBUG NAMES ${PCRE_NAMES_DEBUG} ${${search}} PATH_SUFFIXES lib) + endforeach() + + include(SelectLibraryConfigurations) + select_library_configurations(PCRE) +endif() + +unset(PCRE_NAMES) +unset(PCRE_NAMES_DEBUG) + +mark_as_advanced(PCRE_LIBRARY PCRE_INCLUDE_DIR) + +if(PCRE_INCLUDE_DIR AND EXISTS "${PCRE_INCLUDE_DIR}/pcre.h") + file(STRINGS "${PCRE_INCLUDE_DIR}/pcre.h" PCRE_H REGEX "^#define PCRE_VERSION \"[^\"]*\"$") + + string(REGEX REPLACE "^.*PCRE_VERSION \"([0-9]+).*$" "\\1" PCRE_VERSION_MAJOR "${PCRE_H}") + string(REGEX REPLACE "^.*PCRE_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" PCRE_VERSION_MINOR "${PCRE_H}") + string(REGEX REPLACE "^.*PCRE_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" PCRE_VERSION_PATCH "${PCRE_H}") + set(PCRE_VERSION_STRING "${PCRE_VERSION_MAJOR}.${PCRE_VERSION_MINOR}.${PCRE_VERSION_PATCH}") + + # only append a TWEAK version if it exists: + set(PCRE_VERSION_TWEAK "") + if( "${PCRE_H}" MATCHES "PCRE_VERSION \"[0-9]+\\.[0-9]+\\.[0-9]+\\.([0-9]+)") + set(PCRE_VERSION_TWEAK "${CMAKE_MATCH_1}") + set(PCRE_VERSION_STRING "${PCRE_VERSION_STRING}.${PCRE_VERSION_TWEAK}") + endif() + + set(PCRE_MAJOR_VERSION "${PCRE_VERSION_MAJOR}") + set(PCRE_MINOR_VERSION "${PCRE_VERSION_MINOR}") + set(PCRE_PATCH_VERSION "${PCRE_VERSION_PATCH}") +endif() + +# handle the QUIETLY and REQUIRED arguments and set PCRE_FOUND to TRUE if +# all listed variables are TRUE +include(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(PCRE REQUIRED_VARS PCRE_LIBRARY PCRE_INCLUDE_DIR + VERSION_VAR PCRE_VERSION_STRING) + +if(PCRE_FOUND) + set(PCRE_INCLUDE_DIRS ${PCRE_INCLUDE_DIR}) + + if(NOT PCRE_LIBRARIES) + set(PCRE_LIBRARIES ${PCRE_LIBRARY}) + endif() + + if(NOT TARGET PCRE::PCRE) + add_library(PCRE::PCRE UNKNOWN IMPORTED) + set_target_properties(PCRE::PCRE PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${PCRE_INCLUDE_DIRS}") + + if(PCRE_LIBRARY_RELEASE) + set_property(TARGET PCRE::PCRE APPEND PROPERTY + IMPORTED_CONFIGURATIONS RELEASE) + set_target_properties(PCRE::PCRE PROPERTIES + IMPORTED_LOCATION_RELEASE "${PCRE_LIBRARY_RELEASE}") + endif() + + if(PCRE_LIBRARY_DEBUG) + set_property(TARGET PCRE::PCRE APPEND PROPERTY + IMPORTED_CONFIGURATIONS DEBUG) + set_target_properties(PCRE::PCRE PROPERTIES + IMPORTED_LOCATION_DEBUG "${PCRE_LIBRARY_DEBUG}") + endif() + + if(NOT PCRE_LIBRARY_RELEASE AND NOT PCRE_LIBRARY_DEBUG) + set_property(TARGET PCRE::PCRE APPEND PROPERTY + IMPORTED_LOCATION "${PCRE_LIBRARY}") + endif() + endif() +endif() diff --git a/cmake/configure.cmake b/cmake/configure.cmake index 618cc2b4e..eaf534932 100644 --- a/cmake/configure.cmake +++ b/cmake/configure.cmake @@ -5,7 +5,14 @@ if (${with_crypto_library} STREQUAL "openssl") find_package(OpenSSL REQUIRED) set(USE_CRYPTO_OPENSSL 1) set(LIBCRYPTO + ${ZLIB_LIBRARIES} ${OPENSSL_CRYPTO_LIBRARY}) + + include_directories(${ZLIB_INCLUDE_DIR}) + include_directories(${OPENSSL_INCLUDE_DIR}) + + list ( APPEND CMAKE_REQUIRED_INCLUDES ${ZLIB_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR}) + elseif(${with_crypto_library} STREQUAL "polarssl") find_package(polarssl REQUIRED) set(USE_CRYPTO_POLARSSL 1) @@ -14,6 +21,10 @@ elseif(${with_crypto_library} STREQUAL "mbedtls") set(USE_CRYPTO_MBEDTLS 1) endif() +find_package(PCRE REQUIRED) +include_directories(${PCRE_INCLUDE_DIR}) +list ( APPEND CMAKE_REQUIRED_INCLUDES ${PCRE_INCLUDE_DIR}) + # Platform checks include ( CheckFunctionExists ) @@ -102,8 +113,32 @@ check_function_exists ( inet_ntop HAVE_DECL_INET_NTOP ) check_symbol_exists ( PTHREAD_PRIO_INHERIT "pthread.h" HAVE_PTHREAD_PRIO_INHERIT ) check_symbol_exists ( PTHREAD_CREATE_JOINABLE "pthread.h" HAVE_PTHREAD_CREATE_JOINABLE ) check_symbol_exists ( EINPROGRESS "sys/errno.h" HAVE_EINPROGRESS ) -check_symbol_exists ( WSAEWOULDBLOCK "winerrno.h" HAVE_WSAEWOULDBLOCK ) - +check_symbol_exists ( WSAEWOULDBLOCK "winerror.h" HAVE_WSAEWOULDBLOCK ) + +# winsock2.h and ws2_32 should provide these +if(HAVE_WINSOCK2_H) + set(HAVE_GETHOSTNAME ON) + set(HAVE_SELECT ON) + set(HAVE_SOCKET ON) + set(HAVE_INET_NTOA ON) + set(HAVE_RECV ON) + set(HAVE_SEND ON) + set(HAVE_RECVFROM ON) + set(HAVE_SENDTO ON) + set(HAVE_GETHOSTBYNAME ON) + set(HAVE_GETSERVBYNAME ON) +else(HAVE_WINSOCK2_H) + check_function_exists(gethostname HAVE_GETHOSTNAME) + check_function_exists(select HAVE_SELECT) + check_function_exists(socket HAVE_SOCKET) + check_function_exists(inet_ntoa HAVE_INET_NTOA) + check_function_exists(recv HAVE_RECV) + check_function_exists(send HAVE_SEND) + check_function_exists(recvfrom HAVE_RECVFROM) + check_function_exists(sendto HAVE_SENDTO) + check_function_exists(gethostbyname HAVE_GETHOSTBYNAME) + check_function_exists(getservbyname HAVE_GETSERVBYNAME) +endif(HAVE_WINSOCK2_H) find_library ( HAVE_LIBPCRE pcre ) find_library ( HAVE_LIBRT rt ) @@ -152,7 +187,11 @@ elseif(${HAVE_WSAEWOULDBLOCK}) set (CONNECT_IN_PROGRESS WSAEWOULDBLOCK) endif() +#SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99") + + set (HAVE_IPv6 1) ADD_DEFINITIONS(-DHAVE_CONFIG_H) +ADD_DEFINITIONS(-D__USE_MINGW_ANSI_STDIO=1) diff --git a/config.h.cmake b/config.h.cmake index 5d51e2253..acc664e53 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -7,16 +7,16 @@ #define CONNECT_IN_PROGRESS @CONNECT_IN_PROGRESS@ /* Override libev default fd conversion macro. */ -#undef EV_FD_TO_WIN32_HANDLE +#define EV_FD_TO_WIN32_HANDLE(fd) (fd) /* Override libev default fd close macro. */ -#undef EV_WIN32_CLOSE_FD +#define EV_WIN32_CLOSE_FD(fd) closesocket(fd) /* Override libev default handle conversion macro. */ -#undef EV_WIN32_HANDLE_TO_FD +#define EV_WIN32_HANDLE_TO_FD(handle) (handle) /* Reset max file descriptor size. */ -#undef FD_SETSIZE +#define FD_SETSIZE 2048 /* Define to 1 if you have the header file. */ #cmakedefine HAVE_ARPA_INET_H 1 diff --git a/libcork/CMakeLists.txt b/libcork/CMakeLists.txt index 6fe768c67..7798e2f3a 100644 --- a/libcork/CMakeLists.txt +++ b/libcork/CMakeLists.txt @@ -110,12 +110,12 @@ add_c_library( ds/managed-buffer.c ds/ring-buffer.c ds/slice.c - posix/directory-walker.c - posix/env.c - posix/exec.c - posix/files.c +# posix/directory-walker.c +# posix/env.c +# posix/exec.c +# posix/files.c posix/process.c - posix/subprocess.c +# posix/subprocess.c pthreads/thread.c LIBRARIES threads diff --git a/libcork/core/timestamp.c b/libcork/core/timestamp.c index 331bca72b..5da1337eb 100644 --- a/libcork/core/timestamp.c +++ b/libcork/core/timestamp.c @@ -139,6 +139,34 @@ cork_timestamp_format_parts(const cork_timestamp ts, struct tm *tm, return 0; } +#ifdef __MINGW32__ +static struct tm *__cdecl gmtime_r(const time_t *_Time, struct tm *_Tm) +{ + struct tm *p = gmtime(_Time); + if (!p) + return NULL; + if (_Tm) { + memcpy(_Tm, p, sizeof(struct tm)); + return _Tm; + } else + return p; +} + +static struct tm *__cdecl localtime_r(const time_t *_Time, struct tm *_Tm) +{ + struct tm *p = localtime(_Time); + if (!p) + return NULL; + if (_Tm) { + memcpy(_Tm, p, sizeof(struct tm)); + return _Tm; + } else + return p; +} + +#endif + + int cork_timestamp_format_utc(const cork_timestamp ts, const char *format, struct cork_buffer *dest) diff --git a/libcork/include/libcork/config/mingw32.h b/libcork/include/libcork/config/mingw32.h index c62cbc520..889822d65 100644 --- a/libcork/include/libcork/config/mingw32.h +++ b/libcork/include/libcork/config/mingw32.h @@ -48,5 +48,7 @@ static inline int mingw_mkdir(const char *path, int mode) } #define mkdir mingw_mkdir +#define S_ISLNK(x) 0 + #endif /* LIBCORK_CONFIG_MINGW32_H */ diff --git a/libcork/include/libcork/core/attributes.h b/libcork/include/libcork/core/attributes.h index 7ae39b027..609df563f 100644 --- a/libcork/include/libcork/core/attributes.h +++ b/libcork/include/libcork/core/attributes.h @@ -96,7 +96,7 @@ * format_index and args_index are 1-based. */ -#if CORK_CONFIG_HAVE_GCC_ATTRIBUTES +#if CORK_CONFIG_HAVE_GCC_ATTRIBUTES && !(defined(__CYGWIN__)||defined(__MINGW32__)) #define CORK_ATTR_PRINTF(format_index, args_index) \ __attribute__((format(printf, format_index, args_index))) #else @@ -137,7 +137,7 @@ * it's internal to the current library. */ -#if CORK_CONFIG_HAVE_GCC_ATTRIBUTES && !defined(__CYGWIN__) +#if CORK_CONFIG_HAVE_GCC_ATTRIBUTES && !(defined(__CYGWIN__)||defined(__MINGW32__)) #define CORK_EXPORT __attribute__((visibility("default"))) #define CORK_IMPORT __attribute__((visibility("default"))) #define CORK_LOCAL __attribute__((visibility("hidden"))) diff --git a/libcork/posix/env.c b/libcork/posix/env.c index 1599f2f7c..e5f5fd79a 100644 --- a/libcork/posix/env.c +++ b/libcork/posix/env.c @@ -25,7 +25,7 @@ #include #define environ (*_NSGetEnviron()) -#else +#elif !defined(__MINGW32__) /* On all other POSIX platforms, we assume that environ is available in shared * libraries. */ extern char **environ; @@ -33,6 +33,71 @@ extern char **environ; #endif +#ifdef __MINGW32__ + +int setenv(const char *name, const char *value, int replace) +{ + int out; + size_t namelen, valuelen; + char *envstr; + + if (!name || !value) return -1; + if (!replace) { + char *oldval = NULL; + oldval = getenv(name); + if (oldval) return 0; + } + + namelen = strlen(name); + valuelen = strlen(value); + envstr = malloc((namelen + valuelen + 2)); + if (!envstr) return -1; + + memcpy(envstr, name, namelen); + envstr[namelen] = '='; + memcpy(envstr + namelen + 1, value, valuelen); + envstr[namelen + valuelen + 1] = 0; + + out = putenv(envstr); + /* putenv(3) makes the argument string part of the environment, + * and changing that string modifies the environment --- which + * means we do not own that storage anymore. Do not free + * envstr. + */ + + return out; +} + +int unsetenv(const char *env) +{ + char *name; + int ret; + + name = malloc(strlen(env)+2); + strcat(strcpy(name, env), "="); + ret = putenv(name); + free(name); + + return ret; +} + +int clearenv(void) +{ + char **env = environ; + if (!env) + return 0; + while (*env) { + free(*env); + env++; + } + free(env); + environ = NULL; + return 0; +} + +#endif + + struct cork_env_var { const char *name; const char *value; @@ -174,7 +239,6 @@ cork_env_remove(struct cork_env *env, const char *name) } } - static enum cork_hash_table_map_result cork_env_set_vars(void *user_data, struct cork_hash_table_entry *entry) { diff --git a/libev/CMakeLists.txt b/libev/CMakeLists.txt index 02df5abec..58c622b37 100644 --- a/libev/CMakeLists.txt +++ b/libev/CMakeLists.txt @@ -7,10 +7,10 @@ project ( libev C ) cmake_minimum_required ( VERSION 2.8 ) include ( cmake/dist.cmake ) -include ( configure ) +#include ( configure ) -configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h ) -include_directories( ${CMAKE_CURRENT_BINARY_DIR} ) +#configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h ) +include_directories( ${CMAKE_BINARY_DIR} ) set ( EV_SRC ev.c diff --git a/libev/ev.c b/libev/ev.c index 6016d514e..8a3ae88a1 100644 --- a/libev/ev.c +++ b/libev/ev.c @@ -90,7 +90,7 @@ # define EV_USE_NANOSLEEP 0 # endif -# if HAVE_SELECT && HAVE_SYS_SELECT_H +# if HAVE_SELECT //&& HAVE_SYS_SELECT_H # ifndef EV_USE_SELECT # define EV_USE_SELECT EV_FEATURE_BACKENDS # endif diff --git a/libsodium/CMakeLists.txt b/libsodium/CMakeLists.txt index 3d3b4b957..181b4f9fd 100644 --- a/libsodium/CMakeLists.txt +++ b/libsodium/CMakeLists.txt @@ -15,7 +15,7 @@ set (LIBSODIUM_BUILD_SHARED_LIBRARIES OFF) set (ENABLE_BLOCKING_RANDOM ON) set (ENABLE_MINIMAL_BUILD OFF) set (ENABLE_TESTS OFF) -set (DISABLE_ASM OFF) +set (DISABLE_ASM ON) set (DISABLE_PIE OFF) set (DISABLE_SSP ON) diff --git a/libudns/CMakeLists.txt b/libudns/CMakeLists.txt index 194947600..509c75dc5 100644 --- a/libudns/CMakeLists.txt +++ b/libudns/CMakeLists.txt @@ -16,7 +16,7 @@ set ( UDNS_SRC udns_rr_srv.c udns_rr_naptr.c udns_codes.c udns_jran.c ) -if (CYGWIN) +if (WIN32) list ( APPEND UDNS_LIBS Ws2_32 ) endif () diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 755ba59e8..d7c3e1709 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -85,11 +85,15 @@ set (ss_lib_net ${ss_lib_common} ${LIBCRYPTO} libipset - pcre + ${PCRE_LIBRARIES} udns sodium ) +if (WIN32) + list ( APPEND ss_lib_net Ws2_32 ) +endif() + target_link_libraries(ss_local ${ss_lib_net} ) diff --git a/src/encrypt.c b/src/encrypt.c index 2301f5ec2..8216b25fb 100644 --- a/src/encrypt.c +++ b/src/encrypt.c @@ -76,6 +76,10 @@ #include #endif +#ifdef __MINGW32__ +#include "winsock2.h" +#endif + #include "cache.h" #include "encrypt.h" #include "utils.h" diff --git a/src/tls.c b/src/tls.c index 5c4221605..a82dbd365 100644 --- a/src/tls.c +++ b/src/tls.c @@ -40,7 +40,7 @@ #ifndef __MINGW32__ #include #else -#include +#include "win32.h" #endif #include "tls.h"