# How can I modify and rebuild a port?



## jtl (Jan 20, 2011)

Apologies if this more properly belongs in "Porting new software" or somewhere else.

I want to modify the code in a port and rebuild it.  Is there a way to do this?  I've (naively) tried modifying the code in the work directory and then doing `# make deinstall``# make install`, but this doesn't work.  Thanks for any help.

- Jon


----------



## JokerBoy (Jan 20, 2011)

well, you can make patches and add them to files directory as patch-NameOfPatch..


----------



## ohauer (Jan 20, 2011)

If this is an existing port, and there are already existing patches do the following.

`# cd $port`
`# cp -Rp files files.save` (if directory files does not exits create it manually)
`# make clean` 
`# make extract`
`# make patch`
`# cd work/portname`

If the file has already an applied patch you see $filename and $filename.orig, else `# cp $file $file.orig`.
Now make your changes.
`# cd $port`
`# make makepatch`

The `# make makepatch` command creates patch files and store them into the $port/files directory.

If you do now a `# make clean patch` then your patches should be applied cleanly, else you have your old files.save directory as reference to figure out the reason.


----------



## wblock@ (Jan 20, 2011)

`% man ports | less +/following`

shows the steps.  You can extract and patch the port, then make your changes, then either install it (if you feel lucky) or generate patches from the changes you've made and then install it.


----------



## jtl (Jan 21, 2011)

Thanks - I've successfully tested building the port with some trivial changes.  If I make another change after doing a `# make build` it seems that I have to do a `# make clean` before building again, because the `# make build` doesn't seem to know that one of the source files has been updated.  Is there a way to get `# make build` to rebuild only what's necessary?


----------



## wblock@ (Jan 21, 2011)

The easy way is to put your changes into a patch in the files/ directory.  Then let the port infrastructure handle it for you.


----------



## jtl (Jan 21, 2011)

`# make makepatch` doesn't recognize the makepatch target, nor is the makepatch target described in the man page for ports(7) or in /usr/ports/Mk/bsd.port.mk.  I'm running 8.1-RELEASE.  Do I need to update something, or is there another equivalent target?


----------



## wblock@ (Jan 21, 2011)

You need to have a recent ports tree and need to be in the port directory, not somewhere down in work/.  Actually, I haven't used makepatch, I just use diff.


----------



## jtl (Jan 21, 2011)

Thanks - I was in the wrong directory.  It looks like manual patches would be created with`# diff -ud old_file new_file`Does that look correct?


----------



## SirDice (Jan 21, 2011)

If I'm not mistaken these are (roughly) the steps a *make install* does:


```
make fetch         # Fetches the distfile
make extract       # Extracts the distfile into the work directory
make patch         # Patches the files in work with patches in the files directory
make configure     # Runs ./configure in the work directory
make install       # Installs the package
```


----------



## wblock@ (Jan 21, 2011)

jtl said:
			
		

> Thanks - I was in the wrong directory.  It looks like manual patches would be created with`# diff -ud old_file new_file`Does that look correct?



-d is probably fine, but I don't bother.  One gotcha is that port patches need to be relative to the ${WRKSRC} directory.  The Patching section of the Porter's Handbook covers it.


----------



## ckester (Jan 21, 2011)

SirDice said:
			
		

> If I'm not mistaken these are (roughly) the steps a *make install* does:
> 
> 
> ```
> ...



Before the final 
	
	



```
make install
```
 there's also a 
	
	



```
make build
```
 step.  Useful if you just want to verify that a port builds without errors, but don't want to actually install it.


----------



## jalla (Jan 21, 2011)

jtl said:
			
		

> Thanks - I've successfully tested building the port with some trivial changes.  If I make another change after doing a `# make build` it seems that I have to do a `# make clean` before building again, because the `# make build` doesn't seem to know that one of the source files has been updated.  Is there a way to get `# make build` to rebuild only what's necessary?



You don't have to _make clean_, just remove a couple of files from /usr/ports/whatever/portname/work


```
# rm work/.build_done.portname._usr_local work/.install_done.portname._usr_local
```


----------



## wblock@ (Jan 21, 2011)

ckester said:
			
		

> Before the final
> 
> 
> 
> ...



"build" is the default target, so just cd to the port directory and
`# make`

The man page link in post #4 shows the whole list of targets leading up to build, in order.


----------



## jtl (Jan 21, 2011)

Once I've done a `# make build`, it won't build again, even though I modify the source.

@jalla  Thank you - that was exactly what I needed.  I can now easily tweak the code and rebuild without doing a `# make clean`


----------



## silicium (Apr 18, 2011)

*automated editor with diff to patchfile ?*

Is there any editor which can be customized to automatically save a modified file in /tmp/ instead of work/, run *diff* and output patchfile to files/patch-*?


----------

