构建 PyXPCOM

These are the instructions for building PyXPCOM.

Note: 为了那些想在Firefox和XULRunner中使用PyXPCOM的,PythonExt扩展为Firefox/XULRunner提供了预编译的PyXPCOM绑定。

Python安装

PyXPCOM安装的基本要求

  • PyXPCOM需要Python2.3或以上版本(Python2.3和Python2.4都已经测试过)。
  • PyXPCOM需要访问一个共享的Python库。

Linux

作为Linux用户,构建脚本会检查你是否安装了一个适合版本的Python共享库。跳到Compiling Mozilla部分,如果你接收到了错误并且提示你Python共享库无法被定位,那么你需要按照其后的步骤编译Python并且使用--enable-shared编译参数。

Recent Fedora and Ubuntu builds

These distributions generally have a shared Python already available.

Gentoo systems

Building a shared Python library seems to only be possible with python-2.3* or later, so you'll need to upgrade if you're using python-2.2* or earlier. For ebuilds python-2.3* and later the shared library is built and installed by default.

FreeBSD

从Ports(lang/python24)构建的Python已经提供了共享库。

Mac OS X

OS X 10.3和10.4系统中Python2.3框架已经可以。如果你想构建Python2.4,那么你可以手动从源代码进行构建、通过DarwinPortsFink构建、或者是安装MacPythonActivePython。然后你可以跳过编译过程。如果你想发布你的应用,你可能首选使用内建的Python2.3框架,因为嵌入Python框架到应用的捆绑形式可能会有用到一些非标准的自定义Python构建脚本。

Windows

官方的Windows Python安装版会自动安装共享库,所以Windows用户仅需要执行Python的安装而并不需要编译

其它系统

If you don't use any of the above systems, get the latest stable Python source tarball from python.org and do:

tar xjf Python-2.4.2.tar.bz2
cd Python-2.4.2
./configure --enable-shared --prefix=/usr  # Adjust --prefix to install over your current Python
make
sudo make install

编译 PyXPCOM

你应该熟悉了Mozilla构建过程

  • 下载或构建XULRunner SDK(需要10.0.2版本的XULRunner SDK)。
  • 创建pyxpcom目录并进入到此目录中:mkdir pyxpcom; cd pyxpcom
  • 使用Mercurial得到PyXPCOM源代码: hg clone http://hg.mozilla.org/pyxpcom src
  • 生成源代码配置脚本:cd src;  autoconf2.13; cd ..
  • 创建PyXPCOM构建目录: mkdir build; cd build
  • 配置: ../src/configure --with-libxul-sdk=/path/to/libxul-sdk
  • Make: make

Note

新的编译器似乎有一些问题,追加CXXFLAGS='-std=c++0x'编译参数进行make可能会有所帮助。当前已经的可以工作的编译是gcc 4.4.3和g++ 4.4.3。

最后的主版本也有一些问题,不知为什么没有设置PYTHON_SO,解决方法是追加-DPYTHON_SO=\"libpythondotted.version.so"\到CXXFLAGS参数中。

一些linux发行版本需要--prefix=/usr参数。

一些python所需的模块位于dist-packages目录中而不是site-packages目录,所以在执行make安装之前需要做一下替换工作 sed -i -e 's/site-packages/dist-packages/'

指定Python版本

The configure script will attempt to locate a Python version to use. In most cases, all you need to do is ensure the Python version you wish to use is on your PATH before configuring Mozilla.

Alternatively, you can also set a PYTHON environment variable that points to a Python executable (not a directory - it must be a Python executable). If set, the configure script will use the Python pointed to by the variable.

You can confirm what Python is being used by carefully watching the configure process. You should see a message similar to:

Building PyXPCOM using Python-2.4 from c:/Python24

Windows/MSVC users: make sure you don't use cygwin's Python. It is likely to cause compilation errors later.

Testing PyXPCOM

First test your fresh PyXPCOM build from within Mozilla's runtime environment.

Linux and OS X:

cd suite-debug/dist/bin
export PYTHONPATH=$PYTHONPATH:$HOME/mozilla/suite-debug/dist/bin/python # Adjust this to your PyXPCOM build path
./run-mozilla.sh ./seamonkey -chrome chrome://pyxultest/content 

Windows:

cd dist\bin
set PYTHONPATH=%PYTHONPATH%;C:\mozilla\dist\bin\python
seamonkey.exe -chrome chrome://pyxultest/content 

A window with controls should pop up. Run the tests it contains.

In the same directory you can also perform a simple test of the Python xpcom module from a standalone Python environment.

Linux and OS X:

export MOZILLA_FIVE_HOME=$HOME/mozilla/suite-debug/dist/bin # Adjust this to your Mozilla build path
export LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME
python -c 'from xpcom import components; print components.classes["@mozilla.org/file/local;1"]' 

Windows:

set PATH=%PATH%;C:\mozilla\dist\bin
set MOZILLA_FIVE_HOME=C:\mozilla\dist\bin
set LD_LIBRARY_PATH=%MOZILLA_FIVE_HOME%
python -c "from xpcom import components; print components.classes['@mozilla.org/file/local;1']" 

You should see output similar to the following:

Type Manifest File: /home/you/mozilla/suite-debug/dist/bin/components/xpti.dat
<xpcom.components._Class instance at 0xb7c1be8c>
nsStringStats
 => mAllocCount:            431
 => mReallocCount:          270
 => mFreeCount:             423  --  LEAKED 8 !!!
 => mShareCount:            450
 => mAdoptCount:              0
 => mAdoptFreeCount:          0

If you see Python error messages instead, make sure $PYTHONPATH is still set the same as in the first test.

Document Tags and Contributors

Contributors to this page: du.wei
最后编辑者: du.wei,