Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions configure_file.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

CMAKE_FALSE_CONSTANTS = ["0", "OFF", "NO", "FALSE", "N", "IGNORE", "NOTFOUND"]

def _configure_file_impl(ctx):
vars = {}
for (key, val) in ctx.attr.vars.items():
for var in ctx.attr.vars:
key_val = var.split("=")
if len(key_val) != 2:
# skip if var is not in the right format: <key>=<value>
continue
key = key_val[0]
val = key_val[1]

cmake_define = "#cmakedefine {}".format(key)
define = "// #undef {}".format(key) if val in CMAKE_FALSE_CONSTANTS else "#define {}".format(key)

Expand All @@ -21,11 +30,31 @@ def _configure_file_impl(ctx):
configure_file = rule(
implementation = _configure_file_impl,
attrs = {
"vars": attr.string_dict(),
"vars": attr.string_list(),
"out": attr.string(),
"template": attr.label(
allow_single_file = [".in"],
mandatory = True,
),
},
)

def config_var(var, config):
return select({
config: [var + "=ON"],
"//conditions:default": [var + "=OFF"],
})

def var(var, value):
return [var + "=" + value]

def config_flag(config, flag):
string_flag(
name = flag,
build_setting_default = "",
)

native.config_setting(
name = config,
flag_values = {":" + flag: "ON"},
)