如何解析正则表达式的gcc输出以获取默认的包含路径?

时间:2018-12-26 21:13:29

标签: python linux gcc

我知道 gcc -xc -E -v - 在末尾打印默认的包含路径。

[patryk@patryk-asus-manjaro ~]$ gcc -xc -E -v -  
Using built-in specs.
COLLECT_GCC=gcc
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp --enable-cet=auto
Thread model: posix
gcc version 8.2.1 20181127 (GCC) 
COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/cc1 -E -quiet -v - -mtune=generic -march=x86-64
ignoring nonexistent directory "/usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/include-fixed
 /usr/include
End of search list.
^C
[patryk@patryk-asus-manjaro ~]$

我创建了正则表达式

#include <\.\.\.> search starts here:$
(^.+$)*
^End of search list\.$

但是它不能正常工作。 另一个问题是该参数的gcc不会终止。我必须在Linux上调用SIGINT Ctrl + C 。我正在使用Python 3和subprocess.run

2 个答案:

答案 0 :(得分:1)

仅使用外壳的解决方案:

gcc -x c -E -v /dev/null 2>&1 | sed -n '/include.*search starts here/,/End of search list/{s/^ //p}'

如果相关,请确保处理以下情况:

  • 该目录涉及以下符号链接
  • 目录重复
  • 该目录不存在
  • 可能安装了多个gcc二进制文件(不同版本,不同的arch等),每个都有不同的路径
  • 即使对于单个gcc二进制文件,编译器标志(例如-m32)也会影响包含路径

答案 1 :(得分:0)

我写了一些spagheti

def find_re_it_in_list(pattern, input, start=0, stop=-1, flags=0):
    length = len(input)

    if length == 0:
        return None

    end_it = max(0, length - 1)

    if start >= end_it:
        return None

    if stop<0:
        stop = length

    if stop <= start:
        return None

    for it in range(max(0, start), min(stop, length)):
        elem = input[it]
        match = re.match(pattern, elem, flags)
        if match:
            return it

def get_includes(self):
    args = [self.conf['gcc_path'], '-xc', '-E', '-v', os.devnull]
    args.extend(self.env.get('flags', []))

    incl_start_regex = r' *#include <\.\.\.> search starts here: *'
    incl_end_regex = r' *End of search list\. *'

    proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT , text=True)
    lines = proc.stdout.splitlines()

    start_it = find_re_it_in_list(incl_start_regex, lines)
    if start_it == None:
        return []

    end_it = find_re_it_in_list(incl_end_regex, lines, start_it)
    if end_it == None:
        return []

    # theres no paths between them
    if (end_it - start_it) == 1:
        return []

    return lines[start_it+1 : end_it]