summaryrefslogtreecommitdiff
path: root/CC/prebuilt/read_pkgconfig.py
diff options
context:
space:
mode:
authorOliver Reiche <oliver.reiche@huawei.com>2023-01-12 19:23:39 +0100
committerOliver Reiche <oliver.reiche@huawei.com>2023-01-20 16:02:11 +0100
commite75f101e8b988adb458e594ffaaaebc0c01f65df (patch)
tree1f55772fe9040ab9f03d5b6c9d29b8eacd19c5b6 /CC/prebuilt/read_pkgconfig.py
parent4a8579a2a4ef252644df0c29893e70ad8438ae82 (diff)
downloadrules-cc-e75f101e8b988adb458e594ffaaaebc0c01f65df.tar.gz
Support generating flag-files for prebuilt libraries
Diffstat (limited to 'CC/prebuilt/read_pkgconfig.py')
-rwxr-xr-xCC/prebuilt/read_pkgconfig.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/CC/prebuilt/read_pkgconfig.py b/CC/prebuilt/read_pkgconfig.py
new file mode 100755
index 0000000..cb4154f
--- /dev/null
+++ b/CC/prebuilt/read_pkgconfig.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python3
+# Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import os
+import subprocess
+import sys
+from pathlib import Path
+
+
+def run_pkgconfig(args: list[str], env: dict[str, str]) -> str:
+ result = subprocess.run(["pkg-config"] + args, env=env, capture_output=True)
+ if result.returncode != 0:
+ print(result.stderr.decode("utf-8"), file=sys.stderr)
+ exit(1)
+ return result.stdout.decode("utf-8").strip()
+
+
+def read_ldflags(pkg: str, env: dict[str, str]) -> str:
+ def libname(filename: str) -> str:
+ return filename.split(".")[0]
+
+ local_libs = {
+ libname(f)
+ for it in os.walk(".") for f in it[2] if f.startswith("lib")
+ }
+
+ link_flags = run_pkgconfig(["--libs-only-l", pkg], env).split(" ")
+
+ # deduplicate, keep right-most
+ seen: set[str] = set()
+ link_flags = [
+ f for f in link_flags[::-1] if f not in seen and not seen.add(f)
+ ][::-1]
+
+ def is_local(flag: str) -> bool:
+ if not flag.startswith("-l"):
+ return False
+ lib = libname(flag[3:]) if flag.startswith("-l:") else f"lib{flag[2:]}"
+ return lib in local_libs
+
+ return " ".join([f for f in link_flags if not is_local(f)])
+
+
+def read_pkgconfig():
+ if len(sys.argv) < 3:
+ print(f"usage: read_pkgconfig OUT_NAME PC_FILE")
+ exit(1)
+
+ name = sys.argv[1]
+ pkg = Path(sys.argv[2]).stem
+ env = dict(os.environ, PKG_CONFIG_PATH="./lib/pkgconfig")
+
+ if name.endswith(".cflags"):
+ data = run_pkgconfig(["--cflags-only-other", pkg], env)
+ else:
+ data = read_ldflags(pkg, env)
+
+ with open(f"{name}", 'w') as f:
+ f.write(data)
+
+
+if __name__ == "__main__":
+ read_pkgconfig()