Skip to content

Commit

Permalink
mkdef for 1.1.0 / unset CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS
Browse files Browse the repository at this point in the history
  • Loading branch information
Fsu0413 committed Jan 23, 2024
1 parent 0de989c commit 13a1949
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 19 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ endif()
if (WIN32)
set(CMAKE_SHARED_LIBRARY_PREFIX "")
set(CMAKE_SHARED_MODULE_PREFIX "")
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS YES)
elseif (APPLE AND NOT IOS)
set(CMAKE_SHARED_MODULE_SUFFIX ".dylib")
endif()
Expand Down
26 changes: 17 additions & 9 deletions crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,20 @@ endif()
if (WIN32)
configure_file(crypto.rc.cmake.in crypto.rc @ONLY)
set(LIBCRYPTO_SOURCES ${LIBCRYPTO_SOURCES} crypto.rc)
set(LIBCRYPTO_RUNTIME_OUTPUT_NAME )
if (OPENSSL_TARGET_ARCH STREQUAL x64)
set(LIBCRYPTO_RUNTIME_OUTPUT_NAME libcrypto-1_1-x64)
else()
set(LIBCRYPTO_RUNTIME_OUTPUT_NAME libcrypto-1_1)
endif()

add_custom_command(
OUTPUT crypto.def
COMMAND "${CMAKE_COMMAND}" -DNUMFILE="${CMAKE_SOURCE_DIR}/openssl/util/libcrypto.num" -DLIBRARY_FILE="${LIBCRYPTO_RUNTIME_OUTPUT_NAME}" -DBINARY_DIR="${CMAKE_BINARY_DIR}" -DOUTPUT_FILE="${CMAKE_CURRENT_BINARY_DIR}/crypto.def" -DPLATFORM=_WIN32 -P "${CMAKE_CURRENT_SOURCE_DIR}/mkdef.cmake"
MAIN_DEPENDENCY "${CMAKE_SOURCE_DIR}/openssl/util/libcrypto.num"
DEPENDS mkdef.cmake
)
set(LIBCRYPTO_SOURCES ${LIBCRYPTO_SOURCES} crypto.def)
endif()
add_library(crypto
${CMAKE_BINARY_DIR}/include/openssl/opensslconf.h
Expand Down Expand Up @@ -513,15 +527,9 @@ if (OPENSSL_ZLIB AND ( NOT OPENSSL_ZLIB_DYNAMIC ) )
target_link_libraries(crypto PUBLIC ZLIB::ZLIB)
endif()
if (WIN32)
if (OPENSSL_TARGET_ARCH STREQUAL x64)
set_target_properties(crypto PROPERTIES
RUNTIME_OUTPUT_NAME libcrypto-1_1-x64
)
else()
set_target_properties(crypto PROPERTIES
RUNTIME_OUTPUT_NAME libcrypto-1_1
)
endif()
set_target_properties(crypto PROPERTIES
RUNTIME_OUTPUT_NAME "${LIBCRYPTO_RUNTIME_OUTPUT_NAME}"
)
if (MSVC)
set_target_properties(crypto PROPERTIES
ARCHIVE_OUTPUT_NAME libcrypto
Expand Down
175 changes: 175 additions & 0 deletions crypto/mkdef.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@

# Help:
# cmake
# -DNUMFILE=xxx.num
# -DLIBRARY_FILE=LIBEAY32 / SSLEAY32
# -DBINARY_DIR=<output-directory>
# -DOUTPUT_FILE=output.def
# -DPLATFORM=<platform> - currently only WIN32 is recognized, other possible value maybe "VMS" / "VOS" / "__FreeBSD__"...
# -P <this cmake>

# Depends on CMake 3.17 which is the version CMake supports load_cache in CMake -P mode

# There are duplicates in either the symbol name and the number so neither of them can be the key of our kv-pair
# So the key can be only line number
# Fortrunately there is no semicolon ";" in the .num file so we can use file(STRINGS) function!

cmake_policy(VERSION ${CMAKE_VERSION})

file(STRINGS "${NUMFILE}" NUMFILE_CONTENT)

# Read content from content to variables
set(I 0)
foreach(CONTENT_ITEM IN LISTS NUMFILE_CONTENT)
if (CONTENT_ITEM MATCHES "^([A-Za-z_][A-Za-z0-9_]*)[ \t\r\n]+([1-9][0-9]*)[ \t\r\n]+([0-9]+_[0-9]+_[0-9]+[a-z]*)[ \t\r\n]+(EXIST|NOEXIST):([^:]*):(FUNCTION|VARIABLE):([^:]*)$")
# directly strip nonexist symbol!
if (CMAKE_MATCH_4 STREQUAL "EXIST")
set("CONTENT_ITEM_${I}_SYMBOL" "${CMAKE_MATCH_1}")
set("CONTENT_ITEM_${I}_PLACE" "${CMAKE_MATCH_2}")
# set("CONTENT_ITEM_${I}_VERSION" "${CMAKE_MATCH_3}") # Useless on Windows
set("CONTENT_ITEM_${I}_PLATFORM" "${CMAKE_MATCH_5}")
# set("CONTENT_ITEM_${I}_TYPE" "${CMAKE_MATCH_6}") # Useless on Windows
set("CONTENT_ITEM_${I}_CONDITION" "${CMAKE_MATCH_7}")

math(EXPR I "${I} + 1")
endif()
endif()
endforeach()
set(CONTENT_LENGTH "${I}")

# Convert CONTENT_ITEM_${I}_PLATFORM and CONTENT_ITEM_${I}_CONDITION to list (by converting comma "," to semicolon ";"), and calculate their length
set(I 0)
while (I LESS CONTENT_LENGTH)
string(REPLACE "," ";" CONTENT_ITEM_${I}_PLATFORM "${CONTENT_ITEM_${I}_PLATFORM}")
list(LENGTH CONTENT_ITEM_${I}_PLATFORM CONTENT_ITEM_${I}_PLATFORM_LENGTH)
string(REPLACE "," ";" CONTENT_ITEM_${I}_CONDITION "${CONTENT_ITEM_${I}_CONDITION}")
list(LENGTH CONTENT_ITEM_${I}_CONDITION CONTENT_ITEM_${I}_CONDITION_LENGTH)
math(EXPR I "${I} + 1")
endwhile()

# Merge all condition into one, then load the cached values
set(I 0)
# Some conditions act as platform options instead of a condition
set(ALL_CONDITIONS OPENSSL_ZLIB OPENSSL_MIN_API_LEVEL)
while (I LESS CONTENT_LENGTH)
foreach (C IN LISTS CONTENT_ITEM_${I}_CONDITION)
list(APPEND ALL_CONDITIONS "OPENSSL_${C}")
endforeach()
math(EXPR I "${I} + 1")
endwhile()
list(REMOVE_DUPLICATES ALL_CONDITIONS)
load_cache("${BINARY_DIR}" READ_WITH_PREFIX "" ${ALL_CONDITIONS})

# Now that OPENSSL_* is available here!
if (PLATFORM STREQUAL "_WIN32")
set(OPENSSL_EXPORT_VAR_AS_FUNCTION 1)
endif()
set(OPENSSL_${PLATFORM} 1)
set(OPENSSL_DEPRECATEDIN_1_1_0 1)
set(OPENSSL_DEPRECATEDIN_1_0_0 1)
set(OPENSSL_DEPRECATEDIN_0_9_8 1)
if (DEFINED OPENSSL_MIN_API_LEVEL)
if (OPENSSL_MIN_API_LEVEL STREQUAL "1.1.0")
unset(OPENSSL_DEPRECATEDIN_1_1_0)
set(OPENSSL_MIN_API_LEVEL "1.0.0")
endif()
if (OPENSSL_MIN_API_LEVEL STREQUAL "1.0.0")
unset(OPENSSL_DEPRECATEDIN_1_0_0)
set(OPENSSL_MIN_API_LEVEL "0.9.8")
endif()
if (OPENSSL_MIN_API_LEVEL STREQUAL "0.9.8")
unset(OPENSSL_DEPRECATEDIN_0_9_8)
endif()
endif()

# Judge platform options and condition, set(CONTENT_ITEM_${I}_SUPPRESS 1) if not fit
set(I 0)
while (I LESS CONTENT_LENGTH)
if (CONTENT_ITEM_${I}_PLATFORM_LENGTH GREATER 0)
# P: platform option
# RJ: judgement is reversed
# PJ: word to be judged
# PR: word matches
foreach (P IN LISTS CONTENT_ITEM_${I}_PLATFORM)
set(RJ no)
string(SUBSTRING "${P}" 0 1 P1)
set(PJ "${P}")
if (P1 STREQUAL "!")
string(SUBSTRING "${P}" 1 -1 PJ)
set(RJ yes)
endif()
if (OPENSSL_${PJ})
set(PR yes)
else()
set(PR no)
endif()
if (RJ STREQUAL PR)
set(CONTENT_ITEM_${I}_SUPPRESS 1)
endif()
unset(RJ)
unset(PR)
unset(PJ)
unset(P1)
endforeach()
endif()

if (CONTENT_ITEM_${I}_CONDITION_LENGTH GREATER 0)
foreach (C IN LISTS CONTENT_ITEM_${I}_CONDITION)
if (NOT OPENSSL_${C})
set(CONTENT_ITEM_${I}_SUPPRESS 1)
endif()
endforeach()
endif()
math(EXPR I "${I} + 1")
endwhile()

# Sort the needed symbols
# The needed symbols should be unique now!
set(I 0)
set(SYMBOLS)
while (I LESS CONTENT_LENGTH)
if (NOT DEFINED CONTENT_ITEM_${I}_SUPPRESS)
if (NOT "${CONTENT_ITEM_${I}_SYMBOL}" IN_LIST SYMBOLS)
set("PLACE_FOR_${CONTENT_ITEM_${I}_SYMBOL}" "${CONTENT_ITEM_${I}_PLACE}")
list(APPEND SYMBOLS "${CONTENT_ITEM_${I}_SYMBOL}")
else()
message(WARNING "Duplicated symbol: ${CONTENT_ITEM_${I}_SYMBOL}. The one with place ${CONTENT_ITEM_${I}_PLACE} is ignored.")
endif()
endif()
math(EXPR I "${I} + 1")
endwhile()
list(SORT SYMBOLS)

# Finally generate the output file
# S: symbol
# SL: length of the symbol
# WL: length of the whitespaces
# W: whitespaces
set(EXPORT_TABLE)
foreach (S IN LISTS SYMBOLS)
string(LENGTH "${S}" SL)
math(EXPR WL "40 - ${SL}")
if (WL LESS 1)
set(WL 1)
endif()
string(REPEAT " " "${WL}" W)
string(APPEND EXPORT_TABLE " ${S}
")
unset(SL)
unset(WL)
unset(W)
endforeach()

if (NOT DEFINED OUTPUT_FILE)
message(NOTICE "${EXPORT_TABLE}")
else()
file(WRITE "${OUTPUT_FILE}" "; Generated by mkdef.cmake
; Definition file for the DLL version of the ${LIBRARY_FILE} library from OpenSSL
;
LIBRARY ${LIBRARY_FILE}
EXPORTS
${EXPORT_TABLE}
")
endif()
25 changes: 16 additions & 9 deletions ssl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,27 @@ set(LIBSSL_SOURCES
if (WIN32)
configure_file(ssl.rc.cmake.in ssl.rc @ONLY)
set(LIBSSL_SOURCES ${LIBSSL_SOURCES} ssl.rc)
if (OPENSSL_TARGET_ARCH STREQUAL x64)
set(LIBSSL_RUNTIME_OUTPUT_NAME libssl-1_1-x64)
else()
set(LIBSSL_RUNTIME_OUTPUT_NAME libssl-1_1)
endif()

add_custom_command(
OUTPUT ssl.def
COMMAND "${CMAKE_COMMAND}" -DNUMFILE="${CMAKE_SOURCE_DIR}/openssl/util/libssl.num" -DLIBRARY_FILE="${LIBSSL_RUNTIME_OUTPUT_NAME}" -DBINARY_DIR="${CMAKE_BINARY_DIR}" -DOUTPUT_FILE="${CMAKE_CURRENT_BINARY_DIR}/ssl.def" -DPLATFORM=_WIN32 -P "${CMAKE_CURRENT_SOURCE_DIR}/../crypto/mkdef.cmake"
MAIN_DEPENDENCY "${CMAKE_SOURCE_DIR}/openssl/util/libssl.num"
DEPENDS ../crypto/mkdef.cmake
)
set(LIBSSL_SOURCES ${LIBSSL_SOURCES} ssl.def)
endif()
add_library(ssl
${LIBSSL_SOURCES}
)
if (WIN32)
if (OPENSSL_TARGET_ARCH STREQUAL x64)
set_target_properties(ssl PROPERTIES
RUNTIME_OUTPUT_NAME libssl-1_1-x64
)
else()
set_target_properties(ssl PROPERTIES
RUNTIME_OUTPUT_NAME libssl-1_1
)
endif()
set_target_properties(ssl PROPERTIES
RUNTIME_OUTPUT_NAME "${LIBSSL_RUNTIME_OUTPUT_NAME}"
)
if (MSVC)
set_target_properties(ssl PROPERTIES
ARCHIVE_OUTPUT_NAME libssl
Expand Down

0 comments on commit 13a1949

Please sign in to comment.