cryptographyをpipインストールする際の fatal error: 'openssl/opensslv.h' file not found 対策メモ

OSX環境(10.11.6 Beta(15G12a))で、cryptographyをpipでインストールしようとした際に遭遇したエラーに対する対策メモ。

いまおもえばpastebinにでも貼っておいたほうがよかった。

問題

brewでインストールしたOpenSSLのヘッダファイル(opensslv.h)を見つけてくれず、コンパイル時にエラーとなる。

% pip install cryptography
Collecting cryptography
  Using cached cryptography-1.4.tar.gz
<...>
  building '_openssl' extension
  creating build/temp.macosx-10.11-intel-2.7/build
  creating build/temp.macosx-10.11-intel-2.7/build/temp.macosx-10.11-intel-2.7
  cc -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c build/temp.macosx-10.11-intel-2.7/_openssl.c -o build/temp.macosx-10.11-intel-2.7/build/temp.macosx-10.11-intel-2.7/_openssl.o
  build/temp.macosx-10.11-intel-2.7/_openssl.c:423:10: fatal error: 'openssl/opensslv.h' file not found
  #include <openssl/opensslv.h>
           ^
  1 error generated.
  error: command 'cc' failed with exit status 1

  ----------------------------------------
  Failed building wheel for cryptography
Failed to build cryptography
Installing collected packages: cryptography
  Running setup.py install for cryptography
    Complete output from command /Users/saitou/venv/2.7.10/ansible/bin/python -c "import setuptools, tokenize;__file__='/private/var/folders/kk/ftd09wdd0sz27nbm8zwzjsdh0000gn/T/pip-build-u8oOQQ/cryptography/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/kk/ftd09wdd0sz27nbm8zwzjsdh0000gn/T/pip-d3jDDM-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/saitou/venv/2.7.10/ansible/include/site/python2.7/cryptography:

<...>
    building '_openssl' extension
    cc -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c build/temp.macosx-10.11-intel-2.7/_openssl.c -o build/temp.macosx-10.11-intel-2.7/build/temp.macosx-10.11-intel-2.7/_openssl.o
    build/temp.macosx-10.11-intel-2.7/_openssl.c:423:10: fatal error: 'openssl/opensslv.h' file not found
    #include <openssl/opensslv.h>
             ^
    1 error generated.
    error: command 'cc' failed with exit status 1

    ----------------------------------------

<...>

対策

本家のHomebrewの項に書いてあった...

% brew install openssl
% env CRYPTOGRAPHY_OSX_NO_LINK_FLAGS=1 LDFLAGS="$(brew --prefix openssl)/lib/libssl.a $(brew --prefix openssl)/lib/libcrypto.a" CFLAGS="-I$(brew --prefix openssl)/include" pip install cryptography

恐らく一週間後には覚えてなさそうなのでメモしておく。