[vlc-devel] [PATCH] contrib: Workaround for absolute paths in pc files

Marvin Scholz epirat07 at gmail.com
Fri Oct 4 14:23:36 CEST 2019


Meson generates pc files with absolute paths to static libraries, while
this is completely fine, libtool can not deal with this and produces
broken build results in that case.
---
 contrib/src/libplacebo/rules.mak    |   5 ++
 contrib/src/pkg-rewrite-absolute.py | 107 ++++++++++++++++++++++++++++
 2 files changed, 112 insertions(+)
 create mode 100755 contrib/src/pkg-rewrite-absolute.py

diff --git a/contrib/src/libplacebo/rules.mak b/contrib/src/libplacebo/rules.mak
index 9d4295e72c..ef3d0f19ef 100644
--- a/contrib/src/libplacebo/rules.mak
+++ b/contrib/src/libplacebo/rules.mak
@@ -36,4 +36,9 @@ libplacebo: $(PLACEBO_ARCHIVE) .sum-libplacebo
 	cd $< && cd build && ninja install
 # Work-around messon issue https://github.com/mesonbuild/meson/issues/4091
 	sed -i.orig -e 's/Libs: \(.*\) -L$${libdir} -lplacebo/Libs: -L$${libdir} -lplacebo \1/g' $(PREFIX)/lib/pkgconfig/libplacebo.pc
+# Work-around for full paths to static libraries, which libtool does not like
+# See https://github.com/mesonbuild/meson/issues/5479
+	(cd $(UNPACK_DIR) && $(SRC_BUILT)/pkg-rewrite-absolute.py \
+		"$(PREFIX)/lib/pkgconfig/libplacebo.pc" "$(PREFIX)/lib/pkgconfig/libplacebo.pc.tmp" && \
+		mv "$(PREFIX)/lib/pkgconfig/libplacebo.pc.tmp" "$(PREFIX)/lib/pkgconfig/libplacebo.pc")
 	touch $@
diff --git a/contrib/src/pkg-rewrite-absolute.py b/contrib/src/pkg-rewrite-absolute.py
new file mode 100755
index 0000000000..4eb78b92e8
--- /dev/null
+++ b/contrib/src/pkg-rewrite-absolute.py
@@ -0,0 +1,107 @@
+#!/usr/bin/env python3
+import os
+import argparse
+
+class PkgConfigFile():
+    """Representation of a pkg-config file (.pc)"""
+
+    pc_variables = {}
+    pc_variables_expanded = {}
+
+    pc_keywords = {}
+
+    def __init__(self, file):
+        for line in file:
+            self.parse_pc_line(line)
+
+    def parse_pc_line(self, line):
+        for i, c in enumerate(line):
+            if c is '=':
+                # This is a pkg-config variable line
+                key = line[:i].strip()
+                val = line[(i + 1):].strip()
+
+                # Add unexpanded version of variable
+                self.pc_variables.update({ key : val })
+
+                # Add expanded version of variable
+                self.pc_variables_expanded.update({ key : self.expand_pc_vars(val) })
+            elif c is ':':
+                # This is a pkg-config keyword line
+                key = line[:i].strip()
+                val = line[(i + 1):].strip()
+
+                self.pc_keywords.update({ key : val })
+
+    def expand_pc_vars(self, line):
+        for key, val in self.pc_variables_expanded.items():
+            line = line.replace('${' + key + '}', val)
+        return line
+
+    def get_variable(self, key, expand=True):
+        if expand:
+            return self.pc_variables_expanded.get(key, None)
+        else:
+            return self.pc_variables.get(key, None)
+
+    def get_keyword(self, key, expand=True):
+        keyword = self.pc_keywords.get(key, None)
+        if expand and keyword != None:
+            return self.expand_pc_vars(keyword)
+        else:
+            return keyword
+
+    def set_keyword(self, key, value):
+        self.pc_keywords.update({ key : value })
+
+    def write(self, file):
+        pc_contents = ''
+        # Print variables
+        for key, val in self.pc_variables.items():
+            pc_contents += key + '=' + val + '\n'
+        pc_contents += '\n'
+        # Print keywords
+        for key, val in self.pc_keywords.items():
+            pc_contents += key + ': ' + val + '\n'
+
+        file.write(pc_contents)
+
+# Remove all absolute paths to static libraries like /foo/bar/baz.a
+# and replace with -L/foo/bar -lbaz instead.
+def libs_replace_absolutes(args):
+    lib_paths = []
+    out_args = []
+    for arg in args:
+        if arg[:2] == '-L':
+            path = arg[2:]
+            path = pc_file.expand_pc_vars(path)
+            lib_paths.append(path)
+
+        # Filter all absolute static library paths
+        if arg[0] == '/' and arg[-2:] == '.a':
+            lib_path = os.path.dirname(arg)
+            lib_filename = os.path.basename(arg)
+            lib_name = lib_filename[3:-2] # Remove lib prefix and .a suffix
+            if lib_path not in lib_paths:
+                out_args.append('-L' + lib_path)
+                lib_paths.append(lib_path)
+            out_args.append('-l' + lib_name)
+        else:
+            out_args.append(arg)
+    return out_args
+
+# Argument parsing
+parser = argparse.ArgumentParser()
+parser.add_argument('infile', type=argparse.FileType('r'))
+parser.add_argument('outfile', type=argparse.FileType('w'))
+args = parser.parse_args()
+
+with args.infile:
+    pc_file = PkgConfigFile(args.infile)
+
+    linker_args_list = pc_file.get_keyword('Libs', expand=False).split()
+
+    linker_args_list = libs_replace_absolutes(linker_args_list)
+    linker_args = ' '.join(linker_args_list)
+    pc_file.set_keyword('Libs', linker_args)
+    pc_file.write(args.outfile)
-- 
2.20.1 (Apple Git-117)



More information about the vlc-devel mailing list