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
123 changes: 123 additions & 0 deletions drichardson/huffman/v1.2.3/huffman_llar.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import (
"os"
"path/filepath"
"slices"
"strings"
)

const consumerSource = `#include <stdint.h>
#include <stdlib.h>

#include "huffman/huffman.h"

int main(void) {
uint8_t input[3] = {'a', 'b', 'c'};
unsigned char *encoded = NULL;
uint32_t encoded_len = 0;

if (huffman_encode_memory(input, 3, &encoded, &encoded_len) != 0) {
return 1;
}

free(encoded);
return encoded_len == 0;

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.

[P3] Non-obvious consumer assertion could use a one-line comment

return encoded_len == 0; makes the consumer exit non-zero when the encoder produced no output — a deliberate assertion that encoding yielded bytes. It reads like a bug at a glance. A short comment (e.g. // non-zero exit if encoding produced nothing) would make the intent clear to future maintainers.

}
`

id "drichardson/huffman"

// v1.2.3 is the first upstream tag with the CMake build and install contract
// used by v1.2.3 through v1.2.7.
fromVer "v1.2.3"

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

// Match the Conan recipe's Cygwin source fix while retaining upstream
// behavior on native Windows and other platforms.
sourcePath := filepath.join(ctx.SourceDir, "huffman.c")
source := string(os.readFile(sourcePath)!)
source = strings.replace(source, "#ifdef WIN32", "#if defined _WIN32 || defined __CYGWIN__", 1)
os.writeFile(sourcePath, []byte(source), 0644)!

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.defineBool "BUILD_SHARED_LIBS", shared
c.defineBool "CMAKE_POSITION_INDEPENDENT_CODE", fPIC
c.configure
c.build
c.install

licenseDir := filepath.join(installDir, "licenses")
os.mkdirAll(licenseDir, 0755)!
os.writeFile(filepath.join(licenseDir, "LICENSE"), os.readFile(filepath.join(ctx.SourceDir, "LICENSE"))!, 0644)!

libs := "-L$${libdir} -lhuffman"
if slices.contains(target.require["os"], "windows") {
libs += " -lws2_32"
}

pcDir := filepath.join(installDir, "lib", "pkgconfig")
os.mkdirAll(pcDir, 0755)!
pc := `prefix=$${pcfiledir}/../..
includedir=$${prefix}/include
libdir=$${prefix}/lib

Name: huffman
Description: Huffman encoder and decoder library
Version: 1.2.3
Libs: ` + libs + `
Cflags: -I$${includedir}
`
os.writeFile(filepath.join(pcDir, "huffman.pc"), []byte(pc), 0644)!
pkgconfig.use installDir
ctx.setMetadata pkgconfig.lookup("huffman")!
}

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

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

binary := filepath.join(testDir, "consumer")
pkgconfig.use installDir
flags := strings.fields(pkgconfig.lookup("huffman")!)
args := []string{sourcePath}
args <- flags...
args <- "-o", binary
cc! args...

if slices.contains(target.options["shared"], "ON") {
os.setenv("LD_LIBRARY_PATH", filepath.join(installDir, "lib"))!
os.setenv("DYLD_LIBRARY_PATH", filepath.join(installDir, "lib"))!
if slices.contains(target.require["os"], "windows") {
path := os.getenv("PATH")
os.setenv("PATH", filepath.join(installDir, "bin")+";"+path)!
}
}
exec! binary
}
4 changes: 4 additions & 0 deletions drichardson/huffman/versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"path": "drichardson/huffman",
"deps": {}
}
Loading