build-and-test.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env bash
  2. PYVER=$(${CONDA_PREFIX}/bin/python -c 'import sys; sys.stdout.write("{}.{}".format(sys.version_info[0], sys.version_info[1]))')
  3. if [[ $(uname) == Darwin ]]; then
  4. # The macOS linker does not work like this. If you want a single archive to get linked in then you need to
  5. # identify it to the linker with the full path.
  6. # NATURE_STATIC=-Wl,-static
  7. # NATURE_SHARED=-Wl,-dynamic
  8. NATURE_STATIC=
  9. NATURE_SHARED=
  10. REPLACEMENT_STATIC=${CONDA_PREFIX}/lib/libpython${PYVER}.a
  11. RPATH=-Wl,-rpath,${CONDA_PREFIX}/lib
  12. DEFAULT_EXPECTED_NATURE=shared
  13. else
  14. NATURE_STATIC=-Wl,-Bstatic
  15. NATURE_SHARED=-Wl,-Bdynamic
  16. REPLACEMENT_STATIC=-lpython${PYVER}
  17. RPATH=-Wl,-rpath,${CONDA_PREFIX}/lib
  18. DEFAULT_EXPECTED_NATURE=static
  19. fi
  20. ERRORS=no
  21. declare -a NATURES=(shared)
  22. if [[ ${PKG_NAME} == libpython-static ]]; then
  23. NATURES+=(static)
  24. NATURES+=(default)
  25. fi
  26. for NATURE in "${NATURES[@]}"; do
  27. LDFLAGS_ORIG=$(python3-config --ldflags --embed)
  28. re='^(.*)(-lpython[^ ]*)(.*)$'
  29. FIRST_LIBPATH=
  30. if [[ ${LDFLAGS_ORIG} =~ $re ]]; then
  31. if [[ ${NATURE} == static ]]; then
  32. LDFLAGS_NATURE="${BASH_REMATCH[1]} ${NATURE_STATIC} ${REPLACEMENT_STATIC} ${NATURE_SHARED} ${BASH_REMATCH[3]}"
  33. RPATH_NATURE=
  34. elif [[ ${NATURE} == shared ]]; then
  35. # Yes, force shared then back to the default, which is also shared.
  36. LDFLAGS_NATURE="${BASH_REMATCH[1]} ${NATURE_SHARED} ${BASH_REMATCH[2]} ${NATURE_SHARED} ${BASH_REMATCH[3]}"
  37. RPATH_NATURE=${RPATH}
  38. # We ensure -L${PREFIX}/lib comes before -L$PREFIX/lib/python3.8/config-3.8-x86_64-linux-gnu as that contains only a
  39. # static lib and since that dir would (otherwise) be searched first, on Linux, this shadows our dynamic library and
  40. # dynamic linking will not happen
  41. FIRST_LIBPATH=-L${PREFIX}/lib
  42. else
  43. LDFLAGS_NATURE=${LDFLAGS_ORIG}
  44. if [[ ${DEFAULT_EXPECTED_NATURE} == shared ]]; then
  45. RPATH_NATURE=${RPATH}
  46. else
  47. RPATH_NATURE=
  48. fi
  49. fi
  50. fi
  51. echo "INFO :: Testing embedded ${NATURE} python interpreter"
  52. echo "${CC} ${FIRST_LIBPATH} $(dirname ${BASH_SOURCE[0]})/a.c $(python3-config --cflags) ${LDFLAGS_NATURE} -o ${CONDA_PREFIX}/bin/embedded-python-${NATURE} ${RPATH_NATURE}"
  53. ${CC} ${FIRST_LIBPATH} $(dirname ${BASH_SOURCE[0]})/a.c $(python3-config --cflags) ${LDFLAGS_NATURE} -o ${CONDA_PREFIX}/bin/embedded-python-${NATURE} ${RPATH_NATURE}
  54. if ! [[ -f ${CONDA_PREFIX}/bin/embedded-python-${NATURE} ]]; then
  55. echo "ERROR :: Failed to build ${CONDA_PREFIX}/bin/embedded-python-${NATURE}"
  56. ERRORS=yes
  57. continue
  58. fi
  59. if ! ${CONDA_PREFIX}/bin/embedded-python-${NATURE}; then
  60. echo "ERROR :: Failed to run ${CONDA_PREFIX}/bin/embedded-python-${NATURE} failed"
  61. ERRORS=yes
  62. fi
  63. if [[ -n ${READELF} ]]; then
  64. ${READELF} -d ${CONDA_PREFIX}/bin/embedded-python-${NATURE} | rg libpython
  65. if [[ $? == 0 ]]; then
  66. TRUE_NATURE=shared
  67. else
  68. TRUE_NATURE=static
  69. fi
  70. elif [[ -n ${OTOOL} ]]; then
  71. ${OTOOL} -l ${CONDA_PREFIX}/bin/embedded-python-${NATURE} | rg libpython
  72. if [[ $? == 0 ]]; then
  73. TRUE_NATURE=shared
  74. else
  75. TRUE_NATURE=static
  76. fi
  77. fi
  78. EXPECTED_NATURE=${NATURE}
  79. if [[ ${EXPECTED_NATURE} == default ]]; then
  80. EXPECTED_NATURE=${DEFAULT_EXPECTED_NATURE}
  81. fi
  82. if [[ ${EXPECTED_NATURE} != ${TRUE_NATURE} ]]; then
  83. echo "ERROR :: Embedded python with ${EXPECTED_NATURE} (${NATURE}) nature is linked to ${TRUE_NATURE} python library"
  84. ERRORS=yes
  85. fi
  86. done
  87. if [[ ${ERRORS} == yes ]]; then
  88. echo "ERROR :: Please see 'python linked to' messages above."
  89. exit 1
  90. else
  91. echo "INFO :: Python embedding tests passed OK"
  92. exit 0
  93. fi