# OpenWrt Build System on FreeBSD



## balanga (Feb 14, 2019)

I don't really like using Linux but sometimes you have to... the problem is which flavour?
 it
I've come across OpenWrt over the last year and it seems nice and compact and it comes with a Build System for building the system as well as apps for it. I've managed to put together this build system on FreeBSD, but am unsure about how to test it.

Am I correct in thinking that FreeBSD can be used for building apps on other systems?

I've put together a simple script for installing the build system and will post it here if anyone is interested.


----------



## Lamia (Feb 14, 2019)

Do post it. Keep in mind that development environments vary with projects. 

FreeBSD has a vast amount of packages for different build environments that's why you got the Openwrt's. You may also need interact with its developers say in the IRC network or mailing list for some more concise responses.


----------



## T-Daemon (Feb 14, 2019)

balanga said:


> I've come across OpenWrt over the last year and it seems nice and compact and it comes with a Build System for building the system as well as apps for it. I've managed to put together this build system on FreeBSD, but am unsure about how to test it.


Here is a description how to build OpenWRT under FreeBSD. Don’t get to exited, it certainly won’t work in the present form with current FreeBSD versions. Informative is the shell script there. The script configures a proper environment to build with gnu tools natively under FreeBSD. It could serve as a guide for your build system. Even with proper environment, a successful or complete build may not be guaranteed. See last paragraph.

Wouldn’t it be easier to build on a standalone Linux installation or in a virtual machine, as recommend by OpenWRT?


----------



## balanga (Feb 14, 2019)

Lamia said:


> Do post it. Keep in mind that development environments vary with projects.



Here it comes -


```
pkg bootstrap -y

#  Install required pkgs

cat <<EOF | xargs pkg install -y
git
bash
gmake
gtar
gcc
gawk
ncurses
findutils
patch
wget
getopt
coreutils
EOF

# Build and install GNU time
#
#wget https://ftp.gnu.org/gnu/time/time-1.9.tar.gz
#tar zxf time-1.9.tar.gz
#cd time-1.9
#./configure
#make
#make install
#mv /usr/local/bin/time /usr/local/bin/gtime

# Install OpenWrt Build System

mkdir ~/OpenWrt-BuildSystem
cd ~/OpenWrt-BuildSystem
git clone https://github.com/openwrt/openwrt.git
cd openwrt

# patch mkhash.c

wget 'https://bugs.openwrt.org/index.php?getfile=292' -O patchfile
patch scripts/mkhash.c -i patchfile -o scripts/mkhash.c

# Let's go!!!

./scripts/feeds update -a
./scripts/feeds install -a

make  menuconfig
```

bear in mind it is fairly bareboned and could do with some refinement.

Building GNU time is commented out because it is no longer needed, but I've left it in to remind me how to build it should the need arise.

I'd be interested in any feedback...


----------



## balanga (Feb 14, 2019)

T-Daemon said:


> Here is a description how to build OpenWRT under FreeBSD. Don’t get to exited, it certainly won’t work in the present form with current FreeBSD versions. Informative is the shell script there. The script configures a proper environment to build with gnu tools natively under FreeBSD. It could serve as a guide for your build system. Even with proper environment, a successful or complete build may not be guaranteed. See last paragraph.
> 
> Wouldn’t it be easier to build on a standalone Linux installation or in a virtual machine, as recommend by OpenWRT?



Thanks for the pointer to the page you mentioned. I notice that it was written six years ago, and as we all know, something which worked six years ago may well not work today.

I have managed to create the OpenWrt Build System on FreeBSD, but not really sure how to test it..

Of course it's easier to use Linux for this sort of thing, but I notice that you can't build this Build System on OpenWrt itself.
However I much prefer FreeBSD to Linux and don't like the idea of FreeBSD not being able to do something which Linux can.


----------



## ogogon (May 17, 2020)

Please tell me, now do you have a working configuration for building on FreeBSD?

Ogogon.


----------



## balanga (Nov 22, 2021)

ogogon said:


> Please tell me, now do you have a working configuration for building on FreeBSD?
> 
> Ogogon.


I've been out of the loop for over a year so have only just come across this post.

I'm trying to get things working because I have a problem getting an image for a TP-LINK Archer MR200 v4 which is misbehaving and source code is available for this model, but you have to build it yourself.

Surprisingly, the build script above almost worked in creating the build system. I'm looking into a couple of errors at the moment, but will update the script, as and when I sort them.


----------



## diizzy (Nov 22, 2021)

ogogon
Just use bhyve and a Debian VM, upstream pretty much doesn't care about anything else (OS X some love though) even though Alpine could be used at some point but is now broken again afaik.


----------



## balanga (Dec 2, 2021)

After revisiting my script and updating FreeBSD to 13.0-RELEASE I am left with one outstanding error, namely:-



> Checking 'getopt'... failed.


My updated script consists of:-


> ```
> pkg bootstrap -y
> 
> #  Install required pkgs
> ...



After enquiring on the OpenWrt forum it was suggested that there is GNU getopt which is different to the one used by FreeBSD. Is this the case, and if so is the GNU version available?

I think the check for getopt consists of the following code


```
$(eval $(call SetupHostCommand,getopt, \
    Please install an extended getopt version that supports --long, \
    gnugetopt -o t --long test -- --test | grep '^ *--test *--', \
    getopt -o t --long test -- --test | grep '^ *--test *--', \
    /usr/local/opt/gnu-getopt/bin/getopt -o t --long test -- --test | grep '^ *--test *--'))
```

See https://forum.openwrt.org/t/anyone-using-freebsd/31320/20

Any advice would be welcomed.


----------



## covacat (Dec 2, 2021)

install misc/getopt and rename the binary gnugetop or put /usr/local/bin in from of /usr/bin in PATH


----------



## T-Daemon (Dec 2, 2021)

covacat said:


> install misc/getopt and rename the binary gnugetop


Renaming the binary is not a good idea. The pkg(8) database wouldn't be aware of the renaming. The renamed binary would stay outside of any pkg(8) manipulation, like when the package/port is updated or deleted.


----------



## balanga (Dec 2, 2021)

covacat said:


> install misc/getopt and rename the binary gnugetop or put /usr/local/bin in from of /usr/bin in PATH


misc/getopt was already installed, but I found that just renaming it to gnugetopt got around the problem....
I noticed that the info about FreeBSD's getopt says:-


> Rewrite of getopt(1) with the following features:
> 
> * It can do anything that the GNU getopt(3) routines can do.
> * It can cope with spaces and shell metacharacters within arguments.
> ...



So it looks like this sort of error shouldn't occur.


----------



## covacat (Dec 2, 2021)

it looks like it passes the above grep test
if you want to avoid the renaming trouble copy the binary in ~/bin and add ~/bin first in PATH while you run the failing script


----------



## balanga (Dec 3, 2021)

Can someone translate the following code into English - it's not really clear to me what is being tested..


```
$(eval $(call SetupHostCommand,getopt, \
        Please install an extended getopt version that supports --long, \
        gnugetopt -o t --long test -- --test | grep '^ *--test *--', \
        getopt -o t --long test -- --test | grep '^ *--test *--', \
        /usr/local/opt/gnu-getopt/bin/getopt -o t --long test -- --test | grep '^ *--test *--'))
```

Is there any way to change this so that a test for gnugetopt is made only if the getopt test fails?

At the moment, if I rename FreeBSD's getopt as gnugetopt the test passes.


----------



## T-Daemon (Dec 3, 2021)

Try setting a evaluation line for misc/getopt installed `/usr/local/bin/gtopt` to prereq-build.mk:

```
$(eval $(call SetupHostCommand,getopt, \
          ...
          /usr/local/bin/getopt -o t --long test -- --test | grep '^ *--test *--', \
          /usr/local/opt/gnu-getopt/bin/getopt -o t --long test -- --test | grep '^ *--test *--'))
```


----------



## balanga (Dec 3, 2021)

It's only just dawned on me that FreeBSD has a builtin *getopt* as well as a pkg -misc/getopt


----------



## T-Daemon (Dec 3, 2021)

Then why did the evaluations of

```
getopt -o t --long test -- --test | grep '^ *--test *--', \
```
returned  `Checking 'getopt'... failed.` ?

getopt is in PATH, `whereis getopt` returns 

```
getopt: /usr/bin/getopt /usr/share/man/man1/getopt.1.gz /usr/ports/misc/getopt
```


----------



## balanga (Dec 4, 2021)

T-Daemon said:


> Then why did the evaluations of
> 
> ```
> getopt -o t --long test -- --test | grep '^ *--test *--', \
> ...


The first test uses FreeBSD's builtin getopt, which seems incompatible *gnugetopt *whereas misc/getopt works fine so I've created a patch which includes a test  for /usr/local/bin/getopt. That seems to be the cleanest way of handling things.


----------



## balanga (Dec 4, 2021)

Patch for using misc/getopt when trying to create an OpenWrt build system on FreeBSD:-

```
--- include/prereq-build.mk     2021-12-03 21:03:44.406974000 +0000
+++ include/prereq-build.mk-FreeBSD-getopt      2021-12-03 21:56:19.603555000 +0000
@@ -130,6 +130,7 @@
        Please install an extended getopt version that supports --long, \
        gnugetopt -o t --long test -- --test | grep '^ *--test *--', \
        getopt -o t --long test -- --test | grep '^ *--test *--', \
+       /usr/local/bin/getopt -o t --long test -- --test | grep '^ *--test *--', \
        /usr/local/opt/gnu-getopt/bin/getopt -o t --long test -- --test | grep '^ *--test *--'))

 $(eval $(call SetupHostCommand,stat,Cannot find a file stat utility, \
```


----------



## DenisVS (Dec 12, 2021)

Now you have to build OpenWRT with FreeBSD kernel


----------



## balanga (Nov 28, 2022)

After leaving this for a year, I thought I revisit things to see how well this build system works.  

Here is my latest script which I run in a 13.1-RELEASE jail to make sure I have a clean environment.

```
pkg bootstrap -y

#  Install required pkgs

cat <<EOF | xargs pkg install -y
git
rsync
bash
gmake
gtar
gcc
gawk
ncurses
findutils
diffutils
patch
wget
getopt
coreutils
EOF

# Install OpenWrt Build System

mkdir ~/OpenWrt-BuildSystem
cd ~/OpenWrt-BuildSystem
git clone https://github.com/openwrt/openwrt.git
cd openwrt

# amend prereq-build.mk

sed -E -i "" -e "/^.getopt/a\\
        \/usr\/local\/bin\/getopt -o t --long test -- --test \| grep '\^ \*--test \*--', \\\\" include/prereq-build.mk

# Let's go!!!

./scripts/feeds update -a
./scripts/feeds install -a

make  menuconfig
```

Unfortunately I get the following error:


```
Installing package 'sipp' from telephony
Installing package 'siproxd' from telephony
Installing package 'sngrep' from telephony
Installing package 'yate' from telephony
gmake[1]: Entering directory '/root/OpenWrt-BuildSystem/openwrt'
make -s -C scripts/config mconf: build failed. Please re-run make with -j1 V=s or V=sc for a higher verbosity level to see what's going on
gmake[1]: *** [/root/OpenWrt-BuildSystem/openwrt/include/toplevel.mk:106: scripts/config/mconf] Error 1
gmake[1]: Leaving directory '/root/OpenWrt-BuildSystem/openwrt'
*** Error code 2

Stop.
make: stopped in /root/OpenWrt-BuildSystem/openwrt

Script done, output file is OpenWrt_Build-System-install_2211281717.log
      656.68 real       290.41 user        39.91 sys
```

Any pointers would be appreciated.


----------



## balanga (Nov 29, 2022)

On further investigation, I  get this error msg:-

```
gmake[4]: Entering directory '/root/OpenWrt-BuildSystem/openwrt/scripts/config'
; mkdir -p ./; trap "rm -f ./.mconf-cfg.tmp" EXIT; { /bin/sh mconf-cfg.sh; } > ./.mconf-cfg.tmp; if [ ! -r mconf-cfg ] || ! cmp -s mconf-cfg ./.mconf-cfg.tmp; then true '  UPD     mconf-cfg'; mv -f ./.mconf-cfg.tmp mconf-cfg; fi
*
* Unable to find the ncurses package.
* Install ncurses (ncurses-devel or libncurses-dev
* depending on your distribution).
*
* You may also need to install pkg-config to find the
* ncurses installed in a non-default location.
```

Any advice on which ncurses pkg I need to install?


----------

