title: How do I build python-nss and NSS for Mac OS X?
body: <p>I'm trying to build <a href="http://people.redhat.com/jdennis/python-nss/doc/api/html/index.html" rel="nofollow">python-nss</a>, a python interface for the <a href="http://www.mozilla.org/projects/security/pki/nss/" rel="nofollow">Mozilla NSS library</a>, on a 64-bit Mac running Mac OS 10.6.5, for use in python software running on Mac OS X 10.6 or later. I can get NSS itself to build successfully, using certain options, but the python-nss build gives several warnings, and the resulting Python module isn't usable.</p>
  
  <p>To build NSS, I'm following <a href="https://developer.mozilla.org/en/NSS_reference/Building_and_installing_NSS/Build_instructions" rel="nofollow">these instructions</a>, but using this <a href="http://ftp.mozilla.org/pub/mozilla.org/security/nss/releases/NSS_3_12_9_RTM/src/nss-3.12.9-with-nspr-4.8.7.tar.gz" rel="nofollow">NSS source code</a>, rather than a checkout from cvs. If I just run <code>make nss_build_all</code>, I encounter an error:</p>
  
  <pre><code>ncraike@ncraikework ~/Installs/nss-3.12.9/mozilla/security/nss
  $ make nss_build_all
  ...
  drbg.c: In function ‘RNG_RandomUpdate’:
  drbg.c:516: error: size of array ‘arg’ is negative
  make[3]: *** [Darwin10.5.0_DBG.OBJ/Darwin_SINGLE_SHLIB/drbg.o] Error 1
  make[2]: *** [libs] Error 2
  make[1]: *** [libs] Error 2
  make: *** [libs] Error 2
  </code></pre>
  
  <p>The line in question (line 516 of <code>mozilla/security/nss/lib/freebl/drbg.c</code>) is an assertion that a particular type is of an expected size:</p>
  
  <pre><code>    PR_STATIC_ASSERT(sizeof(size_t) &lt;= 4);
  </code></pre>
  
  <p>If I write a quick test program, sizeof(size_t) seems to be 8, so perhaps the 64-bit version is being built, despite this on the above instructions page:</p>
  
  <blockquote>
    <p>On Unix platforms, except Alpha/OSF1, if you want a build for the system's 64-bit ABI, set USE_64=1 in your environment. By default, NSS builds for the 32-bit environment on all platforms except Alpha/OSF1.</p>
  </blockquote>
  
  <p>Adding the gcc option <code>-arch i386</code> (suggested for a <a href="http://www.qtcentre.org/threads/30670-On-Mac-quot-...file-was-built-for-unsupported-file-format..-quot-SOLVED" rel="nofollow">similar issue</a>) doesn't help, but building with the USE_64 environment variable is successful (although a 64-bit build may not be what I need):</p>
  
  <pre><code>ncraike@ncraikework ~/Installs/nss-3.12.9/mozilla/security/nss
  $ USE_64=1 make nss_build_all
  </code></pre>
  
  <p>This may be fine, but problems occur when I try to build python-nss (using <a href="http://ftp.mozilla.org/pub/mozilla.org/security/python-nss/releases/PYNSS_RELEASE_0_10_0/src/python-nss-0.10.tar.bz2" rel="nofollow">this source</a>).</p>
  
  <p>Some modifications to python-nss's <code>setup.py</code> are needed to include the NSS libraries just built. The original <code>setup.py</code> hard-codes the include directories for each extension, for example:</p>
  
  <pre><code>nss_nss_extension = \
      Extension('nss.nss',
                sources            = ['src/py_nss.c'],
                include_dirs       = ['src', '/usr/include/nss3', '/usr/include/nspr4'],
                libraries          = ['nspr4', 'ssl3', 'nss3'],
                extra_compile_args = extra_compile_args,
                )
  </code></pre>
  
  <p>So I've modified the extension declarations by adding these lines:</p>
  
  <pre><code>DIST_ROOT = '/Users/ncraike/Installs/nss-3.12.9/mozilla/dist/'
  INCLUDE_DIRS = [DIST_ROOT+'Darwin10.5.0_64_DBG.OBJ/include', DIST_ROOT+'public/nss/', DIST_ROOT+'private/nss/']
  LIB_DIRS = [DIST_ROOT+'Darwin10.5.0_64_DBG.OBJ/lib/']
  </code></pre>
  
  <p>...and changing each extension to add an INCLUDE_DIRS list and include a <code>library_dirs</code> argument (as described in the <a href="http://docs.python.org/release/2.6.6/distutils/setupscript.html#library-options" rel="nofollow">distutils documentation</a>). For example:</p>
  
  <pre><code>nss_nss_extension = \
      Extension('nss.nss',
                sources            = ['src/py_nss.c'],
                include_dirs       = ['src'] + INCLUDE_DIRS,
                libraries          = ['nspr4', 'ssl3', 'nss3'],
                library_dirs       = LIB_DIRS,
                extra_compile_args = extra_compile_args,
                )
  </code></pre>
  
  <p>After these changes, <code>python setup.py build</code> runs and seems to be aware of the NSS libraries, but produces several warnings, including:</p>
  
  <pre><code>src/py_nss.c:12640: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘Py_ssize_t’
  gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup -arch i386 -arch ppc -arch x86_64 build/temp.macosx-10.6-universal-2.6/src/py_nss.o -L/Users/ncraike/Installs/nss-3.12.9/mozilla/dist/Darwin10.5.0_64_DBG.OBJ/lib/ -lnspr4 -lssl3 -lnss3 -o build/lib.macosx-10.6-universal-2.6/nss/nss.so
  ld: warning: in build/temp.macosx-10.6-universal-2.6/src/py_nss.o, file was built for unsupported file format which is not the architecture being linked (i386)
  ld: warning: in /Users/ncraike/Installs/nss-3.12.9/mozilla/dist/Darwin10.5.0_64_DBG.OBJ/lib//libnspr4.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
  ld: warning: in /Users/ncraike/Installs/nss-3.12.9/mozilla/dist/Darwin10.5.0_64_DBG.OBJ/lib//libssl3.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
  </code></pre>
  
  <p>"file was built for unsupported file format which is not the architecture being linked" is the most common warning. This error is mentioned in <a href="http://www.qtcentre.org/threads/30670-On-Mac-quot-...file-was-built-for-unsupported-file-format..-quot-SOLVED" rel="nofollow">a post on another site</a>, with a possible solution of using the <code>-arch i386</code> option with gcc. I'm not sure at what step in the build process to add this option (NSS or python-nss?), and how I might add it to the python distutils build script.</p>
  
  <p>The build does complete, but the resulting python module doesn't seem usable:</p>
  
  <pre><code>ncraike@ncraikework ~/Installs/python-nss-0.10/build/lib.macosx-10.6-universal-2.6
  $ ls
  nss
  ncraike@ncraikework ~/Installs/python-nss-0.10/build/lib.macosx-10.6-universal-2.6
  $ python
  Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
  [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import nss.nss
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  ImportError: dlopen(nss/nss.so, 2): Library not loaded: @executable_path/libssl3.dylib
    Referenced from: /Users/ncraike/Installs/python-nss-0.10/build/lib.macosx-10.6-universal-2.6/nss/nss.so
    Reason: image not found
  </code></pre>
  
  <p>Is my error in how I'm building NSS or how I'm building python-nss? How should I be telling the python-nss build script to link with the Mac OS X NSS libraries correctly? I have much more python experience than C experience, so if I've made some simple building  error I won't be surprised.</p>
  
  <p>I'm running Mac OS 10.6.5, with Xcode 3.2.4 (64-bit) installed. <code>gcc -v</code> gives <code>gcc version 4.2.1 (Apple Inc. build 5664)</code> and <code>Target: i686-apple-darwin10</code>.</p>
  
  <p>Thanks.</p>