CMakeLists.txt 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. cmake_minimum_required(VERSION 3.9)
  2. project(openssl C)
  3. if(NOT SOURCE_PATH)
  4. message(FATAL_ERROR "Requires SOURCE_PATH")
  5. endif()
  6. if(CMAKE_SYSTEM_NAME STREQUAL "Android" OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
  7. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  8. set(PLATFORM linux-x86_64)
  9. else()
  10. set(PLATFORM linux-generic32)
  11. endif()
  12. elseif(CMAKE_SYSTEM_NAME STREQUAL "iOS")
  13. if(VCPKG_TARGET_ARCHITECTURE MATCHES "arm64")
  14. set(PLATFORM ios64-xcrun)
  15. elseif(VCPKG_TARGET_ARCHITECTURE MATCHES "arm")
  16. set(PLATFORM ios-xcrun)
  17. elseif(VCPKG_TARGET_ARCHITECTURE MATCHES "x86" OR
  18. VCPKG_TARGET_ARCHITECTURE MATCHES "x64")
  19. set(PLATFORM iossimulator-xcrun)
  20. else()
  21. message(FATAL_ERROR "Unknown iOS target architecture: ${VCPKG_TARGET_ARCHITECTURE}")
  22. endif()
  23. # disable that makes linkage error (e.g. require stderr usage)
  24. list(APPEND DISABLES no-ui no-asm)
  25. elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  26. if(VCPKG_TARGET_ARCHITECTURE MATCHES "arm64")
  27. set(PLATFORM darwin64-arm64-cc)
  28. else()
  29. set(PLATFORM darwin64-x86_64-cc)
  30. endif()
  31. elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
  32. set(PLATFORM BSD-generic64)
  33. elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
  34. set(PLATFORM BSD-generic64)
  35. elseif(MINGW)
  36. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  37. set(PLATFORM mingw64)
  38. else()
  39. set(PLATFORM mingw)
  40. endif()
  41. elseif(EMSCRIPTEN)
  42. set(MAKE $ENV{EMSDK}/upstream/emscripten/emmake)
  43. set(ENV{MAKE} $ENV{EMSDK}/upstream/emscripten/emmake)
  44. else()
  45. message(FATAL_ERROR "Unknown platform")
  46. endif()
  47. get_filename_component(COMPILER_ROOT "${CMAKE_C_COMPILER}" DIRECTORY)
  48. message("CMAKE_C_COMPILER=${CMAKE_C_COMPILER}")
  49. message("COMPILER_ROOT=${COMPILER_ROOT}")
  50. message("CMAKE_SYSROOT=${CMAKE_SYSROOT}")
  51. message("CMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT}")
  52. message("CMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}")
  53. message("CMAKE_C_FLAGS=${CMAKE_C_FLAGS}")
  54. message("CMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE}")
  55. message("CMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}")
  56. message("CMAKE_INCLUDE_SYSTEM_FLAG_C=${CMAKE_INCLUDE_SYSTEM_FLAG_C}")
  57. message("CMAKE_C_OSX_DEPLOYMENT_TARGET_FLAG=${CMAKE_C_OSX_DEPLOYMENT_TARGET_FLAG}")
  58. string(TOUPPER "${CMAKE_BUILD_TYPE}" BUILD_TYPE)
  59. set(CFLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${BUILD_TYPE}}")
  60. if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
  61. set(CFLAGS "${CFLAGS} -Wno-error=unused-command-line-argument")
  62. endif()
  63. if(CMAKE_C_COMPILER_TARGET AND CMAKE_C_COMPILE_OPTIONS_TARGET)
  64. set(CFLAGS "${CFLAGS} ${CMAKE_C_COMPILE_OPTIONS_TARGET}${CMAKE_C_COMPILER_TARGET}")
  65. endif()
  66. if(CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN AND CMAKE_C_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN)
  67. set(CFLAGS "${CFLAGS} ${CMAKE_C_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN}${CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN}")
  68. endif()
  69. if(CMAKE_SYSROOT AND CMAKE_C_COMPILE_OPTIONS_SYSROOT)
  70. set(CFLAGS "${CFLAGS} ${CMAKE_C_COMPILE_OPTIONS_SYSROOT}${CMAKE_SYSROOT}")
  71. elseif(CMAKE_OSX_SYSROOT AND CMAKE_C_COMPILE_OPTIONS_SYSROOT)
  72. set(CFLAGS "${CFLAGS} ${CMAKE_C_COMPILE_OPTIONS_SYSROOT}${CMAKE_OSX_SYSROOT}")
  73. endif()
  74. if (CMAKE_OSX_DEPLOYMENT_TARGET AND CMAKE_C_OSX_DEPLOYMENT_TARGET_FLAG)
  75. set(CFLAGS "${CFLAGS} ${CMAKE_C_OSX_DEPLOYMENT_TARGET_FLAG}${CMAKE_OSX_DEPLOYMENT_TARGET}")
  76. elseif((CMAKE_SYSTEM_NAME STREQUAL "Darwin") AND (VCPKG_TARGET_ARCHITECTURE MATCHES "arm64"))
  77. set(CFLAGS "${CFLAGS} -mmacosx-version-min=11.0")
  78. endif()
  79. string(REGEX REPLACE "^ " "" CFLAGS "${CFLAGS}")
  80. set(ENV{ANDROID_DEV} "${CMAKE_SYSROOT}/usr")
  81. if(NOT IOS)
  82. set(ENV{CC} "${CMAKE_C_COMPILER}")
  83. endif()
  84. message("ENV{ANDROID_DEV}=$ENV{ANDROID_DEV}")
  85. get_filename_component(SOURCE_PATH_NAME "${SOURCE_PATH}" NAME)
  86. set(BUILDDIR "${CMAKE_CURRENT_BINARY_DIR}/${SOURCE_PATH_NAME}")
  87. if(NOT EXISTS "${BUILDDIR}")
  88. file(COPY ${SOURCE_PATH} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
  89. endif()
  90. get_filename_component(MSYS_BIN_DIR "${MAKE}" DIRECTORY)
  91. if(BUILD_SHARED_LIBS)
  92. set(SHARED shared)
  93. file(STRINGS "${BUILDDIR}/VERSION.dat" SHLIB_VERSION
  94. REGEX "^SHLIB_VERSION=.*")
  95. string(REGEX REPLACE "^(SHLIB_VERSION=)(.*)$" "\\2"
  96. SHLIB_VERSION "${SHLIB_VERSION}")
  97. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
  98. set(LIB_EXT dylib)
  99. set(LIB_EXTS ${SHLIB_VERSION}.${LIB_EXT})
  100. elseif(MINGW)
  101. string(REPLACE "." "_" SHLIB_VERSION "${SHLIB_VERSION}")
  102. set(BIN_EXT dll)
  103. set(LIB_EXT dll.a)
  104. else()
  105. set(LIB_EXT so)
  106. set(LIB_EXTS ${LIB_EXT}.${SHLIB_VERSION})
  107. endif()
  108. list(APPEND BIN_EXTS ${BIN_EXT})
  109. list(APPEND LIB_EXTS ${LIB_EXT})
  110. else()
  111. set(SHARED no-shared no-module)
  112. set(LIB_EXTS a)
  113. endif()
  114. set(INSTALL_PKG_CONFIGS "${BUILDDIR}/openssl.pc")
  115. foreach(lib ssl crypto)
  116. foreach(ext ${LIB_EXTS})
  117. list(APPEND INSTALL_LIBS "${BUILDDIR}/lib${lib}.${ext}")
  118. list(APPEND INSTALL_PKG_CONFIGS "${BUILDDIR}/lib${lib}.pc")
  119. endforeach()
  120. foreach(ext ${BIN_EXTS})
  121. # This might be wrong for targets which don't follow this naming scheme, but I'm not aware of any
  122. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  123. list(APPEND INSTALL_BINS "${BUILDDIR}/lib${lib}-${SHLIB_VERSION}-x64.${ext}")
  124. else()
  125. list(APPEND INSTALL_BINS "${BUILDDIR}/lib${lib}-${SHLIB_VERSION}.${ext}")
  126. endif()
  127. endforeach()
  128. endforeach()
  129. if(CMAKE_HOST_WIN32)
  130. set(ENV_COMMAND set)
  131. set(PATH_VAR ";%PATH%")
  132. else()
  133. set(ENV_COMMAND export)
  134. set(PATH_VAR ":$ENV{PATH}")
  135. endif()
  136. add_custom_command(
  137. OUTPUT "${BUILDDIR}/Makefile"
  138. COMMAND ${ENV_COMMAND} "PATH=${MSYS_BIN_DIR}${PATH_VAR}"
  139. VERBATIM
  140. WORKING_DIRECTORY "${BUILDDIR}"
  141. )
  142. if(NOT IOS)
  143. add_custom_command(
  144. OUTPUT "${BUILDDIR}/Makefile"
  145. COMMAND ${ENV_COMMAND} CC=${CMAKE_C_COMPILER}
  146. COMMAND ${ENV_COMMAND} AR=${CMAKE_AR}
  147. COMMAND ${ENV_COMMAND} LD=${CMAKE_LINKER}
  148. COMMAND ${ENV_COMMAND} RANLIB=${CMAKE_RANLIB}
  149. COMMAND ${ENV_COMMAND} MAKE=${MAKE}
  150. COMMAND ${ENV_COMMAND} MAKEDEPPROG=${CMAKE_C_COMPILER}
  151. COMMAND ${ENV_COMMAND} WINDRES=${CMAKE_RC_COMPILER}
  152. VERBATIM
  153. APPEND
  154. )
  155. if(EMSCRIPTEN)
  156. list(APPEND DISABLES
  157. threads
  158. no-engine
  159. no-dso
  160. no-asm
  161. no-shared
  162. no-sse2
  163. no-srtp
  164. )
  165. else()
  166. list(APPEND DISABLES
  167. enable-static-engine
  168. no-zlib
  169. no-ssl2
  170. no-idea
  171. no-cast
  172. no-seed
  173. no-md2
  174. no-tests)
  175. endif()
  176. endif()
  177. if(EMSCRIPTEN)
  178. add_custom_command(
  179. OUTPUT "${BUILDDIR}/Makefile"
  180. COMMAND "$ENV{EMSDK}/upstream/emscripten/emconfigure" ./config
  181. ${SHARED}
  182. ${DISABLES}
  183. "--prefix=${CMAKE_INSTALL_PREFIX}"
  184. "--openssldir=/etc/ssl"
  185. "--cross-compile-prefix=\"/\""
  186. VERBATIM
  187. APPEND
  188. )
  189. add_custom_target(build_libs ALL
  190. COMMAND ${ENV_COMMAND} "PATH=${MSYS_BIN_DIR}${PATH_VAR}"
  191. COMMAND "${CMAKE_COMMAND}" -E touch "${BUILDDIR}/krb5.h"
  192. COMMAND "${MAKE}" make build_libs
  193. VERBATIM
  194. WORKING_DIRECTORY "${BUILDDIR}"
  195. DEPENDS "${BUILDDIR}/Makefile"
  196. BYPRODUCTS ${INSTALL_LIBS}
  197. )
  198. else()
  199. add_custom_command(
  200. OUTPUT "${BUILDDIR}/Makefile"
  201. COMMAND "${PERL}" Configure
  202. ${SHARED}
  203. ${DISABLES}
  204. ${PLATFORM}
  205. "--prefix=${CMAKE_INSTALL_PREFIX}"
  206. "--libdir=${CMAKE_INSTALL_PREFIX}/lib"
  207. "--openssldir=/etc/ssl"
  208. ${CFLAGS}
  209. VERBATIM
  210. APPEND
  211. )
  212. add_custom_target(build_libs ALL
  213. COMMAND ${ENV_COMMAND} "PATH=${MSYS_BIN_DIR}${PATH_VAR}"
  214. COMMAND "${CMAKE_COMMAND}" -E touch "${BUILDDIR}/krb5.h"
  215. COMMAND "${MAKE}" -j ${VCPKG_CONCURRENCY} build_libs
  216. VERBATIM
  217. WORKING_DIRECTORY "${BUILDDIR}"
  218. DEPENDS "${BUILDDIR}/Makefile"
  219. BYPRODUCTS ${INSTALL_LIBS}
  220. )
  221. endif()
  222. add_custom_command(
  223. OUTPUT "${BUILDDIR}/Makefile"
  224. COMMAND "${CMAKE_COMMAND}" "-DDIR=${BUILDDIR}" -P "${CMAKE_CURRENT_LIST_DIR}/remove-deps.cmake"
  225. VERBATIM
  226. APPEND
  227. )
  228. if((CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS") AND BUILD_SHARED_LIBS)
  229. if(DEFINED CMAKE_INSTALL_NAME_DIR)
  230. set(ID_PREFIX "${CMAKE_INSTALL_NAME_DIR}")
  231. else()
  232. set(ID_PREFIX "@rpath")
  233. endif()
  234. add_custom_command(
  235. TARGET build_libs
  236. COMMAND /usr/bin/install_name_tool -id "${ID_PREFIX}/libssl.${SHLIB_VERSION}.dylib"
  237. "${BUILDDIR}/libssl.${SHLIB_VERSION}.dylib"
  238. COMMAND /usr/bin/install_name_tool -id "${ID_PREFIX}/libcrypto.${SHLIB_VERSION}.dylib"
  239. "${BUILDDIR}/libcrypto.${SHLIB_VERSION}.dylib"
  240. COMMAND /usr/bin/install_name_tool -change "${CMAKE_INSTALL_PREFIX}/lib/libcrypto.${SHLIB_VERSION}.dylib"
  241. "${ID_PREFIX}/libcrypto.${SHLIB_VERSION}.dylib"
  242. "${BUILDDIR}/libssl.${SHLIB_VERSION}.dylib"
  243. VERBATIM
  244. )
  245. endif()
  246. install(
  247. FILES ${INSTALL_LIBS}
  248. DESTINATION lib
  249. )
  250. install(
  251. FILES ${INSTALL_BINS}
  252. DESTINATION bin
  253. )
  254. install(
  255. FILES ${INSTALL_PKG_CONFIGS}
  256. DESTINATION lib/pkgconfig
  257. )