diff --git a/pmb/aportgen/binutils.py b/pmb/aportgen/binutils.py index 875ec3f4..b1726007 100644 --- a/pmb/aportgen/binutils.py +++ b/pmb/aportgen/binutils.py @@ -73,4 +73,4 @@ def generate(args, pkgname): } pmb.aportgen.core.rewrite(args, pkgname, "main/binutils", fields, - "binutils", replace_functions) + "binutils", replace_functions, remove_indent=8) diff --git a/pmb/aportgen/core.py b/pmb/aportgen/core.py index 63e7a1ff..31169e6d 100644 --- a/pmb/aportgen/core.py +++ b/pmb/aportgen/core.py @@ -18,14 +18,28 @@ along with pmbootstrap. If not, see . """ import fnmatch import logging +import re import pmb.helpers.git +def indent_size(line): + """ + Number of spaces at the beginning of a string. + """ + matches = re.findall("^[ ]*", line) + if len(matches) == 1: + return len(matches[0]) + return 0 + + def format_function(name, body, remove_indent=4): """ Format the body of a shell function passed to rewrite() below, so it fits the format of the original APKBUILD. + :param remove_indent: Maximum number of spaces to remove from the + beginning of each line of the function body. """ + tab_width = 4 ret = "" lines = body.split("\n") for i in range(len(lines)): @@ -33,12 +47,23 @@ def format_function(name, body, remove_indent=4): if not line.strip(): if not ret or i == len(lines) - 1: continue - ret += line[remove_indent:] + "\n" + + # Remove indent + spaces = min(indent_size(line), remove_indent) + line = line[spaces:] + + # Convert spaces to tabs + spaces = indent_size(line) + tabs = int(spaces / tab_width) + line = ("\t" * tabs) + line[spaces:] + + ret += line + "\n" return name + "() {\n" + ret + "}\n" def rewrite(args, pkgname, path_original, fields={}, replace_pkgname=None, - replace_functions={}, replace_simple={}, below_header=""): + replace_functions={}, replace_simple={}, below_header="", + remove_indent=4): """ Append a header to $WORK/aportgen/APKBUILD, delete maintainer/contributor lines (so they won't be bugged with issues regarding our generated aports), @@ -54,6 +79,8 @@ def rewrite(args, pkgname, path_original, fields={}, replace_pkgname=None, :param replace_simple: Lines, that fnmatch the pattern, get replaced/deleted. Example: {"*test*": "# test", "*mv test.bin*": None} :param below_header: String, that gets directly placed below the header. + :param remove_indent: Number of spaces to remove from function body provided + to replace_functions. """ # Header @@ -88,7 +115,8 @@ def rewrite(args, pkgname, path_original, fields={}, replace_pkgname=None, if line.startswith(func + "() {"): skip_in_func = True if body: - lines_new += format_function(func, body) + lines_new += format_function(func, body, + remove_indent=remove_indent) break if skip_in_func: continue diff --git a/test/testdata/aportgen/pmaports/cross/binutils-armhf/APKBUILD b/test/testdata/aportgen/pmaports/cross/binutils-armhf/APKBUILD index f567a309..887e2dcc 100644 --- a/test/testdata/aportgen/pmaports/cross/binutils-armhf/APKBUILD +++ b/test/testdata/aportgen/pmaports/cross/binutils-armhf/APKBUILD @@ -35,30 +35,30 @@ fi # - CVE-2017-7614 build() { - _target="$(arch_to_hostspec armhf)" - cd "$builddir" - "$builddir"/configure \ - --build="$CBUILD" \ - --target=$_target \ - --with-lib-path=/usr/lib \ - --prefix=/usr \ - --with-sysroot=/usr/$_target \ - --enable-ld=default \ - --enable-gold=yes \ - --enable-plugins \ - --enable-deterministic-archives \ - --disable-multilib \ - --disable-werror \ - --disable-nls - make + _target="$(arch_to_hostspec armhf)" + cd "$builddir" + "$builddir"/configure \ + --build="$CBUILD" \ + --target=$_target \ + --with-lib-path=/usr/lib \ + --prefix=/usr \ + --with-sysroot=/usr/$_target \ + --enable-ld=default \ + --enable-gold=yes \ + --enable-plugins \ + --enable-deterministic-archives \ + --disable-multilib \ + --disable-werror \ + --disable-nls + make } package() { - cd "$builddir" - make install DESTDIR="$pkgdir" + cd "$builddir" + make install DESTDIR="$pkgdir" - # remove man, info folders - rm -rf "$pkgdir"/usr/share + # remove man, info folders + rm -rf "$pkgdir"/usr/share }