Skip to content
Open
Show file tree
Hide file tree
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
193 changes: 193 additions & 0 deletions yaml/libyaml/0.2.5/libyaml_llar.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import (
"os"
"path/filepath"
"slices"
)

const consumerSource = `#include <yaml.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct fruit {
char *name;
char *color;
int count;
};
struct fruit data[] = {
{"apple", "red", 12},
{"orange", "orange", 3},
{"banana", "yellow", 4},
{"mango", "green", 1},
{NULL, NULL, 0}
};

int main(void)
{
yaml_emitter_t emitter;
yaml_event_t event;
struct fruit *f;
char buffer[64];

yaml_emitter_initialize(&emitter);
yaml_emitter_set_output_file(&emitter, stdout);

yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
1, YAML_ANY_MAPPING_STYLE);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *)"fruit", strlen("fruit"), 1, 0, YAML_PLAIN_SCALAR_STYLE);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_sequence_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_SEQ_TAG,
1, YAML_ANY_SEQUENCE_STYLE);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

for (f = data; f->name; f++) {
yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
1, YAML_ANY_MAPPING_STYLE);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *)"name", strlen("name"), 1, 0, YAML_PLAIN_SCALAR_STYLE);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *)f->name, strlen(f->name), 1, 0, YAML_PLAIN_SCALAR_STYLE);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *)"color", strlen("color"), 1, 0, YAML_PLAIN_SCALAR_STYLE);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *)f->color, strlen(f->color), 1, 0, YAML_PLAIN_SCALAR_STYLE);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *)"count", strlen("count"), 1, 0, YAML_PLAIN_SCALAR_STYLE);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

sprintf(buffer, "%d", f->count);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info — optional hardening. sprintf(buffer, "%d", f->count) into the fixed 64-byte buffer is safe as written (values come only from the hardcoded data[]; a 32-bit int is ≤11 chars). No bug. If you want to future-proof against copy-paste reuse, snprintf(buffer, sizeof(buffer), "%d", f->count) is the trivial change.

yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_INT_TAG,
(yaml_char_t *)buffer, strlen(buffer), 1, 0, YAML_PLAIN_SCALAR_STYLE);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_mapping_end_event_initialize(&event);
if (!yaml_emitter_emit(&emitter, &event)) goto error;
}

yaml_sequence_end_event_initialize(&event);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_mapping_end_event_initialize(&event);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_document_end_event_initialize(&event, 0);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_stream_end_event_initialize(&event);
if (!yaml_emitter_emit(&emitter, &event)) goto error;

yaml_emitter_delete(&emitter);
return 0;

error:
fprintf(stderr, "Failed to emit event %d: %s\n", event.type, emitter.problem);
yaml_emitter_delete(&emitter);
return 1;
}
`

id "yaml/libyaml"

fromVer "0.2.5"

defaults {
"shared": "OFF",
"fPIC": "ON",
}

filter => {
for name, values in target.options {
if name != "shared" && name != "fPIC" {
return false
}
for value in values {
if value != "ON" && value != "OFF" {
return false
}
}
}
return true
}

onBuild ctx => {
installDir := ctx.outputDir
shared := slices.contains(target.options["shared"], "ON")
fPIC := slices.contains(target.options["fPIC"], "ON")

c := cmake.new(ctx.SourceDir, filepath.join(ctx.SourceDir, "_build"), installDir)
c.define "INSTALL_CMAKE_DIR", "lib/cmake/libyaml"
c.define "YAML_STATIC_LIB_NAME", "yaml"
c.define "CMAKE_POLICY_VERSION_MINIMUM", "3.5"
c.define "CMAKE_INSTALL_LIBDIR", "lib"
c.define "INSTALL_LIB_DIR", "lib"
c.defineBool "BUILD_SHARED_LIBS", shared
c.defineBool "CMAKE_POSITION_INDEPENDENT_CODE", fPIC
c.defineBool "BUILD_TESTING", false
c.configure
c.build
c.install

licenseDir := filepath.join(installDir, "licenses")
os.mkdirAll(licenseDir, 0o755)!
os.writeFile(filepath.join(licenseDir, "License"), os.readFile(filepath.join(ctx.SourceDir, "License"))!, 0o644)!

// Upstream CMake does not install pkg-config metadata.
pcDir := filepath.join(installDir, "lib", "pkgconfig")
os.mkdirAll(pcDir, 0o755)!
pc := `prefix=$${pcfiledir}/../..
exec_prefix=$${prefix}
libdir=$${prefix}/lib
includedir=$${prefix}/include

Name: yaml

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low — .pc Name/Description diverge from upstream. Upstream's own template (yaml-0.1.pc.in) uses Name: LibYAML and Description: Library to parse and emit YAML. Functionally harmless — pkgconfig.lookup("yaml") resolves by filename, not the Name field — but consumers inspecting pkg-config --description yaml will see different text. Consider aligning with upstream for accuracy. (Version: 0.2.5 is correct.)

Description: LibYAML is a YAML parser and emitter library
Version: 0.2.5
Libs: -L$${libdir} -lyaml
Cflags: -I$${includedir}
`
os.writeFile(filepath.join(pcDir, "yaml.pc"), []byte(pc), 0o644)!

pkgconfig.use installDir
ctx.setMetadata pkgconfig.lookup("yaml")!
}

onTest ctx => {
installDir := ctx.outputDir
testDir := filepath.join(ctx.SourceDir, "_llar_consumer")
os.mkdirAll(testDir, 0o755)!

consumer := filepath.join(testDir, "consumer.c")
os.writeFile(consumer, []byte(consumerSource), 0o644)!

binary := filepath.join(testDir, "consumer")
pkgconfig.use installDir
flagsFile := filepath.join(testDir, "yaml.flags")
os.writeFile(flagsFile, []byte(pkgconfig.lookup("yaml")!), 0o644)!
cc! consumer, "@${flagsFile}", "-o", binary

if slices.contains(target.options["shared"], "ON") {
os.setenv("LD_LIBRARY_PATH", filepath.join(installDir, "lib"))!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low — loader path is overwritten, not prepended. os.setenv("LD_LIBRARY_PATH", ...) (and DYLD_LIBRARY_PATH on the next line) replace any inherited value rather than prepending the install lib dir. If the test env relies on an inherited loader path, the shared-build consumer could fail to resolve unrelated libraries. Consider prepending the existing value, e.g. filepath.join(installDir, "lib") + ":" + os.getenv("LD_LIBRARY_PATH"). Test-reliability only, not a security issue.

os.setenv("DYLD_LIBRARY_PATH", filepath.join(installDir, "lib"))!
}
exec! binary
}
4 changes: 4 additions & 0 deletions yaml/libyaml/versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"path": "yaml/libyaml",
"deps": {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit — indentation. Other versions.json files in the repo (fast-pack/streamvbyte, madler/zlib, json-c/json-c) use tab indentation; this file uses two spaces. Worth normalizing to tabs for consistency.

}
Loading