English Chinese (Traditional) (zh_TW)
Chapter 4. Slow Porting
Slow Porting
Okay, so it was not that simple, and the port required some modifications to get it to work. In this section, we will explain, step by step, how to modify it to get it to work with the ports paradigm.
How Things Work
First, this is the sequence of events which occurs when the user first types `make` in the port's directory. Having [.filename]#bsd.port.mk# in another window while reading this really helps to understand it.
But do not worry, not many people understand exactly how [.filename]#bsd.port.mk# is working... _:-)_
The `fetch` target is run. The `fetch` target is responsible for making sure that the tarball exists locally in `DISTDIR`. If `fetch` cannot find the required files in `DISTDIR` it will look up the URL `MASTER_SITES`, which is set in the Makefile, as well as our FTP mirrors where we put distfiles as backup. It will then attempt to fetch the named distribution file with `FETCH`, assuming that the requesting site has direct access to the Internet. If that succeeds, it will save the file in `DISTDIR` for future use and proceed.
The `extract` target is run. It looks for the port's distribution file (typically a compressed tarball) in `DISTDIR` and unpacks it into a temporary subdirectory specified by `WRKDIR` (defaults to [.filename]#work#).
The `patch` target is run. First, any patches defined in `PATCHFILES` are applied. Second, if any patch files named [.filename]#patch-*# are found in `PATCHDIR` (defaults to the [.filename]#files# subdirectory), they are applied at this time in alphabetical order.
The `configure` target is run. This can do any one of many different things.
If it exists, [.filename]#scripts/configure# is run.
If `HAS_CONFIGURE` or `GNU_CONFIGURE` is set, [.filename]#WRKSRC/configure# is run.
The `build` target is run. This is responsible for descending into the port's private working directory (`WRKSRC`) and building it.
The `stage` target is run. This puts the final set of built files into a temporary directory (`STAGEDIR`, see <<staging,Staging>>). The hierarchy of this directory mirrors that of the system on which the package will be installed.
The `package` target is run. This creates a package using the files from the temporary directory created during the `stage` target and the port's [.filename]#pkg-plist#.
The `install` target is run. This installs the package created during the `package` target into the host system.
The above are the default actions. In addition, define targets `pre-_something_` or `post-_something_`, or put scripts with those names, in the [.filename]#scripts# subdirectory, and they will be run before or after the default actions are done.
For example, if there is a `post-extract` target defined in the [.filename]#Makefile#, and a file [.filename]#pre-build# in the [.filename]#scripts# subdirectory, the `post-extract` target will be called after the regular extraction actions, and [.filename]#pre-build# will be executed before the default build rules are done. It is recommended to use [.filename]#Makefile# targets if the actions are simple enough, because it will be easier for someone to figure out what kind of non-default action the port requires.
The default actions are done by the `do-_something_` targets from [.filename]#bsd.port.mk#. For example, the commands to extract a port are in the target `do-extract`. If the default target does not do the job right, redefine the `do-_something_` target in the [.filename]#Makefile#.
The "main" targets (for example, `extract`, `configure`, etc.) do nothing more than make sure all the stages up to that one are completed and call the real targets or scripts, and they are not intended to be changed. To fix the extraction, fix `do-extract`, but never ever change the way `extract` operates! Additionally, the target `post-deinstall` is invalid and is not run by the ports infrastructure.