-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
369 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
"""Define the inline source object and its functions. | ||
""" | ||
|
||
visibility("//lang/...") | ||
|
||
_INLINE_SRC_TYPE = "INLINE_SRC" | ||
|
||
def is_inline_src(obj): | ||
"""Test if an object is an inline source object or not. | ||
Args: | ||
An object to test. | ||
Returns: | ||
True it's an inline source object, false otherwise. | ||
""" | ||
return type(obj) == "struct" and getattr(obj, "type", None) == _INLINE_SRC_TYPE | ||
|
||
def inline_src_c(content): | ||
"""Construct an inline source object for C. | ||
Args: | ||
content(str): Content of the source file. | ||
Returns: | ||
An inline source object for C. | ||
""" | ||
|
||
return struct( | ||
type = _INLINE_SRC_TYPE, | ||
content = content, | ||
extension = "c", | ||
) | ||
|
||
def inline_src_cpp(content): | ||
"""Construct an inline source object for C++. | ||
Args: | ||
content(str): Content of the source file. | ||
Returns: | ||
An inline source object for C++. | ||
""" | ||
|
||
return struct( | ||
type = _INLINE_SRC_TYPE, | ||
content = content, | ||
extension = "cpp", | ||
) | ||
|
||
def _generate_inline_src_rule_impl(ctx): | ||
"""Implementation of `_generate_inline_src_rule`. | ||
Args: | ||
ctx: Rule context. | ||
Returns: | ||
Provider for the rule. | ||
""" | ||
output = ctx.actions.declare_file(ctx.label.name + "." + ctx.attr.extension) | ||
ctx.actions.write( | ||
output = output, | ||
content = ctx.attr.content, | ||
) | ||
return [DefaultInfo(files = depset([output]))] | ||
|
||
_generate_inline_src_rule = rule( | ||
implementation = _generate_inline_src_rule_impl, | ||
attrs = { | ||
"content": attr.string( | ||
doc = ( | ||
"The content of the source file." | ||
), | ||
mandatory = True, | ||
), | ||
"extension": attr.string( | ||
doc = ( | ||
"The extension of the source file." | ||
), | ||
mandatory = True, | ||
), | ||
}, | ||
provides = [DefaultInfo], | ||
) | ||
|
||
def generate_inline_src(*, name, inline_src, **kwargs): | ||
"""Rule to generate inline source. | ||
Args: | ||
name(str): The name of the target. | ||
inline_src(inline source object): The inline source object to generate. | ||
**kwargs(dict): Passed to internal rules. | ||
""" | ||
if not is_inline_src(inline_src): | ||
fail("Precondition: `inline_src` must be an inline source object.") | ||
|
||
_generate_inline_src_rule( | ||
name = name, | ||
content = inline_src.content, | ||
extension = inline_src.extension, | ||
**kwargs | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"""Define `inline_src`. | ||
""" | ||
|
||
load( | ||
"//inline_src:inline_src.bzl", | ||
"inline_src_c", | ||
"inline_src_cpp", | ||
) | ||
|
||
visibility("private") | ||
|
||
inline_src = struct( | ||
c = inline_src_c, | ||
cpp = inline_src_cpp, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
load("@bazel_skylib//rules:build_test.bzl", "build_test") | ||
load("//lang/cc:defs.bzl", "inline_src") | ||
load("//matcher:defs.bzl", "matcher") | ||
load("//tests/cc:utils.bzl", "check_build_and_test") | ||
|
||
check_build_and_test( | ||
name = "plain", | ||
src = inline_src.c(""" | ||
#include <assert.h> | ||
int main(void) { | ||
#ifndef __cplusplus | ||
static_assert(0, "Compile error message for inline src"); | ||
#endif | ||
return 0; | ||
} | ||
"""), | ||
) | ||
|
||
check_build_and_test( | ||
name = "with_substr_matcher", | ||
src = inline_src.c(""" | ||
#include <assert.h> | ||
int main(void) { | ||
#ifndef __cplusplus | ||
static_assert(0, "Compile error message for inline src"); | ||
#endif | ||
return 0; | ||
} | ||
"""), | ||
compile_stderr = matcher.has_substr("static assertion failed"), | ||
) | ||
|
||
check_build_and_test( | ||
name = "with_basic_regex_matcher", | ||
src = inline_src.c(""" | ||
#include <assert.h> | ||
int main(void) { | ||
#ifndef __cplusplus | ||
static_assert(0, "Compile error message for inline src"); | ||
#endif | ||
return 0; | ||
} | ||
"""), | ||
compile_stderr = matcher.contains_basic_regex(r"static[[:space:]]assertion \(failed\|error\)"), | ||
) | ||
|
||
check_build_and_test( | ||
name = "with_extended_regex_matcher", | ||
src = inline_src.c(""" | ||
#include <assert.h> | ||
int main(void) { | ||
#ifndef __cplusplus | ||
static_assert(0, "Compile error message for inline src"); | ||
#endif | ||
return 0; | ||
} | ||
"""), | ||
compile_stderr = matcher.contains_extended_regex(r"static[[:space:]]assertion (failed|error)"), | ||
) | ||
|
||
build_test( | ||
name = "build_test", | ||
targets = [ | ||
":plain", | ||
":with_substr_matcher", | ||
":with_basic_regex_matcher", | ||
":with_extended_regex_matcher", | ||
], | ||
) | ||
|
||
# Test case which should fail to build | ||
check_build_and_test( | ||
name = "incorrect_extension", | ||
src = inline_src.cpp(""" | ||
#include <assert.h> | ||
int main(void) { | ||
#ifndef __cplusplus | ||
static_assert(0, "Compile error message for inline src"); | ||
#endif | ||
return 0; | ||
} | ||
"""), | ||
compile_stderr = matcher.has_substr("static assertion failed"), | ||
tags = ["manual"], | ||
) |
Oops, something went wrong.