47
Embedded System Design GNU tool chain 2 Zhaofeng SJTU-SoME 2007-09-29

Embedded System Design GNU tool chain ( 2 )

  • Upload
    miyo

  • View
    45

  • Download
    0

Embed Size (px)

DESCRIPTION

Embedded System Design GNU tool chain ( 2 ). Zhaofeng SJTU-SoME 2007-09-29. Courseware WebSites. 1. Google svn server TortoiseSVN (svn client) http://esdlab.googlecode.com/svn/trunk/ 2. FTP ftp://202.120.46.8/Master/ESD_M/esd 3. Moodle 网站 http://202.120.46.2/moodle - PowerPoint PPT Presentation

Citation preview

Page 1: Embedded System Design GNU tool chain ( 2 )

Embedded System Design

GNU tool chain ( 2 )

Zhaofeng

SJTU-SoME2007-09-29

Page 2: Embedded System Design GNU tool chain ( 2 )

Courseware WebSites

• 1. Google svn server– TortoiseSVN (svn client)– http://esdlab.googlecode.com/svn/trunk/

• 2. FTP– ftp://202.120.46.8/Master/ESD_M/esd

• 3. Moodle 网站– http://202.120.46.2/moodle

• 4. www.eefocus.com: 公开课程• 5. www.openhard.com: 公开课程项目

Page 3: Embedded System Design GNU tool chain ( 2 )

Review

• 试写出一个完整 makefile ,来编译 facedetect.c;facedetect.h;main.c; 生成可执行文件facedetect 。要求写出一个稳健的 clean 伪目标。

Page 4: Embedded System Design GNU tool chain ( 2 )

Outline

• GNU history

• Tool Chain Overview

• A hello world

• Advanced topic

• Version Control System

• Auto tools

Page 5: Embedded System Design GNU tool chain ( 2 )

Version Control System

Page 6: Embedded System Design GNU tool chain ( 2 )

VCS 的分类• 1. 集中式模型

– 文件锁定(锁定-修改-解锁)• VSS

– 版本合并• CVS/SVN

• 2. 分布式模型– Bazaar/Git/Mercurial

• *. 手工作坊式: rar+msn :土八路

Page 7: Embedded System Design GNU tool chain ( 2 )

What is CVS?

• CVS = Concurrent Versions System

• Open source

• Client/server design

• Client and servers available for just about any platform OS X, Windows, and Linux

• Traditionally command line based but there are several excellent GUI clients

Page 8: Embedded System Design GNU tool chain ( 2 )

Terminology

• Repository - area on the server where the files are stored

• Sandbox - a local copy of the code which you work on and then commit to the repository

• Checkout - act of getting a local a copy of the latest version of the code from the repository

• Commit - saving the changes to your local file(s) to the cvs repository

Page 9: Embedded System Design GNU tool chain ( 2 )

术语• CVSROOT: 代码库 • Repository: 项目仓库• Version ( revision ) : 版本号• Branch: 版本分支

Page 10: Embedded System Design GNU tool chain ( 2 )

Terminology (part 2)

• Update - getting code changes that have been committed since you checked out the project

• Merge - combining changes between two versions of the same file

• History - shows a list of commit messages, times and, who committed for a particular file

• Revision - cvs assigned version number for a file• Tagging - a way to mark a group of files and revisions• Branching - a way to work on multiple copies of your app

lication

Page 11: Embedded System Design GNU tool chain ( 2 )

Other CVS Tools

• ViewCVS - http://viewcvs.sourceforge.net/

• CVSMonitor - http://ali.as/devel/cvsmonitor/

Page 12: Embedded System Design GNU tool chain ( 2 )

多人协同开发中的问题• 同步的最新修改• 文件的版本回溯• 多人同时修改同一个文件产生的冲突• 项目的分支开发• 文件权限控制

Page 13: Embedded System Design GNU tool chain ( 2 )

正确的运行模式用一个代码管理库集中管理代码同步

版本库

user1

user2

user3

Page 14: Embedded System Design GNU tool chain ( 2 )

CVS 开始第一个项目• 环境变量 $CVSROOT :

– bash: CVSROOT=/home/cvsroot ; export CVSROOT

– tcsh: setenv CVSROOT /home/cvsroot

• CVSROOT 库初始化: cvs init

• 项目包首次导入: cvs import– cvs import -m "write some comments here" project_name vender_tag release_ta

g

Page 15: Embedded System Design GNU tool chain ( 2 )

CVS 的使用命令• cvs checkout proj_name 导出项目包• cvs commit –m “write comments” file_name 提交更新• cvs update –m “write comments” file_name 同步库• cvs log file_name 查看修改日志• cvs diff –r1.2 –r1.3 file_name 查看文件版本区别• cvs add file_name 添加新文件到项目包• cvs remove file_name 从项目包里删除文件

Page 16: Embedded System Design GNU tool chain ( 2 )

CVS 的使用流程cvs checkout( 首次导出 ) cvs up( 同步代

码 )cvs commit( 提交修改 )cvs up (同步代码) cvs commit( 提交修改 )…..

恢复到一个旧的版本:如果用 cvs update -r1.2 file.name

这个命令是给 file.name 加一个 STICK TAG : "1.2" ,虽然你的本意只是想将它恢复到 1.2 版本

正确的恢复版本的方法是: cvs update -p -r1.2 file.name >file.name

如果不小心忘了 -p 加成 STICK TAG 的话:用 cvs update –A 解决

Page 17: Embedded System Design GNU tool chain ( 2 )

CVS vs VSS

• CVSupdate/commit

• CVS 没有锁定机制 edit/unedit

• VSS get_lastest_version /check_in

• checkout/undo checkout

通用的标记$Header$$Author$$Date$ $Revision$

Page 18: Embedded System Design GNU tool chain ( 2 )

WinCVS

http://www.wincvs.org/1. Cvs Folders->Macros folder->Change Root 2. Cvs Admin->Preferences

cvs server: [email protected]:/path/to/cvsrootAuthenitication:passwd at cvs server

3. Cvs Admin->Login… 4. Cvs Admin->Checkout module…

Page 19: Embedded System Design GNU tool chain ( 2 )

CVSTrac

WEB 界面的• CVS 用户管理• CVS WEB 浏览• BUG TRACKING

• Wiki 讨论版

Page 20: Embedded System Design GNU tool chain ( 2 )

What cvs isn’t/doesn’t...

• build system• project management• talking to your friends• change control:

– all changes are isolated vs. single logical change– bug fix tracking– track change verification

• testing program (regression testing)• work flow or process model

Page 21: Embedded System Design GNU tool chain ( 2 )

SVN

• CVS 的替代产品• 使用 SVN客户端从

http://esdbook.googlecode.com/svn/trunk/

下载第二章,阅读版本控制一节• 要求会用 svn 管理自己的代码和文档• 要求结合 google 管理项目组的代码和文档

Page 22: Embedded System Design GNU tool chain ( 2 )

Auto tools

Page 23: Embedded System Design GNU tool chain ( 2 )

GNU AutoTools 简介

Autoconf , Automake and Libtool

Page 24: Embedded System Design GNU tool chain ( 2 )

GNU Package 的典型安装• FAQ 0039 @ smth

– 下载源代码包 foo-1.0.tar.gz– tar xvzf foo-1.0.tar.gz– cd foo-1.0– ./configure– make– (su) make install

问题 1 :配置脚本 configure 是怎么生成的?

问题 2 : configure 脚本怎么知道该如何生成 Makefile

幕后英雄——GNU Auto Tools :

autoconf, automake, libtool, autoscan, autoheader……

Page 25: Embedded System Design GNU tool chain ( 2 )

Makefile’s Big Trouble?

• GNU 的 Makefile 规范相当复杂,随着项目复杂性的增加,要写出一个正确而高效的 Makefile 相当困难,对新手而言更是如此。

• 不同的类 Unix 操作系统之间存在着差异,移植性如何保证?需要大量手工编写的脚本和条件编译。

Page 26: Embedded System Design GNU tool chain ( 2 )

All in One Solution?

• GNU Auto Tools 是上个世纪 90年代开始发展起来的一系列辅助开发、打安装包的自动化工具。

• 各种工具分别开发,但是协同工作的很好。比如autoconf, automake, libtool等等。

• 但是,连开发者自己也承认,这套工具虽然好用,但是学习曲线很陡峭。

• 困难在于:一套新的思路和习惯,一套自定义的宏和规则

Page 27: Embedded System Design GNU tool chain ( 2 )

Hello World – Step by Step• 1. hello.c#include <stdio.h>int main() { printf(“Hello World!\n”); return 0; }• 2. Makefile.ambin_PROGRAMS = hellohello_SOURCES = hello.c• 3. configure.inAC_INIT(hello.c)AM_INIT_AUTOMAKE(hello, 0.1)AC_PROG_CCAC_OUTPUT(Makefile)• 4. aclocal • 5. autoconf• 6. automake --add-missing --foreign• 7. ./configure• 8. make (make install 安装, make dist 产生安装包 )

Page 28: Embedded System Design GNU tool chain ( 2 )

Hello World! 演示

—— 如何杀鸡用牛刀

Page 29: Embedded System Design GNU tool chain ( 2 )

来龙去脉

autoscanconfigure.scan configure.in

Makefile.am Makefile.in

Makefile

automake

editaclocal.m4

aclocal

configure

autoconf

Src code

Page 30: Embedded System Design GNU tool chain ( 2 )

configure.in(1)

• 是 configure 脚本的输入文件,为了解决在不同 unix 变种之间移植程序的问题:库名可能不同,应用程序名可能不同,结构和常量的定义可能不同……

• configure 脚本完成 autoconf与 automake 的初始化工作,为不同的平台定义相应的宏,检测并指定适当的程序名、库名、结构和常量名等等,指定要为哪些目录输出 Makefile 文件。总之,为编译程序做好一切准备工作。

Page 31: Embedded System Design GNU tool chain ( 2 )

configure.in(2)

configure.in 的八股文1. 基本初始化部分:包括 AC_INIT (必须第

一个出现 ), AM_INIT_AUTOMAKE( 程序包名,版本号 ), AC_CONFIG_HEADER

2. 可选宏:如 AC_ARG_ENABLE

3. 检测某些程序的存在性4. 检查程序用到的库

Page 32: Embedded System Design GNU tool chain ( 2 )

configure.in(3)

5. 检查某些头文件是否存在。6. 检查 Typedefs and structures 。7. 检查 Functions 。8. 指定在哪些目录输出 Makefile 。Seems troublesome, mmm? Don’t worry,

autoscan will do most of the work for you.

Page 33: Embedded System Design GNU tool chain ( 2 )

Makefile.am(1)

• 一种比Makefile 更高层次的规则。只指定要生成什么目标,它由什么源文件生成,要安装到什么目录。

• Just simple and stupid! • 可执行文件:bin_PROGRAMS = foofoo_SOURCES = foo1.c foo1.h foo2.cfoo_LDADD = foo3.o -lm foo4.afoo_LDFLAGS = -L<lib_path>foo_DEPENDENCIES =

Page 34: Embedded System Design GNU tool chain ( 2 )

Makefile.am(2)

• 对静态库:lib_LIBRARIES = libfoo.afoo_a_SOURCES =foo_a_LDADD = foo_a_LIBADD =foo_a_LDFLAGS = 只在 make 时做静态连接用,不安装的库:noinst_LIBRARIES = libfoo.a……• 对头文件: include_HEADERS = foo.h• 对数据文件: data_DATA = data1 data2

Page 35: Embedded System Design GNU tool chain ( 2 )

Makefile.am(3)

• 全局变量 ( 对所有目标都适用 )

INCLUDES = -I/dir1 -I/dir2

LDFLAGS = -L/dir1 -L/dir2

LDADD = foo.o foo.a -lfoo

EXTRA_DIST = file1 file2 源程序和一些默认的文件自动打入 .tar.gz 包,其它文件若要进入 .tar.gz 包可以用这种办法,比如配置文件 , 数据文件等等。

SUBDIRS = dir1 dir2 在处理本目录之前要递归处理哪些子目录

Page 36: Embedded System Design GNU tool chain ( 2 )

Makefile.am(4)

• 标准安装路径$(prefix) = /usr/local 是所有安装目录的默认前缀,

可以通过 ./configure --prefix=<new_prefix> 的方法覆盖。其它的预定义目录如: bindir = $(prefix)/bin, libdir

= $(prefix)/lib, datadir = $(prefix)/share, sysconfdir = $(prefix)/etc, …

• 想定义一个新的安装路径?比如 config, 可定义confdir = $(prefix)/config, 然后 conf_DATA = file1

file2 ,则 file1, file2 会作为数据文件安装到 $(prefix)/config 目录下。

Page 37: Embedded System Design GNU tool chain ( 2 )

Makefile.am(5)

• 尽量用相对路径引用源程序的位置,以下两个变量是预定义好的:– $(top_srcdir)无论在哪个目录层次,该变量定义了包含 src 目录的目录位置,用于引用源程序;

– $(top_builddir) 定义了生成目标文件上最上层目录,用于引用 .o等编译出来的目标文件。

– ……

Page 38: Embedded System Design GNU tool chain ( 2 )

Makefile.am to Makefile

• Automake takes a (relatively) simple file called Makefile.am and

• Creates a (relatively) complicated file called Makefile

• Notes:– Makefile.am is 85 lines (mostly boilerplate).– Makefile (generated) is 672 lines

Page 39: Embedded System Design GNU tool chain ( 2 )

configure’s Gifts

• configure 脚本生成的 Makefile 中已经带了很多常用的目标如: check, all, install, uninstall, clean, dist, distcheck, distclean, tags, maintainerclean.– If configure or make did it, make distclean und

oes it.– If make did it, make clean undoes it.– If make install did it, make uninstall undoes it.– If you did it, make maintainer-clean undoes it.

Page 40: Embedded System Design GNU tool chain ( 2 )

libtool 简介• 生成各种程序库的方便工具。• 提供一个统一的接口,程序员不用关心各种烦人

的底层细节:不同的平台的库可能要求不同的后缀,不同平台对库的安装方法不同,有些平台不支持动态库等等。

• 生成高层次的库,称为 libtool library ,后缀是 .la 。用它连接时,默认产生动态连接库,也可以用 -static参数指定生成静态连接库。

• 既可单独使用又可与 automake 和 autoconf 一起使用更加强大、方便。

Page 41: Embedded System Design GNU tool chain ( 2 )

使用 libtool

• 在 configure.in 文件中加上 AC_PROG_LIBTOOL宏,如果原来有 AC_PROG_RANLIB宏,删去它。

• 在 Makefile.am 文件中: lib_LTLIBRARIES = libshell.la libshell_la_SOURCES = object.c subr.c symbol.c 与原来的写法非常相似!

• .la 库只能连入 .lo( 使用 libtool 生成的目标文件 ) libshell_la_LDADD = xmalloc.lo @LTLIBOBJS@ • 传入库的版本号:

libshell_la_LDFLAGS = -version-info 1:0:1 • 与其它目标文件连接时用 LDFLAGS指定连接的方式 (默认是动态方式): -static, --all-static指定静态连接。

Page 42: Embedded System Design GNU tool chain ( 2 )

小结• Autoconf :根据用户提供的 configure.in 文件,生成一

个名为 configure 的脚本。该脚本可以搜集有关移植性的平台相关信息,这些信息被用来生成Makefiles ,配置头文件和其它平台相关的文件。

• Automake :根据用户提供的一个高层次的生成规则Makefile.am ,生成Makefile 文件的模板Makefile.in 。 Automake 生成的Makefiles符合 GNU的Makefile 标准,用户无需再手工编写Makefile 文件。

• Libtool :生成内存位置无关的代码且让共享库在不同平台间移植成为可能。它可以不依赖 autoconf 和 automake单独运行,另一方面, automake 和 libtool 可以无缝地集成使用。

Page 43: Embedded System Design GNU tool chain ( 2 )

GNU autoconf

autoscan

configure.scan

configure.ac(configure.in)

autoconf

configure

Source Code aclocal

Page 44: Embedded System Design GNU tool chain ( 2 )

GNU automake

Makefile.am

automake

Makefile.in

configure

Makefile

Page 45: Embedded System Design GNU tool chain ( 2 )

Homework

• 1. TASK1-LAB4-AUTOtools.doc

• 2. TASK1-LAB5-CVServerv0.doc• 3. 使用 SVN客户端从 http://

esdbook.googlecode.com/svn/trunk/

下载第二章,阅读版本控制一节– 要求会用 svn 管理自己的代码和文档– 要求结合 google 管理项目组的代码和文档

Page 46: Embedded System Design GNU tool chain ( 2 )

Case Study 1: Face Detection

• CaseStudy1_FaceDetection.ppt

Page 47: Embedded System Design GNU tool chain ( 2 )

Project

• 1. 建立 7 个组:人脸识别 (3) 指纹识别 (4)

• 2. 在 www.openhard.com 上建立项目组• 3. 第一阶段任务,完成 PC上的演示程序

– 时间:两周: 07.10.16 日晚结束–验收:课上演示,并完成设计过程报告