summaryrefslogtreecommitdiff
path: root/CC/prebuilt
diff options
context:
space:
mode:
Diffstat (limited to 'CC/prebuilt')
-rw-r--r--CC/prebuilt/EXPRESSIONS503
-rw-r--r--CC/prebuilt/RULES147
-rwxr-xr-xCC/prebuilt/read_pkgconfig.py77
3 files changed, 727 insertions, 0 deletions
diff --git a/CC/prebuilt/EXPRESSIONS b/CC/prebuilt/EXPRESSIONS
new file mode 100644
index 0000000..b84e494
--- /dev/null
+++ b/CC/prebuilt/EXPRESSIONS
@@ -0,0 +1,503 @@
+{ "check-file-ending":
+ { "doc": ["Returns true if all file names end with the given ending."]
+ , "vars": ["files", "ending", "invert"]
+ , "vars_doc":
+ { "files": ["Artifact map that contains the files."]
+ , "ending": ["The ending to check for (without \".\")."]
+ , "invert": ["Invert condition to file name not ending with \"ending\"."]
+ }
+ , "expression":
+ { "type": "and"
+ , "$1":
+ { "type": "foreach"
+ , "var": "file"
+ , "range": {"type": "keys", "$1": {"type": "var", "name": "files"}}
+ , "body":
+ { "type": "let*"
+ , "bindings":
+ [ [ "is-equal"
+ , { "type": "=="
+ , "$1": {"type": "var", "name": "file"}
+ , "$2":
+ { "type": "change_ending"
+ , "$1": {"type": "var", "name": "file"}
+ , "ending":
+ { "type": "join"
+ , "$1": [".", {"type": "var", "name": "ending"}]
+ }
+ }
+ }
+ ]
+ ]
+ , "body":
+ { "type": "if"
+ , "cond": {"type": "var", "name": "invert", "default": false}
+ , "then":
+ { "type": "if"
+ , "cond": {"type": "var", "name": "is-equal"}
+ , "then": false
+ , "else": true
+ }
+ , "else": {"type": "var", "name": "is-equal"}
+ }
+ }
+ }
+ }
+ }
+, "check-libs-non-static":
+ { "doc":
+ [ "Returns true if all libs are non-static libraries or false otherwise."
+ , "Throws if static and non-static libraries are mixed."
+ ]
+ , "vars": ["libs"]
+ , "vars_doc": {"libs": ["List of artifact maps that contain the libraries."]}
+ , "imports": {"check-file-ending": "check-file-ending"}
+ , "expression":
+ { "type": "let*"
+ , "bindings":
+ [ ["files", {"type": "map_union", "$1": {"type": "var", "name": "libs"}}]
+ , [ "static"
+ , { "type": "let*"
+ , "bindings": [["ending", "a"]]
+ , "body": {"type": "CALL_EXPRESSION", "name": "check-file-ending"}
+ }
+ ]
+ , [ "shared"
+ , { "type": "let*"
+ , "bindings": [["ending", "a"], ["invert", true]]
+ , "body": {"type": "CALL_EXPRESSION", "name": "check-file-ending"}
+ }
+ ]
+ ]
+ , "body":
+ { "type": "if"
+ , "cond":
+ { "type": "or"
+ , "$1":
+ [ {"type": "var", "name": "static"}
+ , {"type": "var", "name": "shared"}
+ ]
+ }
+ , "then": {"type": "var", "name": "shared"}
+ , "else":
+ { "type": "fail"
+ , "msg": "Prebuilt library types (static/shared) may not be mixed."
+ }
+ }
+ }
+ }
+, "prebuilt result":
+ { "vars":
+ [ "ENV"
+ , "name"
+ , "version"
+ , "stage"
+ , "cflags"
+ , "ldflags"
+ , "libs"
+ , "hdrs"
+ , "config_reader"
+ , "pkg-configs"
+ , "deps-fieldnames"
+ ]
+ , "imports":
+ { "check-libs-non-static": "check-libs-non-static"
+ , "compile-deps": ["./", "..", "compile-deps"]
+ , "compile-args-deps": ["./", "..", "compile-args-deps"]
+ , "link-deps": ["./", "..", "link-deps"]
+ , "link-args-deps": ["./", "..", "link-args-deps"]
+ , "run-libs-deps": ["./", "..", "run-libs-deps"]
+ , "run-libs-args-deps": ["./", "..", "run-libs-args-deps"]
+ , "cflags-files-deps": ["./", "..", "cflags-files-deps"]
+ , "ldflags-files-deps": ["./", "..", "ldflags-files-deps"]
+ , "default-ENV": ["./", "..", "default-ENV"]
+ , "default-PATH": ["./", "..", "default-PATH"]
+ }
+ , "expression":
+ { "type": "let*"
+ , "bindings":
+ [ [ "shared"
+ , {"type": "CALL_EXPRESSION", "name": "check-libs-non-static"}
+ ]
+ , [ "lib-stage"
+ , { "type": "if"
+ , "cond": {"type": "var", "name": "shared"}
+ , "then": "."
+ , "else": {"type": "var", "name": "stage"}
+ }
+ ]
+ , [ "staged-libs"
+ , { "type": "foreach"
+ , "var": "lib"
+ , "range": {"type": "var", "name": "libs"}
+ , "body":
+ { "type": "to_subdir"
+ , "subdir": {"type": "var", "name": "lib-stage"}
+ , "flat": true
+ , "msg": "prebuilt libraries may not overlap"
+ , "$1": {"type": "var", "name": "lib"}
+ }
+ }
+ ]
+ , [ "link-args"
+ , { "type": "if"
+ , "cond": {"type": "var", "name": "shared"}
+ , "then": []
+ , "else":
+ { "type": "++"
+ , "$1":
+ { "type": "foreach"
+ , "var": "libs"
+ , "range": {"type": "var", "name": "staged-libs"}
+ , "body": {"type": "keys", "$1": {"type": "var", "name": "libs"}}
+ }
+ }
+ }
+ ]
+ , [ "run-libs-args"
+ , { "type": "if"
+ , "cond": {"type": "var", "name": "shared"}
+ , "then":
+ { "type": "++"
+ , "$1":
+ { "type": "foreach"
+ , "var": "libs"
+ , "range": {"type": "var", "name": "staged-libs"}
+ , "body": {"type": "keys", "$1": {"type": "var", "name": "libs"}}
+ }
+ }
+ }
+ ]
+ , [ "libs"
+ , { "type": "disjoint_map_union"
+ , "msg": "prebuilt libraries may not overlap"
+ , "$1": {"type": "var", "name": "staged-libs"}
+ }
+ ]
+ , [ "hdrs"
+ , { "type": "to_subdir"
+ , "subdir": {"type": "var", "name": "stage"}
+ , "$1":
+ { "type": "disjoint_map_union"
+ , "msg": "prebuilt headers may not overlap"
+ , "$1": {"type": "var", "name": "hdrs"}
+ }
+ }
+ ]
+ , ["compile-deps", {"type": "CALL_EXPRESSION", "name": "compile-deps"}]
+ , [ "compile-args"
+ , { "type": "nub_right"
+ , "$1":
+ { "type": "++"
+ , "$1":
+ [ {"type": "var", "name": "cflags"}
+ , {"type": "CALL_EXPRESSION", "name": "compile-args-deps"}
+ ]
+ }
+ }
+ ]
+ , ["link-deps", {"type": "CALL_EXPRESSION", "name": "link-deps"}]
+ , [ "link-args"
+ , { "type": "nub_right"
+ , "$1":
+ { "type": "++"
+ , "$1":
+ [ {"type": "var", "name": "link-args"}
+ , {"type": "CALL_EXPRESSION", "name": "link-args-deps"}
+ ]
+ }
+ }
+ ]
+ , ["run-libs", {"type": "CALL_EXPRESSION", "name": "run-libs-deps"}]
+ , [ "run-libs-args"
+ , { "type": "nub_right"
+ , "$1":
+ { "type": "++"
+ , "$1":
+ [ {"type": "var", "name": "run-libs-args"}
+ , {"type": "var", "name": "ldflags"}
+ , {"type": "CALL_EXPRESSION", "name": "run-libs-args-deps"}
+ ]
+ }
+ }
+ ]
+ , [ "ENV"
+ , { "type": "map_union"
+ , "$1":
+ [ {"type": "CALL_EXPRESSION", "name": "default-ENV"}
+ , {"type": "var", "name": "ENV", "default": {"type": "empty_map"}}
+ ]
+ }
+ ]
+ , [ "ENV_PATH"
+ , { "type": "lookup"
+ , "map": {"type": "var", "name": "ENV"}
+ , "key": "PATH"
+ }
+ ]
+ , [ "ENV"
+ , { "type": "map_union"
+ , "$1":
+ [ {"type": "var", "name": "ENV"}
+ , { "type": "singleton_map"
+ , "key": "PATH"
+ , "value":
+ { "type": "join"
+ , "separator": ":"
+ , "$1":
+ { "type": "++"
+ , "$1":
+ [ {"type": "CALL_EXPRESSION", "name": "default-PATH"}
+ , { "type": "if"
+ , "cond": {"type": "var", "name": "ENV_PATH"}
+ , "then": [{"type": "var", "name": "ENV_PATH"}]
+ }
+ ]
+ }
+ }
+ }
+ ]
+ }
+ ]
+ , [ "main-pkg-config"
+ , { "type": "foldl"
+ , "range":
+ { "type": "++"
+ , "$1":
+ { "type": "foreach"
+ , "var": "configs"
+ , "range": {"type": "var", "name": "pkg-configs", "default": []}
+ , "body":
+ {"type": "keys", "$1": {"type": "var", "name": "configs"}}
+ }
+ }
+ , "start": null
+ , "accum_var": "name"
+ , "var": "config"
+ , "body":
+ { "type": "if"
+ , "cond": {"type": "var", "name": "name"}
+ , "then": {"type": "var", "name": "name"}
+ , "else": {"type": "var", "name": "config"}
+ }
+ }
+ ]
+ , [ "reader-inputs"
+ , { "type": "to_subdir"
+ , "subdir": "lib"
+ , "$1":
+ { "type": "map_union"
+ , "$1":
+ [ {"type": "var", "name": "libs"}
+ , {"type": "var", "name": "link-deps"}
+ , {"type": "var", "name": "run-libs"}
+ , { "type": "to_subdir"
+ , "subdir": "pkgconfig"
+ , "flat": true
+ , "$1":
+ { "type": "map_union"
+ , "$1": {"type": "var", "name": "pkg-configs", "default": []}
+ }
+ }
+ ]
+ }
+ }
+ ]
+ , [ "pc-args"
+ , { "type": "if"
+ , "cond": {"type": "var", "name": "shared"}
+ , "then": []
+ , "else": ["--static"]
+ }
+ ]
+ , [ "cflags-filename"
+ , {"type": "join", "$1": [{"type": "var", "name": "name"}, ".cflags"]}
+ ]
+ , [ "cflags-file"
+ , { "type": "to_subdir"
+ , "subdir": {"type": "var", "name": "stage"}
+ , "$1":
+ { "type": "if"
+ , "cond": {"type": "var", "name": "pkg-configs"}
+ , "then":
+ { "type": "ACTION"
+ , "inputs":
+ { "type": "map_union"
+ , "$1":
+ [ {"type": "var", "name": "config_reader"}
+ , {"type": "var", "name": "reader-inputs"}
+ ]
+ }
+ , "cmd":
+ { "type": "++"
+ , "$1":
+ [ [ "./config_reader"
+ , {"type": "var", "name": "cflags-filename"}
+ , {"type": "var", "name": "main-pkg-config"}
+ ]
+ , {"type": "var", "name": "pc-args"}
+ ]
+ }
+ , "env": {"type": "var", "name": "ENV"}
+ , "outs": [{"type": "var", "name": "cflags-filename"}]
+ }
+ , "else": {"type": "empty_map"}
+ }
+ }
+ ]
+ , [ "cflags-files"
+ , { "type": "map_union"
+ , "$1":
+ [ {"type": "CALL_EXPRESSION", "name": "cflags-files-deps"}
+ , {"type": "var", "name": "cflags-file"}
+ ]
+ }
+ ]
+ , [ "compile-args"
+ , { "type": "++"
+ , "$1":
+ [ {"type": "var", "name": "compile-args"}
+ , { "type": "if"
+ , "cond": {"type": "var", "name": "cflags-file"}
+ , "then":
+ [ { "type": "join"
+ , "$1":
+ { "type": "++"
+ , "$1":
+ [ ["@"]
+ , { "type": "keys"
+ , "$1": {"type": "var", "name": "cflags-file"}
+ }
+ ]
+ }
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ , [ "ldflags-filename"
+ , {"type": "join", "$1": [{"type": "var", "name": "name"}, ".ldflags"]}
+ ]
+ , [ "ldflags-file"
+ , { "type": "to_subdir"
+ , "subdir": {"type": "var", "name": "stage"}
+ , "$1":
+ { "type": "if"
+ , "cond": {"type": "var", "name": "pkg-configs"}
+ , "then":
+ { "type": "ACTION"
+ , "inputs":
+ { "type": "map_union"
+ , "$1":
+ [ {"type": "var", "name": "config_reader"}
+ , {"type": "var", "name": "reader-inputs"}
+ ]
+ }
+ , "cmd":
+ { "type": "++"
+ , "$1":
+ [ [ "./config_reader"
+ , {"type": "var", "name": "ldflags-filename"}
+ , {"type": "var", "name": "main-pkg-config"}
+ ]
+ , {"type": "var", "name": "pc-args"}
+ ]
+ }
+ , "env": {"type": "var", "name": "ENV"}
+ , "outs": [{"type": "var", "name": "ldflags-filename"}]
+ }
+ , "else": {"type": "empty_map"}
+ }
+ }
+ ]
+ , [ "ldflags-files"
+ , { "type": "map_union"
+ , "$1":
+ [ {"type": "CALL_EXPRESSION", "name": "ldflags-files-deps"}
+ , {"type": "var", "name": "ldflags-file"}
+ ]
+ }
+ ]
+ , [ "run-libs"
+ , { "type": "map_union"
+ , "$1":
+ [ { "type": "if"
+ , "cond": {"type": "var", "name": "shared"}
+ , "then": {"type": "var", "name": "libs"}
+ , "else": {"type": "empty_map"}
+ }
+ , {"type": "var", "name": "run-libs"}
+ ]
+ }
+ ]
+ , [ "run-libs-args"
+ , { "type": "++"
+ , "$1":
+ [ {"type": "var", "name": "run-libs-args"}
+ , { "type": "if"
+ , "cond": {"type": "var", "name": "ldflags-file"}
+ , "then":
+ [ { "type": "join"
+ , "$1":
+ { "type": "++"
+ , "$1":
+ [ ["@"]
+ , { "type": "keys"
+ , "$1": {"type": "var", "name": "ldflags-file"}
+ }
+ ]
+ }
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ , [ "package"
+ , { "type": "let*"
+ , "bindings":
+ [ ["name", {"type": "var", "name": "name"}]
+ , [ "version"
+ , { "type": "if"
+ , "cond": {"type": "var", "name": "version"}
+ , "then":
+ { "type": "join"
+ , "separator": "."
+ , "$1": {"type": "var", "name": "version"}
+ }
+ , "else": null
+ }
+ ]
+ ]
+ , "body":
+ { "type": "env"
+ , "vars": ["name", "version", "cflags-files", "ldflags-files"]
+ }
+ }
+ ]
+ ]
+ , "body":
+ { "type": "RESULT"
+ , "artifacts": {"type": "var", "name": "libs"}
+ , "runfiles": {"type": "var", "name": "hdrs"}
+ , "provides":
+ { "type": "map_union"
+ , "$1":
+ [ { "type": "env"
+ , "vars":
+ [ "compile-deps"
+ , "compile-args"
+ , "link-deps"
+ , "link-args"
+ , "run-libs"
+ , "run-libs-args"
+ , "package"
+ ]
+ }
+ ]
+ }
+ }
+ }
+ }
+}
diff --git a/CC/prebuilt/RULES b/CC/prebuilt/RULES
new file mode 100644
index 0000000..16f9f26
--- /dev/null
+++ b/CC/prebuilt/RULES
@@ -0,0 +1,147 @@
+{ "library":
+ { "doc": ["A prebuilt C++ library"]
+ , "target_fields": ["lib", "hdrs", "deps", "pkg-config"]
+ , "string_fields":
+ ["name", "version", "stage", "defines", "cflags", "ldflags"]
+ , "implicit":
+ { "defaults": [["./", "..", "defaults"]]
+ , "config_reader": [["CC", "prebuilt/read_pkgconfig.py"]]
+ }
+ , "config_vars": ["ENV"]
+ , "field_doc":
+ { "name":
+ [ "The name of the library (without leading \"lib\" or trailing file name"
+ , "extension), also used as name for pkg-config files."
+ ]
+ , "version":
+ [ "The library version, used for pkg-config files. Individual version"
+ , "components are joined with \".\"."
+ ]
+ , "lib":
+ [ "The actual prebuilt library. If multiple libraries are specified"
+ , "(e.g., one depends on the other), the order matters. Library types"
+ , "cannot be mixed. All of them have to be either static or shared."
+ ]
+ , "hdrs": ["Any public header files of the library."]
+ , "deps": ["Any other libraries this library depends upon."]
+ , "stage":
+ [ "The logical location of all header and source files, as well as the"
+ , "resulting library file. Individual directory components are joined"
+ , "with \"/\"."
+ ]
+ , "defines":
+ [ "List of defines set for this target and its consumers."
+ , "Each list entry will be prepended by \"-D\"."
+ ]
+ , "cflags":
+ ["List of compile flags set for this target and its consumers."]
+ , "ldflags":
+ ["Additional linker flags (typically for linking system libraries)."]
+ , "pkg-config":
+ [ "Pkg-config file for optional infer of public cflags and ldflags. If"
+ , "multiple files are specified (e.g., one depends on the other), the"
+ , "first one is used as entry. Note that if this field is non-empty the"
+ , "tool \"pkg-config\" must be available in \"PATH\", which is taken"
+ , "from [\"CC\", \"defaults\"] or the \"ENV\" variable."
+ ]
+ }
+ , "artifacts_doc":
+ [ "The actual library (libname.a or libname.so). Only static libraries are"
+ , "staged in the specified directory."
+ ]
+ , "runfiles_doc": ["The public headers staged to the specified directory."]
+ , "provides_doc":
+ { "compile-deps":
+ [ "Map of artifacts specifying any additional files that, besides the"
+ , "runfiles have to be present in compile actions of targets depending on"
+ , "this library."
+ ]
+ , "link-deps":
+ [ "Map of artifacts specifying any additional files that, besides the artifacts,"
+ , "have to be present in a link actions of targets depending on this library"
+ ]
+ , "run-libs":
+ [ "Map of artifacts specifying all files, including the artifacts of this"
+ , "library if shared, that have to be present in link actions of targets"
+ , "depending on this library."
+ ]
+ , "link-args":
+ [ "List of strings that have to be added to the command line for linking"
+ , "actions in targets depending on this library."
+ ]
+ , "run-libs-args":
+ [ "List of strings that have to be added to the command line for linking"
+ , "runtime libraries in targets depending on this library."
+ ]
+ , "compile-args":
+ [ "List of strings that have to be added to the command line for compile"
+ , "actions in targets depending on this library."
+ ]
+ }
+ , "imports":
+ { "artifacts": ["./", "../..", "field_artifacts"]
+ , "artifacts_list": ["./", "../..", "field_artifacts_list"]
+ , "stage_field": ["", "stage_singleton_field"]
+ , "prebuilt result": "prebuilt result"
+ }
+ , "expression":
+ { "type": "let*"
+ , "bindings":
+ [ [ "name"
+ , { "type": "assert_non_empty"
+ , "msg": "name is required for prebuilt library"
+ , "$1": {"type": "join", "$1": {"type": "FIELD", "name": "name"}}
+ }
+ ]
+ , ["version", {"type": "FIELD", "name": "version"}]
+ , [ "stage"
+ , { "type": "join"
+ , "separator": "/"
+ , "$1": {"type": "FIELD", "name": "stage"}
+ }
+ ]
+ , [ "cflags"
+ , { "type": "++"
+ , "$1":
+ [ {"type": "FIELD", "name": "cflags"}
+ , { "type": "foreach"
+ , "var": "def"
+ , "range": {"type": "FIELD", "name": "defines"}
+ , "body":
+ {"type": "join", "$1": ["-D", {"type": "var", "name": "def"}]}
+ }
+ ]
+ }
+ ]
+ , ["ldflags", {"type": "FIELD", "name": "ldflags"}]
+ , [ "libs"
+ , { "type": "let*"
+ , "bindings": [["fieldname", "lib"]]
+ , "body": {"type": "CALL_EXPRESSION", "name": "artifacts_list"}
+ }
+ ]
+ , [ "hdrs"
+ , { "type": "let*"
+ , "bindings": [["fieldname", "hdrs"]]
+ , "body": {"type": "CALL_EXPRESSION", "name": "artifacts_list"}
+ }
+ ]
+ , [ "config_reader"
+ , { "type": "let*"
+ , "bindings":
+ [["fieldname", "config_reader"], ["location", "config_reader"]]
+ , "body": {"type": "CALL_EXPRESSION", "name": "stage_field"}
+ }
+ ]
+ , [ "pkg-configs"
+ , { "type": "let*"
+ , "bindings": [["fieldname", "pkg-config"]]
+ , "body": {"type": "CALL_EXPRESSION", "name": "artifacts_list"}
+ }
+ ]
+ , ["deps-fieldnames", ["deps"]]
+ ]
+ , "body": {"type": "CALL_EXPRESSION", "name": "prebuilt result"}
+ }
+ }
+}
diff --git a/CC/prebuilt/read_pkgconfig.py b/CC/prebuilt/read_pkgconfig.py
new file mode 100755
index 0000000..115c305
--- /dev/null
+++ b/CC/prebuilt/read_pkgconfig.py
@@ -0,0 +1,77 @@
+#!/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
+from typing import Dict, List, Set
+
+
+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, args: List[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(args + ["--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() -> None:
+ if len(sys.argv) < 3:
+ print(f"usage: read_pkgconfig OUT_NAME PC_FILE [PC_ARGS...]")
+ exit(1)
+
+ name = sys.argv[1]
+ pkg = Path(sys.argv[2]).stem
+ args = sys.argv[3:]
+ env = dict(os.environ, PKG_CONFIG_PATH="./lib/pkgconfig")
+
+ if name.endswith(".cflags"):
+ data = run_pkgconfig(args + ["--cflags-only-other", pkg], env)
+ else:
+ data = read_ldflags(pkg, args, env)
+
+ with open(f"{name}", 'w') as f:
+ f.write(data)
+
+
+if __name__ == "__main__":
+ read_pkgconfig()