build_openssl.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. import os
  6. import sys
  7. from distutils import dist
  8. from distutils.ccompiler import get_default_compiler
  9. from distutils.command.config import config
  10. from _cffi_src.utils import (
  11. build_ffi_for_binding,
  12. compiler_type,
  13. extra_link_args,
  14. )
  15. def _get_openssl_libraries(platform):
  16. if os.environ.get("CRYPTOGRAPHY_SUPPRESS_LINK_FLAGS", None):
  17. return []
  18. # OpenSSL goes by a different library name on different operating systems.
  19. if platform == "win32" and compiler_type() == "msvc":
  20. return [
  21. "libssl",
  22. "libcrypto",
  23. "advapi32",
  24. "crypt32",
  25. "gdi32",
  26. "user32",
  27. "ws2_32",
  28. ]
  29. else:
  30. # darwin, linux, mingw all use this path
  31. # In some circumstances, the order in which these libs are
  32. # specified on the linker command-line is significant;
  33. # libssl must come before libcrypto
  34. # (https://marc.info/?l=openssl-users&m=135361825921871)
  35. # -lpthread required due to usage of pthread an potential
  36. # existance of a static part containing e.g. pthread_atfork
  37. # (https://github.com/pyca/cryptography/issues/5084)
  38. if sys.platform == "zos":
  39. return ["ssl", "crypto"]
  40. else:
  41. return ["ssl", "crypto", "pthread"]
  42. def _extra_compile_args(platform):
  43. """
  44. We set -Wconversion args here so that we only do Wconversion checks on the
  45. code we're compiling and not on cffi itself (as passing -Wconversion in
  46. CFLAGS would do). We set no error on sign conversion because some
  47. function signatures in LibreSSL differ from OpenSSL have changed on long
  48. vs. unsigned long in the past. Since that isn't a precision issue we don't
  49. care.
  50. """
  51. # make sure the compiler used supports the flags to be added
  52. is_gcc = False
  53. if get_default_compiler() == "unix":
  54. d = dist.Distribution()
  55. cmd = config(d)
  56. cmd._check_compiler()
  57. is_gcc = (
  58. "gcc" in cmd.compiler.compiler[0]
  59. or "clang" in cmd.compiler.compiler[0]
  60. )
  61. if is_gcc or not (
  62. platform in ["win32", "hp-ux11", "sunos5"]
  63. or platform.startswith("aix")
  64. ):
  65. return ["-Wconversion", "-Wno-error=sign-conversion"]
  66. else:
  67. return []
  68. ffi = build_ffi_for_binding(
  69. module_name="_openssl",
  70. module_prefix="_cffi_src.openssl.",
  71. modules=[
  72. # This goes first so we can define some cryptography-wide symbols.
  73. "cryptography",
  74. "aes",
  75. "asn1",
  76. "bignum",
  77. "bio",
  78. "cmac",
  79. "conf",
  80. "crypto",
  81. "ct",
  82. "dh",
  83. "dsa",
  84. "ec",
  85. "ecdh",
  86. "ecdsa",
  87. "engine",
  88. "err",
  89. "evp",
  90. "fips",
  91. "hmac",
  92. "nid",
  93. "objects",
  94. "ocsp",
  95. "opensslv",
  96. "osrandom_engine",
  97. "pem",
  98. "pkcs12",
  99. "rand",
  100. "rsa",
  101. "ssl",
  102. "x509",
  103. "x509name",
  104. "x509v3",
  105. "x509_vfy",
  106. "pkcs7",
  107. "callbacks",
  108. ],
  109. libraries=_get_openssl_libraries(sys.platform),
  110. extra_compile_args=_extra_compile_args(sys.platform),
  111. extra_link_args=extra_link_args(compiler_type()),
  112. )