Skip to content

Commit

Permalink
Compile mbedtls with CMake instead of using pre-compiled lib on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
andyli committed Apr 17, 2016
1 parent ed098f7 commit 888a4b5
Show file tree
Hide file tree
Showing 70 changed files with 66 additions and 23,541 deletions.
60 changes: 35 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -812,38 +812,48 @@ set_target_properties(mod_tora2.ndll

add_library(ssl.ndll MODULE ${EXCLUDE_SSL_NDLL_FROM_ALL} libs/ssl/ssl.c)

if (WIN32)
find_library(MBEDTLS_LIBRARIES NAMES mbedTLS.lib PATHS "./libs/include/ssl" DOC "mbedTLS library")
find_path(MBEDTLS_INCLUDE_DIR mbedtls/ssl.h "./libs/include/ssl" DOC "mbedTLS includes")
target_link_libraries(ssl.ndll ws2_32 Advapi32 Crypt32)
else()
if (STATIC_MBEDTLS)
ExternalProject_Add(MbedTLS
${EP_CONFIGS}
URL https://tls.mbed.org/download/mbedtls-2.2.1-apache.tgz
URL_MD5 77751c0e370ed2ab01934e4c5e1d380f
CMAKE_ARGS
-Wno-dev
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
-DLINK_WITH_PTHREAD=ON
-DENABLE_PROGRAMS=OFF
-DENABLE_TESTING=OFF
-DUSE_STATIC_MBEDTLS_LIBRARY=ON
-DCMAKE_C_FLAGS=-fPIC
PATCH_COMMAND ${CMAKE_COMMAND} -Dconfig=${CMAKE_BINARY_DIR}/libs/src/MbedTLS/include/mbedtls/config.h -P ${CMAKE_SOURCE_DIR}/cmake/patch_mbedtls.cmake
INSTALL_COMMAND ""
if (STATIC_MBEDTLS)
set(MBEDTLS_CMAKE_ARGS
-Wno-dev
-DENABLE_PROGRAMS=OFF
-DENABLE_TESTING=OFF
-DUSE_STATIC_MBEDTLS_LIBRARY=ON
)
if (UNIX)
list(APPEND MBEDTLS_CMAKE_ARGS
-DLINK_WITH_PTHREAD=ON
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
-DCMAKE_C_FLAGS=-fPIC
)
set_target_properties(MbedTLS PROPERTIES ${EP_PROPS})
set(MBEDTLS_INCLUDE_DIR ${CMAKE_BINARY_DIR}/libs/src/MbedTLS/include)
endif()
ExternalProject_Add(MbedTLS
${EP_CONFIGS}
URL https://tls.mbed.org/download/mbedtls-2.2.1-apache.tgz
URL_MD5 77751c0e370ed2ab01934e4c5e1d380f
CMAKE_ARGS ${MBEDTLS_CMAKE_ARGS}
PATCH_COMMAND ${CMAKE_COMMAND} -Dsource=${CMAKE_SOURCE_DIR} -DMbedTLS_source=${CMAKE_BINARY_DIR}/libs/src/MbedTLS -P ${CMAKE_SOURCE_DIR}/cmake/patch_mbedtls.cmake
INSTALL_COMMAND echo skip install
)
set_target_properties(MbedTLS PROPERTIES ${EP_PROPS})
set(MBEDTLS_INCLUDE_DIR ${CMAKE_BINARY_DIR}/libs/src/MbedTLS/include)

if (WIN32)
set(MBEDTLS_LIBRARIES
${CMAKE_BINARY_DIR}/libs/src/MbedTLS-build/library/${CMAKE_CFG_INTDIR}/mbedx509.lib
${CMAKE_BINARY_DIR}/libs/src/MbedTLS-build/library/${CMAKE_CFG_INTDIR}/mbedtls.lib
${CMAKE_BINARY_DIR}/libs/src/MbedTLS-build/library/${CMAKE_CFG_INTDIR}/mbedcrypto.lib
)
target_link_libraries(ssl.ndll ws2_32 Advapi32 Crypt32)
else()
set(MBEDTLS_LIBRARIES
${CMAKE_BINARY_DIR}/libs/src/MbedTLS-build/library/libmbedx509.a
${CMAKE_BINARY_DIR}/libs/src/MbedTLS-build/library/libmbedtls.a
${CMAKE_BINARY_DIR}/libs/src/MbedTLS-build/library/libmbedcrypto.a
)
add_dependencies(ssl.ndll MbedTLS)
else()
find_package(MbedTLS REQUIRED)
endif()
add_dependencies(ssl.ndll MbedTLS)
else()
find_package(MbedTLS REQUIRED)
endif()

target_include_directories(ssl.ndll
Expand Down
38 changes: 31 additions & 7 deletions cmake/patch_mbedtls.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Apply config adjustments similer to Debian's
# https://anonscm.debian.org/cgit/collab-maint/mbedtls.git/tree/debian/patches/01_config.patch

set(config ${MbedTLS_source}/include/mbedtls/config.h)

file(READ ${config} content)

# disable support for SSL 3.0
Expand All @@ -9,12 +11,34 @@ string(REPLACE
"//#define MBEDTLS_SSL_PROTO_SSL3"
content ${content}
)
# enable pthread mutexes
string(REPLACE
"//#define MBEDTLS_THREADING_PTHREAD"
"#define MBEDTLS_THREADING_PTHREAD"
content ${content}
)

if (WIN32)
# allow alternate threading implementation
string(REPLACE
"//#define MBEDTLS_THREADING_ALT"
"#define MBEDTLS_THREADING_ALT"
content ${content}
)
# disable the TCP/IP networking routines
# such that it wouldn't interfere with the #include <windows.h> in our threading_alt.h
string(REPLACE
"#define MBEDTLS_NET_C"
"//#define MBEDTLS_NET_C"
content ${content}
)

file(COPY ${source}/libs/include/ssl/threading_alt.h
DESTINATION ${MbedTLS_source}/include/mbedtls/
)
else()
# enable pthread mutexes
string(REPLACE
"//#define MBEDTLS_THREADING_PTHREAD"
"#define MBEDTLS_THREADING_PTHREAD"
content ${content}
)
endif()

# enable the HAVEGE random generator
string(REPLACE
"//#define MBEDTLS_HAVEGE_C"
Expand All @@ -40,4 +64,4 @@ string(REPLACE
content ${content}
)

file(WRITE ${config} ${content})
file(WRITE ${config} ${content})
Binary file removed libs/include/ssl/mbedTLS.lib
Binary file not shown.
Loading

0 comments on commit 888a4b5

Please sign in to comment.