[Solved] Add a new package to the source

https://wiki.openwrt.org/doc/devel/packages
https://lede-project.org/docs/guide-developer/helloworld

@Antek, please let us continue here. Everything over again:

    • Get OpenWRT source (in Ubuntu 17.10 64 bit, user andrei):

    ~/Git$ git clone https://github.com/openwrt/openwrt.git
    ~/Git/openwrt$ ./scripts/feeds update -a
    ~/Git/openwrt$ ./scripts/feeds install -a
    ~/Git/openwrt$ make menuconfig

    • In make menuconfig change:

      a. * for LuCi.
      b. Select Ralink Mediatek MIPS / MT7621 / ZBT-WE1326 profile.

    • Compile source:

    ~/Git/openwrt$ make prereq
    ~/Git/openwrt$ make download
    ~/Git/openwrt$ make check
    ~/Git/openwrt$ make V=s

    • Create package helloworld:

~/helloworld$ touch helloworld.c

in helloworld.c:

#include <stdio.h>
int main(void) { printf("\nHello, world!\n\n"); return 0; }

compile it:

~/helloworld$ gcc -c -o helloworld.o helloworld.c -Wall
~/helloworld$ gcc -o helloworld helloworld.o
~/helloworld$ ls
helloworld  helloworld.c  helloworld.o
~/helloworld$ ./helloworld
Hello, world!

create Makefile:

~/mypackages/examples/helloworld$ touch Makefile

in Makefile (not makefile and replace in it all spaces with tabs):

include $(TOPDIR)/rules.mk

# Name, version and release number
# The name and version of your package are used to define the variable to point to the build directory of your package: $(PKG_BUILD_DIR)
PKG_NAME:=helloworld
PKG_VERSION:=1.0
PKG_RELEASE:=1

# Source settings (i.e. where to find the source codes)
# This is a custom variable, used below
SOURCE_DIR:=/home/andrei/helloworld

include $(INCLUDE_DIR)/package.mk

# Package definition; instructs on how and where our package will appear in the overall configuration menu ('make menuconfig')
define Package/helloworld
	SECTION:=examples
	CATEGORY:=Examples
	TITLE:=Hello, world!
endef

# Package description; a more verbose description on what our package does
define Package/helloworld/description
	A simple "Hello, world!" -application.
endef

# Package preparation instructions; create the build directory and copy the source code. 
# The last command is necessary to ensure our preparation instructions remain compatible with the patching system.
define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	cp $(SOURCE_DIR)/* $(PKG_BUILD_DIR)
	$(Build/Patch)
endef

# Package build instructions; invoke the target-specific compiler to first compile the source file, and then to link the file into the final executable
define Build/Compile
	$(TARGET_CC) $(TARGET_CFLAGS) -o $(PKG_BUILD_DIR)/helloworld.o -c $(PKG_BUILD_DIR)/helloworld.c
	$(TARGET_CC) $(TARGET_LDFLAGS) -o $(PKG_BUILD_DIR)/$1 $(PKG_BUILD_DIR)/helloworld.o
endef

# Package install instructions; create a directory inside the package to hold our executable, and then copy the executable we built previously into the folder
define Package/helloworld/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/usr/bin
endef

# This command is always the last, it uses the definitions and variables we give above in order to get the job done
$(eval $(call BuildPackage,helloworld))
  • feed:

    ~/Git/openwrt$ touch feeds.conf

in feeds.conf: src-link mypackages /home/andrei/mypackages

    • Install mypackages:

    ~/Git/openwrt$ ./scripts/feeds update mypackages
    ~/Git/openwrt$ ./scripts/feeds install -a -p mypackages
    ~/Git/openwrt$ make menuconfig

  • in submenu Examples mark helloworld with *.
    • Compile helloworld package:
      ~/Git/openwrt$ make package/helloworld/compile

If everything went successfully, we are presented with a brand new package named helloworld_1.0-1_???.ipk in bin/packages/???/mypackages folder.

1 Like

The steps you take seem correct. If the compilation succeeds, the package should be found from somewhere under ~/Git/openwrt/bin/packages folder.

The final folder structure depends on your target/subtarget/profile selections, so just use find to locate the package. It contains "helloworld_1.0-1" in its name.

If you use make instead of make package/helloworld/compile then you'll be presented with a firmware image that installs helloworld into the directory that you specify in Package/helloworld/install section of the package manifest file.

2 Likes

Step 5:

~/Git/openwrt$ ./scripts/feeds update mypackages
Updating feed 'mypackages' from '/home/andrei/mypackages' ...
Create index file './feeds/mypackages.index' 
Collecting package info: done
~/Git/openwrt$ ./scripts/feeds install -a -p mypackages
Installing all packages from feed mypackages.
Installing package 'helloworld' from mypackages
~/Git/openwrt$ make menuconfig

in Examples select helloworld. Save, exit.

Step 6:

~/Git/openwrt$ make package/helloworld/compile V=s
make[1]: Entering directory '/home/andrei/Git/openwrt'
make[2]: Entering directory '/home/andrei/Git/openwrt/package/libs/toolchain'
echo "libc" >> /home/andrei/Git/openwrt/staging_dir/target-mipsel_24kc_musl/pkginfo/toolchain.default.install
echo "libgcc" >> /home/andrei/Git/openwrt/staging_dir/target-mipsel_24kc_musl/pkginfo/toolchain.default.install
echo "libpthread" >> /home/andrei/Git/openwrt/staging_dir/target-mipsel_24kc_musl/pkginfo/toolchain.default.install
make[2]: Leaving directory '/home/andrei/Git/openwrt/package/libs/toolchain'
make[2]: Entering directory '/home/andrei/mypackages/examples/helloworld'
echo "helloworld" >> /home/andrei/Git/openwrt/staging_dir/target-mipsel_24kc_musl/pkginfo/helloworld.default.install
make[2]: Leaving directory '/home/andrei/mypackages/examples/helloworld'
make[1]: Leaving directory '/home/andrei/Git/openwrt'
~/Git/openwrt$ 

Step 7: ~/Git/openwrt$ make V=s

Deploying and testing your package

Now that the package is ready, we can deploy and install it on the target router. The author recommends using a SCP client such as WinSCP in order to transfer the file from the development environment into your router. For installation purposes, you can save the package to the /tmp folder on your router.

Assuming you transferred the package to the /tmp folder, you can use the OPKG tool to install the package using the following command:

root@OpenWrt:/# opkg install /tmp/helloworld_1.0-1_<arch>.ipk
Installing helloworld (1.0-1) to root...
Configuring helloworld.

After installation, you should be able to run the application by calling the executable:

root@OpenWrt:/# helloworld

Hello, world!

Removing your package

You can remove the installation of the package using the OPKG tool:

root@OpenWrt:/# opkg remove helloworld
Removing package helloworld from root...

And you can delete the installation package, if you no longer need it:

root@OpenWrt:/# rm /tmp/helloworld_1.0-1_<arch>.ipk

"Hello, world!" test:

~/Git/openwrt$ ssh-keygen -f "/home/andrei/.ssh/known_hosts" -R 192.168.1.1
# Host 192.168.1.1 found: line 1
/home/andrei/.ssh/known_hosts updated.
Original contents retained as /home/andrei/.ssh/known_hosts.old
~/Git/openwrt$ ssh root@192.168.1.1
The authenticity of host '192.168.1.1 (192.168.1.1)' can't be established.
RSA key fingerprint is SHA256:L8HE3xSG7Hfvs0dW28Zq6J9gUMusmHfBmPTlK6nnBGU.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.1' (RSA) to the list of known hosts.

BusyBox v1.27.2 () built-in shell (ash)

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 OpenWrt SNAPSHOT, r6067-e2ec3f7550

root@OpenWrt:~# helloworld

Hello, world!

root@OpenWrt:~#

Thank you very much for the help Antek, the problem was a wrong name for Makefile (makefile) and spaces for tabs in this file.

The next problem is to compile 15.05 release ("Build dependency: Please install Git (git-core) >= 1.6.5" message): https://forum.openwrt.org/t/how-to-compile-15-05-release-build-dependency-please-install-git-git-core-1-6-5-message.

Who can help me?

Please continue posting in the new thread, rather than posting the same question on a new issue in two different threads.

Thanks.

Sure. This because I placed at the end a link for the new thread. All my threads is about one project, one step by an other.

Doesn't matter.

When you have a new issue, create a new thread.

People benefit more when they find threads that match their issue, not your project.

Ok. You wrote "Splved". I guess it should means "Solved".

When you solve an issue, mark the thread as solved.

I can place a link for the new thread at the end?

You already have.

In the future, just start a new thread...without links to it in another thread.

Ok :pray:.

Oh, I can do it self? let me try pls... Yes, I can. Thnks.

Hi,
I did what you have shown in install the package Helloworld, it is what I have received:

nettan@debian-lenovo:~/source-17.01.0-rc1$ ./scripts/feeds update Helloworld
Updating feed 'Helloworld' from '~/Helloworld/src' ...
Create index file './feeds/Helloworld.index' 
nettan@debian-lenovo:~/source-17.01.0-rc1$ ./scripts/feeds update helloworld
nettan@debian-lenovo:~/source-17.01.0-rc1$ ./scripts/feeds update Helloworld
Updating feed 'Helloworld' from '~/Helloworld/src' ...
Create index file './feeds/Helloworld.index' 
nettan@debian-lenovo:~/source-17.01.0-rc1$ ./scripts/feeds install -a -p Helloworld
Installing all packages from feed Helloworld.
nettan@debian-lenovo:~/source-17.01.0-rc1$ make menuconfig

But in menuconfig, the suboption Example doesnt show up.

Please help me out.
Thanks in advance.
Tony.

And when I run:

~/Git/openwrt$ make package/helloworld/compile

this is what I received:

make[1]: Entering directory '/home/nettan/source-17.01.0-rc1'
make[1]: *** No rule to make target 'package/helloworld/compile'.  Stop.
make[1]: Leaving directory '/home/nettan/source-17.01.0-rc1'
/home/nettan/source-17.01.0-rc1/include/toplevel.mk:197: recipe for target 'package/helloworld/compile' failed
make: *** [package/helloworld/compile] Error 2

I got the submenu appeared, but still cannot compile the package helloworld: make package/helloworld/compile

Anyone please give me a hint?

Tony