-
Notifications
You must be signed in to change notification settings - Fork 24
Generate C/C++ headers in compile using --include (like xxd)
#596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| #!/bin/sh | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
|
|
||
| TMP="$(mktemp -d)" | ||
| clean() { rm -rf "$TMP"; } | ||
| trap clean EXIT | ||
|
|
||
| cat << 'EOF' > "$TMP/schema.json" | ||
| { | ||
| "$id": "https://example.com", | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "type": "string" | ||
| } | ||
| EOF | ||
|
|
||
| # Test with hyphen (invalid) | ||
| "$1" compile "$TMP/schema.json" --include my-schema 2>"$TMP/stderr.txt" \ | ||
| && CODE="$?" || CODE="$?" | ||
| test "$CODE" = "1" || exit 1 | ||
|
|
||
| cat << 'EOF' > "$TMP/expected.txt" | ||
| error: The include identifier is not a valid C/C++ identifier | ||
| at identifier my-schema | ||
| EOF | ||
|
|
||
| diff "$TMP/stderr.txt" "$TMP/expected.txt" | ||
|
|
||
| # JSON error | ||
| "$1" compile "$TMP/schema.json" --include my-schema --json > "$TMP/stdout.txt" 2>&1 \ | ||
| && CODE="$?" || CODE="$?" | ||
| test "$CODE" = "1" || exit 1 | ||
|
|
||
| cat << 'EOF' > "$TMP/expected.txt" | ||
| { | ||
| "error": "The include identifier is not a valid C/C++ identifier", | ||
| "identifier": "my-schema" | ||
| } | ||
| EOF | ||
|
|
||
| diff "$TMP/stdout.txt" "$TMP/expected.txt" |
This file contains hidden or 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,55 @@ | ||
| #!/bin/sh | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
|
|
||
| TMP="$(mktemp -d)" | ||
| clean() { rm -rf "$TMP"; } | ||
| trap clean EXIT | ||
|
|
||
| cat << 'EOF' > "$TMP/schema.json" | ||
| { | ||
| "$id": "https://example.com", | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "type": "string" | ||
| } | ||
| EOF | ||
|
|
||
| "$1" compile "$TMP/schema.json" --include TEST_SCHEMA > "$TMP/output.h" | ||
|
|
||
| cat << 'EOF' > "$TMP/expected.h" | ||
| #ifndef SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_ | ||
| #define SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_ | ||
|
|
||
| #ifdef __cplusplus | ||
| #include <string_view> | ||
| #endif | ||
|
|
||
| static const char TEST_SCHEMA_DATA[] = | ||
| "\x5b\x66\x61\x6c\x73\x65\x2c\x74\x72\x75\x65\x2c\x5b\x22\x22\x2c" | ||
| "\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x65\x78\x61\x6d\x70\x6c\x65" | ||
| "\x2e\x63\x6f\x6d\x22\x5d\x2c\x5b\x5b\x31\x31\x2c\x22\x2f\x74\x79" | ||
| "\x70\x65\x22\x2c\x22\x22\x2c\x22\x23\x2f\x74\x79\x70\x65\x22\x2c" | ||
| "\x32\x2c\x5b\x38\x2c\x34\x5d\x5d\x5d\x5d"; | ||
| static const unsigned int TEST_SCHEMA_LENGTH = 74; | ||
|
|
||
| #ifdef __cplusplus | ||
| static constexpr std::string_view TEST_SCHEMA{TEST_SCHEMA_DATA, TEST_SCHEMA_LENGTH}; | ||
| #endif | ||
|
|
||
| #endif | ||
| EOF | ||
|
|
||
| diff "$TMP/output.h" "$TMP/expected.h" | ||
|
|
||
| # Verify the header compiles with a C compiler | ||
| cat << 'EOF' > "$TMP/test.c" | ||
| #include "output.h" | ||
| EOF | ||
| cc -c "$TMP/test.c" -o "$TMP/test.o" | ||
|
|
||
| # Verify the header compiles with a C++ compiler | ||
| cat << 'EOF' > "$TMP/test.cc" | ||
| #include "output.h" | ||
| EOF | ||
| c++ -std=c++17 -c "$TMP/test.cc" -o "$TMP/test_cpp.o" |
This file contains hidden or 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,43 @@ | ||
| #!/bin/sh | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
|
|
||
| TMP="$(mktemp -d)" | ||
| clean() { rm -rf "$TMP"; } | ||
| trap clean EXIT | ||
|
|
||
| cat << 'EOF' > "$TMP/schema.json" | ||
| { | ||
| "$id": "https://example.com", | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "type": "string" | ||
| } | ||
| EOF | ||
|
|
||
| "$1" compile "$TMP/schema.json" --include test_schema > "$TMP/output.h" | ||
|
|
||
| cat << 'EOF' > "$TMP/expected.h" | ||
| #ifndef SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_ | ||
| #define SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_ | ||
|
|
||
| #ifdef __cplusplus | ||
| #include <string_view> | ||
| #endif | ||
|
|
||
| static const char TEST_SCHEMA_DATA[] = | ||
| "\x5b\x66\x61\x6c\x73\x65\x2c\x74\x72\x75\x65\x2c\x5b\x22\x22\x2c" | ||
| "\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x65\x78\x61\x6d\x70\x6c\x65" | ||
| "\x2e\x63\x6f\x6d\x22\x5d\x2c\x5b\x5b\x31\x31\x2c\x22\x2f\x74\x79" | ||
| "\x70\x65\x22\x2c\x22\x22\x2c\x22\x23\x2f\x74\x79\x70\x65\x22\x2c" | ||
| "\x32\x2c\x5b\x38\x2c\x34\x5d\x5d\x5d\x5d"; | ||
| static const unsigned int TEST_SCHEMA_LENGTH = 74; | ||
|
|
||
| #ifdef __cplusplus | ||
| static constexpr std::string_view TEST_SCHEMA{TEST_SCHEMA_DATA, TEST_SCHEMA_LENGTH}; | ||
| #endif | ||
|
|
||
| #endif | ||
| EOF | ||
|
|
||
| diff "$TMP/output.h" "$TMP/expected.h" |
This file contains hidden or 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,44 @@ | ||
| #!/bin/sh | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
|
|
||
| TMP="$(mktemp -d)" | ||
| clean() { rm -rf "$TMP"; } | ||
| trap clean EXIT | ||
|
|
||
| cat << 'EOF' > "$TMP/schema.json" | ||
| { | ||
| "$id": "https://example.com", | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "type": "string" | ||
| } | ||
| EOF | ||
|
|
||
| # Use short option -n | ||
| "$1" compile "$TMP/schema.json" -n TEST_SCHEMA > "$TMP/output.h" | ||
|
|
||
| cat << 'EOF' > "$TMP/expected.h" | ||
| #ifndef SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_ | ||
| #define SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_ | ||
|
|
||
| #ifdef __cplusplus | ||
| #include <string_view> | ||
| #endif | ||
|
|
||
| static const char TEST_SCHEMA_DATA[] = | ||
| "\x5b\x66\x61\x6c\x73\x65\x2c\x74\x72\x75\x65\x2c\x5b\x22\x22\x2c" | ||
| "\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x65\x78\x61\x6d\x70\x6c\x65" | ||
| "\x2e\x63\x6f\x6d\x22\x5d\x2c\x5b\x5b\x31\x31\x2c\x22\x2f\x74\x79" | ||
| "\x70\x65\x22\x2c\x22\x22\x2c\x22\x23\x2f\x74\x79\x70\x65\x22\x2c" | ||
| "\x32\x2c\x5b\x38\x2c\x34\x5d\x5d\x5d\x5d"; | ||
| static const unsigned int TEST_SCHEMA_LENGTH = 74; | ||
|
|
||
| #ifdef __cplusplus | ||
| static constexpr std::string_view TEST_SCHEMA{TEST_SCHEMA_DATA, TEST_SCHEMA_LENGTH}; | ||
| #endif | ||
|
|
||
| #endif | ||
| EOF | ||
|
|
||
| diff "$TMP/output.h" "$TMP/expected.h" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.