Chinaunix首页 | 论坛 | 博客
  • 博客访问: 540739
  • 博文数量: 51
  • 博客积分: 345
  • 博客等级: 民兵
  • 技术积分: 534
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-21 12:02
个人简介

文章分类

全部博文(51)

文章存档

2023年(2)

2022年(1)

2021年(7)

2020年(10)

2019年(2)

2016年(20)

2015年(5)

2014年(1)

2011年(3)

我的朋友

分类: C/C++

2016-09-06 11:30:18

Atheros提供的无线管理工具为例,来说明如何将多个独立的bin文件-编译合并为一个可执行文件。

wireless_tools目录下存在多个独立的文件,用于配置/管理无线资源,如:iwlistiwpriv等;这些工具可以编译为独立二进制;Atheros提供iwmulticall.c文件可以将多个二进制合并为一个,使用软链接来调用各个工具;

[lvjianlin@ubuntu wireless_tools.29]$ls

19-udev-ifrename.rules  IFRENAME-VS-XXX.txt  iwlib.c        macaddr.c                 wireless.10.h  wireless.19.h

CHANGELOG.h             iftab.5              iwlib.h        Makefile                  wireless.11.h  wireless.20.h

COPYING                 INSTALL              iwlist.8       PCMCIA.txt                wireless.12.h  wireless.21.h

cs                      iwconfig.8           iwlist.c       README                    wireless.13.h  wireless.22.h

DISTRIBUTIONS.txt       iwconfig.c           iwmulticall.c  README.fr                 wireless.14.h  wireless.7

fr                      iwevent.8            iwpriv.8       sample_enc.c              wireless.15.h

HOTPLUG.txt             iwevent.c            iwpriv.c       sample_pm.c               wireless.16.h

ifrename.8              iwgetid.8            iwspy.8        sample_priv_addr.c        wireless.17.h

ifrename.c              iwgetid.c            iwspy.c        udev.import_devpath.diff  wireless.18.h

root@OpenWrt:~# ls /usr/sbin/iw* -l

-rwxr-xr-x    1 root     root         75505 Sep  4 07:26 /usr/sbin/iw

-rwxr-xr-x    1 root     root         61056 Sep  6 02:18 /usr/sbin/iwconfig

lrwxrwxrwx    1 root     root             8 Sep  6 02:18 /usr/sbin/iwgetid -> iwconfig

lrwxrwxrwx    1 root     root             8 Sep  6 02:18 /usr/sbin/iwlist -> iwconfig

lrwxrwxrwx    1 root     root             8 Sep  6 02:18 /usr/sbin/iwpriv -> iwconfig

lrwxrwxrwx    1 root     root             8 Sep  6 02:18 /usr/sbin/iwspy -> iwconfig

Code

实现合并工具主要是靠iwmulticall.c文件组织;

/* Prototypes of the main of each tool */

extern int

              main_iwconfig(int       argc,

                                  char **            argv);

extern int

              main_iwlist(int             argc,

                                char **   argv);

extern int

              main_iwspy(int            argc,

                               char **      argv);

extern int

              main_iwpriv(int           argc,

                                char **   argv);

extern int

              main_iwgetid(int         argc,

                                 char **               argv);

 

/************************** MULTICALL HACK **************************/

/*

 * The idea for multicall is to put all the tools and the library in

 * the same binary. This way, you can save the overhead of the library,

 * of each tool, can better optimise the code and throw away the stuff

 * you don't need from the library.

 * This almost divide the size of the tools by two (without stripping).

 * On the down side, you no longer have the libiw for other tools to

 * use, but for the target systems (embedded), this doesn't matter

 * much, as they just need to configure the card...

 * Note that splitting the lib and the multicall tools would not

 * make sense, as most gains are found in the inclusion of the lib...

 *

 * Our strategy is to include directly the *.c, rather than compile

 * them separatly. This allow to simplify compilation and hide the

 * multicall tweaks from the other tools.

 * Yeah, this leads to a bit a preprocessor abuse...

 * Jean II

 */

 

/* We need the library */

#include "iwlib.c"

 

/* Get iwconfig in there. Mandatory. */ //这里将iwconfig.c文件内main(),iw_usage(),find_command进行替换,这样就不会出现符号冲突的情况;下面的iwlist.ciwlist.c等文件使用同样的宏格式进行替换;

#define main(args...) main_iwconfig(args)

#define iw_usage(args...) iwconfig_usage(args)

#define find_command(args...) iwconfig_find_command(args)

#include "iwconfig.c"

#undef find_command

#undef iw_usage

#undef main

 

/* Get iwlist in there. Scanning support is pretty sweet. */

#define main(args...) main_iwlist(args)

#define iw_usage(args...) iwlist_usage(args)

#define find_command(args...) iwlist_find_command(args)

#include "iwlist.c"

#undef find_command

#undef iw_usage

#undef main

 

#ifndef WE_ESSENTIAL

/* Get iwspy in there, it's not that big. */

#define main(args...) main_iwspy(args)

#include "iwspy.c"

#undef main

#endif

 

/* Get iwpriv in there. Mandatory for HostAP and some other drivers. */

#define main(args...) main_iwpriv(args)

#define iw_usage(args...) iwpriv_usage(args)

#include "iwpriv.c"

#undef iw_usage

#undef main

 

#ifndef WE_ESSENTIAL

/* Do we really need iwgetid ? Well, it's not like it's a big one */

#define main(args...) main_iwgetid(args)

#define iw_usage(args...) iwgetid_usage(args)

#include "iwgetid.c"

#undef iw_usage

#undef main

#endif

 

/* iwevent is useless for most people, don't grab it ? */

 

/* ifrename is big and useless for those systems */

 

 

/******************************* MAIN ********************************/

 

/*------------------------------------------------------------------*/

/*

 * The main !

 */

int

main(int            argc,

     char **               argv)

{

  char *           call_name = basename(argv[0]);     /* Strip path  此处获取是软链接的文件名称,也就是期望使用的工具名*/

 

  /* This is a testing hack */

  if(!strcmp(call_name, "iwmulticall") && (argc > 0))

    {

      argv++;

      argc--;

      call_name = basename(argv[0]);

    }

 

  /* Just check the name under which we were called... */ //进入各个工具函数入口

 

  if(!strcmp(call_name, "iwconfig"))

    return(main_iwconfig(argc, argv));

  if(!strcmp(call_name, "iwlist"))

    return(main_iwlist(argc, argv));

#ifndef WE_ESSENTIAL

  if(!strcmp(call_name, "iwspy"))

    return(main_iwspy(argc, argv));

#endif

  if(!strcmp(call_name, "iwpriv"))

    return(main_iwpriv(argc, argv));

#ifndef WE_ESSENTIAL

  if(!strcmp(call_name, "iwgetid"))

    return(main_iwgetid(argc, argv));

#endif

 

  /* Uh oh... Not supposed to come here. */

  printf("iwmulticall : you are not supposed to call me this way...\n");

  return(0);

}

 

Makefile

# Targets to build

STATIC=libiw.a

DYNAMIC=libiw.so.$(WT_VERSION)

PROGS= iwconfig iwlist iwpriv iwspy iwgetid iwevent ifrename         //期望编译的工具

MANPAGES8=iwconfig.8 iwlist.8 iwpriv.8 iwspy.8 iwgetid.8 iwevent.8 ifrename.8

MANPAGES7=wireless.7

MANPAGES5=iftab.5

EXTRAPROGS= macaddr iwmulticall

# Install directories

INSTALL_DIR= $(PREFIX)/sbin/

INSTALL_LIB= $(PREFIX)/lib/

INSTALL_INC= $(PREFIX)/include/

INSTALL_MAN= $(PREFIX)/man/

# Standard compilation targets    //编译.o

all:: $(IWLIB) $(PROGS) install-iwmulticall

#       for f in $(PROGS); do \

#         cp -f $$f $(FILESYSTEM_ROOT_DIR)/usr/sbin; \

#       done

#       cp -f $(DYNAMIC) $(FILESYSTEM_ROOT_DIR)/usr/lib

 

%: %.o

         $(CC) $(LDFLAGS) $(STRIPFLAGS) $(XCFLAGS) -o $@ $^ $(LIBS)

%.o: %.c wireless.h

         $(CC) $(XCFLAGS) -c $<

%.so: %.c wireless.h

         $(CC) $(XCFLAGS) $(PICFLAG) -c -o $@ $<

 

iwconfig: iwconfig.o $(IWLIB)

 

iwlist: iwlist.o $(IWLIB)

 

iwpriv: iwpriv.o $(IWLIB)

 

iwspy: iwspy.o $(IWLIB)

 

iwgetid: iwgetid.o $(IWLIB)

 

iwevent: iwevent.o $(IWLIB)

 

ifrename: ifrename.o $(IWLIB)

 

macaddr: macaddr.o $(IWLIB)

 

# Always do symbol stripping here

iwmulticall: iwmulticall.o

         $(CC) $(LDFLAGS) -Wl,-s $(XCFLAGS) -o $@ $^ $(LIBS)

 

# It's a kind of magic...

wireless.h:

         cp $(WEXT_HEADER) wireless.h

         @echo ${TOOLCHAINDIR}

install-iwmulticall:: iwmulticall     //编译iwmulticall

         install -m 755 -d $(INSTALL_DIR)

         install -m 755 $< $(INSTALL_DIR)/iwconfig     //此处$<代表iwmulticall,将其重命名为iwconfig

         ( cd $(INSTALL_DIR) ; \

           ln -f -s iwconfig iwlist ; \

           ln -f -s iwconfig iwspy ; \

           ln -f -s iwconfig iwpriv ; \

           ln -f -s iwconfig iwgetid )

 

阅读(3381) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~