cscope15.7a之windows下编译

注:已发布至 google code

准备工作:
 编译工具:MinGW 4.4.0
 额外第3方库:pdcurses 3.4,pcre 7.9或者其他posix兼容的正则表达式库(mingw网站的libgnurx也可,或者grep2.54源码自带lib文件夹中的regex)
 pcre是BSD授权,所以优先采用它而不是gnu c库中的regex。采用pcre时简单建立个文件regex.h,然后里面添加下面一行即可
 #include <pcreposix.h>
 
代码简单修改:
1) 运行./configure脚本,配置环境
2) 在根目录下config.h文件最下面添加2行
    #define __DJGPP__
    #define __MSDOS__
   
3) 给./src/exec.c和./src/mypopen.c的
    #include <sys/wait.h>
    添加条件
    #ifndef _WIN32
    #  include <sys/wait.h>
    #endif // _WIN32
4) 给./src/main.c中涉及到umask的356行和366行加上
    #ifndef _WIN32
    orig_umask = umask(S_IRWXG|S_IRWXO);
    #endif
   
    #ifndef _WIN32
    umask(orig_umask);
    #endif
   
5) ./src/main.c中涉及到SIGHUP、SIGQUIT、SIGPIPE的行加上如下
    #ifdef SIGHUP
    signal(SIGHUP, myexit);
    #endif
   
    #ifdef SIGQUIT
    /* dump core for debugging on the quit signal */
    if (sig == SIGQUIT) {
     abort();
    }
    #endif
   
6) ./src/main.c中359行,也就是mkdir函数那行改为
    #ifdef _WIN32
        if(mkdir(tempdirpv)) {
    #else
        if(mkdir(tempdirpv,S_IRWXU)) {
    #endif // _WIN32

7) ./src/mypopen.c中84行涉及fcntl的行修改为如下
    #ifdef __DJGPP__  /* FIXME: test feature, not platform */
        /* HBB 20010312: DOS GCC doesn't have FD_CLOEXEC (yet), so it
         * always fails this call. Have to skip that step */
        if(fd != -1)
            return(fd);
    #else
        if(fd != -1 && (fcntl(fd, F_SETFD, CLOSE_ON_EXEC) != -1))
            return(fd);
    #endif /* __DJGPP__ */

8) 修改./src/Makefile文件
    133行,CFLAGS去掉-g,可去掉gdb调试信息见小文件大小
    137行,CURSES_LIBS改为CURSES_LIBS = -lpdcurses -lpcreposix

这样就可以编译出cscope了。

功能改进:
1) win32下,没有TMPDIR该环境变量,我们将./src/main.c中
    tmpdir = mygetenv("TMPDIR", TMPDIR);
    改为
    #ifdef _WIN32
        tmpdir = mygetenv("TEMP", TMPDIR);
    #else
        tmpdir = mygetenv("TMPDIR", TMPDIR);
    #endif /* _WIN32 */

2) win32下,在工作路径包含空格时,会在读取已有交叉引用文件时出错,出异常处的代码如下:
        /* get the crossref file version but skip the current directory */
     if (fscanf(oldrefs, "cscope %d %*s", &fileversion) != 1) {
         postfatal("cscope: cannot read file version from file %s\n",
            reffile);
         /* NOTREACHED */
     fscanf遇到空格就终止了,并没有完全跳过当前路径。这里将putheader函数写入路径时额外添加‘\n'字符。
     main函数中部分修改代码:
      /* get the crossref file version but skip the current directory */
     if (fscanf(oldrefs, "cscope %d ", &fileversion) != 1) {
         postfatal("cscope: cannot read file version from file %s\n",
            reffile);
         /* NOTREACHED */
     }
        while(fgetc(oldrefs)!='\n')
            ;
    同时对putheader函数修改部分代码如下:
    dboffset = fprintf(newrefs, "cscope %d %s\n", FILEVERSION, dir);
    build函数修改部分代码如下:
        /* if there is an old cross-reference and its current directory matches */
        /* or this is an unconditional build */
        if ((oldrefs = vpfopen(reffile, "rb")) != NULL
     && unconditional == NO
     && fscanf(oldrefs, "cscope %d ", &fileversion) == 1
        && fgets(olddir, PATHLEN, oldrefs)
     && (strcmp(olddir, currentdir) == 0 /* remain compatible */
         || strcmp(olddir, newdir) == 0)) {

3) 当使用cscope -q构建inverted index时,需要用的外部的sort工具,目前只能在unix下的sort工具才能正常工作,win自带的sort无法正常工作,因为win下的sort不区分大小写。所以使用-q选项构建时,只能用cygwin或msys下使用。为了解除该限制,加入sort程序全路径,也就是只调用cscope.exe同一路径下的sort程序。 因为使用GetModuleFileNameA函数,链接时会出现无法找到该函数,只需在链接时加上"/c/WINDOWS/system32/kernel32.dll"
    #ifdef _WIN32
        /* only use the program sort that was accompany by cscope */
        char sortfullpath[PATHLEN+1];
        GetModuleFileNameA(NULL, sortfullpath, sizeof(sortfullpath));
        char *lastbackslash = strrchr(sortfullpath, '\\');
        strcpy(++lastbackslash, "sort");

     snprintf(sortcommand, sizeof(sortcommand)/sizeof(char), "set LC_ALL=C && \"%s\" -T \"%s\" \"%s\"", sortfullpath, tmpdir, temp1);
    #else
     snprintf(sortcommand, sizeof(sortcommand)/sizeof(char), "env LC_ALL=C sort -T %s %s", tmpdir, temp1);
    #endif

 

    
注意:
1) 由于win32下cscope采用pdcurses,这样就导致无法在msys的rxvt仅仅只能用来构建交叉引用,而不能启动程序进入其查询界面。

 错误之处恳请指正,谢谢。
cscope源码及可执行程序(附带gnu sort


 

 

 

 

posted on 2009-08-04 11:44  JesseFang  阅读(1650)  评论(0编辑  收藏  举报