Openwrt Compilar Programa Externo

De Wiki de BandaAncha.eu
Saltar a: navegación, buscar

Prefacio

Aunque el firmware Openwrt es utilizado sobre para temas de routing, en realidad , es un Sistema Operativo multi-proposito, para sistemas embebidos. En su día me topé con la problemática de que quería hacer funcionar una impresora en Openwrt Chaos Calmer. Encontré tutoriales sobre cómo instalar CUPS, pero ninguno sobre cómo instalar el filtro especificado de la impresora (que tiene dos componentes: uno universal, que es un fichero PPD, y otro, que es un ejecutable binario que hay que compilar). El binario asociado al filtro, era proporcionado por el fabricante, y la verdad, compilarlo e instalarlo en un ordenador es tan sencillo como hacer un configure, make y make install. Ahora bien, la compilación para Openwrt se hace en una máquina externa, ¿cómo compilamos un binario compatible con nuestro router, pero desde nuestro propio ordenador? ¿Cómo instalamos y usamos el toolchain de Openwrt? Pues este ultimo tiene bastante envoltorios, y no basta con una simple añadidura al $path. A modo de ejemplo, mostraré cómo se compila un ejecutable para Huawei HG556A.

Preparativos

Requisitos Previos

Los requisitos previos son los del propio toolchain de Openwrt Una vez cumplidos los prerequisitos, necesitamos básicamente dos elementos

  1. El toolchain (llamado también SDK), para nuestro router. Para HG556A y Barrier Breaker, es este
  2. En el SDK, haber instalado las dependencias en librerías de nuestro paquete (mediante feeds install, p.e.) (p.e. el que hice yo, tenía dependencia de lcups y lcupsimage)
  3. Un Makefile envoltorio, especial de Openwrt. (se puede adaptar facilmente de de un paquete para otro) El mío tiene esta pinta:
##############################################
# OpenWrt Makefile for rastertostar program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to 
# specify a basic description of the package, 
# where to build our program, where to find 
# the source files, and where to install the 
# compiled program on the router. 
# 
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and 
# there should be no trailing whitespace in
# lines that are not commented.
# 
##############################################

include $(TOPDIR)/rules.mk

# Name and release number of this package
PKG_NAME:=rastertostar
PKG_RELEASE:=1


# This specifies the directory where we're going to build the program.  
# The root build directory, $(BUILD_DIR), is by default the build_mipsel 
# directory in your OpenWrt SDK directory
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)


include $(INCLUDE_DIR)/package.mk



# Specify package information for this program. 
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION 
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/rastertostar
	SECTION:=utils
	CATEGORY:=Utilities
	TITLE:=rastertostar -- prints a snarky message
	DEPENDS:=+libcupsimage +libcups
endef


# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
#define Package/rastertostar/description
#	If you can't figure out what this program does, you're probably
#	brain-dead and need immediate medical attention.
#endef



# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)/
endef


# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one


# Specify where and how to install the program. Since we only have one file, 
# the rastertostar executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running 
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install 
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the 
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/rastertostar/install
	$(INSTALL_DIR) $(1)/usr/lib/cups/filter/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/rastertostar $(1)/usr/lib/cups/filter/
endef


# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,rastertostar))

Preparación del entorno de compilación cruzada

Hacer que el SDK se autocompile (ver la documentación oficial de Openwrt)

Compilación del binario

  1. Colocamos el Makefile envoltorio en una subcarpeta del SDK, que en mi caso es /home/barfelix/arcadyan/OpenWrt-SDK-15.05.1-brcm63xx-smp_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64/package/feeds/printing/rastertostar
  2. P.e., en la carpeta arriba mencionada, creamos una subcarpeta que en mi caso se llama src, dondé está todo el arbol de código fuente (con su Makefile de fábrica)