Skip to content

Commit

Permalink
feat: compiler static library for phe_kit
Browse files Browse the repository at this point in the history
  • Loading branch information
dterazhao committed Nov 11, 2024
1 parent 752bcb6 commit 3d3a9a3
Show file tree
Hide file tree
Showing 25 changed files with 829 additions and 65 deletions.
5 changes: 4 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ build --linkopt -fvisibility=hidden
build --linkopt -fvisibility-inlines-hidden
build --linkopt -lm
build --linkopt -ldl
build --action_env=PATH=/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin


# Binary safety flags
build --host_copt=-fPIE
build --host_copt=-fstack-protector-strong
build:linux --host_copt=-Wl,-z,noexecstack
build:macos --host_copt=-Wa,--noexecstack
build:macos --action_env=PATH=/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

# platform specific config
# Bazel will automatic pick platform config since we have enable_platform_specific_config set
Expand Down Expand Up @@ -70,6 +70,9 @@ build:ubsan --copt -g
build:ubsan --copt -fno-omit-frame-pointer
build:ubsan --linkopt -fsanitize=undefined

#build:monolithic --define framework_shared_object=false
build:exp --experimental_cc_static_library

test --keep_going
test --test_output=errors
test --test_timeout=600
Expand Down
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.5.0
6.5.0
21 changes: 15 additions & 6 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
###############################################################################
# Bazel now uses Bzlmod by default to manage external dependencies.
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
#
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
###############################################################################
"""Bazel build and test dependencies."""

# NOTE: When editing this file, also update the lockfile.
# bazel mod deps --lockfile_mode=update

module(
name = "algo",
repo_name = "algorithm",
)

# =========================================
# Bazel module dependencies
# =========================================

#bazel_dep(name = "rules_cc", version = "0.0.13")
12 changes: 6 additions & 6 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions bazel/algo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
"""
wrapper bazel cc_xx to modify flags.
"""
#load("@_builtins//:common/cc/experimental_cc_static_library.bzl", "cc_static_library")
load("//bazel:cc_static_library.bzl", "cc_static_library")
load("@yacl//bazel:yacl.bzl", "yacl_cc_library", "yacl_cc_binary", "yacl_cc_test",
"yacl_cmake_external", "yacl_configure_make")

load("@rules_cc//cc/private/toolchain:cc_configure.bzl", _cc_configure = "cc_configure")

INCLUDE_COPTS = ["-Inative/src/main/native/cn/cstn/algorithm/javacpp"] + ["-Ibazel/third_party/include"]

WARNING_FLAGS = [
"-Wall",
"-Wextra",
"-Werror",
"-Wno-unknown-pragmas",
]
# set `SPDLOG_ACTIVE_LEVEL=1(SPDLOG_LEVEL_DEBUG)` to enable debug level log
DEBUG_FLAGS = ["-DSPDLOG_ACTIVE_LEVEL=1", "-O0", "-g"]
Expand All @@ -38,12 +43,25 @@ def _algo_copts():
"//conditions:default": FAST_FLAGS,
}) + WARNING_FLAGS

def add_prefix_for_local_deps(deps=[]):
prefix_deps=[]
if type(deps) == "list":
for dep in deps:
if not dep.startswith(("@", "//", ":")):
dep = "//native/src/main/native/cn/cstn/algorithm/javacpp/" + dep
prefix_deps.append(dep)
return prefix_deps
else:
return deps

def algo_cc_binary(
copts = [],
deps = [],
linkopts = [],
**kargs):
yacl_cc_binary(
copts = _algo_copts() + copts,
deps = add_prefix_for_local_deps(deps),
linkopts = linkopts,
**kargs
)
Expand All @@ -54,7 +72,16 @@ def algo_cc_library(
**kargs):
yacl_cc_library(
copts = _algo_copts() + copts,
deps = deps,
deps = add_prefix_for_local_deps(deps),
**kargs
)


def algo_cc_static_library(
deps = [],
**kargs):
cc_static_library(
deps = add_prefix_for_local_deps(deps),
**kargs
)

Expand All @@ -65,7 +92,7 @@ def algo_cc_test(
**kwargs):
yacl_cc_test(
copts = _algo_copts() + copts,
deps = deps,
deps = add_prefix_for_local_deps(deps),
linkopts = linkopts,
**kwargs
)
Expand Down
11 changes: 10 additions & 1 deletion bazel/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@ echo "Current Directory: $CD"
#"$CD"/third_party/build.sh

cd ..
bazel build -c opt //...
OPTS="-c opt"
if [[ "$OSTYPE" == "darwin"* ]]; then
echo 6.5.0 > .bazelversion
else
echo 7.4.0 > .bazelversion
OPTS="$OPTS --config=exp"
fi
# --config=monolithic --spawn_strategy=standalone --genrule_strategy=standalone --output_filter=DONT_MATCH_ANYTHING
# shellcheck disable=SC2086
bazel build $OPTS //native/src/main/native/cn/cstn/algorithm/javacpp/...
112 changes: 112 additions & 0 deletions bazel/cc_static_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
`cc_static_library` creates a self-contained static library from the rule's
dependencies.
"""

load("@rules_cc//cc:action_names.bzl", "CPP_LINK_STATIC_LIBRARY_ACTION_NAME")
load("@rules_cc//cc:toolchain_utils.bzl", "find_cpp_toolchain")

def _cc_static_library_impl(ctx):
output_file_name = "lib%s.a" % ctx.label.name
output_file = ctx.actions.declare_file(output_file_name)

input_objects = []
for dep_target in ctx.attr.deps:
for dep in dep_target[CcInfo].linking_context.linker_inputs.to_list():
for library in dep.libraries:
input_objects += library.objects

cc_toolchain = find_cpp_toolchain(ctx)

feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)

library = cc_common.create_library_to_link(
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
static_library = output_file,
)
linker_input = cc_common.create_linker_input(
owner = ctx.label,
libraries = depset(direct = [library]),
)

compilation_context = cc_common.create_compilation_context()
linking_context = cc_common.create_linking_context(
linker_inputs = depset(direct = [linker_input]),
)

archiver_path = cc_common.get_tool_for_action(
feature_configuration = feature_configuration,
action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
)
archiver_variables = cc_common.create_link_variables(
cc_toolchain = cc_toolchain,
feature_configuration = feature_configuration,
output_file = output_file.path,
is_using_linker = False,
)
command_line = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
variables = archiver_variables,
)

arguments = ctx.actions.args()
arguments.add_all(command_line)
arguments.add_all([obj.path for obj in input_objects])

env = cc_common.get_environment_variables(
feature_configuration = feature_configuration,
action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
variables = archiver_variables,
)

ctx.actions.run(
outputs = [output_file],
inputs = depset(
direct = input_objects,
transitive = [cc_toolchain.all_files],
),
executable = archiver_path,
arguments = [arguments],
mnemonic = "StaticArchive",
progress_message = "Creating static archive %s" % output_file_name,
env = env,
)

default_info = DefaultInfo(
files = depset([output_file]),
runfiles = ctx.runfiles(files = [output_file]),
)
this_cc_info = CcInfo(
compilation_context = compilation_context,
linking_context = linking_context,
)
deps_cc_infos = [dep[CcInfo] for dep in ctx.attr.deps]
cc_info = cc_common.merge_cc_infos(
cc_infos = [this_cc_info] + deps_cc_infos,
)
return [default_info, cc_info]

cc_static_library = rule(
implementation = _cc_static_library_impl,
attrs = {
"deps": attr.label_list(providers = [CcInfo]),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
},
fragments = ["cpp"],
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
incompatible_use_toolchain_transition = True,
doc = "Create a self-contained static library from the dependencies.",
)
13 changes: 13 additions & 0 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,31 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

def algo_deps():
#_bazel()
_rules_cc()
_heu()
_spu()
_gmp()

def _bazel():
maybe(
http_archive,
name = "bazel",
sha256 = "2b75cc9aa0a23cf594b96c4ad1cbf5eae9360dba98949b1b42e5d37360333149",
strip_prefix = "bazel-7.4.0",
url = "https://github.com/bazelbuild/bazel/archive/refs/tags/7.4.0.tar.gz",
)

def _rules_cc():
maybe(
http_archive,
name = "rules_cc",
sha256 = "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf",
strip_prefix = "rules_cc-0.0.9",
url = "https://github.com/bazelbuild/rules_cc/releases/download/0.0.9/rules_cc-0.0.9.tar.gz",
#sha256 = "906e89286acc67c20819c3c88b3283de0d5868afda33635d70acae0de9777bb7",
#strip_prefix = "rules_cc-0.0.14",
#url = "https://github.com/bazelbuild/rules_cc/releases/download/0.0.14/rules_cc-0.0.14.tar.gz",
)

def _heu():
Expand Down
4 changes: 2 additions & 2 deletions bazel/third_party/gmp.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ filegroup(
configure_make(
name = "gmp",
args = ["-j 4"],
configure_command = "Configure",
configure_command = "configure",
configure_in_place = True,
configure_options = [ "--enable-fat", "--enable-cxx", "CFLAGS=-Dredacted" ],
configure_options = [ "--enable-fat" ],
env = select({
"@platforms//os:macos": {
"AR": "",
Expand Down
Loading

0 comments on commit 3d3a9a3

Please sign in to comment.